1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Posts sync module. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Constants as Jetpack_Constants; |
11
|
|
|
use Automattic\Jetpack\Roles; |
12
|
|
|
use Automattic\Jetpack\Sync\Settings; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class to handle sync for posts. |
16
|
|
|
*/ |
17
|
|
|
class Posts extends Module { |
18
|
|
|
/** |
19
|
|
|
* The post IDs of posts that were just published but not synced yet. |
20
|
|
|
* |
21
|
|
|
* @access private |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $just_published = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The previous status of posts that we use for calculating post status transitions. |
29
|
|
|
* |
30
|
|
|
* @access private |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $previous_status = array(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Action handler callable. |
38
|
|
|
* |
39
|
|
|
* @access private |
40
|
|
|
* |
41
|
|
|
* @var callable |
42
|
|
|
*/ |
43
|
|
|
private $action_handler; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Import end. |
47
|
|
|
* |
48
|
|
|
* @access private |
49
|
|
|
* |
50
|
|
|
* @todo This appears to be unused - let's remove it. |
51
|
|
|
* |
52
|
|
|
* @var boolean |
53
|
|
|
*/ |
54
|
|
|
private $import_end = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Default previous post state. |
58
|
|
|
* Used for default previous post status. |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
const DEFAULT_PREVIOUS_STATE = 'new'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sync module name. |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function name() { |
74
|
|
|
return 'posts'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* The table in the database. |
79
|
|
|
* |
80
|
|
|
* @access public |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function table_name() { |
85
|
|
|
return 'posts'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retrieve a post by its ID. |
90
|
|
|
* |
91
|
|
|
* @access public |
92
|
|
|
* |
93
|
|
|
* @param string $object_type Type of the sync object. |
94
|
|
|
* @param int $id ID of the sync object. |
95
|
|
|
* @return \WP_Post|bool Filtered \WP_Post object, or false if the object is not a post. |
96
|
|
|
*/ |
97
|
|
|
public function get_object_by_id( $object_type, $id ) { |
98
|
|
|
if ( 'post' === $object_type ) { |
99
|
|
|
$post = get_post( (int) $id ); |
100
|
|
|
if ( $post ) { |
101
|
|
|
return $this->filter_post_content_and_add_links( $post ); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Initialize posts action listeners. |
110
|
|
|
* |
111
|
|
|
* @access public |
112
|
|
|
* |
113
|
|
|
* @param callable $callable Action handler callable. |
114
|
|
|
*/ |
115
|
|
|
public function init_listeners( $callable ) { |
116
|
|
|
$this->action_handler = $callable; |
117
|
|
|
|
118
|
|
|
add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), 11, 3 ); |
119
|
|
|
add_action( 'wp_after_insert_post', array( $this, 'wp_after_insert_post' ), 11, 2 ); |
120
|
|
|
add_action( 'jetpack_sync_save_post', $callable, 10, 4 ); |
121
|
|
|
|
122
|
|
|
add_action( 'deleted_post', $callable, 10 ); |
123
|
|
|
add_action( 'jetpack_published_post', $callable, 10, 2 ); |
124
|
|
|
add_filter( 'jetpack_sync_before_enqueue_deleted_post', array( $this, 'filter_blacklisted_post_types_deleted' ) ); |
125
|
|
|
|
126
|
|
|
add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 ); |
127
|
|
|
add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_post', array( $this, 'filter_blacklisted_post_types' ) ); |
128
|
|
|
|
129
|
|
|
// Listen for meta changes. |
130
|
|
|
$this->init_listeners_for_meta_type( 'post', $callable ); |
131
|
|
|
$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) ); |
132
|
|
|
|
133
|
|
|
add_action( 'jetpack_daily_akismet_meta_cleanup_before', array( $this, 'daily_akismet_meta_cleanup_before' ) ); |
134
|
|
|
add_action( 'jetpack_daily_akismet_meta_cleanup_after', array( $this, 'daily_akismet_meta_cleanup_after' ) ); |
135
|
|
|
add_action( 'jetpack_post_meta_batch_delete', $callable, 10, 2 ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Before Akismet's daily cleanup of spam detection metadata. |
140
|
|
|
* |
141
|
|
|
* @access public |
142
|
|
|
* |
143
|
|
|
* @param array $feedback_ids IDs of feedback posts. |
144
|
|
|
*/ |
145
|
|
|
public function daily_akismet_meta_cleanup_before( $feedback_ids ) { |
146
|
|
|
remove_action( 'deleted_post_meta', $this->action_handler ); |
147
|
|
|
|
148
|
|
|
if ( ! is_array( $feedback_ids ) || count( $feedback_ids ) < 1 ) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$ids_chunks = array_chunk( $feedback_ids, 100, false ); |
153
|
|
|
foreach ( $ids_chunks as $chunk ) { |
154
|
|
|
/** |
155
|
|
|
* Used for syncing deletion of batch post meta |
156
|
|
|
* |
157
|
|
|
* @since 6.1.0 |
158
|
|
|
* |
159
|
|
|
* @module sync |
160
|
|
|
* |
161
|
|
|
* @param array $feedback_ids feedback post IDs |
162
|
|
|
* @param string $meta_key to be deleted |
163
|
|
|
*/ |
164
|
|
|
do_action( 'jetpack_post_meta_batch_delete', $chunk, '_feedback_akismet_values' ); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* After Akismet's daily cleanup of spam detection metadata. |
170
|
|
|
* |
171
|
|
|
* @access public |
172
|
|
|
* |
173
|
|
|
* @param array $feedback_ids IDs of feedback posts. |
174
|
|
|
*/ |
175
|
|
|
public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
176
|
|
|
add_action( 'deleted_post_meta', $this->action_handler ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Initialize posts action listeners for full sync. |
181
|
|
|
* |
182
|
|
|
* @access public |
183
|
|
|
* |
184
|
|
|
* @param callable $callable Action handler callable. |
185
|
|
|
*/ |
186
|
|
|
public function init_full_sync_listeners( $callable ) { |
187
|
|
|
add_action( 'jetpack_full_sync_posts', $callable ); // Also sends post meta. |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Initialize the module in the sender. |
192
|
|
|
* |
193
|
|
|
* @access public |
194
|
|
|
*/ |
195
|
|
|
public function init_before_send() { |
196
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'expand_jetpack_sync_save_post' ) ); |
197
|
|
|
|
198
|
|
|
// Full sync. |
199
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Enqueue the posts actions for full sync. |
204
|
|
|
* |
205
|
|
|
* @access public |
206
|
|
|
* |
207
|
|
|
* @param array $config Full sync configuration for this sync module. |
208
|
|
|
* @param int $max_items_to_enqueue Maximum number of items to enqueue. |
209
|
|
|
* @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
210
|
|
|
* @return array Number of actions enqueued, and next module state. |
211
|
|
|
*/ |
212
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
213
|
|
|
global $wpdb; |
214
|
|
|
|
215
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Retrieve an estimated number of actions that will be enqueued. |
220
|
|
|
* |
221
|
|
|
* @access public |
222
|
|
|
* |
223
|
|
|
* @todo Use $wpdb->prepare for the SQL query. |
224
|
|
|
* |
225
|
|
|
* @param array $config Full sync configuration for this sync module. |
226
|
|
|
* @return array 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->posts WHERE " . $this->get_where_sql( $config ); |
232
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
233
|
|
|
$count = $wpdb->get_var( $query ); |
234
|
|
|
|
235
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Retrieve the WHERE SQL clause based on the module config. |
240
|
|
|
* |
241
|
|
|
* @access public |
242
|
|
|
* |
243
|
|
|
* @param array $config Full sync configuration for this sync module. |
244
|
|
|
* @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
245
|
|
|
*/ |
246
|
|
View Code Duplication |
public function get_where_sql( $config ) { |
247
|
|
|
$where_sql = Settings::get_blacklisted_post_types_sql(); |
248
|
|
|
|
249
|
|
|
// Config is a list of post IDs to sync. |
250
|
|
|
if ( is_array( $config ) ) { |
251
|
|
|
$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $where_sql; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Retrieve the actions that will be sent for this module during a full sync. |
259
|
|
|
* |
260
|
|
|
* @access public |
261
|
|
|
* |
262
|
|
|
* @return array Full sync actions of this module. |
263
|
|
|
*/ |
264
|
|
|
public function get_full_sync_actions() { |
265
|
|
|
return array( 'jetpack_full_sync_posts' ); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Process content before send. |
270
|
|
|
* |
271
|
|
|
* @param array $args Arguments of the `wp_insert_post` hook. |
272
|
|
|
* |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
public function expand_jetpack_sync_save_post( $args ) { |
276
|
|
|
list( $post_id, $post, $update, $previous_state ) = $args; |
277
|
|
|
return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state ); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Filter all blacklisted post types. |
282
|
|
|
* |
283
|
|
|
* @param array $args Hook arguments. |
284
|
|
|
* @return array|false Hook arguments, or false if the post type is a blacklisted one. |
285
|
|
|
*/ |
286
|
|
|
public function filter_blacklisted_post_types_deleted( $args ) { |
287
|
|
|
|
288
|
|
|
// deleted_post is called after the SQL delete but before cache cleanup. |
289
|
|
|
// There is the potential we can't detect post_type at this point. |
290
|
|
|
if ( ! $this->is_post_type_allowed( $args[0] ) ) { |
291
|
|
|
return false; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $args; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Filter all blacklisted post types. |
299
|
|
|
* |
300
|
|
|
* @param array $args Hook arguments. |
301
|
|
|
* @return array|false Hook arguments, or false if the post type is a blacklisted one. |
302
|
|
|
*/ |
303
|
|
|
public function filter_blacklisted_post_types( $args ) { |
304
|
|
|
$post = $args[1]; |
305
|
|
|
|
306
|
|
|
if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) { |
307
|
|
|
return false; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $args; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Filter all meta that is not blacklisted, or is stored for a disallowed post type. |
315
|
|
|
* |
316
|
|
|
* @param array $args Hook arguments. |
317
|
|
|
* @return array|false Hook arguments, or false if meta was filtered. |
318
|
|
|
*/ |
319
|
|
|
public function filter_meta( $args ) { |
320
|
|
|
if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) { |
321
|
|
|
return $args; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return false; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Whether a post meta key is whitelisted. |
329
|
|
|
* |
330
|
|
|
* @param string $meta_key Meta key. |
331
|
|
|
* @return boolean Whether the post meta key is whitelisted. |
332
|
|
|
*/ |
333
|
|
|
public function is_whitelisted_post_meta( $meta_key ) { |
334
|
|
|
// The _wpas_skip_ meta key is used by Publicize. |
335
|
|
|
return in_array( $meta_key, Settings::get_setting( 'post_meta_whitelist' ), true ) || wp_startswith( $meta_key, '_wpas_skip_' ); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Whether a post type is allowed. |
340
|
|
|
* A post type will be disallowed if it's present in the post type blacklist. |
341
|
|
|
* |
342
|
|
|
* @param int $post_id ID of the post. |
343
|
|
|
* @return boolean Whether the post type is allowed. |
344
|
|
|
*/ |
345
|
|
|
public function is_post_type_allowed( $post_id ) { |
346
|
|
|
$post = get_post( (int) $post_id ); |
347
|
|
|
|
348
|
|
|
if ( isset( $post->post_type ) ) { |
349
|
|
|
return ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ); |
350
|
|
|
} |
351
|
|
|
return false; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Remove the embed shortcode. |
356
|
|
|
* |
357
|
|
|
* @global $wp_embed |
358
|
|
|
*/ |
359
|
|
View Code Duplication |
public function remove_embed() { |
360
|
|
|
global $wp_embed; |
361
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
362
|
|
|
// remove the embed shortcode since we would do the part later. |
363
|
|
|
remove_shortcode( 'embed' ); |
364
|
|
|
// Attempts to embed all URLs in a post. |
365
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Add the embed shortcode. |
370
|
|
|
* |
371
|
|
|
* @global $wp_embed |
372
|
|
|
*/ |
373
|
|
View Code Duplication |
public function add_embed() { |
374
|
|
|
global $wp_embed; |
375
|
|
|
add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
376
|
|
|
// Shortcode placeholder for strip_shortcodes(). |
377
|
|
|
add_shortcode( 'embed', '__return_false' ); |
378
|
|
|
// Attempts to embed all URLs in a post. |
379
|
|
|
add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Expands wp_insert_post to include filtered content |
384
|
|
|
* |
385
|
|
|
* @param \WP_Post $post_object Post object. |
386
|
|
|
*/ |
387
|
|
|
public function filter_post_content_and_add_links( $post_object ) { |
388
|
|
|
global $post; |
389
|
|
|
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
390
|
|
|
$post = $post_object; |
391
|
|
|
|
392
|
|
|
// Return non existant post. |
393
|
|
|
$post_type = get_post_type_object( $post->post_type ); |
394
|
|
View Code Duplication |
if ( empty( $post_type ) || ! is_object( $post_type ) ) { |
395
|
|
|
$non_existant_post = new \stdClass(); |
396
|
|
|
$non_existant_post->ID = $post->ID; |
397
|
|
|
$non_existant_post->post_modified = $post->post_modified; |
398
|
|
|
$non_existant_post->post_modified_gmt = $post->post_modified_gmt; |
399
|
|
|
$non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; |
400
|
|
|
$non_existant_post->post_type = $post->post_type; |
401
|
|
|
|
402
|
|
|
return $non_existant_post; |
403
|
|
|
} |
404
|
|
|
/** |
405
|
|
|
* Filters whether to prevent sending post data to .com |
406
|
|
|
* |
407
|
|
|
* Passing true to the filter will prevent the post data from being sent |
408
|
|
|
* to the WordPress.com. |
409
|
|
|
* Instead we pass data that will still enable us to do a checksum against the |
410
|
|
|
* Jetpacks data but will prevent us from displaying the data on in the API as well as |
411
|
|
|
* other services. |
412
|
|
|
* |
413
|
|
|
* @since 4.2.0 |
414
|
|
|
* |
415
|
|
|
* @param boolean false prevent post data from being synced to WordPress.com |
416
|
|
|
* @param mixed $post \WP_Post object |
417
|
|
|
*/ |
418
|
|
View Code Duplication |
if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) { |
|
|
|
|
419
|
|
|
// We only send the bare necessary object to be able to create a checksum. |
420
|
|
|
$blocked_post = new \stdClass(); |
421
|
|
|
$blocked_post->ID = $post->ID; |
422
|
|
|
$blocked_post->post_modified = $post->post_modified; |
423
|
|
|
$blocked_post->post_modified_gmt = $post->post_modified_gmt; |
424
|
|
|
$blocked_post->post_status = 'jetpack_sync_blocked'; |
425
|
|
|
$blocked_post->post_type = $post->post_type; |
426
|
|
|
|
427
|
|
|
return $blocked_post; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
// lets not do oembed just yet. |
431
|
|
|
$this->remove_embed(); |
432
|
|
|
|
433
|
|
|
if ( 0 < strlen( $post->post_password ) ) { |
434
|
|
|
$post->post_password = 'auto-' . wp_generate_password( 10, false ); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** This filter is already documented in core. wp-includes/post-template.php */ |
438
|
|
|
if ( Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { |
439
|
|
|
global $shortcode_tags; |
440
|
|
|
/** |
441
|
|
|
* Filter prevents some shortcodes from expanding. |
442
|
|
|
* |
443
|
|
|
* Since we can can expand some type of shortcode better on the .com side and make the |
444
|
|
|
* expansion more relevant to contexts. For example [galleries] and subscription emails |
445
|
|
|
* |
446
|
|
|
* @since 4.5.0 |
447
|
|
|
* |
448
|
|
|
* @param array of shortcode tags to remove. |
449
|
|
|
*/ |
450
|
|
|
$shortcodes_to_remove = apply_filters( |
451
|
|
|
'jetpack_sync_do_not_expand_shortcodes', |
452
|
|
|
array( |
453
|
|
|
'gallery', |
454
|
|
|
'slideshow', |
455
|
|
|
) |
456
|
|
|
); |
457
|
|
|
$removed_shortcode_callbacks = array(); |
458
|
|
|
foreach ( $shortcodes_to_remove as $shortcode ) { |
459
|
|
|
if ( isset( $shortcode_tags[ $shortcode ] ) ) { |
460
|
|
|
$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ]; |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) ); |
465
|
|
|
|
466
|
|
|
$post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
467
|
|
|
$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); |
468
|
|
|
|
469
|
|
|
foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) { |
470
|
|
|
add_shortcode( $shortcode, $callback ); |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
$this->add_embed(); |
475
|
|
|
|
476
|
|
|
if ( has_post_thumbnail( $post->ID ) ) { |
477
|
|
|
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); |
478
|
|
|
if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { |
479
|
|
|
$post->featured_image = $image_attributes[0]; |
480
|
|
|
} |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
$post->permalink = get_permalink( $post->ID ); |
484
|
|
|
$post->shortlink = wp_get_shortlink( $post->ID ); |
485
|
|
|
|
486
|
|
|
if ( function_exists( 'amp_get_permalink' ) ) { |
487
|
|
|
$post->amp_permalink = amp_get_permalink( $post->ID ); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
return $post; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Handle transition from another post status to a published one. |
495
|
|
|
* |
496
|
|
|
* @param string $new_status New post status. |
497
|
|
|
* @param string $old_status Old post status. |
498
|
|
|
* @param \WP_Post $post Post object. |
499
|
|
|
*/ |
500
|
|
|
public function save_published( $new_status, $old_status, $post ) { |
501
|
|
|
if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
502
|
|
|
$this->just_published[ $post->ID ] = true; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
$this->previous_status[ $post->ID ] = $old_status; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* When publishing or updating a post, the Gutenberg editor sends two requests: |
510
|
|
|
* 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` |
511
|
|
|
* 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` |
512
|
|
|
* |
513
|
|
|
* The 2nd request is to update post meta, which is not supported on WP REST API. |
514
|
|
|
* When syncing post data, we will include if this was a meta box update. |
515
|
|
|
* |
516
|
|
|
* @todo Implement nonce verification. |
517
|
|
|
* |
518
|
|
|
* @return boolean Whether this is a Gutenberg meta box update. |
519
|
|
|
*/ |
520
|
|
|
public function is_gutenberg_meta_box_update() { |
521
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
522
|
|
|
return ( |
523
|
|
|
isset( $_POST['action'], $_GET['classic-editor'], $_GET['meta_box'] ) && |
524
|
|
|
'editpost' === $_POST['action'] && |
525
|
|
|
'1' === $_GET['classic-editor'] && |
526
|
|
|
'1' === $_GET['meta_box'] |
527
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
528
|
|
|
); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Handler for the wp_insert_post hook. |
533
|
|
|
* Called upon creation of a new post. |
534
|
|
|
* |
535
|
|
|
* @param int $post_ID Post ID. |
|
|
|
|
536
|
|
|
* @param \WP_Post $post Post object. |
537
|
|
|
* @param boolean $update Whether this is an existing post being updated or not. |
|
|
|
|
538
|
|
|
*/ |
539
|
|
|
public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
540
|
|
|
if ( ! is_numeric( $post_ID ) || is_null( $post ) ) { |
541
|
|
|
return; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
// Workaround for https://github.com/woocommerce/woocommerce/issues/18007. |
545
|
|
|
if ( $post && 'shop_order' === $post->post_type ) { |
546
|
|
|
$post = get_post( $post_ID ); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
$previous_status = isset( $this->previous_status[ $post_ID ] ) ? $this->previous_status[ $post_ID ] : self::DEFAULT_PREVIOUS_STATE; |
550
|
|
|
|
551
|
|
|
$just_published = isset( $this->just_published[ $post_ID ] ) ? $this->just_published[ $post_ID ] : false; |
552
|
|
|
|
553
|
|
|
$state = array( |
554
|
|
|
'is_auto_save' => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ), |
555
|
|
|
'previous_status' => $previous_status, |
556
|
|
|
'just_published' => $just_published, |
557
|
|
|
'is_gutenberg_meta_box_update' => $this->is_gutenberg_meta_box_update(), |
558
|
|
|
); |
559
|
|
|
/** |
560
|
|
|
* Filter that is used to add to the post flags ( meta data ) when a post gets published |
561
|
|
|
* |
562
|
|
|
* @since 5.8.0 |
563
|
|
|
* |
564
|
|
|
* @param int $post_ID the post ID |
565
|
|
|
* @param mixed $post \WP_Post object |
566
|
|
|
* @param bool $update Whether this is an existing post being updated or not. |
567
|
|
|
* @param mixed $state state |
568
|
|
|
* |
569
|
|
|
* @module sync |
570
|
|
|
*/ |
571
|
|
|
do_action( 'jetpack_sync_save_post', $post_ID, $post, $update, $state ); |
572
|
|
|
unset( $this->previous_status[ $post_ID ] ); |
573
|
|
|
|
574
|
|
|
/* |
575
|
|
|
* WP 5.6 introduced the wp_after_insert_post hook that triggers when |
576
|
|
|
* the post, meta and terms has been updated. We are migrating send_published |
577
|
|
|
* function to this hook to ensure we have all data for WP.com functionality. |
578
|
|
|
* @todo: remove full if statement when WordPress 5.6 is the minimum required version. |
579
|
|
|
*/ |
580
|
|
|
if ( ! function_exists( 'wp_after_insert_post' ) ) { |
581
|
|
|
$this->send_published( $post_ID, $post ); |
582
|
|
|
} |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* Handler for the wp_after_insert_post hook. |
587
|
|
|
* Called after creation/update of a new post. |
588
|
|
|
* |
589
|
|
|
* @param int $post_ID Post ID. |
590
|
|
|
* @param \WP_Post $post Post object. |
591
|
|
|
**/ |
592
|
|
|
public function wp_after_insert_post( $post_ID, $post ) { |
593
|
|
|
if ( ! is_numeric( $post_ID ) || is_null( $post ) ) { |
594
|
|
|
return; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
// Workaround for https://github.com/woocommerce/woocommerce/issues/18007. |
598
|
|
|
if ( $post && 'shop_order' === $post->post_type ) { |
599
|
|
|
$post = get_post( $post_ID ); |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
$this->send_published( $post_ID, $post ); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* Send a published post for sync. |
607
|
|
|
* |
608
|
|
|
* @param int $post_ID Post ID. |
609
|
|
|
* @param \WP_Post $post Post object. |
610
|
|
|
*/ |
611
|
|
|
public function send_published( $post_ID, $post ) { |
612
|
|
|
if ( ! isset( $this->just_published[ $post_ID ] ) ) { |
613
|
|
|
return; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
// Post revisions cause race conditions where this send_published add the action before the actual post gets synced. |
617
|
|
|
if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
618
|
|
|
return; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
$post_flags = array( |
622
|
|
|
'post_type' => $post->post_type, |
623
|
|
|
); |
624
|
|
|
|
625
|
|
|
$author_user_object = get_user_by( 'id', $post->post_author ); |
626
|
|
|
if ( $author_user_object ) { |
627
|
|
|
$roles = new Roles(); |
628
|
|
|
|
629
|
|
|
$post_flags['author'] = array( |
630
|
|
|
'id' => $post->post_author, |
631
|
|
|
'wpcom_user_id' => get_user_meta( $post->post_author, 'wpcom_user_id', true ), |
632
|
|
|
'display_name' => $author_user_object->display_name, |
633
|
|
|
'email' => $author_user_object->user_email, |
634
|
|
|
'translated_role' => $roles->translate_user_to_role( $author_user_object ), |
635
|
|
|
); |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* Filter that is used to add to the post flags ( meta data ) when a post gets published |
640
|
|
|
* |
641
|
|
|
* @since 4.4.0 |
642
|
|
|
* |
643
|
|
|
* @param mixed array post flags that are added to the post |
644
|
|
|
* @param mixed $post \WP_Post object |
645
|
|
|
*/ |
646
|
|
|
$flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post ); |
|
|
|
|
647
|
|
|
|
648
|
|
|
// Only Send Pulished Post event if post_type is not blacklisted. |
649
|
|
|
if ( ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) { |
650
|
|
|
/** |
651
|
|
|
* Action that gets synced when a post type gets published. |
652
|
|
|
* |
653
|
|
|
* @since 4.4.0 |
654
|
|
|
* |
655
|
|
|
* @param int $post_ID |
656
|
|
|
* @param mixed array $flags post flags that are added to the post |
657
|
|
|
*/ |
658
|
|
|
do_action( 'jetpack_published_post', $post_ID, $flags ); |
659
|
|
|
} |
660
|
|
|
unset( $this->just_published[ $post_ID ] ); |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* Send additional sync action for Activity Log when post is a Customizer publish |
664
|
|
|
*/ |
665
|
|
|
if ( 'customize_changeset' === $post->post_type ) { |
666
|
|
|
$post_content = json_decode( $post->post_content, true ); |
667
|
|
|
foreach ( $post_content as $key => $value ) { |
668
|
|
|
// Skip if it isn't a widget. |
669
|
|
|
if ( 'widget_' !== substr( $key, 0, strlen( 'widget_' ) ) ) { |
670
|
|
|
continue; |
671
|
|
|
} |
672
|
|
|
// Change key from "widget_archives[2]" to "archives-2". |
673
|
|
|
$key = str_replace( 'widget_', '', $key ); |
674
|
|
|
$key = str_replace( '[', '-', $key ); |
675
|
|
|
$key = str_replace( ']', '', $key ); |
676
|
|
|
|
677
|
|
|
global $wp_registered_widgets; |
678
|
|
|
if ( isset( $wp_registered_widgets[ $key ] ) ) { |
679
|
|
|
$widget_data = array( |
680
|
|
|
'name' => $wp_registered_widgets[ $key ]['name'], |
681
|
|
|
'id' => $key, |
682
|
|
|
'title' => $value['value']['title'], |
683
|
|
|
); |
684
|
|
|
do_action( 'jetpack_widget_edited', $widget_data ); |
685
|
|
|
} |
686
|
|
|
} |
687
|
|
|
} |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
/** |
691
|
|
|
* Expand post IDs to post objects within a hook before they are serialized and sent to the server. |
692
|
|
|
* |
693
|
|
|
* @access public |
694
|
|
|
* |
695
|
|
|
* @param array $args The hook parameters. |
696
|
|
|
* @return array $args The expanded hook parameters. |
697
|
|
|
*/ |
698
|
|
|
public function expand_post_ids( $args ) { |
699
|
|
|
list( $post_ids, $previous_interval_end) = $args; |
700
|
|
|
|
701
|
|
|
$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) ); |
702
|
|
|
$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts ); |
703
|
|
|
$posts = array_values( $posts ); // Reindex in case posts were deleted. |
704
|
|
|
|
705
|
|
|
return array( |
706
|
|
|
$posts, |
707
|
|
|
$this->get_metadata( $post_ids, 'post', Settings::get_setting( 'post_meta_whitelist' ) ), |
708
|
|
|
$this->get_term_relationships( $post_ids ), |
709
|
|
|
$previous_interval_end, |
710
|
|
|
); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
715
|
|
|
* |
716
|
|
|
* @access public |
717
|
|
|
* |
718
|
|
|
* @param int $batch_size The batch size for objects. |
719
|
|
|
* @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
720
|
|
|
* |
721
|
|
|
* @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. |
722
|
|
|
*/ |
723
|
|
|
public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { |
724
|
|
|
return parent::get_min_max_object_ids_for_batches( $batch_size, $this->get_where_sql( $where_sql ) ); |
|
|
|
|
725
|
|
|
} |
726
|
|
|
} |
727
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.