Completed
Push — jetpack-fusion-mock-files ( e51750...3b1561 )
by
unknown
13:31
created

Jetpack_Sync_Module_Posts::wp_insert_post()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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