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