Completed
Push — add/jetpack-start-reconnect-us... ( 7f1fbb )
by
unknown
10:36
created

Jetpack_Sync_Module_Posts::save_published()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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