Show
Ignore:
Timestamp:
06/29/08 04:45:56 (5 months ago)
Author:
rickc
Message:

Plugin: preapproved.

1. Added configuration form so the user can set the number of approved comments the commenter must have in the database before a comment is automatically approved. This includes setting the number of approved comments to zero, so all comments are automatically approved.

2. Removed filtering from action_comment_insert_before() because the content was already filtered before we got this far.

3. Added readme file.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/preapproved/trunk/preapproved.plugin.php

    r6 r654  
    11<?php 
    22 
    3 /** 
     3/* 
    44 * PreApproved Class 
    55 *  
    6  * This class allows us to auto-approve previously approved commenters 
     6 * This class allows us to auto-approve comments 
    77 * 
    8  **/ 
     8 */ 
    99 
    1010class PreApproved extends Plugin 
    1111{ 
    12     /** 
     12    /* 
    1313     * function info 
    1414     * Returns information about this plugin 
    1515     * @return array Plugin info array 
    16      **/ 
     16     */ 
    1717    function info() 
    1818    { 
     
    2222            'author' => 'Habari Community', 
    2323            'authorurl' => 'http://habariproject.org/', 
    24             'version' => '1.1', 
    25             'description' => 'Automatically approve commenters that have approved comments in the database.', 
     24            'version' => '1.2', 
     25            'description' => 'Automatically approve comments based on the number of approved comments the commenter has previously made.', 
    2626            'license' => 'Apache License 2.0', 
    2727        ); 
    2828    } 
    29      
    30     /** 
     29 
     30    /* 
    3131     * Register the PreApproved event type with the event log 
    32      */   
    33     public function action_plugin_activation( $file ) { 
     32     */ 
     33    public function action_plugin_activation( $file ) 
     34    { 
    3435        if ( realpath( $file ) == __FILE__ ) { 
    3536            EventLog::register_type( 'PreApproved' ); 
     37            if( !(Options::get( 'preapproved__approved_count' ) ) ) { 
     38                Options::set( 'preapproved__approved_count', 1 ); 
     39            } 
    3640        } 
    3741    } 
    38      
    39     /** 
    40      * Unregister the PreApproved event type on deactivation 
    41      * @todo Should we be doing this? 
    42      */       
    43     public function action_plugin_deactivation( $file ) { 
     42 
     43    /* 
     44     * Unregister the PreApproved event type on deactivation if it isn't being used 
     45     */ 
     46    public function action_plugin_deactivation( $file ) 
     47    { 
    4448        if ( realpath( $file ) == __FILE__ ) { 
    4549            EventLog::unregister_type( 'PreApproved' ); 
    4650        } 
    4751    } 
    48      
    49     /** 
     52 
     53    public function filter_plugin_config($actions, $plugin_id) 
     54    { 
     55        if ($plugin_id == $this->plugin_id()) { 
     56            $actions[] = _t( 'Configure' ); 
     57        } 
     58        return $actions; 
     59    } 
     60 
     61    public function action_plugin_ui($plugin_id, $action) 
     62    { 
     63        if ( $plugin_id == $this->plugin_id() ) { 
     64            switch ($action) { 
     65                case _t( 'Configure' ): 
     66                    $form= new FormUI( 'preapproved' ); 
     67                    $form->append( 'text', 'approved_count', 'option:preapproved__approved_count', _t( 'Required number of approved comments: ' ) ); 
     68                    $form->approved_count->add_validator( array( $this, 'validate_integer' ) ); 
     69                    $form->append( 'submit', 'save', _t( 'Save' ) ); 
     70                    $form->out(); 
     71                break; 
     72            } 
     73        } 
     74    } 
     75 
     76    /* 
    5077     * function act_comment_insert_before 
    5178     * This function is executed when the action "comment_insert_before" 
    5279     * is invoked from a Comment object. 
    5380     * The parent class, Plugin, handles registering the action  
    54      * and hook name using the name of the function to determine  
     81     * and hook name using the name of the function to determine coun 
    5582     * where it will be applied. 
    5683     * You can still register functions as hooks without using 
     
    5885     * @param Comment The comment that will be processed before storing it in the database. 
    5986     * @return Comment The comment result to store. 
    60      **/                     
    61     function action_comment_insert_before ( $comment ) { 
     87     */ 
     88    function action_comment_insert_before ( $comment ) 
     89    { 
    6290        // This plugin ignores non-comments 
    63         if( $comment->type != Comment::COMMENT ) { 
    64             return $comment; 
     91        if( $comment->type == Comment::COMMENT ) { 
     92            if( Comments::get( array( 'email' => $comment->email, 'name' => $comment->name,  
     93                                'url' => $comment->url, 'status' => Comment::STATUS_APPROVED ) )->count >= Options::get( 'preapproved__approved_count' ) ) { 
     94                $comment->status = Comment::STATUS_APPROVED; 
     95                EventLog::log( 'Comment by ' . $comment->name . ' automatically approved.', 'info', 'PreApproved', 'Preapproved' ); 
     96            } 
    6597        } 
    66          
    67         // <script> is bad, mmmkay? 
    68         $comment->content = InputFilter::filter( $comment->content ); 
     98        return $comment; 
     99    } 
    69100 
    70         if( Comments::get( array( 'email' => $comment->email, 'name' => $comment->name,  
    71                             'url' => $comment->url, 'status' => Comment::STATUS_APPROVED ) )->count ) { 
    72             $comment->status = Comment::STATUS_APPROVED; 
    73             EventLog::log( 'Comment by ' . $comment->name . ' automatically approved.', 'info', 'comment', 'preapproved' ); 
    74         } 
    75         return; 
    76     } 
    77      
    78     function set_priorities() { 
     101    function set_priorities() 
     102    { 
    79103      return array( 'action_comment_insert_before' => 10 ); 
    80104    } 
    81      
    82     /** 
    83      * Add update beacon support  
    84      **/ 
    85      
    86     function action_update_check() { 
    87       Update::add( 'PreApproved', '0fa22c74-a0d6-11dc-8314-0800200c9a66', $this->info->version );  
    88     } 
     105 
     106    /* 
     107    * Add update beacon support  
     108    */ 
     109    function action_update_check() 
     110    { 
     111        Update::add( 'PreApproved', '0fa22c74-a0d6-11dc-8314-0800200c9a66', $this->info->version );  
     112    } 
     113 
     114    /* 
     115     * A validation function that returns an error if the value passed in is not  an integer 
     116     * 
     117     * @param string $value A value to test if it is an integer 
     118     * @param formcontrol $control The control containing the value 
     119     * @param formui $form The form containing the control 
     120     * @return array An empty array if the value is an integer or an array with strings describing the errors 
     121     */ 
     122    function validate_integer( $value, $control, $form ) 
     123    { 
     124        if( !ctype_digit( $value ) ) { 
     125            return array( _t( 'Please enter a valid positive integer.' ) ); 
     126        } 
     127        $val = intval( $value ); 
     128        if( !is_int( $val ) ) { 
     129            return array( _t( 'Please enter a valid positive integer.' ) ); 
     130        } 
     131        return array(); 
     132    } 
     133 
    89134} 
    90135?>