PluginProbe
Patchstack – WordPress & Plugins Security / 2.3.5
Patchstack – WordPress & Plugins Security v2.3.5
2.3.7 trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.17 2.1.18 2.1.19 2.1.2 2.1.20 2.1.21 2.1.22 2.1.23 2.1.24 2.1.25 2.1.3 2.1.4 2.1.5 2.1.6 All 49 releases
patchstack / includes / events / comments.php

comments.php in Patchstack – WordPress & Plugins Security 2.3.5, at includes/events/comments.php

108 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( ! get_option( 'patchstack_activity_log_comments', 0 ) ) {
20 return;
21 }
22
23 add_action( 'wp_insert_comment', [ &$this, 'handleCommentLog' ], 10, 2 );
24 add_action( 'edit_comment', [ &$this, 'handleCommentLog' ] );
25 add_action( 'trash_comment', [ &$this, 'handleCommentLog' ] );
26 add_action( 'untrash_comment', [ &$this, 'handleCommentLog' ] );
27 add_action( 'spam_comment', [ &$this, 'handleCommentLog' ] );
28 add_action( 'unspam_comment', [ &$this, 'handleCommentLog' ] );
29 add_action( 'delete_comment', [ &$this, 'handleCommentLog' ] );
30 add_action( 'transition_comment_status', [ &$this, 'transitionCommentStatus' ], 10, 3 );
31 }
32
33 /**
34 * Log the action regarding the comment.
35 *
36 * @param integer $id
37 * @param string $action
38 * @param string $comment
39 * @return void
40 */
41 protected function _addCommentLog( $id, $action, $comment = null ) {
42 if ( is_null( $comment ) ) {
43 $comment = get_comment( $id );
44 }
45
46 $this->insert(
47 [
48 'action' => $action,
49 'object' => 'comment',
50 'object_id' => $id,
51 'object_name' => esc_html( get_the_title( $comment->comment_post_ID ) ),
52 ]
53 );
54 }
55
56 /**
57 * Determine what kind of comment action is executed.
58 *
59 * @param integer $comment_id
60 * @param string $comment
61 * @return void
62 */
63 public function handleCommentLog( $comment_id, $comment = null ) {
64 if ( is_null( $comment ) ) {
65 $comment = get_comment( $comment_id );
66 }
67
68 $action = 'created';
69 switch ( current_filter() ) {
70 case 'wp_insert_comment':
71 $action = 1 === (int) $comment->comment_approved ? 'approved' : 'pending';
72 break;
73 case 'edit_comment':
74 $action = 'updated';
75 break;
76 case 'delete_comment':
77 $action = 'deleted';
78 break;
79 case 'trash_comment':
80 $action = 'trashed';
81 break;
82 case 'untrash_comment':
83 $action = 'untrashed';
84 break;
85 case 'spam_comment':
86 $action = 'spammed';
87 break;
88 case 'unspam_comment':
89 $action = 'unspammed';
90 break;
91 }
92
93 $this->_addCommentLog( $comment_id, $action, $comment );
94 }
95
96 /**
97 * When the status of a comment is transitioned.
98 *
99 * @param string $new_status
100 * @param string $old_status
101 * @param string $comment
102 * @return void
103 */
104 public function transitionCommentStatus( $new_status, $old_status, $comment ) {
105 $this->_addCommentLog( $comment->comment_ID, $new_status, $comment );
106 }
107 }
108