PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-sync / src / modules / class-posts.php

class-posts.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.5, at jetpack_vendor/automattic/jetpack-sync/src/modules/class-posts.php

1,152 lines 36.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Activity_Log_Event;
13 use Automattic\Jetpack\Sync\Modules;
14 use Automattic\Jetpack\Sync\Settings;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit( 0 );
18 }
19
20 /**
21 * Class to handle sync for posts.
22 */
23 class Posts extends Module {
24 /**
25 * The post IDs of posts that were just published but not synced yet.
26 *
27 * @access private
28 *
29 * @var array
30 */
31 private $just_published = array();
32
33 /**
34 * The previous status of posts that we use for calculating post status transitions.
35 *
36 * @access private
37 *
38 * @var array
39 */
40 private $previous_status = array();
41
42 /**
43 * Action handler callable.
44 *
45 * @access private
46 *
47 * @var callable
48 */
49 private $action_handler;
50
51 /**
52 * Mark posts that are deleted in the current request.
53 *
54 * @access private
55 *
56 * @var array
57 */
58 private static $deleted_posts_in_request = array();
59
60 /**
61 * Import end.
62 *
63 * @access private
64 *
65 * @todo This appears to be unused - let's remove it.
66 *
67 * @var boolean
68 */
69 private $import_end = false;
70
71 /**
72 * Max bytes allowed for post_content => length.
73 * Current Setting : 5MB.
74 *
75 * @access public
76 *
77 * @var int
78 */
79 const MAX_POST_CONTENT_LENGTH = 5000000;
80
81 /**
82 * Default previous post state.
83 * Used for default previous post status.
84 *
85 * @access public
86 *
87 * @var string
88 */
89 const DEFAULT_PREVIOUS_STATE = 'new';
90
91 /**
92 * Sync module name.
93 *
94 * @access public
95 *
96 * @return string
97 */
98 public function name() {
99 return 'posts';
100 }
101
102 /**
103 * The table name.
104 *
105 * @access public
106 *
107 * @return string
108 * @deprecated since 3.11.0 Use table() instead.
109 */
110 public function table_name() {
111 _deprecated_function( __METHOD__, '3.11.0', 'Automattic\\Jetpack\\Sync\\Posts->table' );
112 return 'posts';
113 }
114
115 /**
116 * The table in the database with the prefix.
117 *
118 * @access public
119 *
120 * @return string|bool
121 */
122 public function table() {
123 global $wpdb;
124 return $wpdb->posts;
125 }
126
127 /**
128 * Retrieve a post by its ID.
129 *
130 * @access public
131 *
132 * @param string $object_type Type of the sync object.
133 * @param int $id ID of the sync object.
134 * @return \WP_Post|bool Filtered \WP_Post object, or false if the object is not a post.
135 */
136 public function get_object_by_id( $object_type, $id ) {
137 if ( 'post' === $object_type ) {
138 $post = get_post( (int) $id );
139 if ( $post ) {
140 return $this->filter_post_content_and_add_links( $post );
141 }
142 }
143
144 return false;
145 }
146
147 /**
148 * Initialize posts action listeners.
149 *
150 * @access public
151 *
152 * @param callable $callable Action handler callable.
153 */
154 public function init_listeners( $callable ) {
155 $this->action_handler = $callable;
156
157 add_action( 'before_delete_post', array( $this, 'mark_post_is_being_deleted' ), 0, 1 );
158 add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), 11, 3 );
159 add_action( 'wp_after_insert_post', array( $this, 'wp_after_insert_post' ), 11, 2 );
160 add_action( 'jetpack_sync_save_post', $callable, 10, 4 );
161
162 add_action( 'deleted_post', $callable, 10 );
163 add_action( 'jetpack_published_post', $callable, 10, 3 );
164 add_filter( 'jetpack_sync_before_enqueue_deleted_post', array( $this, 'filter_blacklisted_post_types_deleted' ) );
165
166 add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
167
168 // Listen for meta changes.
169 $this->init_listeners_for_meta_type( 'post', $callable );
170 add_filter( 'jetpack_sync_before_enqueue_added_post_meta', array( $this, 'filter_meta' ) );
171 add_filter( 'jetpack_sync_before_enqueue_updated_post_meta', array( $this, 'filter_updated_post_meta' ) );
172 add_filter( 'jetpack_sync_before_enqueue_deleted_post_meta', array( $this, 'filter_deleted_post_meta' ) );
173
174 add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_post', array( $this, 'filter_jetpack_sync_before_enqueue_jetpack_sync_save_post' ) );
175 add_filter( 'jetpack_sync_before_enqueue_jetpack_published_post', array( $this, 'filter_jetpack_sync_before_enqueue_jetpack_published_post' ) );
176
177 add_action( 'jetpack_daily_akismet_meta_cleanup_before', array( $this, 'daily_akismet_meta_cleanup_before' ) );
178 add_action( 'jetpack_daily_akismet_meta_cleanup_after', array( $this, 'daily_akismet_meta_cleanup_after' ) );
179 add_action( 'jetpack_post_meta_batch_delete', $callable, 10, 2 );
180
181 add_action( 'deleted_post', array( $this, 'unmark_post_being_deleted' ), 11, 1 );
182 }
183
184 /**
185 * Before Akismet's daily cleanup of spam detection metadata.
186 *
187 * @access public
188 *
189 * @param array $feedback_ids IDs of feedback posts.
190 */
191 public function daily_akismet_meta_cleanup_before( $feedback_ids ) {
192 remove_action( 'deleted_post_meta', $this->action_handler );
193
194 if ( ! is_array( $feedback_ids ) || count( $feedback_ids ) < 1 ) {
195 return;
196 }
197
198 $ids_chunks = array_chunk( $feedback_ids, 100, false );
199 foreach ( $ids_chunks as $chunk ) {
200 /**
201 * Used for syncing deletion of batch post meta
202 *
203 * @since 1.6.3
204 * @since-jetpack 6.1.0
205 *
206 * @module sync
207 *
208 * @param array $feedback_ids feedback post IDs
209 * @param string $meta_key to be deleted
210 */
211 do_action( 'jetpack_post_meta_batch_delete', $chunk, '_feedback_akismet_values' );
212 }
213 }
214
215 /**
216 * After Akismet's daily cleanup of spam detection metadata.
217 *
218 * @access public
219 *
220 * @param array $feedback_ids IDs of feedback posts.
221 */
222 public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
223 add_action( 'deleted_post_meta', $this->action_handler );
224 }
225
226 /**
227 * Initialize posts action listeners for full sync.
228 *
229 * @access public
230 *
231 * @param callable $callable Action handler callable.
232 */
233 public function init_full_sync_listeners( $callable ) {
234 add_action( 'jetpack_full_sync_posts', $callable ); // Also sends post meta.
235 }
236
237 /**
238 * Initialize the module in the sender.
239 *
240 * @access public
241 */
242 public function init_before_send() {
243 // meta.
244 add_filter( 'jetpack_sync_before_send_added_post_meta', array( $this, 'filter_added_post_meta_before_send' ), 5 ); // Incase this filter is used elsewhere, we run early.
245 add_filter( 'jetpack_sync_before_send_updated_post_meta', array( $this, 'filter_updated_post_meta_before_send' ), 5 ); // Incase this filter is used elsewhere, we run early.
246 add_filter( 'jetpack_sync_before_send_deleted_post_meta', array( $this, 'trim_post_meta' ) );
247 // Full sync.
248 $sync_module = Modules::get_module( 'full-sync' );
249 if ( $sync_module instanceof Full_Sync_Immediately ) {
250 add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'build_full_sync_action_array' ) );
251 } else {
252 add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_posts_with_metadata_and_terms' ) );
253 }
254 }
255
256 /**
257 * Enqueue the posts actions for full sync.
258 *
259 * @access public
260 *
261 * @param array $config Full sync configuration for this sync module.
262 * @param int $max_items_to_enqueue Maximum number of items to enqueue.
263 * @param boolean $state True if full sync has finished enqueueing this module, false otherwise.
264 * @return array Number of actions enqueued, and next module state.
265 */
266 public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
267 global $wpdb;
268
269 return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
270 }
271
272 /**
273 * Retrieve an estimated number of actions that will be enqueued.
274 *
275 * @access public
276 *
277 * @todo Use $wpdb->prepare for the SQL query.
278 *
279 * @param array $config Full sync configuration for this sync module.
280 * @return int Number of items yet to be enqueued.
281 */
282 public function estimate_full_sync_actions( $config ) {
283 global $wpdb;
284
285 $query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
286 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
287 $count = (int) $wpdb->get_var( $query );
288
289 return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
290 }
291
292 /**
293 * Retrieve the WHERE SQL clause based on the module config.
294 *
295 * @access public
296 *
297 * @param array $config Full sync configuration for this sync module.
298 * @return string WHERE SQL clause, or `null` if no comments are specified in the module config.
299 */
300 public function get_where_sql( $config ) {
301 $where_sql = Settings::get_blacklisted_post_types_sql();
302
303 // Config is a list of post IDs to sync.
304 if ( is_array( $config ) && ! empty( $config ) ) {
305 $where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
306 }
307
308 return $where_sql;
309 }
310
311 /**
312 * Retrieve the actions that will be sent for this module during a full sync.
313 *
314 * @access public
315 *
316 * @return array Full sync actions of this module.
317 */
318 public function get_full_sync_actions() {
319 return array( 'jetpack_full_sync_posts' );
320 }
321
322 /**
323 * Filter meta arguments so that we don't sync meta_values over MAX_META_LENGTH.
324 *
325 * @param array $args action arguments.
326 *
327 * @return array filtered action arguments.
328 */
329 public function trim_post_meta( $args ) {
330 list( $meta_id, $object_id, $meta_key, $meta_value ) = $args;
331 // Explicitly truncate meta_value when it exceeds limit.
332 // Large content will cause OOM issues and break Sync.
333 $serialized_value = maybe_serialize( $meta_value );
334 if ( $serialized_value === null || strlen( $serialized_value ) >= self::MAX_META_LENGTH ) {
335 $meta_value = '';
336 }
337 return array( $meta_id, $object_id, $meta_key, $meta_value );
338 }
339
340 /**
341 * Updated post meta send-time filter: refreshes _wp_attachment_metadata to the latest DB value, then trims.
342 *
343 * @param array $args [ $meta_id, $object_id, $meta_key, $meta_value ].
344 * @return array Filtered args.
345 */
346 public function filter_updated_post_meta_before_send( $args ) {
347 if ( ! is_array( $args ) || count( $args ) < 4 ) {
348 return $args;
349 }
350 list( $meta_id, $object_id, $meta_key, $meta_value ) = $args;
351 if ( '_wp_attachment_metadata' !== $meta_key || 'attachment' !== get_post_type( (int) $object_id ) ) {
352 return $this->trim_post_meta( $args );
353 }
354 $current_value = wp_get_attachment_metadata( (int) $object_id );
355 if ( is_array( $current_value ) && ! empty( $current_value ) ) {
356 $meta_value = $current_value;
357 }
358 return $this->trim_post_meta( array( $meta_id, $object_id, $meta_key, $meta_value ) );
359 }
360
361 /**
362 * Added post meta send-time filter: refreshes _wp_attachment_metadata to the latest DB value, then trims.
363 *
364 * @param array $args [ $meta_id, $object_id, $meta_key, $meta_value ].
365 * @return array|false Filtered args, or false to skip sending when the snapshot is clearly incomplete.
366 */
367 public function filter_added_post_meta_before_send( $args ) {
368 if ( ! is_array( $args ) || count( $args ) < 4 ) {
369 return $args;
370 }
371 list( $meta_id, $object_id, $meta_key, $meta_value ) = $args;
372 if ( '_wp_attachment_metadata' !== $meta_key || 'attachment' !== get_post_type( (int) $object_id ) ) {
373 return $this->trim_post_meta( $args );
374 }
375 $current_value = wp_get_attachment_metadata( (int) $object_id );
376 // For added_post_meta, skip clearly incomplete snapshots (e.g., missing or empty sizes).
377 if ( ! is_array( $current_value ) || empty( $current_value ) ) {
378 return false;
379 }
380 if ( isset( $current_value['sizes'] ) && is_array( $current_value['sizes'] ) && count( $current_value['sizes'] ) === 0 ) {
381 return false;
382 }
383 $meta_value = $current_value;
384 return $this->trim_post_meta( array( $meta_id, $object_id, $meta_key, $meta_value ) );
385 }
386
387 /**
388 * Mark a post as being deleted in the current request.
389 *
390 * @param int $post_id ID of the post being deleted.
391 */
392 public function mark_post_is_being_deleted( $post_id ) {
393 self::$deleted_posts_in_request[ (int) $post_id ] = true;
394 }
395
396 /**
397 * Enqueue-time per-request dedupe for deleted post metadata, if the post itself is being deleted.
398 *
399 * @param array $args [ $meta_id, $post_id, $meta_key, $meta_value ].
400 * @return array|false
401 */
402 public function maybe_skip_deleted_post_meta( $args ) {
403 if ( is_array( $args ) && isset( $args[1] ) && is_numeric( $args[1] ) ) {
404 $post_id = (int) $args[1];
405 if ( isset( self::$deleted_posts_in_request[ $post_id ] ) ) {
406 return false;
407 }
408 }
409 return $args;
410 }
411
412 /**
413 * Unmark a post as being deleted in the current request, to clean up.
414 *
415 * @param int $post_id ID of the post.
416 */
417 public function unmark_post_being_deleted( $post_id ) {
418 unset( self::$deleted_posts_in_request[ (int) $post_id ] );
419 }
420
421 /**
422 * Enqueue-time per-request dedupe for updated attachment metadata.
423 *
424 * @param array $args [ $meta_id, $object_id, $meta_key, $meta_value ].
425 * @return array|false
426 */
427 public function on_before_enqueue_updated_attachment_metadata( $args ) {
428 if ( ! is_array( $args ) || count( $args ) < 3 ) {
429 return $args;
430 }
431 $post_id = (int) $args[1];
432 $meta_key = $args[2];
433 if ( '_wp_attachment_metadata' !== $meta_key || 'attachment' !== get_post_type( $post_id ) ) {
434 return $args;
435 }
436 static $seen_updated_meta_for_post = array();
437 if ( isset( $seen_updated_meta_for_post[ $post_id ] ) ) {
438 return false;
439 }
440 $seen_updated_meta_for_post[ $post_id ] = true;
441 return $args;
442 }
443
444 /**
445 * Process content before send.
446 *
447 * @param array $args Arguments of the `wp_insert_post` hook.
448 *
449 * @return array
450 */
451 public function expand_jetpack_sync_save_post( $args ) {
452 list( $post_id, $post, $update, $previous_state ) = $args;
453 return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state );
454 }
455
456 /**
457 * Filter all blacklisted post types and add filtered post content.
458 *
459 * @param array $args Hook arguments.
460 * @return array|false Hook arguments, or false if the post type is a blacklisted one.
461 */
462 public function filter_jetpack_sync_before_enqueue_jetpack_sync_save_post( $args ) {
463 if (
464 ! is_array( $args )
465 || ! array_key_exists( 0, $args ) || ! is_numeric( $args[0] )
466 || ! array_key_exists( 1, $args ) || ! ( $args[1] instanceof \WP_Post )
467 ) {
468 return false;
469 }
470
471 list( $post_id, $post, $update, $previous_state ) = array_pad( $args, 4, null );
472
473 if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) {
474 return false;
475 }
476
477 // During incremental sync, skip posts whose type is not registered (e.g. CPT unregistered before sync).
478 // Full sync may have already sent them; we simply don't enqueue incremental updates for them.
479 if ( ! get_post_type_object( $post->post_type ) ) {
480 return false;
481 }
482
483 if ( Activity_Log_Event::POST_TYPE === $post->post_type && ! Activity_Log_Event::is_valid_post( $post ) ) {
484 return false;
485 }
486
487 return array( (int) $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state );
488 }
489
490 /**
491 * Add filtered post content.
492 *
493 * @param array $args Hook arguments.
494 * @return array|false Hook arguments, or false if the arguments are invalid.
495 */
496 public function filter_jetpack_sync_before_enqueue_jetpack_published_post( $args ) {
497 if (
498 ! is_array( $args )
499 || ! array_key_exists( 0, $args ) || ! is_numeric( $args[0] )
500 || ! array_key_exists( 1, $args ) || ! is_array( $args[1] )
501 || ! array_key_exists( 2, $args ) || ! ( $args[2] instanceof \WP_Post )
502 ) {
503 return false;
504 }
505
506 list( $post_id, $flags, $post ) = $args;
507
508 if ( Activity_Log_Event::POST_TYPE === $post->post_type && ! Activity_Log_Event::is_valid_post( $post ) ) {
509 return false;
510 }
511
512 return array( (int) $post_id, $flags, $this->filter_post_content_and_add_links( $post ) );
513 }
514
515 /**
516 * Filter all blacklisted post types.
517 *
518 * @param array $args Hook arguments.
519 * @return array|false Hook arguments, or false if the post type is a blacklisted one.
520 */
521 public function filter_blacklisted_post_types_deleted( $args ) {
522 if ( ! is_array( $args ) || ! array_key_exists( 0, $args ) || ! is_numeric( $args[0] ) ) {
523 return false;
524 }
525 // deleted_post is called after the SQL delete but before cache cleanup.
526 // There is the potential we can't detect post_type at this point.
527 if ( ! $this->is_post_type_allowed( $args[0] ) ) {
528 return false;
529 }
530
531 return $args;
532 }
533
534 /**
535 * Filter all meta that is not blacklisted, or is stored for a disallowed post type.
536 *
537 * @param array|false $args Hook arguments.
538 * @return array|false Hook arguments, or false if meta was filtered.
539 */
540 public function filter_meta( $args ) {
541 if ( ! $this->has_valid_meta_args( $args ) || ! is_numeric( $args[1] ) ) {
542 return false;
543 }
544
545 return $this->is_allowed_post_meta( $args[1], $args[2] ) ? $args : false;
546 }
547
548 /**
549 * Filter updated post meta that is not whitelisted, is stored for a disallowed post type,
550 * or is duplicate attachment metadata.
551 *
552 * @param array|false $args Hook arguments.
553 * @return array|false Hook arguments, or false if meta was filtered.
554 */
555 public function filter_updated_post_meta( $args ) {
556 $args = $this->on_before_enqueue_updated_attachment_metadata( $args );
557 if ( false === $args ) {
558 return false;
559 }
560
561 return $this->filter_meta( $args );
562 }
563
564 /**
565 * Filter deleted post meta that is not whitelisted, or is stored for a disallowed post type.
566 *
567 * @param array|false $args Hook arguments.
568 * @return array|false Hook arguments, or false if meta was filtered.
569 */
570 public function filter_deleted_post_meta( $args ) {
571 if ( ! $this->has_valid_meta_args( $args ) ) {
572 return false;
573 }
574 // Core uses post ID 0 on this hook for delete-all metadata operations. Only mirror value-constrained deletes.
575 if ( 0 === $args[1] ) {
576 if ( ! array_key_exists( 3, $args ) || '' === $args[3] || null === $args[3] || false === $args[3] ) {
577 return false;
578 }
579
580 return $this->is_whitelisted_post_meta( $args[2] ) ? $args : false;
581 }
582
583 $args = $this->filter_meta( $args );
584 if ( false === $args ) {
585 return false;
586 }
587
588 return $this->maybe_skip_deleted_post_meta( $args );
589 }
590
591 /**
592 * Whether metadata hook arguments include a meta key.
593 *
594 * @param array|false $args Hook arguments.
595 * @return bool Whether metadata hook arguments include a meta key.
596 */
597 private function has_valid_meta_args( $args ) {
598 return is_array( $args ) && array_key_exists( 1, $args ) && array_key_exists( 2, $args ) && is_string( $args[2] );
599 }
600
601 /**
602 * Whether post metadata is allowed to sync.
603 *
604 * @param int|string $object_id Post ID.
605 * @param string $meta_key Meta key.
606 * @return bool Whether post metadata is allowed to sync.
607 */
608 private function is_allowed_post_meta( $object_id, $meta_key ) {
609 return $this->is_post_type_allowed( $object_id ) && $this->is_whitelisted_post_meta( $meta_key );
610 }
611
612 /**
613 * Whether a post meta key is whitelisted.
614 *
615 * @param string $meta_key Meta key.
616 * @return boolean Whether the post meta key is whitelisted.
617 */
618 public function is_whitelisted_post_meta( $meta_key ) {
619 if ( ! is_string( $meta_key ) ) {
620 return false;
621 }
622 // The '_wpas_skip_' meta key prefix is used by Publicize to mark posts that should be skipped.
623 return str_starts_with( $meta_key, '_wpas_skip_' ) || in_array( $meta_key, Settings::get_setting( 'post_meta_whitelist' ), true );
624 }
625
626 /**
627 * Whether a post type is allowed.
628 * A post type will be disallowed if it's present in the post type blacklist.
629 *
630 * @param int $post_id ID of the post.
631 * @return boolean Whether the post type is allowed.
632 */
633 public function is_post_type_allowed( $post_id ) {
634 $post = get_post( (int) $post_id );
635
636 if ( isset( $post->post_type ) ) {
637 return ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true );
638 }
639 return false;
640 }
641
642 /**
643 * Remove the embed shortcode.
644 *
645 * @global $wp_embed
646 */
647 public function remove_embed() {
648 global $wp_embed;
649 remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
650 // remove the embed shortcode since we would do the part later.
651 remove_shortcode( 'embed' );
652 // Attempts to embed all URLs in a post.
653 remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
654 }
655
656 /**
657 * Add the embed shortcode.
658 *
659 * @global $wp_embed
660 */
661 public function add_embed() {
662 global $wp_embed;
663 add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
664 // Shortcode placeholder for strip_shortcodes().
665 add_shortcode( 'embed', '__return_false' );
666 // Attempts to embed all URLs in a post.
667 add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
668 }
669
670 /**
671 * Expands wp_insert_post to include filtered content
672 *
673 * @param \WP_Post $post_object Post object.
674 */
675 public function filter_post_content_and_add_links( $post_object ) {
676 global $post;
677
678 // Used to restore the post global.
679 $current_post = $post;
680
681 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
682 $post = $post_object;
683
684 // Return non existant post.
685 $post_type = get_post_type_object( $post->post_type );
686 if ( empty( $post_type ) || ! is_object( $post_type ) ) {
687 $non_existant_post = new \stdClass();
688 $non_existant_post->ID = $post->ID;
689 $non_existant_post->post_modified = $post->post_modified;
690 $non_existant_post->post_modified_gmt = $post->post_modified_gmt;
691 $non_existant_post->post_status = 'jetpack_sync_non_registered_post_type';
692 $non_existant_post->post_type = $post->post_type;
693 // Restore global post.
694 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
695 $post = $current_post;
696
697 return $non_existant_post;
698 }
699 /**
700 * Filters whether to prevent sending post data to .com
701 *
702 * Passing true to the filter will prevent the post data from being sent
703 * to the WordPress.com.
704 * Instead we pass data that will still enable us to do a checksum against the
705 * Jetpacks data but will prevent us from displaying the data on in the API as well as
706 * other services.
707 *
708 * @since 1.6.3
709 * @since-jetpack 4.2.0
710 *
711 * @param boolean false prevent post data from being synced to WordPress.com
712 * @param mixed $post \WP_Post object
713 */
714 if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
715 // We only send the bare necessary object to be able to create a checksum.
716 $blocked_post = new \stdClass();
717 $blocked_post->ID = $post->ID;
718 $blocked_post->post_modified = $post->post_modified;
719 $blocked_post->post_modified_gmt = $post->post_modified_gmt;
720 $blocked_post->post_status = 'jetpack_sync_blocked';
721 $blocked_post->post_type = $post->post_type;
722
723 // Restore global post.
724 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
725 $post = $current_post;
726
727 return $blocked_post;
728 }
729
730 // lets not do oembed just yet.
731 $this->remove_embed();
732
733 if ( 0 < strlen( $post->post_password ) ) {
734 $post->post_password = 'auto-' . wp_generate_password( 10, false );
735 }
736
737 // Explicitly omit post_content when it exceeds limit.
738 // Large content will cause OOM issues and break Sync.
739 if ( strlen( $post->post_content ) >= self::MAX_POST_CONTENT_LENGTH ) {
740 $post->post_content = '';
741 }
742
743 /** This filter is already documented in core. wp-includes/post-template.php */
744 if ( Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) {
745 global $shortcode_tags;
746 /**
747 * Filter prevents some shortcodes from expanding.
748 *
749 * Since we can can expand some type of shortcode better on the .com side and make the
750 * expansion more relevant to contexts. For example [galleries] and subscription emails
751 *
752 * @since 1.6.3
753 * @since-jetpack 4.5.0
754 *
755 * @param array of shortcode tags to remove.
756 */
757 $shortcodes_to_remove = apply_filters(
758 'jetpack_sync_do_not_expand_shortcodes',
759 array(
760 'gallery',
761 'slideshow',
762 )
763 );
764 $removed_shortcode_callbacks = array();
765 foreach ( $shortcodes_to_remove as $shortcode ) {
766 if ( isset( $shortcode_tags[ $shortcode ] ) ) {
767 $removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ];
768 }
769 }
770
771 array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) );
772 /**
773 * Certain modules such as Likes, Related Posts and Sharedaddy are using `Settings::is_syncing`
774 * in order to NOT get rendered in filtered post content.
775 * Since the current method runs now before enqueueing instead of before sending,
776 * we are setting `is_syncing` flag to true in order to preserve the existing functionality.
777 */
778
779 $is_syncing_current = Settings::is_syncing();
780 Settings::set_is_syncing( true );
781 $post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
782 $post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt );
783 Settings::set_is_syncing( $is_syncing_current );
784
785 foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) {
786 add_shortcode( $shortcode, $callback );
787 }
788 }
789
790 $this->add_embed();
791
792 if ( has_post_thumbnail( $post->ID ) ) {
793 $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
794 if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
795 $post->featured_image = $image_attributes[0];
796 }
797 }
798
799 $post->permalink = get_permalink( $post->ID );
800 $post->shortlink = wp_get_shortlink( $post->ID );
801
802 if ( function_exists( 'amp_get_permalink' ) ) {
803 $post->amp_permalink = amp_get_permalink( $post->ID );
804 }
805
806 $filtered_post = $post;
807
808 // Restore global post.
809 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
810 $post = $current_post;
811
812 return $filtered_post;
813 }
814
815 /**
816 * Handle transition from another post status to a published one.
817 *
818 * @param string $new_status New post status.
819 * @param string $old_status Old post status.
820 * @param \WP_Post $post Post object.
821 */
822 public function save_published( $new_status, $old_status, $post ) {
823 if ( ! $post instanceof \WP_Post ) {
824 return;
825 }
826 if ( 'publish' === $new_status && 'publish' !== $old_status ) {
827 $this->just_published[ $post->ID ] = true;
828 }
829
830 $this->previous_status[ $post->ID ] = $old_status;
831 }
832
833 /**
834 * When publishing or updating a post, the Gutenberg editor sends two requests:
835 * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id`
836 * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1`
837 *
838 * The 2nd request is to update post meta, which is not supported on WP REST API.
839 * When syncing post data, we will include if this was a meta box update.
840 *
841 * @return boolean Whether this is a Gutenberg meta box update.
842 */
843 private function is_gutenberg_meta_box_update() {
844 // phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended -- We only check the request to determine if this is a Gutenberg meta box update, and we only use the result to set a boolean logged in the sync event. If anyone anywhere else gets the flag and does something CSRF-able with it, they should ensure that a nonce has been checked.
845 return (
846 isset( $_POST['action'], $_GET['classic-editor'], $_GET['meta_box'] ) &&
847 'editpost' === $_POST['action'] &&
848 '1' === $_GET['classic-editor'] &&
849 '1' === $_GET['meta_box']
850 // phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
851 );
852 }
853
854 /**
855 * Handler for the wp_insert_post hook.
856 * Called upon creation of a new post.
857 *
858 * @param int $post_ID Post ID.
859 * @param \WP_Post $post Post object.
860 * @param boolean $update Whether this is an existing post being updated or not.
861 */
862 public function wp_insert_post( $post_ID, $post = null, $update = null ) {
863 if ( ! is_numeric( $post_ID ) || ! $post instanceof \WP_Post ) {
864 return;
865 }
866
867 // Workaround for https://github.com/woocommerce/woocommerce/issues/18007.
868 if ( 'shop_order' === $post->post_type ) {
869 $post = get_post( $post_ID );
870 }
871
872 $previous_status = $this->previous_status[ $post_ID ] ?? self::DEFAULT_PREVIOUS_STATE;
873
874 $just_published = $this->just_published[ $post_ID ] ?? false;
875
876 $state = array(
877 'is_auto_save' => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ),
878 'previous_status' => $previous_status,
879 'just_published' => $just_published,
880 'is_gutenberg_meta_box_update' => $this->is_gutenberg_meta_box_update(),
881 );
882 /**
883 * Filter that is used to add to the post flags ( meta data ) when a post gets published
884 *
885 * @since 1.6.3
886 * @since-jetpack 5.8.0
887 *
888 * @param int $post_ID the post ID
889 * @param mixed $post \WP_Post object
890 * @param bool $update Whether this is an existing post being updated or not.
891 * @param mixed $state state
892 *
893 * @module sync
894 */
895 do_action( 'jetpack_sync_save_post', $post_ID, $post, $update, $state );
896 unset( $this->previous_status[ $post_ID ] );
897 }
898
899 /**
900 * Handler for the wp_after_insert_post hook.
901 * Called after creation/update of a new post.
902 *
903 * @param int $post_ID Post ID.
904 * @param \WP_Post $post Post object.
905 **/
906 public function wp_after_insert_post( $post_ID, $post ) {
907 if ( ! is_numeric( $post_ID ) || ! $post instanceof \WP_Post ) {
908 return;
909 }
910
911 // Workaround for https://github.com/woocommerce/woocommerce/issues/18007.
912 if ( 'shop_order' === $post->post_type ) {
913 $post = get_post( $post_ID );
914 }
915
916 $this->send_published( $post_ID, $post );
917 }
918
919 /**
920 * Send a published post for sync.
921 *
922 * @param int $post_ID Post ID.
923 * @param \WP_Post $post Post object.
924 */
925 public function send_published( $post_ID, $post ) {
926 if ( ! isset( $this->just_published[ $post_ID ] ) ) {
927 return;
928 }
929
930 // Post revisions cause race conditions where this send_published add the action before the actual post gets synced.
931 if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
932 return;
933 }
934
935 $post_flags = array(
936 'post_type' => $post->post_type,
937 );
938
939 $author_user_object = get_user_by( 'id', $post->post_author );
940 if ( $author_user_object ) {
941 $roles = new Roles();
942
943 $post_flags['author'] = array(
944 'id' => $post->post_author,
945 'wpcom_user_id' => get_user_meta( $post->post_author, 'wpcom_user_id', true ),
946 'display_name' => $author_user_object->display_name,
947 'email' => $author_user_object->user_email,
948 'translated_role' => $roles->translate_user_to_role( $author_user_object ),
949 );
950 }
951
952 /**
953 * Filter that is used to add to the post flags ( meta data ) when a post gets published
954 *
955 * @since 1.6.3
956 * @since-jetpack 4.4.0
957 *
958 * @param mixed array post flags that are added to the post
959 * @param mixed $post \WP_Post object
960 */
961 $flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post );
962
963 // Only Send Pulished Post event if post_type is not blacklisted.
964 if ( ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) {
965
966 /**
967 * Action that gets synced when a post type gets published.
968 *
969 * @since 1.6.3
970 * @since-jetpack 4.4.0
971 *
972 * @param int $post_ID
973 * @param mixed array $flags post flags that are added to the post
974 * @param WP_Post $post The post object
975 */
976 do_action( 'jetpack_published_post', $post_ID, $flags, $post );
977 }
978 unset( $this->just_published[ $post_ID ] );
979
980 /**
981 * Send additional sync action for Activity Log when post is a Customizer publish
982 */
983 if ( 'customize_changeset' === $post->post_type ) {
984 $post_content = json_decode( $post->post_content, true );
985 if ( ! is_iterable( $post_content ) ) {
986 return;
987 }
988 foreach ( $post_content as $key => $value ) {
989 // Skip if it isn't a widget.
990 if ( 'widget_' !== substr( $key, 0, strlen( 'widget_' ) ) ) {
991 continue;
992 }
993 // Change key from "widget_archives[2]" to "archives-2".
994 $key = str_replace( 'widget_', '', $key );
995 $key = str_replace( '[', '-', $key );
996 $key = str_replace( ']', '', $key );
997
998 global $wp_registered_widgets;
999 if ( isset( $wp_registered_widgets[ $key ] ) ) {
1000 $widget_data = array(
1001 'name' => $wp_registered_widgets[ $key ]['name'],
1002 'id' => $key,
1003 'title' => $value['value']['title'] ?? '',
1004 );
1005 do_action( 'jetpack_widget_edited', $widget_data );
1006 }
1007 }
1008 }
1009 }
1010
1011 /**
1012 * Build the full sync action object for Posts.
1013 *
1014 * @access public
1015 *
1016 * @param array $args An array with the posts and the previous end.
1017 *
1018 * @return array An array with the posts, postmeta and the previous end.
1019 */
1020 public function build_full_sync_action_array( $args ) {
1021 list( $filtered_posts, $previous_end ) = $args;
1022 return array(
1023 $filtered_posts['objects'],
1024 $filtered_posts['meta'],
1025 array(), // WPCOM does not process term relationships in full sync posts actions for a while now, let's skip them.
1026 $previous_end,
1027 );
1028 }
1029
1030 /**
1031 * Add term relationships to post objects within a hook before they are serialized and sent to the server.
1032 * This is used in Full Sync Immediately
1033 *
1034 * @access public
1035 *
1036 * @param array $args The hook parameters.
1037 * @return array $args The expanded hook parameters.
1038 * @deprecated since 4.7.0
1039 */
1040 public function add_term_relationships( $args ) {
1041 _deprecated_function( __METHOD__, '4.7.0' );
1042 list( $filtered_posts, $previous_interval_end ) = $args;
1043
1044 return array(
1045 $filtered_posts['objects'],
1046 $filtered_posts['meta'],
1047 $this->get_term_relationships( $filtered_posts['object_ids'] ),
1048 $previous_interval_end,
1049 );
1050 }
1051
1052 /**
1053 * Expand post IDs to post objects within a hook before they are serialized and sent to the server.
1054 * This is used in Legacy Full Sync
1055 *
1056 * @access public
1057 *
1058 * @param array $args The hook parameters.
1059 * @return array $args The expanded hook parameters.
1060 */
1061 public function expand_posts_with_metadata_and_terms( $args ) {
1062 list( $post_ids, $previous_interval_end ) = $args;
1063
1064 $posts = $this->expand_posts( $post_ids );
1065 $posts_metadata = $this->get_metadata( $post_ids, 'post', Settings::get_setting( 'post_meta_whitelist' ) );
1066 $term_relationships = $this->get_term_relationships( $post_ids );
1067
1068 return array(
1069 $posts,
1070 $posts_metadata,
1071 $term_relationships,
1072 $previous_interval_end,
1073 );
1074 }
1075
1076 /**
1077 * Gets a list of minimum and maximum object ids for each batch based on the given batch size.
1078 *
1079 * @access public
1080 *
1081 * @param int $batch_size The batch size for objects.
1082 * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed.
1083 *
1084 * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found.
1085 */
1086 public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) {
1087 return parent::get_min_max_object_ids_for_batches( $batch_size, $this->get_where_sql( $where_sql ) );
1088 }
1089
1090 /**
1091 * Given the Module Configuration and Status return the next chunk of items to send.
1092 * This function also expands the posts and metadata and filters them based on the maximum size constraints.
1093 *
1094 * @param array $config This module Full Sync configuration.
1095 * @param array $status This module Full Sync status.
1096 * @param int $chunk_size Chunk size.
1097 *
1098 * @return array
1099 */
1100 public function get_next_chunk( $config, $status, $chunk_size ) {
1101
1102 $post_ids = parent::get_next_chunk( $config, $status, $chunk_size );
1103
1104 if ( empty( $post_ids ) ) {
1105 return array();
1106 }
1107
1108 $posts = $this->expand_posts( $post_ids );
1109
1110 // If no posts were fetched, make sure to return the expected structure so that status is updated correctly.
1111 if ( empty( $posts ) ) {
1112 return array(
1113 'object_ids' => $post_ids,
1114 'objects' => array(),
1115 'meta' => array(),
1116 );
1117 }
1118 // Get the post IDs from the posts that were fetched.
1119 $fetched_post_ids = wp_list_pluck( $posts, 'ID' );
1120 $metadata = $this->get_metadata( $fetched_post_ids, 'post', Settings::get_setting( 'post_meta_whitelist' ) );
1121
1122 // Filter the posts and metadata based on the maximum size constraints.
1123 list( $filtered_post_ids, $filtered_posts, $filtered_posts_metadata ) = $this->filter_objects_and_metadata_by_size(
1124 'post',
1125 $posts,
1126 $metadata,
1127 self::MAX_META_LENGTH,
1128 self::MAX_SIZE_FULL_SYNC
1129 );
1130
1131 return array(
1132 'object_ids' => $filtered_post_ids,
1133 'objects' => $filtered_posts,
1134 'meta' => $filtered_posts_metadata,
1135 );
1136 }
1137
1138 /**
1139 * Expand posts.
1140 *
1141 * @param array $post_ids Post IDs.
1142 *
1143 * @return array Expanded posts.
1144 */
1145 private function expand_posts( $post_ids ) {
1146 $posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
1147 $posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
1148 $posts = array_values( $posts ); // Reindex in case posts were deleted.
1149 return $posts;
1150 }
1151 }
1152