PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / audit / comment-audit.php

comment-audit.php in Defender Security – Malware Scanner, Login Security & Firewall 3.1.0, at src/component/audit/comment-audit.php

315 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Author: Hoang Ngo
4 */
5
6 namespace WP_Defender\Component\Audit;
7
8 use WP_Defender\Traits\User;
9 use WP_Defender\Model\Audit_Log;
10
11 class Comment_Audit extends Audit_Event {
12 use User;
13
14 public const ACTION_SPAMMED = 'spammed', ACTION_UNSPAMMED = 'unspammed', ACTION_DUPLICATED = 'duplicated', ACTION_FLOOD = 'flood', ACTION_ADDED = 'added';
15 public const CONTEXT_COMMENT = 'ct_comment';
16
17 /**
18 * @return array
19 */
20 public function get_hooks() {
21
22 return array(
23 'wp_insert_comment' => array(
24 'args' => array( 'comment_ID' ),
25 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
26 'action_type' => self::ACTION_ADDED,
27 'callback' => array( self::class, 'process_generic_comment' ),
28 ),
29 'comment_flood_trigger' => array(
30 'args' => array( 'time_lastcomment', 'time_newcomment' ),
31 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
32 'action_type' => self::ACTION_FLOOD,
33 'context' => self::CONTEXT_COMMENT,
34 'text' => sprintf(
35 /* translators: */
36 esc_html__( '%1$s User %2$s flooded comment', 'wpdef' ),
37 '{{blog_name}}',
38 '{{wp_user}}'
39 ),
40 ),
41 'deleted_comment' => array(
42 'args' => array( 'comment_ID' ),
43 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
44 'action_type' => self::ACTION_DELETED,
45 'callback' => array( self::class, 'process_generic_comment' ),
46 ),
47 'trash_comment' => array(
48 'args' => array( 'comment_ID' ),
49 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
50 'action_type' => self::ACTION_TRASHED,
51 'callback' => array( self::class, 'process_generic_comment' ),
52 ),
53 'untrash_comment' => array(
54 'args' => array( 'comment_ID' ),
55 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
56 'action_type' => self::ACTION_RESTORED,
57 'callback' => array( self::class, 'process_generic_comment' ),
58 ),
59 'spam_comment' => array(
60 'args' => array( 'comment_ID' ),
61 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
62 'action_type' => self::ACTION_SPAMMED,
63 'callback' => array( self::class, 'process_generic_comment' ),
64 ),
65 'unspam_comment' => array(
66 'args' => array( 'comment_ID' ),
67 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
68 'action_type' => self::ACTION_UNSPAMMED,
69 'callback' => array( self::class, 'process_generic_comment' ),
70 ),
71 'transition_comment_status' => array(
72 'args' => array( 'new_status', 'old_status', 'comment' ),
73 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
74 'action_type' => self::ACTION_UPDATED,
75 'callback' => array(
76 self::class,
77 'process_comment_status_changed',
78 ),
79 ),
80 'edit_comment' => array(
81 'args' => array( 'comment_ID' ),
82 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
83 'action_type' => self::ACTION_UPDATED,
84 'callback' => array( self::class, 'process_generic_comment' ),
85 ),
86 'comment_duplicate_trigger' => array(
87 'args' => array( 'commentdata' ),
88 'event_type' => Audit_Log::EVENT_TYPE_COMMENT,
89 'action_type' => self::ACTION_DUPLICATED,
90 'callback' => array(
91 self::class,
92 'process_comment_duplicate_trigger',
93 ),
94 ),
95 );
96 }
97
98 /**
99 * @return array
100 */
101 public function dictionary() {
102
103 return array(
104 self::ACTION_DUPLICATED => esc_html__( 'Duplicated', 'wpdef' ),
105 self::ACTION_SPAMMED => esc_html__( 'Spammed', 'wpdef' ),
106 self::ACTION_UNSPAMMED => esc_html__( 'Unspammed', 'wpdef' ),
107 self::CONTEXT_COMMENT => esc_html__( 'Comment', 'wpdef' ),
108 );
109 }
110
111 /**
112 * @return array
113 */
114 public function process_comment_duplicate_trigger() {
115 $args = func_get_args();
116 $comment_data = $args[1]['commentdata'];
117
118 $post = get_post( $comment_data['comment_post_ID'] );
119 $post_type = get_post_type_object( $post->post_type );
120 $post_type_label = strtolower( $post_type->labels->singular_name );
121 $blog_name = is_multisite() ? '[' . get_bloginfo( 'name' ) . ']' : '';
122 $text = sprintf(
123 /* translators: */
124 esc_html__( '%1$s User %2$s submitted a duplicate comment on %3$s "%4$s"', 'wpdef' ),
125 $blog_name,
126 is_user_logged_in() ? $this->get_user_display( get_current_user_id() ) : $comment_data['comment_author'],
127 $post_type_label,
128 $post->post_title
129 );
130
131 return array( $text, $post_type_label );
132 }
133
134 /**
135 * @return array
136 */
137 public function process_comment_status_changed() {
138 $args = func_get_args();
139 $new_stat = $args[1]['new_status'];
140 $old_stat = $args[1]['old_status'];
141 $comment = $args[1]['comment'];
142 $post = get_post( $comment->comment_post_ID );
143 $post_type = get_post_type_object( $post->post_type );
144 $post_type_label = strtolower( $post_type->labels->singular_name );
145 $blog_name = is_multisite() ? '[' . get_bloginfo( 'name' ) . ']' : '';
146 $text = false;
147 if ( 'unapproved' === $old_stat && 'approved' === $new_stat ) {
148 $text = sprintf(
149 /* translators: */
150 esc_html__( '%1$s %2$s approved comment ID %3$s from %4$s, on %5$s "%6$s"', 'wpdef' ),
151 $blog_name,
152 $this->get_user_display( get_current_user_id() ),
153 $comment->comment_ID,
154 $comment->comment_author,
155 $post_type_label,
156 $post->post_title
157 );
158 } elseif ( 'unapproved' === $new_stat && 'approved' === $old_stat ) {
159 $text = sprintf(
160 /* translators: */
161 esc_html__( '%1$s %2$s unapproved comment ID %3$s from %4$s, on %5$s "%6$s"', 'wpdef' ),
162 $blog_name,
163 $this->get_user_display( get_current_user_id() ),
164 $comment->comment_ID,
165 $comment->comment_author,
166 $post_type_label,
167 $post->post_title
168 );
169 }
170
171 return array( $text, $post_type_label );
172 }
173
174 /**
175 * @return bool|array
176 */
177 public function process_generic_comment() {
178 $args = func_get_args();
179 $hookname = $args[0];
180 $comment_id = $args[1]['comment_ID'];
181 if ( ! isset( $args[1]['commentdata'] ) ) {
182 $comment = get_comment( $comment_id );
183 if ( empty( $comment ) ) {
184 return false;
185 }
186 $comment = $comment->to_array();
187 } else {
188 $comment = $args[1]['commentdata'];
189 if ( empty( $comment ) ) {
190 return false;
191 }
192 }
193 if ( ! isset( $args[1]['comment_approved'] ) ) {
194 $comment_approved = '';
195 } else {
196 $comment_approved = $args[1]['comment_approved'];
197 }
198 $post = get_post( $comment['comment_post_ID'] );
199 $post_type = get_post_type_object( $post->post_type );
200 $post_type_label = strtolower( $post_type->labels->singular_name );
201 $blog_name = is_multisite() ? '[' . get_bloginfo( 'name' ) . ']' : '';
202 switch ( $hookname ) {
203 case 'comment_post':
204 case 'wp_insert_comment':
205 if ( 'spam' === $comment_approved ) {
206 $comment_status = 'spam';
207 } elseif ( 1 === $comment_approved ) {
208 $comment_status = __( 'approved', 'wpdef' );
209 } else {
210 $comment_status = __( 'pending approval', 'wpdef' );
211 }
212 if ( 0 == $comment['comment_parent'] ) {
213 $text = sprintf(
214 /* translators: */
215 __( '%1$s %2$s commented on %3$s "%4$s" - comment status: %5$s', 'wpdef' ),
216 $blog_name,
217 $comment['comment_author'],
218 $post_type_label,
219 $post->post_title,
220 $comment_status
221 );
222 } else {
223 $parent_comment = get_comment( $comment['comment_parent'] );
224 $text = sprintf(
225 /* translators: */
226 __( "%1\$s %2\$s replied to %3\$s's comment on %4\$s \"%5\$s\" - comment status: %6\$s", 'wpdef' ),
227 $blog_name,
228 $comment['comment_author'],
229 $parent_comment->comment_author,
230 $post_type_label,
231 $post->post_title,
232 $comment_status
233 );
234 }
235 break;
236 case 'deleted_comment':
237 $text = sprintf(
238 /* translators: */
239 __( '%1$s %2$s deleted comment ID %3$s, comment author: %4$s on %5$s "%6$s"', 'wpdef' ),
240 $blog_name,
241 $this->get_user_display( get_current_user_id() ),
242 $comment_id,
243 $comment['comment_author'],
244 $post_type_label,
245 $post->post_title
246 );
247 break;
248 case 'trash_comment':
249 $text = sprintf(
250 /* translators: */
251 __( '%1$s %2$s trashed comment ID %3$s, comment author: %4$s on %5$s "%6$s"', 'wpdef' ),
252 $blog_name,
253 $this->get_user_display( get_current_user_id() ),
254 $comment_id,
255 $comment['comment_author'],
256 $post_type_label,
257 $post->post_title
258 );
259 break;
260 case 'untrash_comment':
261 $text = sprintf(
262 /* translators: */
263 __( '%1$s %2$s untrashed comment ID %3$s, comment author: %4$s on %5$s "%6$s"', 'wpdef' ),
264 $blog_name,
265 $this->get_user_display( get_current_user_id() ),
266 $comment_id,
267 $comment['comment_author'],
268 $post_type_label,
269 $post->post_title
270 );
271 break;
272 case 'spam_comment':
273 $text = sprintf(
274 /* translators: */
275 __( '%1$s %2$s marked comment ID %3$s, comment author: %4$s on %5$s "%6$s" as spam', 'wpdef' ),
276 $blog_name,
277 $this->get_user_display( get_current_user_id() ),
278 $comment_id,
279 $comment['comment_author'],
280 $post_type_label,
281 $post->post_title
282 );
283 break;
284 case 'unspam_comment':
285 $text = sprintf(
286 /* translators: */
287 __( '%1$s %2$s unmarked comment ID %3$s, comment author: %4$s on %5$s "%6$s" as spam', 'wpdef' ),
288 $blog_name,
289 $this->get_user_display( get_current_user_id() ),
290 $comment_id,
291 $comment['comment_author'],
292 $post_type_label,
293 $post->post_title
294 );
295 break;
296 case 'edit_comment':
297 $text = sprintf(
298 /* translators: */
299 __( '%1$s %2$s edited comment ID %3$s, comment author: %4$s on %5$s "%6$s"', 'wpdef' ),
300 $blog_name,
301 $this->get_user_display( get_current_user_id() ),
302 $comment_id,
303 $comment['comment_author'],
304 $post_type_label,
305 $post->post_title
306 );
307 break;
308 default:
309 return false;
310 }
311
312 return array( $text, $post_type_label );
313 }
314 }
315