1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Comments sync module. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Sync\Settings; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class to handle sync for comments. |
14
|
|
|
*/ |
15
|
|
|
class Comments extends Module { |
16
|
|
|
/** |
17
|
|
|
* An estimate of how many rows per second can be synced during a full sync. |
18
|
|
|
* |
19
|
|
|
* @access static |
20
|
|
|
* |
21
|
|
|
* @var int|null Null if speed is not important in a full sync. |
22
|
|
|
*/ |
23
|
|
|
static $sync_speed = 43; |
24
|
|
|
/** |
25
|
|
|
* Sync module name. |
26
|
|
|
* |
27
|
|
|
* @access public |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function name() { |
32
|
|
|
return 'comments'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The id field in the database. |
37
|
|
|
* |
38
|
|
|
* @access public |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function id_field() { |
43
|
|
|
return 'comment_ID'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The table in the database. |
48
|
|
|
* |
49
|
|
|
* @access public |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function table_name() { |
54
|
|
|
return 'comments'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retrieve a comment by its ID. |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
* |
62
|
|
|
* @param string $object_type Type of the sync object. |
63
|
|
|
* @param int $id ID of the sync object. |
64
|
|
|
* @return \WP_Comment|bool Filtered \WP_Comment object, or false if the object is not a comment. |
65
|
|
|
*/ |
66
|
|
|
public function get_object_by_id( $object_type, $id ) { |
67
|
|
|
$comment_id = intval( $id ); |
68
|
|
|
if ( 'comment' === $object_type ) { |
69
|
|
|
$comment = get_comment( $comment_id ); |
70
|
|
|
if ( $comment ) { |
71
|
|
|
return $this->filter_comment( $comment ); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Initialize comments action listeners. |
80
|
|
|
* Also responsible for initializing comment meta listeners. |
81
|
|
|
* |
82
|
|
|
* @access public |
83
|
|
|
* |
84
|
|
|
* @param callable $callable Action handler callable. |
85
|
|
|
*/ |
86
|
|
|
public function init_listeners( $callable ) { |
87
|
|
|
add_action( 'wp_insert_comment', $callable, 10, 2 ); |
88
|
|
|
add_action( 'deleted_comment', $callable ); |
89
|
|
|
add_action( 'trashed_comment', $callable ); |
90
|
|
|
add_action( 'spammed_comment', $callable ); |
91
|
|
|
add_action( 'trashed_post_comments', $callable, 10, 2 ); |
92
|
|
|
add_action( 'untrash_post_comments', $callable ); |
93
|
|
|
add_action( 'comment_approved_to_unapproved', $callable ); |
94
|
|
|
add_action( 'comment_unapproved_to_approved', $callable ); |
95
|
|
|
add_action( 'jetpack_modified_comment_contents', $callable, 10, 2 ); |
96
|
|
|
add_action( 'untrashed_comment', $callable, 10, 2 ); |
97
|
|
|
add_action( 'unspammed_comment', $callable, 10, 2 ); |
98
|
|
|
add_filter( 'wp_update_comment_data', array( $this, 'handle_comment_contents_modification' ), 10, 3 ); |
99
|
|
|
add_filter( 'jetpack_sync_before_enqueue_wp_insert_comment', array( $this, 'only_allow_white_listed_comment_types' ) ); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Even though it's messy, we implement these hooks because |
103
|
|
|
* the edit_comment hook doesn't include the data |
104
|
|
|
* so this saves us a DB read for every comment event. |
105
|
|
|
*/ |
106
|
|
|
foreach ( $this->get_whitelisted_comment_types() as $comment_type ) { |
107
|
|
|
foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
108
|
|
|
$comment_action_name = "comment_{$comment_status}_{$comment_type}"; |
109
|
|
|
add_action( $comment_action_name, $callable, 10, 2 ); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Listen for meta changes. |
114
|
|
|
$this->init_listeners_for_meta_type( 'comment', $callable ); |
115
|
|
|
$this->init_meta_whitelist_handler( 'comment', array( $this, 'filter_meta' ) ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Handler for any comment content updates. |
120
|
|
|
* |
121
|
|
|
* @access public |
122
|
|
|
* |
123
|
|
|
* @param array $new_comment The new, processed comment data. |
124
|
|
|
* @param array $old_comment The old, unslashed comment data. |
125
|
|
|
* @param array $new_comment_with_slashes The new, raw comment data. |
126
|
|
|
* @return array The new, processed comment data. |
127
|
|
|
*/ |
128
|
|
|
public function handle_comment_contents_modification( $new_comment, $old_comment, $new_comment_with_slashes ) { |
129
|
|
|
$changes = array(); |
130
|
|
|
$content_fields = array( |
131
|
|
|
'comment_author', |
132
|
|
|
'comment_author_email', |
133
|
|
|
'comment_author_url', |
134
|
|
|
'comment_content', |
135
|
|
|
); |
136
|
|
|
foreach ( $content_fields as $field ) { |
137
|
|
|
if ( $new_comment_with_slashes[ $field ] !== $old_comment[ $field ] ) { |
138
|
|
|
$changes[ $field ] = array( $new_comment[ $field ], $old_comment[ $field ] ); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ( ! empty( $changes ) ) { |
143
|
|
|
/** |
144
|
|
|
* Signals to the sync listener that this comment's contents were modified and a sync action |
145
|
|
|
* reflecting the change(s) to the content should be sent |
146
|
|
|
* |
147
|
|
|
* @since 4.9.0 |
148
|
|
|
* |
149
|
|
|
* @param int $new_comment['comment_ID'] ID of comment whose content was modified |
150
|
|
|
* @param mixed $changes Array of changed comment fields with before and after values |
151
|
|
|
*/ |
152
|
|
|
do_action( 'jetpack_modified_comment_contents', $new_comment['comment_ID'], $changes ); |
153
|
|
|
} |
154
|
|
|
return $new_comment; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Initialize comments action listeners for full sync. |
159
|
|
|
* |
160
|
|
|
* @access public |
161
|
|
|
* |
162
|
|
|
* @param callable $callable Action handler callable. |
163
|
|
|
*/ |
164
|
|
|
public function init_full_sync_listeners( $callable ) { |
165
|
|
|
add_action( 'jetpack_full_sync_comments', $callable ); // Also send comments meta. |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Gets a filtered list of comment types that sync can hook into. |
170
|
|
|
* |
171
|
|
|
* @access public |
172
|
|
|
* |
173
|
|
|
* @return array Defaults to [ '', 'trackback', 'pingback' ]. |
174
|
|
|
*/ |
175
|
|
|
public function get_whitelisted_comment_types() { |
176
|
|
|
/** |
177
|
|
|
* Comment types present in this list will sync their status changes to WordPress.com. |
178
|
|
|
* |
179
|
|
|
* @since 7.6.0 |
180
|
|
|
* |
181
|
|
|
* @param array A list of comment types. |
182
|
|
|
*/ |
183
|
|
|
return apply_filters( |
184
|
|
|
'jetpack_sync_whitelisted_comment_types', |
185
|
|
|
array( '', 'trackback', 'pingback' ) |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Prevents any comment types that are not in the whitelist from being enqueued and sent to WordPress.com. |
191
|
|
|
* |
192
|
|
|
* @param array $args Arguments passed to wp_insert_comment. |
193
|
|
|
* |
194
|
|
|
* @return bool or array $args Arguments passed to wp_insert_comment |
195
|
|
|
*/ |
196
|
|
|
public function only_allow_white_listed_comment_types( $args ) { |
197
|
|
|
$comment = $args[1]; |
198
|
|
|
|
199
|
|
|
if ( ! in_array( $comment->comment_type, $this->get_whitelisted_comment_types(), true ) ) { |
200
|
|
|
return false; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $args; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Initialize the module in the sender. |
208
|
|
|
* |
209
|
|
|
* @access public |
210
|
|
|
*/ |
211
|
|
|
public function init_before_send() { |
212
|
|
|
add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) ); |
213
|
|
|
|
214
|
|
|
foreach ( $this->get_whitelisted_comment_types() as $comment_type ) { |
215
|
|
|
foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
216
|
|
|
$comment_action_name = "comment_{$comment_status}_{$comment_type}"; |
217
|
|
|
add_filter( |
218
|
|
|
'jetpack_sync_before_send_' . $comment_action_name, |
219
|
|
|
array( |
220
|
|
|
$this, |
221
|
|
|
'expand_wp_insert_comment', |
222
|
|
|
) |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// Full sync. |
228
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Enqueue the comments actions for full sync. |
233
|
|
|
* |
234
|
|
|
* @access public |
235
|
|
|
* |
236
|
|
|
* @param array $config Full sync configuration for this sync module. |
237
|
|
|
* @param int $max_items_to_enqueue Maximum number of items to enqueue. |
238
|
|
|
* @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
239
|
|
|
* @return array Number of actions enqueued, and next module state. |
240
|
|
|
*/ |
241
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
242
|
|
|
global $wpdb; |
243
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Retrieve an estimated number of actions that will be enqueued. |
248
|
|
|
* |
249
|
|
|
* @access public |
250
|
|
|
* |
251
|
|
|
* @param array $config Full sync configuration for this sync module. |
252
|
|
|
* @return int Number of items yet to be enqueued. |
253
|
|
|
*/ |
254
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
255
|
|
|
global $wpdb; |
256
|
|
|
|
257
|
|
|
$query = "SELECT count(*) FROM $wpdb->comments"; |
258
|
|
|
|
259
|
|
|
$where_sql = $this->get_where_sql( $config ); |
260
|
|
|
if ( $where_sql ) { |
261
|
|
|
$query .= ' WHERE ' . $where_sql; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// TODO: Call $wpdb->prepare on the following query. |
265
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
266
|
|
|
$count = $wpdb->get_var( $query ); |
267
|
|
|
|
268
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Retrieve the WHERE SQL clause based on the module config. |
273
|
|
|
* |
274
|
|
|
* @access public |
275
|
|
|
* |
276
|
|
|
* @param array $config Full sync configuration for this sync module. |
277
|
|
|
* @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
278
|
|
|
*/ |
279
|
|
|
public function get_where_sql( $config ) { |
280
|
|
|
if ( is_array( $config ) ) { |
281
|
|
|
return 'comment_ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return '1=1'; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Retrieve the actions that will be sent for this module during a full sync. |
289
|
|
|
* |
290
|
|
|
* @access public |
291
|
|
|
* |
292
|
|
|
* @return array Full sync actions of this module. |
293
|
|
|
*/ |
294
|
|
|
public function get_full_sync_actions() { |
295
|
|
|
return array( 'jetpack_full_sync_comments' ); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Count all the actions that are going to be sent. |
300
|
|
|
* |
301
|
|
|
* @access public |
302
|
|
|
* |
303
|
|
|
* @param array $action_names Names of all the actions that will be sent. |
304
|
|
|
* @return int Number of actions. |
305
|
|
|
*/ |
306
|
|
|
public function count_full_sync_actions( $action_names ) { |
307
|
|
|
return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) ); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Expand the comment status change before the data is serialized and sent to the server. |
312
|
|
|
* |
313
|
|
|
* @access public |
314
|
|
|
* @todo This is not used currently - let's implement it. |
315
|
|
|
* |
316
|
|
|
* @param array $args The hook parameters. |
317
|
|
|
* @return array The expanded hook parameters. |
318
|
|
|
*/ |
319
|
|
|
public function expand_wp_comment_status_change( $args ) { |
320
|
|
|
return array( $args[0], $this->filter_comment( $args[1] ) ); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Expand the comment creation before the data is serialized and sent to the server. |
325
|
|
|
* |
326
|
|
|
* @access public |
327
|
|
|
* |
328
|
|
|
* @param array $args The hook parameters. |
329
|
|
|
* @return array The expanded hook parameters. |
330
|
|
|
*/ |
331
|
|
|
public function expand_wp_insert_comment( $args ) { |
332
|
|
|
return array( $args[0], $this->filter_comment( $args[1] ) ); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Filter a comment object to the fields we need. |
337
|
|
|
* |
338
|
|
|
* @access public |
339
|
|
|
* |
340
|
|
|
* @param \WP_Comment $comment The unfiltered comment object. |
341
|
|
|
* @return \WP_Comment Filtered comment object. |
342
|
|
|
*/ |
343
|
|
|
public function filter_comment( $comment ) { |
344
|
|
|
/** |
345
|
|
|
* Filters whether to prevent sending comment data to .com |
346
|
|
|
* |
347
|
|
|
* Passing true to the filter will prevent the comment data from being sent |
348
|
|
|
* to the WordPress.com. |
349
|
|
|
* Instead we pass data that will still enable us to do a checksum against the |
350
|
|
|
* Jetpacks data but will prevent us from displaying the data on in the API as well as |
351
|
|
|
* other services. |
352
|
|
|
* |
353
|
|
|
* @since 4.2.0 |
354
|
|
|
* |
355
|
|
|
* @param boolean false prevent post data from bing synced to WordPress.com |
356
|
|
|
* @param mixed $comment WP_COMMENT object |
357
|
|
|
*/ |
358
|
|
|
if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) { |
359
|
|
|
$blocked_comment = new \stdClass(); |
360
|
|
|
$blocked_comment->comment_ID = $comment->comment_ID; |
361
|
|
|
$blocked_comment->comment_date = $comment->comment_date; |
362
|
|
|
$blocked_comment->comment_date_gmt = $comment->comment_date_gmt; |
363
|
|
|
$blocked_comment->comment_approved = 'jetpack_sync_blocked'; |
364
|
|
|
return $blocked_comment; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
return $comment; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Whether a certain comment meta key is whitelisted for sync. |
372
|
|
|
* |
373
|
|
|
* @access public |
374
|
|
|
* |
375
|
|
|
* @param string $meta_key Comment meta key. |
376
|
|
|
* @return boolean Whether the meta key is whitelisted. |
377
|
|
|
*/ |
378
|
|
|
public function is_whitelisted_comment_meta( $meta_key ) { |
379
|
|
|
return in_array( $meta_key, Settings::get_setting( 'comment_meta_whitelist' ), true ); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Handler for filtering out non-whitelisted comment meta. |
384
|
|
|
* |
385
|
|
|
* @access public |
386
|
|
|
* |
387
|
|
|
* @param array $args Hook args. |
388
|
|
|
* @return array|boolean False if not whitelisted, the original hook args otherwise. |
389
|
|
|
*/ |
390
|
|
|
public function filter_meta( $args ) { |
391
|
|
|
return ( $this->is_whitelisted_comment_meta( $args[2] ) ? $args : false ); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Expand the comment IDs to comment objects and meta before being serialized and sent to the server. |
396
|
|
|
* |
397
|
|
|
* @access public |
398
|
|
|
* |
399
|
|
|
* @param array $args The hook parameters. |
400
|
|
|
* @return array The expanded hook parameters. |
401
|
|
|
*/ |
402
|
|
|
public function expand_comment_ids( $args ) { |
403
|
|
|
list( $comment_ids, $previous_interval_end ) = $args; |
404
|
|
|
$comments = get_comments( |
405
|
|
|
array( |
406
|
|
|
'include_unapproved' => true, |
407
|
|
|
'comment__in' => $comment_ids, |
408
|
|
|
'orderby' => 'comment_ID', |
409
|
|
|
'order' => 'DESC', |
410
|
|
|
) |
411
|
|
|
); |
412
|
|
|
|
413
|
|
|
return array( |
414
|
|
|
$comments, |
415
|
|
|
$this->get_metadata( $comment_ids, 'comment', Settings::get_setting( 'comment_meta_whitelist' ) ), |
416
|
|
|
$previous_interval_end, |
417
|
|
|
); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Gets the sync speed of a module. |
422
|
|
|
* |
423
|
|
|
* @access public |
424
|
|
|
* |
425
|
|
|
* @return int |
426
|
|
|
*/ |
427
|
|
|
public function get_sync_speed() { |
428
|
|
|
return self::$sync_speed; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|