new akismet plugin with php5 classes

vincent3569 Member, Translator
hi

on http://akismet.com/development/ I have found an Akismet php5 class.

but the akismet plugin for zenphoto uses an Akismet php4 class.

so I have decided to create a new akismet plugin for zenphoto, using this php5 class.

after some tests, it seems to work well.

can you explain , how can I give it to the community ?
«1

Comments

  • Specially if you are intending to continue to support the plugin we would suggest you create a web page for it and we will link the entry to that site.

    Otherwise you can provide the new version attached to a ticket.
  • acrylian Administrator, Developer
    Vincent: As said on the other thread, it would be great if you could take it over. Info on how we suggest to do that here: http://www.zenphoto.org/news/general-contributor-guidelines#themes-and-plugins
  • vincent3569 Member, Translator
    hi

    akismet allows to give feedback on messages and gives alert on no detected SPAM.

    with another user of zenphoto, we wish to implement those functions on akismet plugin.
    to do that, I need some improvement on admin-comment.php : if the admin check the message as a spam, use a new method of the plugin (feedbackMessage for exemple).

    I will create a ticket for that, but first, I have a question : how the bulk action works in that case ?
    Could you explain how it works ? Is there a "while loop" on each element ?
  • Bulk actions do indeed perform a while loop on the individual checked items performing the action specified.

    Note that there is are filters `comment_approve` and `comment_disapprove` that you can attach with the plugin.
  • vincent3569 Member, Translator
    mhh,
    unfortunatly, I dont' realy inderstand how the filters work.
    If you could give me some explanation (or link to to some documentation)
  • The general documentation is here: http://www.zenphoto.org/news/zenphoto-plugin-architecture#filters

    The list of filters there represents 1.4.1 so there will be an update for the 1.4.2 additions. (The comment filters are there on 1.4.1)

    So the quick tutorial is:

    add a line in your spam filter towards the front that reads someting like:

    `zp_register_filter('comment_disapprove', 'plugin_handler_function');`

    Then declare a function:
    `
    function plugin_handler_function($commentObj) {
    //do your code here
    return $commentObj;
    }
    `
  • vincent3569 Member, Translator
    ok

    there is the basic structure of the akismet plugin :

    `
    zp_register_filter('comment_disapprove', 'submitSpam');

    class SpamFilter {
    function SpamFilter() {
    [...]
    }

    function getOptionsSupported() {
    [...]
    }

    function handleOption($option, $currentValue) {
    [...]
    }

    function filterMessage($author, $email, $website, $body, $receiver, $ip) {
    [...]
    }

    function submitSpam($comment) {
    [...]
    }
    }

    class Akismet {
    [...]
    }
    `
    according to you, I am on the right way ?
    there is no code to add in admin-comment.php or other core files ?

    why the return of the $commentObj is needed ?
  • acrylian Administrator, Developer
    The call to the class method from the filter will not work. You call it as a function but it is a class method. You probably need a wrapper function for that method outside the class. Not sure if you can use $classobj->submitSpam within hte filter call. sbillard will know.
    why the return of the $commentObj is needed ?

    You mean in the filter? You can call a filter multiple times so someone else may want to use it as well.
  • vincent3569 Member, Translator
    thanks

    do you think the following code is more correct ?

    function submitSpam calls the method feedbackMessage of SpamFilter.

    feedbackMessage invokes the method submitSpam of Akismet

    `
    zp_register_filter('comment_disapprove', 'submitSpam');

    function submitSpam($comment) {
    [...]
    $spamfilter = new SpamFilter();
    $spamfilter->feedbackMessage($author, $email, $website, $body, false);
    return $comment;
    }

    class SpamFilter {
    function SpamFilter() {
    [...]
    }

    function getOptionsSupported() {
    [...]
    }

    function handleOption($option, $currentValue) {
    [...]
    }

    function filterMessage($author, $email, $website, $body, $receiver, $ip) {
    [...]
    }

    function feedbackMessage($author, $email, $website, $body, $goodMessage) {
    [...]
    if (!($goodMessage)) {
    $akismet->submitSpam();
    }
    }
    }
    `
  • acrylian Administrator, Developer
    looks better. I am not in detail familiar with the spam filters but if the class object is already created elsewhere you might want to use a global variable so you don't need to create an extra one within the function.
  • vincent3569 Member, Translator
    the class object is created when a visitor add a comment
    and, of course, for each call of akismet to signalize a SPAM.
    so I need a new object on each comment, don't you think ?

    I do some tests but I am unable to verify the improvement (I can't see if the function submitSpam() is called by the filter).

    Is there a way to add some trace for debugging the code (to generate zp-date/debug.log or something like that) ?

    thanks for your help
  • vincent3569 Member, Translator
    ok, I have found and I have set DEBUG_PLUGINS and DEBUG_FILTERS to true in global-definitions.

    I can't see a call of the hook 'comment_disapprove' when I choose a comment as a Spam.

    I can see the registration, only if I go to admin/options/comment.

    could you help me ?
  • acrylian Administrator, Developer
    Probably you need the class object. As said I am not familiar with the spam(comment) stuff.

    There are several debugLog functions for that within /zp-core/functions.php.
  • vincent3569 Member, Translator
    not sure it is the only problem.

    with DEBUG_FILTERS, I can see :
    - the function submitSpam is only registred when I navigate on admin/options/comment.

    - wathever I do, the test if ( !isset($_zp_filters[$hook]) ) on function zp_apply_filter is false, so my function is not triggered.

    I really need some help on that subject
  • acrylian Administrator, Developer
    I will pass answering to sbillard, he surely can help better.
  • You are supposed to be able to attach class methods to filters, but I do not know if anyone has ever really done that.

    Also, it seems that because the spam filters do not follow the normal filter implementation there is an issue of the filter not getting loaded.

    A work-around would be to create a new plugin that contains the filtering code. It would have to do the "require_once" on the spam filter to get it loaded. Otherwise your code could pretty much stay as above.

    `require_once(SERVERPATH.'/'.USER_PLUGIN_FOLDER.'/spamfilters/akismet_php5.php');
    zp_register_filter('comment_disapprove', 'submitSpam');

    function submitSpam($comment) {
    [...]
    $spamfilter = new SpamFilter();
    $spamfilter->feedbackMessage($author, $email, $website, $body, false);
    return $comment;
    }'

    Really what is needed is to recast the spam filters into normal plugins. But that is a 1.4.3 issue. Still, I think we will do that, so be on notice that the akismit filter will probably need to be change then.

    I will look further to see if some other method will work. I will keep you posted.
  • I think maybe I have a solution. Please try the nightly Beta [8548] admin-comments.php will now load the spam filter. This should make your above code work unchanged.

    Let us know. If it does not work we will have some more digging to do.
  • vincent3569 Member, Translator
    It seems we have the same idea :-)
    I will have a look as soon as possible

    just a question : what is the need to add "$spamfilter = new SpamFilter();" in the code above ?

    I was thinking the 1st part was necessary.

    `
    21 if (!(false === ($requirePath = getPlugin('spamfilters/'.getOption('spam_filter').'.php')))) {
    22 require_once($requirePath);
    23 $spamfilter = new SpamFilter();
    24 }
    `
  • It is probably not needed. Just thought maybe the comment script might someday want to use it. All that is really needed for the filters to be register is for the script to be loaded by the require_once.
  • vincent3569 Member, Translator
    hi

    It seems to work with [8548] :
    when I disapprove or approve a comment, the filter is registreted and the new function of my plugin is called.

    Thanks !

    I have another question : I wish to verify the akismet key when the options of the plugin are saved.

    There is a method of akismet to do that.
    I suppose I have to do the same thing I have done for submit false positives/negatives SPAM : triggered a new function on "save" action on admin/options/comment, but I don't know which filter I could use.

    could you help me ?
  • acrylian Administrator, Developer
    You would actually need a filter that is called on saving plugin options. Not sure we have one at the moment, but sbillard will know it for sure.

    Edit: Is probably possible without a filter. Use the OPTION_TYPE_CUSTOM and create a check with in handleOption(). It would then work like 1. Save the key 2. on page reload this function gets that value and checks it. Then you can put out a warning right with the text field.
  • You do not actually have to change the option type. Just add code in the getSupportedOptions() method to check the validity of the key. If it is not valid (and probably also not empty) add the message to the option description.
  • acrylian Administrator, Developer
    True, why easy if possible complicated...;-)
  • vincent3569 Member, Translator
    thanks sbillard, but could you give me an example how to add code in getSupportedOptions() ?

    fyi, I have a new methoe of SpamFilter :
    `
    function isValidKey() {
    $zp_galerieUrl = FULLWEBPATH; // Set the webpath for the Akismet server
    $zp_akismetKey = getOption('Akismet_key');

    $akismet = new Akismet($zp_galerieUrl, $zp_akismetKey);

    if($akismet->isKeyValid()) {
    return true;
    } else {
    return false;
    }
    }
    `
    how can I do to call this method on validation ?
    how can I add a specific message on zenphoto admin : 'your Akismet key is empty or invalid' if the function returns false ?
  • acrylian Administrator, Developer
    Within getSupportedOptions() call your method and add the text about failure if it fails to the option text itself for example.
  • `
    function getOptionsSupported() {
    if (getOption('Akismet_key') && $this->isValidKey()) {
    $msg = '';
    } else {
    $msg = Gettext('You need to provide a valid Akismet key');
    }
    return array(
    gettext('Akismet key') => array('key' => 'Akismet_key', 'type' => 0, 'desc' => gettext('Proper operation requires an Akismet API key obtained by signing up for a Akismet account.').$msg),
    gettext('Forgiving') => array('key' => 'Forgiving', 'type' => 1, 'desc' => gettext('Mark suspected SPAM for moderation rather than as SPAM'))
    );
    }
    `
    Here is a simple example based on your plugin. Of course the text of the message could be better, and maybe also styled as a notice.
  • acrylian Administrator, Developer
    I guess a wrong key is actually an error. We have serveral predefined class for styling you can use:
    .errorbox, .warningbox and .notebox. The latter is the orange box we often use to highlight. We have also one for "successes" .messagebox which is used on successful saves for example.
  • vincent3569 Member, Translator
    thanks !

    the new release of the plugin is ready.
    improvement :
    - vadidate the provided akismet key in the admin
    - send feedback to Akismet and signal a false negative (no detected SPAM by akismet) / false positive.

    I have to do some testing and I will give it to the community when the 1.4.2 release of Zenphoto will be published.
  • acrylian Administrator, Developer
    Great. You also could release it as a beta so people interessted could try it before 1.4.2 so possible bugs are sorted out.
  • vincent3569 Member, Translator
    hi

    to make it more convenient for you, I will host my plugin akismet_php5 on my website.

    there is 2 releases of the plugin :
    1.0.0 : intial release for zenphoto =< 1.4.1.6
    1.1.0 : with improvements described above, for zenphoto >= 1.4.2 (beta included)

    You can find a news here: http://www.vincentbourganel.fr/news/akismet-spam-filter-for-Zenphoto

    Can you change the link on the page http://www.zenphoto.org/news/spamfilter-akismet_php5 ?

    As said in my news, please report bugs and feedback on the Zenphoto forum and I will try to fix as soon as possible.
Sign In or Register to comment.