Completed
Push — fix/sync-limit-meta-value ( f44aa8 )
by
unknown
175:01 queued 165:39
created

Posts::trim_post_meta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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
	 * Max bytes allowed for post_content => length.
58
	 * Current Setting : 5MB.
59
	 *
60
	 * @access public
61
	 *
62
	 * @var int
63
	 */
64
	const MAX_POST_CONTENT_LENGTH = 2500000;
65
66
	/**
67
	 * Default previous post state.
68
	 * Used for default previous post status.
69
	 *
70
	 * @access public
71
	 *
72
	 * @var string
73
	 */
74
	const DEFAULT_PREVIOUS_STATE = 'new';
75
76
	/**
77
	 * Sync module name.
78
	 *
79
	 * @access public
80
	 *
81
	 * @return string
82
	 */
83
	public function name() {
84
		return 'posts';
85
	}
86
87
	/**
88
	 * The table in the database.
89
	 *
90
	 * @access public
91
	 *
92
	 * @return string
93
	 */
94
	public function table_name() {
95
		return 'posts';
96
	}
97
98
	/**
99
	 * Retrieve a post by its ID.
100
	 *
101
	 * @access public
102
	 *
103
	 * @param string $object_type Type of the sync object.
104
	 * @param int    $id          ID of the sync object.
105
	 * @return \WP_Post|bool Filtered \WP_Post object, or false if the object is not a post.
106
	 */
107
	public function get_object_by_id( $object_type, $id ) {
108
		if ( 'post' === $object_type ) {
109
			$post = get_post( (int) $id );
110
			if ( $post ) {
111
				return $this->filter_post_content_and_add_links( $post );
112
			}
113
		}
114
115
		return false;
116
	}
117
118
	/**
119
	 * Initialize posts action listeners.
120
	 *
121
	 * @access public
122
	 *
123
	 * @param callable $callable Action handler callable.
124
	 */
125
	public function init_listeners( $callable ) {
126
		$this->action_handler = $callable;
127
128
		add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), 11, 3 );
129
		add_action( 'wp_after_insert_post', array( $this, 'wp_after_insert_post' ), 11, 2 );
130
		add_action( 'jetpack_sync_save_post', $callable, 10, 4 );
131
132
		add_action( 'deleted_post', $callable, 10 );
133
		add_action( 'jetpack_published_post', $callable, 10, 2 );
134
		add_filter( 'jetpack_sync_before_enqueue_deleted_post', array( $this, 'filter_blacklisted_post_types_deleted' ) );
135
136
		add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
137
		add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_post', array( $this, 'filter_blacklisted_post_types' ) );
138
139
		// Listen for meta changes.
140
		$this->init_listeners_for_meta_type( 'post', $callable );
141
		$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) );
142
143
		add_action( 'jetpack_daily_akismet_meta_cleanup_before', array( $this, 'daily_akismet_meta_cleanup_before' ) );
144
		add_action( 'jetpack_daily_akismet_meta_cleanup_after', array( $this, 'daily_akismet_meta_cleanup_after' ) );
145
		add_action( 'jetpack_post_meta_batch_delete', $callable, 10, 2 );
146
	}
147
148
	/**
149
	 * Before Akismet's daily cleanup of spam detection metadata.
150
	 *
151
	 * @access public
152
	 *
153
	 * @param array $feedback_ids IDs of feedback posts.
154
	 */
155
	public function daily_akismet_meta_cleanup_before( $feedback_ids ) {
156
		remove_action( 'deleted_post_meta', $this->action_handler );
157
158
		if ( ! is_array( $feedback_ids ) || count( $feedback_ids ) < 1 ) {
159
			return;
160
		}
161
162
		$ids_chunks = array_chunk( $feedback_ids, 100, false );
163
		foreach ( $ids_chunks as $chunk ) {
164
			/**
165
			 * Used for syncing deletion of batch post meta
166
			 *
167
			 * @since 6.1.0
168
			 *
169
			 * @module sync
170
			 *
171
			 * @param array $feedback_ids feedback post IDs
172
			 * @param string $meta_key to be deleted
173
			 */
174
			do_action( 'jetpack_post_meta_batch_delete', $chunk, '_feedback_akismet_values' );
175
		}
176
	}
177
178
	/**
179
	 * After Akismet's daily cleanup of spam detection metadata.
180
	 *
181
	 * @access public
182
	 *
183
	 * @param array $feedback_ids IDs of feedback posts.
184
	 */
185
	public function daily_akismet_meta_cleanup_after( $feedback_ids ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
186
		add_action( 'deleted_post_meta', $this->action_handler );
187
	}
188
189
	/**
190
	 * Initialize posts action listeners for full sync.
191
	 *
192
	 * @access public
193
	 *
194
	 * @param callable $callable Action handler callable.
195
	 */
196
	public function init_full_sync_listeners( $callable ) {
197
		add_action( 'jetpack_full_sync_posts', $callable ); // Also sends post meta.
198
	}
199
200
	/**
201
	 * Initialize the module in the sender.
202
	 *
203
	 * @access public
204
	 */
205
	public function init_before_send() {
206
		add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'expand_jetpack_sync_save_post' ) );
207
208
		// meta.
209
		add_filter( 'jetpack_sync_before_send_added_post_meta', array( $this, 'trim_post_meta' ) );
210
		add_filter( 'jetpack_sync_before_send_updated_post_meta', array( $this, 'trim_post_meta' ) );
211
		add_filter( 'jetpack_sync_before_send_deleted_post_meta', array( $this, 'trim_post_meta' ) );
212
213
		// Full sync.
214
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
215
	}
216
217
	/**
218
	 * Enqueue the posts actions for full sync.
219
	 *
220
	 * @access public
221
	 *
222
	 * @param array   $config               Full sync configuration for this sync module.
223
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
224
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
225
	 * @return array Number of actions enqueued, and next module state.
226
	 */
227
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
228
		global $wpdb;
229
230
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
231
	}
232
233
	/**
234
	 * Retrieve an estimated number of actions that will be enqueued.
235
	 *
236
	 * @access public
237
	 *
238
	 * @todo Use $wpdb->prepare for the SQL query.
239
	 *
240
	 * @param array $config Full sync configuration for this sync module.
241
	 * @return array Number of items yet to be enqueued.
242
	 */
243 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
244
		global $wpdb;
245
246
		$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
247
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
248
		$count = $wpdb->get_var( $query );
249
250
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
251
	}
252
253
	/**
254
	 * Retrieve the WHERE SQL clause based on the module config.
255
	 *
256
	 * @access public
257
	 *
258
	 * @param array $config Full sync configuration for this sync module.
259
	 * @return string WHERE SQL clause, or `null` if no comments are specified in the module config.
260
	 */
261 View Code Duplication
	public function get_where_sql( $config ) {
262
		$where_sql = Settings::get_blacklisted_post_types_sql();
263
264
		// Config is a list of post IDs to sync.
265
		if ( is_array( $config ) ) {
266
			$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
267
		}
268
269
		return $where_sql;
270
	}
271
272
	/**
273
	 * Retrieve the actions that will be sent for this module during a full sync.
274
	 *
275
	 * @access public
276
	 *
277
	 * @return array Full sync actions of this module.
278
	 */
279
	public function get_full_sync_actions() {
280
		return array( 'jetpack_full_sync_posts' );
281
	}
282
283
	/**
284
	 * Filter meta arguments so that we don't sync meta_values over 5MB.
285
	 *
286
	 * @param array $args action arguments.
287
	 *
288
	 * @return array filtered action arguments.
289
	 */
290
	public function trim_post_meta( $args ) {
291
		list( $meta_id, $object_id, $meta_key, $meta_value ) = $args;
292
		// Explicitly truncate meta_value when it exceeds limit.
293
		// Large content will cause OOM issues and break Sync.
294
		if ( strlen( $meta_value ) >= self::MAX_POST_CONTENT_LENGTH ) {
295
			$meta_value = '';
296
		}
297
		return array( $meta_id, $object_id, $meta_key, $meta_value );
298
	}
299
300
	/**
301
	 * Process content before send.
302
	 *
303
	 * @param array $args Arguments of the `wp_insert_post` hook.
304
	 *
305
	 * @return array
306
	 */
307
	public function expand_jetpack_sync_save_post( $args ) {
308
		list( $post_id, $post, $update, $previous_state ) = $args;
309
		return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state );
310
	}
311
312
	/**
313
	 * Filter all blacklisted post types.
314
	 *
315
	 * @param array $args Hook arguments.
316
	 * @return array|false Hook arguments, or false if the post type is a blacklisted one.
317
	 */
318
	public function filter_blacklisted_post_types_deleted( $args ) {
319
320
		// deleted_post is called after the SQL delete but before cache cleanup.
321
		// There is the potential we can't detect post_type at this point.
322
		if ( ! $this->is_post_type_allowed( $args[0] ) ) {
323
			return false;
324
		}
325
326
		return $args;
327
	}
328
329
	/**
330
	 * Filter all blacklisted post types.
331
	 *
332
	 * @param array $args Hook arguments.
333
	 * @return array|false Hook arguments, or false if the post type is a blacklisted one.
334
	 */
335
	public function filter_blacklisted_post_types( $args ) {
336
		$post = $args[1];
337
338
		if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) {
339
			return false;
340
		}
341
342
		return $args;
343
	}
344
345
	/**
346
	 * Filter all meta that is not blacklisted, or is stored for a disallowed post type.
347
	 *
348
	 * @param array $args Hook arguments.
349
	 * @return array|false Hook arguments, or false if meta was filtered.
350
	 */
351
	public function filter_meta( $args ) {
352
		if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) {
353
			return $args;
354
		}
355
356
		return false;
357
	}
358
359
	/**
360
	 * Whether a post meta key is whitelisted.
361
	 *
362
	 * @param string $meta_key Meta key.
363
	 * @return boolean Whether the post meta key is whitelisted.
364
	 */
365
	public function is_whitelisted_post_meta( $meta_key ) {
366
		// The _wpas_skip_ meta key is used by Publicize.
367
		return in_array( $meta_key, Settings::get_setting( 'post_meta_whitelist' ), true ) || ( 0 === strpos( $meta_key, '_wpas_skip_' ) );
368
	}
369
370
	/**
371
	 * Whether a post type is allowed.
372
	 * A post type will be disallowed if it's present in the post type blacklist.
373
	 *
374
	 * @param int $post_id ID of the post.
375
	 * @return boolean Whether the post type is allowed.
376
	 */
377
	public function is_post_type_allowed( $post_id ) {
378
		$post = get_post( (int) $post_id );
379
380
		if ( isset( $post->post_type ) ) {
381
			return ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true );
382
		}
383
		return false;
384
	}
385
386
	/**
387
	 * Remove the embed shortcode.
388
	 *
389
	 * @global $wp_embed
390
	 */
391 View Code Duplication
	public function remove_embed() {
392
		global $wp_embed;
393
		remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
394
		// remove the embed shortcode since we would do the part later.
395
		remove_shortcode( 'embed' );
396
		// Attempts to embed all URLs in a post.
397
		remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
398
	}
399
400
	/**
401
	 * Add the embed shortcode.
402
	 *
403
	 * @global $wp_embed
404
	 */
405 View Code Duplication
	public function add_embed() {
406
		global $wp_embed;
407
		add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
408
		// Shortcode placeholder for strip_shortcodes().
409
		add_shortcode( 'embed', '__return_false' );
410
		// Attempts to embed all URLs in a post.
411
		add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
412
	}
413
414
	/**
415
	 * Expands wp_insert_post to include filtered content
416
	 *
417
	 * @param \WP_Post $post_object Post object.
418
	 */
419
	public function filter_post_content_and_add_links( $post_object ) {
420
		global $post;
421
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
422
		$post = $post_object;
423
424
		// Return non existant post.
425
		$post_type = get_post_type_object( $post->post_type );
426 View Code Duplication
		if ( empty( $post_type ) || ! is_object( $post_type ) ) {
427
			$non_existant_post                    = new \stdClass();
428
			$non_existant_post->ID                = $post->ID;
429
			$non_existant_post->post_modified     = $post->post_modified;
430
			$non_existant_post->post_modified_gmt = $post->post_modified_gmt;
431
			$non_existant_post->post_status       = 'jetpack_sync_non_registered_post_type';
432
			$non_existant_post->post_type         = $post->post_type;
433
434
			return $non_existant_post;
435
		}
436
		/**
437
		 * Filters whether to prevent sending post data to .com
438
		 *
439
		 * Passing true to the filter will prevent the post data from being sent
440
		 * to the WordPress.com.
441
		 * Instead we pass data that will still enable us to do a checksum against the
442
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
443
		 * other services.
444
		 *
445
		 * @since 4.2.0
446
		 *
447
		 * @param boolean false prevent post data from being synced to WordPress.com
448
		 * @param mixed $post \WP_Post object
449
		 */
450 View Code Duplication
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $post.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
451
			// We only send the bare necessary object to be able to create a checksum.
452
			$blocked_post                    = new \stdClass();
453
			$blocked_post->ID                = $post->ID;
454
			$blocked_post->post_modified     = $post->post_modified;
455
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
456
			$blocked_post->post_status       = 'jetpack_sync_blocked';
457
			$blocked_post->post_type         = $post->post_type;
458
459
			return $blocked_post;
460
		}
461
462
		// lets not do oembed just yet.
463
		$this->remove_embed();
464
465
		if ( 0 < strlen( $post->post_password ) ) {
466
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
467
		}
468
469
		// Explicitly truncate post_content when it exceeds limit.
470
		// Large content will cause OOM issues and break Sync.
471
		if ( strlen( $post->post_content ) >= self::MAX_POST_CONTENT_LENGTH ) {
472
			$post->post_content = '';
473
		}
474
475
		/** This filter is already documented in core. wp-includes/post-template.php */
476
		if ( Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) {
477
			global $shortcode_tags;
478
			/**
479
			 * Filter prevents some shortcodes from expanding.
480
			 *
481
			 * Since we can can expand some type of shortcode better on the .com side and make the
482
			 * expansion more relevant to contexts. For example [galleries] and subscription emails
483
			 *
484
			 * @since 4.5.0
485
			 *
486
			 * @param array of shortcode tags to remove.
487
			 */
488
			$shortcodes_to_remove        = apply_filters(
489
				'jetpack_sync_do_not_expand_shortcodes',
490
				array(
491
					'gallery',
492
					'slideshow',
493
				)
494
			);
495
			$removed_shortcode_callbacks = array();
496
			foreach ( $shortcodes_to_remove as $shortcode ) {
497
				if ( isset( $shortcode_tags[ $shortcode ] ) ) {
498
					$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ];
499
				}
500
			}
501
502
			array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) );
503
504
			$post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
505
			$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt );
506
507
			foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) {
508
				add_shortcode( $shortcode, $callback );
509
			}
510
		}
511
512
		$this->add_embed();
513
514
		if ( has_post_thumbnail( $post->ID ) ) {
515
			$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
516
			if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
517
				$post->featured_image = $image_attributes[0];
518
			}
519
		}
520
521
		$post->permalink = get_permalink( $post->ID );
522
		$post->shortlink = wp_get_shortlink( $post->ID );
523
524
		if ( function_exists( 'amp_get_permalink' ) ) {
525
			$post->amp_permalink = amp_get_permalink( $post->ID );
526
		}
527
528
		return $post;
529
	}
530
531
	/**
532
	 * Handle transition from another post status to a published one.
533
	 *
534
	 * @param string   $new_status New post status.
535
	 * @param string   $old_status Old post status.
536
	 * @param \WP_Post $post       Post object.
537
	 */
538
	public function save_published( $new_status, $old_status, $post ) {
539
		if ( 'publish' === $new_status && 'publish' !== $old_status ) {
540
			$this->just_published[ $post->ID ] = true;
541
		}
542
543
		$this->previous_status[ $post->ID ] = $old_status;
544
	}
545
546
	/**
547
	 * When publishing or updating a post, the Gutenberg editor sends two requests:
548
	 * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id`
549
	 * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1`
550
	 *
551
	 * The 2nd request is to update post meta, which is not supported on WP REST API.
552
	 * When syncing post data, we will include if this was a meta box update.
553
	 *
554
	 * @todo Implement nonce verification.
555
	 *
556
	 * @return boolean Whether this is a Gutenberg meta box update.
557
	 */
558
	public function is_gutenberg_meta_box_update() {
559
		// phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
560
		return (
561
			isset( $_POST['action'], $_GET['classic-editor'], $_GET['meta_box'] ) &&
562
			'editpost' === $_POST['action'] &&
563
			'1' === $_GET['classic-editor'] &&
564
			'1' === $_GET['meta_box']
565
			// phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
566
		);
567
	}
568
569
	/**
570
	 * Handler for the wp_insert_post hook.
571
	 * Called upon creation of a new post.
572
	 *
573
	 * @param int      $post_ID Post ID.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $post not be \WP_Post|null?

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.

Loading history...
574
	 * @param \WP_Post $post    Post object.
575
	 * @param boolean  $update  Whether this is an existing post being updated or not.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $update not be boolean|null?

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.

Loading history...
576
	 */
577
	public function wp_insert_post( $post_ID, $post = null, $update = null ) {
578
		if ( ! is_numeric( $post_ID ) || is_null( $post ) ) {
579
			return;
580
		}
581
582
		// Workaround for https://github.com/woocommerce/woocommerce/issues/18007.
583
		if ( $post && 'shop_order' === $post->post_type ) {
584
			$post = get_post( $post_ID );
585
		}
586
587
		$previous_status = isset( $this->previous_status[ $post_ID ] ) ? $this->previous_status[ $post_ID ] : self::DEFAULT_PREVIOUS_STATE;
588
589
		$just_published = isset( $this->just_published[ $post_ID ] ) ? $this->just_published[ $post_ID ] : false;
590
591
		$state = array(
592
			'is_auto_save'                 => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ),
593
			'previous_status'              => $previous_status,
594
			'just_published'               => $just_published,
595
			'is_gutenberg_meta_box_update' => $this->is_gutenberg_meta_box_update(),
596
		);
597
		/**
598
		 * Filter that is used to add to the post flags ( meta data ) when a post gets published
599
		 *
600
		 * @since 5.8.0
601
		 *
602
		 * @param int $post_ID the post ID
603
		 * @param mixed $post \WP_Post object
604
		 * @param bool $update Whether this is an existing post being updated or not.
605
		 * @param mixed $state state
606
		 *
607
		 * @module sync
608
		 */
609
		do_action( 'jetpack_sync_save_post', $post_ID, $post, $update, $state );
610
		unset( $this->previous_status[ $post_ID ] );
611
	}
612
613
	/**
614
	 * Handler for the wp_after_insert_post hook.
615
	 * Called after creation/update of a new post.
616
	 *
617
	 * @param int      $post_ID Post ID.
618
	 * @param \WP_Post $post    Post object.
619
	 **/
620
	public function wp_after_insert_post( $post_ID, $post ) {
621
		if ( ! is_numeric( $post_ID ) || is_null( $post ) ) {
622
			return;
623
		}
624
625
		// Workaround for https://github.com/woocommerce/woocommerce/issues/18007.
626
		if ( $post && 'shop_order' === $post->post_type ) {
627
			$post = get_post( $post_ID );
628
		}
629
630
		$this->send_published( $post_ID, $post );
631
	}
632
633
	/**
634
	 * Send a published post for sync.
635
	 *
636
	 * @param int      $post_ID Post ID.
637
	 * @param \WP_Post $post    Post object.
638
	 */
639
	public function send_published( $post_ID, $post ) {
640
		if ( ! isset( $this->just_published[ $post_ID ] ) ) {
641
			return;
642
		}
643
644
		// Post revisions cause race conditions where this send_published add the action before the actual post gets synced.
645
		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
646
			return;
647
		}
648
649
		$post_flags = array(
650
			'post_type' => $post->post_type,
651
		);
652
653
		$author_user_object = get_user_by( 'id', $post->post_author );
654
		if ( $author_user_object ) {
655
			$roles = new Roles();
656
657
			$post_flags['author'] = array(
658
				'id'              => $post->post_author,
659
				'wpcom_user_id'   => get_user_meta( $post->post_author, 'wpcom_user_id', true ),
660
				'display_name'    => $author_user_object->display_name,
661
				'email'           => $author_user_object->user_email,
662
				'translated_role' => $roles->translate_user_to_role( $author_user_object ),
663
			);
664
		}
665
666
		/**
667
		 * Filter that is used to add to the post flags ( meta data ) when a post gets published
668
		 *
669
		 * @since 4.4.0
670
		 *
671
		 * @param mixed array post flags that are added to the post
672
		 * @param mixed $post \WP_Post object
673
		 */
674
		$flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $post.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
675
676
		// Only Send Pulished Post event if post_type is not blacklisted.
677
		if ( ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) {
678
			/**
679
			 * Action that gets synced when a post type gets published.
680
			 *
681
			 * @since 4.4.0
682
			 *
683
			 * @param int $post_ID
684
			 * @param mixed array $flags post flags that are added to the post
685
			 */
686
			do_action( 'jetpack_published_post', $post_ID, $flags );
687
		}
688
		unset( $this->just_published[ $post_ID ] );
689
690
		/**
691
		 * Send additional sync action for Activity Log when post is a Customizer publish
692
		 */
693
		if ( 'customize_changeset' === $post->post_type ) {
694
			$post_content = json_decode( $post->post_content, true );
695
			foreach ( $post_content as $key => $value ) {
696
				// Skip if it isn't a widget.
697
				if ( 'widget_' !== substr( $key, 0, strlen( 'widget_' ) ) ) {
698
					continue;
699
				}
700
				// Change key from "widget_archives[2]" to "archives-2".
701
				$key = str_replace( 'widget_', '', $key );
702
				$key = str_replace( '[', '-', $key );
703
				$key = str_replace( ']', '', $key );
704
705
				global $wp_registered_widgets;
706
				if ( isset( $wp_registered_widgets[ $key ] ) ) {
707
					$widget_data = array(
708
						'name'  => $wp_registered_widgets[ $key ]['name'],
709
						'id'    => $key,
710
						'title' => $value['value']['title'],
711
					);
712
					do_action( 'jetpack_widget_edited', $widget_data );
713
				}
714
			}
715
		}
716
	}
717
718
	/**
719
	 * Expand post IDs to post objects within a hook before they are serialized and sent to the server.
720
	 *
721
	 * @access public
722
	 *
723
	 * @param array $args The hook parameters.
724
	 * @return array $args The expanded hook parameters.
725
	 */
726
	public function expand_post_ids( $args ) {
727
		list( $post_ids, $previous_interval_end) = $args;
728
729
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
730
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
731
		$posts = array_values( $posts ); // Reindex in case posts were deleted.
732
733
		return array(
734
			$posts,
735
			$this->get_metadata( $post_ids, 'post', Settings::get_setting( 'post_meta_whitelist' ) ),
736
			$this->get_term_relationships( $post_ids ),
737
			$previous_interval_end,
738
		);
739
	}
740
741
	/**
742
	 * Gets a list of minimum and maximum object ids for each batch based on the given batch size.
743
	 *
744
	 * @access public
745
	 *
746
	 * @param int         $batch_size The batch size for objects.
747
	 * @param string|bool $where_sql  The sql where clause minus 'WHERE', or false if no where clause is needed.
748
	 *
749
	 * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found.
750
	 */
751
	public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) {
752
		return parent::get_min_max_object_ids_for_batches( $batch_size, $this->get_where_sql( $where_sql ) );
0 ignored issues
show
Documentation introduced by
$where_sql is of type string|boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
753
	}
754
}
755