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|bool |
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( intval( $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( 'jetpack_sync_save_post', $callable, 10, 4 ); |
120
|
|
|
|
121
|
|
|
add_action( 'deleted_post', $callable, 10 ); |
122
|
|
|
add_action( 'jetpack_published_post', $callable, 10, 2 ); |
123
|
|
|
|
124
|
|
|
add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 ); |
125
|
|
|
add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_post', array( $this, 'filter_blacklisted_post_types' ) ); |
126
|
|
|
|
127
|
|
|
// Listen for meta changes. |
128
|
|
|
$this->init_listeners_for_meta_type( 'post', $callable ); |
129
|
|
|
$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) ); |
130
|
|
|
|
131
|
|
|
add_action( 'jetpack_daily_akismet_meta_cleanup_before', array( $this, 'daily_akismet_meta_cleanup_before' ) ); |
132
|
|
|
add_action( 'jetpack_daily_akismet_meta_cleanup_after', array( $this, 'daily_akismet_meta_cleanup_after' ) ); |
133
|
|
|
add_action( 'jetpack_post_meta_batch_delete', $callable, 10, 2 ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Before Akismet's daily cleanup of spam detection metadata. |
138
|
|
|
* |
139
|
|
|
* @access public |
140
|
|
|
* |
141
|
|
|
* @param array $feedback_ids IDs of feedback posts. |
142
|
|
|
*/ |
143
|
|
|
public function daily_akismet_meta_cleanup_before( $feedback_ids ) { |
144
|
|
|
remove_action( 'deleted_post_meta', $this->action_handler ); |
145
|
|
|
/** |
146
|
|
|
* Used for syncing deletion of batch post meta |
147
|
|
|
* |
148
|
|
|
* @since 6.1.0 |
149
|
|
|
* |
150
|
|
|
* @module sync |
151
|
|
|
* |
152
|
|
|
* @param array $feedback_ids feedback post IDs |
153
|
|
|
* @param string $meta_key to be deleted |
154
|
|
|
*/ |
155
|
|
|
do_action( 'jetpack_post_meta_batch_delete', $feedback_ids, '_feedback_akismet_values' ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* After Akismet's daily cleanup of spam detection metadata. |
160
|
|
|
* |
161
|
|
|
* @access public |
162
|
|
|
* |
163
|
|
|
* @param array $feedback_ids IDs of feedback posts. |
164
|
|
|
*/ |
165
|
|
|
public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
166
|
|
|
add_action( 'deleted_post_meta', $this->action_handler ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Initialize posts action listeners for full sync. |
171
|
|
|
* |
172
|
|
|
* @access public |
173
|
|
|
* |
174
|
|
|
* @param callable $callable Action handler callable. |
175
|
|
|
*/ |
176
|
|
|
public function init_full_sync_listeners( $callable ) { |
177
|
|
|
add_action( 'jetpack_full_sync_posts', $callable ); // Also sends post meta. |
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_jetpack_sync_save_post', array( $this, 'expand_jetpack_sync_save_post' ) ); |
187
|
|
|
|
188
|
|
|
// Full sync. |
189
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Enqueue the posts actions for full sync. |
194
|
|
|
* |
195
|
|
|
* @access public |
196
|
|
|
* |
197
|
|
|
* @param array $config Full sync configuration for this sync module. |
198
|
|
|
* @param int $max_items_to_enqueue Maximum number of items to enqueue. |
199
|
|
|
* @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
200
|
|
|
* @return array Number of actions enqueued, and next module state. |
201
|
|
|
*/ |
202
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
203
|
|
|
global $wpdb; |
204
|
|
|
|
205
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Retrieve an estimated number of actions that will be enqueued. |
210
|
|
|
* |
211
|
|
|
* @access public |
212
|
|
|
* |
213
|
|
|
* @todo Use $wpdb->prepare for the SQL query. |
214
|
|
|
* |
215
|
|
|
* @param array $config Full sync configuration for this sync module. |
216
|
|
|
* @return array Number of items yet to be enqueued. |
217
|
|
|
*/ |
218
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
219
|
|
|
global $wpdb; |
220
|
|
|
|
221
|
|
|
$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config ); |
222
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
223
|
|
|
$count = $wpdb->get_var( $query ); |
224
|
|
|
|
225
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Retrieve the WHERE SQL clause based on the module config. |
230
|
|
|
* |
231
|
|
|
* @access public |
232
|
|
|
* |
233
|
|
|
* @param array $config Full sync configuration for this sync module. |
234
|
|
|
* @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
235
|
|
|
*/ |
236
|
|
View Code Duplication |
public function get_where_sql( $config ) { |
237
|
|
|
$where_sql = Settings::get_blacklisted_post_types_sql(); |
238
|
|
|
|
239
|
|
|
// Config is a list of post IDs to sync. |
240
|
|
|
if ( is_array( $config ) ) { |
241
|
|
|
$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $where_sql; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Retrieve the actions that will be sent for this module during a full sync. |
249
|
|
|
* |
250
|
|
|
* @access public |
251
|
|
|
* |
252
|
|
|
* @return array Full sync actions of this module. |
253
|
|
|
*/ |
254
|
|
|
public function get_full_sync_actions() { |
255
|
|
|
return array( 'jetpack_full_sync_posts' ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Process content before send. |
260
|
|
|
* |
261
|
|
|
* @param array $args Arguments of the `wp_insert_post` hook. |
262
|
|
|
* |
263
|
|
|
* @return array |
264
|
|
|
*/ |
265
|
|
|
public function expand_jetpack_sync_save_post( $args ) { |
266
|
|
|
list( $post_id, $post, $update, $previous_state ) = $args; |
267
|
|
|
return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state ); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Filter all blacklisted post types. |
272
|
|
|
* |
273
|
|
|
* @param array $args Hook arguments. |
274
|
|
|
* @return array|false Hook arguments, or false if the post type is a blacklisted one. |
275
|
|
|
*/ |
276
|
|
|
public function filter_blacklisted_post_types( $args ) { |
277
|
|
|
$post = $args[1]; |
278
|
|
|
|
279
|
|
|
if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) { |
280
|
|
|
return false; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return $args; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Filter all meta that is not blacklisted, or is stored for a disallowed post type. |
288
|
|
|
* |
289
|
|
|
* @param array $args Hook arguments. |
290
|
|
|
* @return array|false Hook arguments, or false if meta was filtered. |
291
|
|
|
*/ |
292
|
|
|
public function filter_meta( $args ) { |
293
|
|
|
if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) { |
294
|
|
|
return $args; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return false; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Whether a post meta key is whitelisted. |
302
|
|
|
* |
303
|
|
|
* @param string $meta_key Meta key. |
304
|
|
|
* @return boolean Whether the post meta key is whitelisted. |
305
|
|
|
*/ |
306
|
|
|
public function is_whitelisted_post_meta( $meta_key ) { |
307
|
|
|
// The _wpas_skip_ meta key is used by Publicize. |
308
|
|
|
return in_array( $meta_key, Settings::get_setting( 'post_meta_whitelist' ), true ) || wp_startswith( $meta_key, '_wpas_skip_' ); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Whether a post type is allowed. |
313
|
|
|
* A post type will be disallowed if it's present in the post type blacklist. |
314
|
|
|
* |
315
|
|
|
* @param int $post_id ID of the post. |
316
|
|
|
* @return boolean Whether the post type is allowed. |
317
|
|
|
*/ |
318
|
|
|
public function is_post_type_allowed( $post_id ) { |
319
|
|
|
$post = get_post( intval( $post_id ) ); |
320
|
|
|
if ( $post->post_type ) { |
321
|
|
|
return ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ); |
322
|
|
|
} |
323
|
|
|
return false; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Remove the embed shortcode. |
328
|
|
|
* |
329
|
|
|
* @global $wp_embed |
330
|
|
|
*/ |
331
|
|
View Code Duplication |
public function remove_embed() { |
332
|
|
|
global $wp_embed; |
333
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
334
|
|
|
// remove the embed shortcode since we would do the part later. |
335
|
|
|
remove_shortcode( 'embed' ); |
336
|
|
|
// Attempts to embed all URLs in a post. |
337
|
|
|
remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Add the embed shortcode. |
342
|
|
|
* |
343
|
|
|
* @global $wp_embed |
344
|
|
|
*/ |
345
|
|
View Code Duplication |
public function add_embed() { |
346
|
|
|
global $wp_embed; |
347
|
|
|
add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 ); |
348
|
|
|
// Shortcode placeholder for strip_shortcodes(). |
349
|
|
|
add_shortcode( 'embed', '__return_false' ); |
350
|
|
|
// Attempts to embed all URLs in a post. |
351
|
|
|
add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Expands wp_insert_post to include filtered content |
356
|
|
|
* |
357
|
|
|
* @param \WP_Post $post_object Post object. |
358
|
|
|
*/ |
359
|
|
|
public function filter_post_content_and_add_links( $post_object ) { |
360
|
|
|
global $post; |
361
|
|
|
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
362
|
|
|
$post = $post_object; |
363
|
|
|
|
364
|
|
|
// Return non existant post. |
365
|
|
|
$post_type = get_post_type_object( $post->post_type ); |
366
|
|
View Code Duplication |
if ( empty( $post_type ) || ! is_object( $post_type ) ) { |
367
|
|
|
$non_existant_post = new \stdClass(); |
368
|
|
|
$non_existant_post->ID = $post->ID; |
369
|
|
|
$non_existant_post->post_modified = $post->post_modified; |
370
|
|
|
$non_existant_post->post_modified_gmt = $post->post_modified_gmt; |
371
|
|
|
$non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; |
372
|
|
|
$non_existant_post->post_type = $post->post_type; |
373
|
|
|
|
374
|
|
|
return $non_existant_post; |
375
|
|
|
} |
376
|
|
|
/** |
377
|
|
|
* Filters whether to prevent sending post data to .com |
378
|
|
|
* |
379
|
|
|
* Passing true to the filter will prevent the post data from being sent |
380
|
|
|
* to the WordPress.com. |
381
|
|
|
* Instead we pass data that will still enable us to do a checksum against the |
382
|
|
|
* Jetpacks data but will prevent us from displaying the data on in the API as well as |
383
|
|
|
* other services. |
384
|
|
|
* |
385
|
|
|
* @since 4.2.0 |
386
|
|
|
* |
387
|
|
|
* @param boolean false prevent post data from being synced to WordPress.com |
388
|
|
|
* @param mixed $post \WP_Post object |
389
|
|
|
*/ |
390
|
|
View Code Duplication |
if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) { |
391
|
|
|
// We only send the bare necessary object to be able to create a checksum. |
392
|
|
|
$blocked_post = new \stdClass(); |
393
|
|
|
$blocked_post->ID = $post->ID; |
394
|
|
|
$blocked_post->post_modified = $post->post_modified; |
395
|
|
|
$blocked_post->post_modified_gmt = $post->post_modified_gmt; |
396
|
|
|
$blocked_post->post_status = 'jetpack_sync_blocked'; |
397
|
|
|
$blocked_post->post_type = $post->post_type; |
398
|
|
|
|
399
|
|
|
return $blocked_post; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
// lets not do oembed just yet. |
403
|
|
|
$this->remove_embed(); |
404
|
|
|
|
405
|
|
|
if ( 0 < strlen( $post->post_password ) ) { |
406
|
|
|
$post->post_password = 'auto-' . wp_generate_password( 10, false ); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** This filter is already documented in core. wp-includes/post-template.php */ |
410
|
|
|
if ( Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { |
411
|
|
|
global $shortcode_tags; |
412
|
|
|
/** |
413
|
|
|
* Filter prevents some shortcodes from expanding. |
414
|
|
|
* |
415
|
|
|
* Since we can can expand some type of shortcode better on the .com side and make the |
416
|
|
|
* expansion more relevant to contexts. For example [galleries] and subscription emails |
417
|
|
|
* |
418
|
|
|
* @since 4.5.0 |
419
|
|
|
* |
420
|
|
|
* @param array of shortcode tags to remove. |
421
|
|
|
*/ |
422
|
|
|
$shortcodes_to_remove = apply_filters( |
423
|
|
|
'jetpack_sync_do_not_expand_shortcodes', |
424
|
|
|
array( |
425
|
|
|
'gallery', |
426
|
|
|
'slideshow', |
427
|
|
|
) |
428
|
|
|
); |
429
|
|
|
$removed_shortcode_callbacks = array(); |
430
|
|
|
foreach ( $shortcodes_to_remove as $shortcode ) { |
431
|
|
|
if ( isset( $shortcode_tags[ $shortcode ] ) ) { |
432
|
|
|
$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ]; |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) ); |
437
|
|
|
|
438
|
|
|
$post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
439
|
|
|
$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); |
440
|
|
|
|
441
|
|
|
foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) { |
442
|
|
|
add_shortcode( $shortcode, $callback ); |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
$this->add_embed(); |
447
|
|
|
|
448
|
|
|
if ( has_post_thumbnail( $post->ID ) ) { |
449
|
|
|
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); |
450
|
|
|
if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { |
451
|
|
|
$post->featured_image = $image_attributes[0]; |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
$post->permalink = get_permalink( $post->ID ); |
456
|
|
|
$post->shortlink = wp_get_shortlink( $post->ID ); |
457
|
|
|
|
458
|
|
|
if ( function_exists( 'amp_get_permalink' ) ) { |
459
|
|
|
$post->amp_permalink = amp_get_permalink( $post->ID ); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
return $post; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Handle transition from another post status to a published one. |
467
|
|
|
* |
468
|
|
|
* @param string $new_status New post status. |
469
|
|
|
* @param string $old_status Old post status. |
470
|
|
|
* @param \WP_Post $post Post object. |
471
|
|
|
*/ |
472
|
|
|
public function save_published( $new_status, $old_status, $post ) { |
473
|
|
|
if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
474
|
|
|
$this->just_published[ $post->ID ] = true; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
$this->previous_status[ $post->ID ] = $old_status; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* When publishing or updating a post, the Gutenberg editor sends two requests: |
482
|
|
|
* 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id` |
483
|
|
|
* 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1` |
484
|
|
|
* |
485
|
|
|
* The 2nd request is to update post meta, which is not supported on WP REST API. |
486
|
|
|
* When syncing post data, we will include if this was a meta box update. |
487
|
|
|
* |
488
|
|
|
* @todo Implement nonce verification. |
489
|
|
|
* |
490
|
|
|
* @return boolean Whether this is a Gutenberg meta box update. |
491
|
|
|
*/ |
492
|
|
|
public function is_gutenberg_meta_box_update() { |
493
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
494
|
|
|
return ( |
495
|
|
|
isset( $_POST['action'], $_GET['classic-editor'], $_GET['meta_box'] ) && |
496
|
|
|
'editpost' === $_POST['action'] && |
497
|
|
|
'1' === $_GET['classic-editor'] && |
498
|
|
|
'1' === $_GET['meta_box'] |
499
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
500
|
|
|
); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Handler for the wp_insert_post hook. |
505
|
|
|
* Called upon creation of a new post. |
506
|
|
|
* |
507
|
|
|
* @param int $post_ID Post ID. |
|
|
|
|
508
|
|
|
* @param \WP_Post $post Post object. |
509
|
|
|
* @param boolean $update Whether this is an existing post being updated or not. |
|
|
|
|
510
|
|
|
*/ |
511
|
|
|
public function wp_insert_post( $post_ID, $post = null, $update = null ) { |
512
|
|
|
if ( ! is_numeric( $post_ID ) || is_null( $post ) ) { |
513
|
|
|
return; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
// Workaround for https://github.com/woocommerce/woocommerce/issues/18007. |
517
|
|
|
if ( $post && 'shop_order' === $post->post_type ) { |
518
|
|
|
$post = get_post( $post_ID ); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
$previous_status = isset( $this->previous_status[ $post_ID ] ) ? |
522
|
|
|
$this->previous_status[ $post_ID ] : |
523
|
|
|
self::DEFAULT_PREVIOUS_STATE; |
524
|
|
|
|
525
|
|
|
$just_published = isset( $this->just_published[ $post_ID ] ) ? |
526
|
|
|
$this->just_published[ $post_ID ] : |
527
|
|
|
false; |
528
|
|
|
|
529
|
|
|
$state = array( |
530
|
|
|
'is_auto_save' => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ), |
531
|
|
|
'previous_status' => $previous_status, |
532
|
|
|
'just_published' => $just_published, |
533
|
|
|
'is_gutenberg_meta_box_update' => $this->is_gutenberg_meta_box_update(), |
534
|
|
|
); |
535
|
|
|
/** |
536
|
|
|
* Filter that is used to add to the post flags ( meta data ) when a post gets published |
537
|
|
|
* |
538
|
|
|
* @since 5.8.0 |
539
|
|
|
* |
540
|
|
|
* @param int $post_ID the post ID |
541
|
|
|
* @param mixed $post \WP_Post object |
542
|
|
|
* @param bool $update Whether this is an existing post being updated or not. |
543
|
|
|
* @param mixed $state state |
544
|
|
|
* |
545
|
|
|
* @module sync |
546
|
|
|
*/ |
547
|
|
|
do_action( 'jetpack_sync_save_post', $post_ID, $post, $update, $state ); |
548
|
|
|
unset( $this->previous_status[ $post_ID ] ); |
549
|
|
|
$this->send_published( $post_ID, $post ); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Send a published post for sync. |
554
|
|
|
* |
555
|
|
|
* @param int $post_ID Post ID. |
556
|
|
|
* @param \WP_Post $post Post object. |
557
|
|
|
*/ |
558
|
|
|
public function send_published( $post_ID, $post ) { |
559
|
|
|
if ( ! isset( $this->just_published[ $post_ID ] ) ) { |
560
|
|
|
return; |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
// Post revisions cause race conditions where this send_published add the action before the actual post gets synced. |
564
|
|
|
if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
565
|
|
|
return; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
$post_flags = array( |
569
|
|
|
'post_type' => $post->post_type, |
570
|
|
|
); |
571
|
|
|
|
572
|
|
|
$author_user_object = get_user_by( 'id', $post->post_author ); |
573
|
|
|
if ( $author_user_object ) { |
574
|
|
|
$roles = new Roles(); |
575
|
|
|
|
576
|
|
|
$post_flags['author'] = array( |
577
|
|
|
'id' => $post->post_author, |
578
|
|
|
'wpcom_user_id' => get_user_meta( $post->post_author, 'wpcom_user_id', true ), |
579
|
|
|
'display_name' => $author_user_object->display_name, |
580
|
|
|
'email' => $author_user_object->user_email, |
581
|
|
|
'translated_role' => $roles->translate_user_to_role( $author_user_object ), |
582
|
|
|
); |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* Filter that is used to add to the post flags ( meta data ) when a post gets published |
587
|
|
|
* |
588
|
|
|
* @since 4.4.0 |
589
|
|
|
* |
590
|
|
|
* @param mixed array post flags that are added to the post |
591
|
|
|
* @param mixed $post \WP_Post object |
592
|
|
|
*/ |
593
|
|
|
$flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post ); |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Action that gets synced when a post type gets published. |
597
|
|
|
* |
598
|
|
|
* @since 4.4.0 |
599
|
|
|
* |
600
|
|
|
* @param int $post_ID |
601
|
|
|
* @param mixed array $flags post flags that are added to the post |
602
|
|
|
*/ |
603
|
|
|
do_action( 'jetpack_published_post', $post_ID, $flags ); |
604
|
|
|
unset( $this->just_published[ $post_ID ] ); |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Send additional sync action for Activity Log when post is a Customizer publish |
608
|
|
|
*/ |
609
|
|
|
if ( 'customize_changeset' === $post->post_type ) { |
610
|
|
|
$post_content = json_decode( $post->post_content, true ); |
611
|
|
|
foreach ( $post_content as $key => $value ) { |
612
|
|
|
// Skip if it isn't a widget. |
613
|
|
|
if ( 'widget_' !== substr( $key, 0, strlen( 'widget_' ) ) ) { |
614
|
|
|
continue; |
615
|
|
|
} |
616
|
|
|
// Change key from "widget_archives[2]" to "archives-2". |
617
|
|
|
$key = str_replace( 'widget_', '', $key ); |
618
|
|
|
$key = str_replace( '[', '-', $key ); |
619
|
|
|
$key = str_replace( ']', '', $key ); |
620
|
|
|
|
621
|
|
|
global $wp_registered_widgets; |
622
|
|
|
if ( isset( $wp_registered_widgets[ $key ] ) ) { |
623
|
|
|
$widget_data = array( |
624
|
|
|
'name' => $wp_registered_widgets[ $key ]['name'], |
625
|
|
|
'id' => $key, |
626
|
|
|
'title' => $value['value']['title'], |
627
|
|
|
); |
628
|
|
|
do_action( 'jetpack_widget_edited', $widget_data ); |
629
|
|
|
} |
630
|
|
|
} |
631
|
|
|
} |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* Expand post IDs to post objects within a hook before they are serialized and sent to the server. |
636
|
|
|
* |
637
|
|
|
* @access public |
638
|
|
|
* |
639
|
|
|
* @param array $args The hook parameters. |
640
|
|
|
* @return array $args The expanded hook parameters. |
641
|
|
|
*/ |
642
|
|
|
public function expand_post_ids( $args ) { |
643
|
|
|
list( $post_ids, $previous_interval_end) = $args; |
644
|
|
|
|
645
|
|
|
$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) ); |
646
|
|
|
$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts ); |
647
|
|
|
$posts = array_values( $posts ); // Reindex in case posts were deleted. |
648
|
|
|
|
649
|
|
|
return array( |
650
|
|
|
$posts, |
651
|
|
|
$this->get_metadata( $post_ids, 'post', Settings::get_setting( 'post_meta_whitelist' ) ), |
652
|
|
|
$this->get_term_relationships( $post_ids ), |
653
|
|
|
$previous_interval_end, |
654
|
|
|
); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
659
|
|
|
* |
660
|
|
|
* @access public |
661
|
|
|
* |
662
|
|
|
* @param int $batch_size The batch size for objects. |
663
|
|
|
* @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
664
|
|
|
* @param bool $distinct True if we should only look at distinct object ids. |
665
|
|
|
* |
666
|
|
|
* @return array|bool An array of min and max ids for each batch. |
667
|
|
|
*/ |
668
|
|
|
public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false, $distinct = false ) { |
669
|
|
|
return parent::get_min_max_object_ids_for_batches( $batch_size, $this->get_where_sql( false ) ); |
|
|
|
|
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.