Completed
Push — master-stable ( 53f101...a82972 )
by
unknown
86:26 queued 76:28
created

Jetpack_Sync_Module_Posts::is_importer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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