-
Notifications
You must be signed in to change notification settings - Fork 0
/
preapproved.plugin.php
97 lines (88 loc) · 3.03 KB
/
preapproved.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
* PreApproved Class
*
* This class allows us to auto-approve comments
*
*/
class PreApproved extends Plugin
{
/*
* Register the PreApproved event type with the event log
*/
public function action_plugin_activation( $file )
{
if ( realpath( $file ) == __FILE__ ) {
EventLog::register_type( 'PreApproved' );
if ( !( Options::get( 'preapproved__approved_count' ) ) ) {
Options::set( 'preapproved__approved_count', 1 );
}
}
}
/*
* Unregister the PreApproved event type on deactivation if it isn't being used
*/
public function action_plugin_deactivation( $file )
{
if ( realpath( $file ) == __FILE__ ) {
EventLog::unregister_type( 'PreApproved' );
}
}
public function configure()
{
$form = new FormUI( 'preapproved' );
$form->append( 'text', 'approved_count', 'option:preapproved__approved_count', _t( 'Required number of approved comments: ' ) );
$form->approved_count->add_validator( array( $this, 'validate_integer' ) );
$form->append( 'submit', 'save', _t( 'Save' ) );
$form->set_option( 'success_message', _t( 'Configuration saved' ) );
$form->out();
}
/*
* function act_comment_insert_before
* This function is executed when the action "comment_insert_before"
* is invoked from a Comment object.
* The parent class, Plugin, handles registering the action
* and hook name using the name of the function to determine
* where it will be applied.
* You can still register functions as hooks without using
* this method, but boy, is it handy.
* @param Comment The comment that will be processed before storing it in the database.
* @return Comment The comment result to store.
*/
function action_comment_insert_before ( $comment )
{
// This plugin ignores non-comments and comments already marked as spam
if( $comment->type == Comment::COMMENT && $comment->status != Comment::STATUS_SPAM) {
if( Comments::get( array( 'email' => $comment->email, 'name' => $comment->name,
'url' => $comment->url, 'status' => Comment::STATUS_APPROVED ) )->count >= Options::get( 'preapproved__approved_count' ) ) {
$comment->status = Comment::STATUS_APPROVED;
EventLog::log( 'Comment by ' . $comment->name . ' automatically approved.', 'info', 'PreApproved', 'PreApproved' );
}
}
return $comment;
}
function set_priorities()
{
return array( 'action_comment_insert_before' => 10 );
}
/*
* A validation function that returns an error if the value passed in is not an integer
*
* @param string $value A value to test if it is an integer
* @param formcontrol $control The control containing the value
* @param formui $form The form containing the control
* @return array An empty array if the value is an integer or an array with strings describing the errors
*/
function validate_integer( $value, $control, $form )
{
if( !ctype_digit( $value ) ) {
return array( _t( 'Please enter a valid positive integer.' ) );
}
$val = intval( $value );
if( !is_int( $val ) ) {
return array( _t( 'Please enter a valid positive integer.' ) );
}
return array();
}
}
?>