| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class is used to log any comment related action. |
| 10 |
*/ |
| 11 |
class P_Event_Comments extends P_Event_Log { |
| 12 |
|
| 13 |
/** |
| 14 |
* Hook into the actions so we can log it. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'wp_insert_comment', [ &$this, 'handleCommentLog' ], 10, 2 ); |
| 20 |
add_action( 'edit_comment', [ &$this, 'handleCommentLog' ] ); |
| 21 |
add_action( 'trash_comment', [ &$this, 'handleCommentLog' ] ); |
| 22 |
add_action( 'untrash_comment', [ &$this, 'handleCommentLog' ] ); |
| 23 |
add_action( 'spam_comment', [ &$this, 'handleCommentLog' ] ); |
| 24 |
add_action( 'unspam_comment', [ &$this, 'handleCommentLog' ] ); |
| 25 |
add_action( 'delete_comment', [ &$this, 'handleCommentLog' ] ); |
| 26 |
add_action( 'transition_comment_status', [ &$this, 'transitionCommentStatus' ], 10, 3 ); |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Log the action regarding the comment. |
| 32 |
* |
| 33 |
* @param integer $id |
| 34 |
* @param string $action |
| 35 |
* @param string $comment |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
protected function _addCommentLog( $id, $action, $comment = null ) { |
| 39 |
if ( is_null( $comment ) ) { |
| 40 |
$comment = get_comment( $id ); |
| 41 |
} |
| 42 |
|
| 43 |
$this->insert( |
| 44 |
[ |
| 45 |
'action' => $action, |
| 46 |
'object' => 'comment', |
| 47 |
'object_id' => $id, |
| 48 |
'object_name' => esc_html( get_the_title( $comment->comment_post_ID ) ), |
| 49 |
] |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Determine what kind of comment action is executed. |
| 55 |
* |
| 56 |
* @param integer $comment_id |
| 57 |
* @param string $comment |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function handleCommentLog( $comment_id, $comment = null ) { |
| 61 |
if ( is_null( $comment ) ) { |
| 62 |
$comment = get_comment( $comment_id ); |
| 63 |
} |
| 64 |
|
| 65 |
$action = 'created'; |
| 66 |
switch ( current_filter() ) { |
| 67 |
case 'wp_insert_comment': |
| 68 |
$action = 1 === (int) $comment->comment_approved ? 'approved' : 'pending'; |
| 69 |
break; |
| 70 |
case 'edit_comment': |
| 71 |
$action = 'updated'; |
| 72 |
break; |
| 73 |
case 'delete_comment': |
| 74 |
$action = 'deleted'; |
| 75 |
break; |
| 76 |
case 'trash_comment': |
| 77 |
$action = 'trashed'; |
| 78 |
break; |
| 79 |
case 'untrash_comment': |
| 80 |
$action = 'untrashed'; |
| 81 |
break; |
| 82 |
case 'spam_comment': |
| 83 |
$action = 'spammed'; |
| 84 |
break; |
| 85 |
case 'unspam_comment': |
| 86 |
$action = 'unspammed'; |
| 87 |
break; |
| 88 |
} |
| 89 |
|
| 90 |
$this->_addCommentLog( $comment_id, $action, $comment ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* When the status of a comment is transitioned. |
| 95 |
* |
| 96 |
* @param string $new_status |
| 97 |
* @param string $old_status |
| 98 |
* @param string $comment |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function transitionCommentStatus( $new_status, $old_status, $comment ) { |
| 102 |
$this->_addCommentLog( $comment->comment_ID, $new_status, $comment ); |
| 103 |
} |
| 104 |
} |
| 105 |
|