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