Completed
Push — add/importer_details ( d6ac0a...e15875 )
by
unknown
10:57
created

Jetpack_Sync_Module_Posts::get_importer_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
rs 10
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
		return isset( $importers[ $importer ] ) ? $importers[ $importer ][0] : 'Unknown Importer';
0 ignored issues
show
Bug introduced by
The variable $importers does not exist. Did you mean $importer?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

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