Completed
Push — update/sync-refactor-import-ac... ( 47110f )
by
unknown
10:30 queued 21s
created

Jetpack_Sync_Module_Posts::send_published()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 46
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 4
nop 2
dl 0
loc 46
rs 8.4751
c 0
b 0
f 0
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
4
5
class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module {
6
7
	private $just_published = array();
8
	private $previous_status = array();
9
	private $action_handler;
10
	private $import_end = false;
11
12
	const DEFAULT_PREVIOUS_STATE = 'new';
13
14
	public function name() {
15
		return 'posts';
16
	}
17
18
	public function get_object_by_id( $object_type, $id ) {
19
		if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) {
20
			return $this->filter_post_content_and_add_links( $post );
21
		}
22
23
		return false;
24
	}
25
26
	public function set_defaults() {
27
		$this->import_end = false;
28
	}
29
30
	public function init_listeners( $callable ) {
31
		$this->action_handler = $callable;
32
33
		// Core < 4.7 doesn't deal with nested wp_insert_post calls very well
34
		global $wp_version;
35
		$priority = version_compare( $wp_version, '4.7-alpha', '<' ) ? 0 : 11;
36
37
		add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), $priority, 3 );
38
		add_action( 'jetpack_sync_save_post', $callable, 10, 4 );
39
40
		add_action( 'deleted_post', $callable, 10 );
41
		add_action( 'jetpack_published_post', $callable, 10, 2 );
42
43
		add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
44
		add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_post', array( $this, 'filter_blacklisted_post_types' ) );
45
46
		// listen for meta changes
47
		$this->init_listeners_for_meta_type( 'post', $callable );
48
		$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) );
49
50
		add_action( 'jetpack_daily_akismet_meta_cleanup_before', array( $this, 'daily_akismet_meta_cleanup_before' ) );
51
		add_action( 'jetpack_daily_akismet_meta_cleanup_after', array( $this, 'daily_akismet_meta_cleanup_after' ) );
52
		add_action( 'jetpack_post_meta_batch_delete', $callable, 10, 2 );
53
54
	}
55
56
57
	public function daily_akismet_meta_cleanup_before( $feedback_ids ) {
58
		remove_action( 'deleted_post_meta', $this->action_handler );
59
		/**
60
		 * Used for syncing deletion of batch post meta
61
		 *
62
		 * @since 6.1.0
63
		 *
64
		 * @module sync
65
		 *
66
		 * $param array $feedback_ids feedback post IDs
67
		 * $param string $meta_key to be deleted
68
		 */
69
		do_action( 'jetpack_post_meta_batch_delete', $feedback_ids, '_feedback_akismet_values');
70
	}
71
72
	public function daily_akismet_meta_cleanup_after( $feedback_ids ) {
73
		add_action( 'deleted_post_meta', $this->action_handler );
74
	}
75
76
	public function init_full_sync_listeners( $callable ) {
77
		add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta
78
	}
79
80
	public function init_before_send() {
81
		add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'expand_jetpack_sync_save_post' ) );
82
83
		// full sync
84
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
85
	}
86
87
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
88
		global $wpdb;
89
90
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
91
	}
92
93 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
94
		global $wpdb;
95
96
		$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
97
		$count = $wpdb->get_var( $query );
98
99
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
100
	}
101
102 View Code Duplication
	private function get_where_sql( $config ) {
103
		$where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql();
104
105
		// config is a list of post IDs to sync
106
		if ( is_array( $config ) ) {
107
			$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
108
		}
109
110
		return $where_sql;
111
	}
112
113
	function get_full_sync_actions() {
114
		return array( 'jetpack_full_sync_posts' );
115
	}
116
117
	/**
118
	 * Process content before send
119
	 *
120
	 * @param array $args wp_insert_post arguments
121
	 *
122
	 * @return array
123
	 */
124
	function expand_jetpack_sync_save_post( $args ) {
125
		list( $post_id, $post, $update, $previous_state ) = $args;
126
		return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state );
127
	}
128
129
	function filter_blacklisted_post_types( $args ) {
130
		$post = $args[1];
131
132
		if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
133
			return false;
134
		}
135
136
		return $args;
137
	}
138
139
	// Meta
140
	function filter_meta( $args ) {
141
		if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) {
142
			return $args;
143
		}
144
145
		return false;
146
	}
147
148
	function is_whitelisted_post_meta( $meta_key ) {
149
		// _wpas_skip_ is used by publicize
150
		return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' );
151
	}
152
153
	function is_post_type_allowed( $post_id ) {
154
		$post = get_post( intval( $post_id ) );
155
		if( $post->post_type ) {
156
			return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) );
157
		}
158
		return false;
159
	}
160
161 View Code Duplication
	function remove_embed() {
162
		global $wp_embed;
163
		remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
164
		// remove the embed shortcode since we would do the part later.
165
		remove_shortcode( 'embed' );
166
		// Attempts to embed all URLs in a post
167
		remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
168
	}
169
170 View Code Duplication
	function add_embed() {
171
		global $wp_embed;
172
		add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
173
		// Shortcode placeholder for strip_shortcodes()
174
		add_shortcode( 'embed', '__return_false' );
175
		// Attempts to embed all URLs in a post
176
		add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
177
	}
178
179
	// Expands wp_insert_post to include filtered content
180
	function filter_post_content_and_add_links( $post_object ) {
181
		global $post;
182
		$post = $post_object;
183
184
		// return non existant post
185
		$post_type = get_post_type_object( $post->post_type );
186 View Code Duplication
		if ( empty( $post_type ) || ! is_object( $post_type ) ) {
187
			$non_existant_post                    = new stdClass();
188
			$non_existant_post->ID                = $post->ID;
189
			$non_existant_post->post_modified     = $post->post_modified;
190
			$non_existant_post->post_modified_gmt = $post->post_modified_gmt;
191
			$non_existant_post->post_status       = 'jetpack_sync_non_registered_post_type';
192
193
			return $non_existant_post;
194
		}
195
		/**
196
		 * Filters whether to prevent sending post data to .com
197
		 *
198
		 * Passing true to the filter will prevent the post data from being sent
199
		 * to the WordPress.com.
200
		 * Instead we pass data that will still enable us to do a checksum against the
201
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
202
		 * other services.
203
		 * @since 4.2.0
204
		 *
205
		 * @param boolean false prevent post data from being synced to WordPress.com
206
		 * @param mixed $post WP_POST object
207
		 */
208 View Code Duplication
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
209
			// We only send the bare necessary object to be able to create a checksum.
210
			$blocked_post                    = new stdClass();
211
			$blocked_post->ID                = $post->ID;
212
			$blocked_post->post_modified     = $post->post_modified;
213
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
214
			$blocked_post->post_status       = 'jetpack_sync_blocked';
215
216
			return $blocked_post;
217
		}
218
219
		// lets not do oembed just yet.
220
		$this->remove_embed();
221
222
		if ( 0 < strlen( $post->post_password ) ) {
223
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
224
		}
225
226
		/** This filter is already documented in core. wp-includes/post-template.php */
227
		if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) {
228
			global $shortcode_tags;
229
			/**
230
			 * Filter prevents some shortcodes from expanding.
231
			 *
232
			 * Since we can can expand some type of shortcode better on the .com side and make the
233
			 * expansion more relevant to contexts. For example [galleries] and subscription emails
234
			 *
235
			 * @since 4.5.0
236
			 *
237
			 * @param array of shortcode tags to remove.
238
			 */
239
			$shortcodes_to_remove        = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array(
240
				'gallery',
241
				'slideshow'
242
			) );
243
			$removed_shortcode_callbacks = array();
244
			foreach ( $shortcodes_to_remove as $shortcode ) {
245
				if ( isset ( $shortcode_tags[ $shortcode ] ) ) {
246
					$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ];
247
				}
248
			}
249
250
			array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) );
251
252
			$post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
253
			$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt );
254
255
			foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) {
256
				add_shortcode( $shortcode, $callback );
257
			}
258
		}
259
260
		$this->add_embed();
261
262
		if ( has_post_thumbnail( $post->ID ) ) {
263
			$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
264
			if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
265
				$post->featured_image = $image_attributes[0];
266
			}
267
		}
268
269
		$post->permalink = get_permalink( $post->ID );
270
		$post->shortlink = wp_get_shortlink( $post->ID );
271
272
		if ( function_exists( 'amp_get_permalink' ) ) {
273
			$post->amp_permalink = amp_get_permalink( $post->ID );
274
		}
275
276
		return $post;
277
	}
278
279
	public function save_published( $new_status, $old_status, $post ) {
280
		if ( 'publish' === $new_status && 'publish' !== $old_status ) {
281
			$this->just_published[ $post->ID ] = true;
282
		}
283
284
		$this->previous_status[ $post->ID ] = $old_status;
285
	}
286
287
	public function wp_insert_post( $post_ID, $post = null, $update = null ) {
288
		if ( ! is_numeric( $post_ID ) || is_null( $post ) ) {
289
			return;
290
		}
291
292
		// workaround for https://github.com/woocommerce/woocommerce/issues/18007
293
		if ( $post && 'shop_order' === $post->post_type ) {
294
			$post = get_post( $post_ID );
295
		}
296
297
		$previous_status = isset( $this->previous_status[ $post_ID ] ) ?
298
			$this->previous_status[ $post_ID ] :
299
			self::DEFAULT_PREVIOUS_STATE;
300
301
		$just_published = isset( $this->just_published[ $post_ID ] ) ?
302
			$this->just_published[ $post_ID ] :
303
			false;
304
305
		$state = array(
306
			'is_auto_save' => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ),
307
			'previous_status' => $previous_status,
308
			'just_published' => $just_published
309
		);
310
		/**
311
		 * Filter that is used to add to the post flags ( meta data ) when a post gets published
312
		 *
313
		 * @since 5.8.0
314
		 *
315
		 * @param int $post_ID the post ID
316
		 * @param mixed $post WP_POST object
317
		 * @param bool  $update Whether this is an existing post being updated or not.
318
		 * @param mixed $state state
319
		 *
320
		 * @module sync
321
		 */
322
		do_action( 'jetpack_sync_save_post', $post_ID, $post, $update, $state );
323
		unset( $this->previous_status[ $post_ID ] );
324
		$this->send_published( $post_ID, $post );
325
	}
326
327
	public function send_published( $post_ID, $post ) {
328
		if ( ! isset( $this->just_published[ $post_ID ] ) ) {
329
			return;
330
		}
331
332
		// Post revisions cause race conditions where this send_published add the action before the actual post gets synced
333
		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
334
			return;
335
		}
336
337
		$post_flags = array(
338
			'post_type' => $post->post_type
339
		);
340
341
		$author_user_object = get_user_by( 'id', $post->post_author );
342
		if ( $author_user_object ) {
343
			$post_flags['author'] = array(
344
				'id'              => $post->post_author,
345
				'wpcom_user_id'   => get_user_meta( $post->post_author, 'wpcom_user_id', true ),
346
				'display_name'    => $author_user_object->display_name,
347
				'email'           => $author_user_object->user_email,
348
				'translated_role' => Jetpack::translate_user_to_role( $author_user_object ),
349
			);
350
		}
351
352
		/**
353
		 * Filter that is used to add to the post flags ( meta data ) when a post gets published
354
		 *
355
		 * @since 4.4.0
356
		 *
357
		 * @param mixed array post flags that are added to the post
358
		 * @param mixed $post WP_POST object
359
		 */
360
		$flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post );
361
362
		/**
363
		 * Action that gets synced when a post type gets published.
364
		 *
365
		 * @since 4.4.0
366
		 *
367
		 * @param int $post_ID
368
		 * @param mixed array $flags post flags that are added to the post
369
		 */
370
		do_action( 'jetpack_published_post', $post_ID, $flags );
371
		unset( $this->just_published[ $post_ID ] );
372
	}
373
374
	public function expand_post_ids( $args ) {
375
		$post_ids = $args[0];
376
377
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
378
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
379
		$posts = array_values( $posts ); // reindex in case posts were deleted
380
381
		return array(
382
			$posts,
383
			$this->get_metadata( $post_ids, 'post', Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ),
384
			$this->get_term_relationships( $post_ids ),
385
		);
386
	}
387
}
388