Completed
Push — content-options-blog-display-7... ( 930de1 )
by
unknown
12:25
created

Jetpack_Sync_Module_Posts::remove_embed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

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