Completed
Push — update/jetpack_published_post_... ( 37957f...5cfe6f )
by
unknown
11:04
created

Jetpack_Sync_Module_Posts::add_wp_insert_post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 9
rs 9.6666
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
9
	public function name() {
10
		return 'posts';
11
	}
12
13
	public function get_object_by_id( $object_type, $id ) {
14
		if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) {
15
			return $this->filter_post_content_and_add_links( $post );
16
		}
17
18
		return false;
19
	}
20
21
	public function set_defaults() {
22
	}
23
24
	public function init_listeners( $callable ) {
25
		add_action( 'wp_insert_post', array( $this, 'add_wp_insert_post' ), 10, 3 );
26
		add_action( 'deleted_post', $callable, 10 );
27
		add_action( 'jetpack_publicize_post', $callable );
28
		add_action( 'jetpack_published_post', $callable, 10, 2 );
29
		add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
30
		add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) );
31
		
32
		// listen for meta changes
33
		$this->init_listeners_for_meta_type( 'post', $callable );
34
		$this->init_meta_whitelist_handler( 'post', array( $this, 'filter_meta' ) );
35
	}
36
37
	public function add_wp_insert_post( $post_ID, $post, $update ) {
38
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
39
		$listener = Jetpack_Sync_Listener::get_instance();
40
		$listener->action_handler( $post_ID, $post, $update );
41
42
		if ( in_array( $post_ID, $this->just_published ) ) {
43
			$this->send_published_post( $post_ID, $post );
44
		}
45
	}
46
47
	public function init_full_sync_listeners( $callable ) {
48
		add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta
49
	}
50
51
	public function init_before_send() {
52
		add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) );
53
54
		// full sync
55
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
56
	}
57
58
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
59
		global $wpdb;
60
61
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
62
	}
63
64 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
65
		global $wpdb;
66
67
		$query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
68
		$count = $wpdb->get_var( $query );
69
70
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
71
	}
72
73 View Code Duplication
	private function get_where_sql( $config ) {
74
		$where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql();
75
76
		// config is a list of post IDs to sync
77
		if ( is_array( $config ) ) {
78
			$where_sql   .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
79
		}
80
81
		return $where_sql;
82
	}
83
84
	function get_full_sync_actions() {
85
		return array( 'jetpack_full_sync_posts' );
86
	}
87
88
	/**
89
	 * Process content before send
90
	 * @param array, wp_insert_post arguments
91
	 * @return array
92
	 */
93
	function expand_wp_insert_post( $args ) {
94
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
95
	}
96
97
	function filter_blacklisted_post_types( $args ) {
98
		$post = $args[1];
99
100
		if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
101
			return false;
102
		}
103
104
		return $args;
105
	}
106
107
	// Meta
108
	function filter_meta( $args ) {
109
		if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) {
110
			return $args;
111
		}
112
113
		return false;
114
	}
115
116
	function is_whitelisted_post_meta( $meta_key ) {
117
		// _wpas_skip_ is used by publicize
118
		return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' );
119
	}
120
121
	function is_post_type_allowed( $post_id ) {
122
		$post = get_post( $post_id );
123
		return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) );
124
	}
125
126 View Code Duplication
	function remove_embed() {
127
		global $wp_embed;
128
		remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
129
		// remove the embed shortcode since we would do the part later.
130
		remove_shortcode( 'embed' );
131
		// Attempts to embed all URLs in a post
132
		remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
133
	}
134
135 View Code Duplication
	function add_embed() {
136
		global $wp_embed;
137
		add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
138
		// Shortcode placeholder for strip_shortcodes()
139
		add_shortcode( 'embed', '__return_false' );
140
		// Attempts to embed all URLs in a post
141
		add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
142
	}
143
144
	// Expands wp_insert_post to include filtered content
145
	function filter_post_content_and_add_links( $post_object ) {
146
		global $post;
147
		$post = $post_object;
148
149
		// return non existant post 
150
		$post_type = get_post_type_object( $post->post_type );
151 View Code Duplication
		if ( empty( $post_type) || ! is_object( $post_type ) ) {
152
			$non_existant_post                    = new stdClass();
153
			$non_existant_post->ID                = $post->ID;
154
			$non_existant_post->post_modified     = $post->post_modified;
155
			$non_existant_post->post_modified_gmt = $post->post_modified_gmt;
156
			$non_existant_post->post_status       = 'jetpack_sync_non_registered_post_type';
157
			
158
			return $non_existant_post;
159
		}
160
		/**
161
		 * Filters whether to prevent sending post data to .com
162
		 *
163
		 * Passing true to the filter will prevent the post data from being sent
164
		 * to the WordPress.com.
165
		 * Instead we pass data that will still enable us to do a checksum against the
166
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
167
		 * other services.
168
		 * @since 4.2.0
169
		 *
170
		 * @param boolean false prevent post data from being synced to WordPress.com
171
		 * @param mixed $post WP_POST object
172
		 */
173 View Code Duplication
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
174
			// We only send the bare necessary object to be able to create a checksum.
175
			$blocked_post                    = new stdClass();
176
			$blocked_post->ID                = $post->ID;
177
			$blocked_post->post_modified     = $post->post_modified;
178
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
179
			$blocked_post->post_status       = 'jetpack_sync_blocked';
180
181
			return $blocked_post;
182
		}
183
184
		// lets not do oembed just yet.
185
		$this->remove_embed();
186
187
		if ( 0 < strlen( $post->post_password ) ) {
188
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
189
		}
190
191
		/** This filter is already documented in core. wp-includes/post-template.php */
192
		if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public  ) {
193
			global $shortcode_tags;
194
			/**
195
			 * Filter prevents some shortcodes from expanding.
196
			 *
197
			 * Since we can can expand some type of shortcode better on the .com side and make the
198
			 * expansion more relevant to contexts. For example [galleries] and subscription emails
199
			 *
200
			 * @since 4.5.0
201
			 *
202
			 * @param array, of shortcode tags to remove.
203
			 */
204
			$shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( 'gallery', 'slideshow' ) );
205
			$removed_shortcode_callbacks = array();
206
			foreach ( $shortcodes_to_remove as $shortcode ) {
207
				if ( isset ( $shortcode_tags[ $shortcode ] )  ) {
208
					$removed_shortcode_callbacks[ $shortcode ] =  $shortcode_tags[ $shortcode ];
209
				}
210
			}
211
212
			array_map( 'remove_shortcode' , array_keys( $removed_shortcode_callbacks ) );
213
214
			$post->post_content_filtered   = apply_filters( 'the_content', $post->post_content );
215
			$post->post_excerpt_filtered   = apply_filters( 'the_excerpt', $post->post_excerpt );
216
217
			foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) {
218
			add_shortcode( $shortcode, $callback );
219
		}
220
		}
221
222
		$this->add_embed();
223
224
		if ( has_post_thumbnail( $post->ID ) ) {
225
			$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
226
			if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
227
				$post->featured_image = $image_attributes[0];
228
			}
229
		}
230
231
		$post->permalink               = get_permalink( $post->ID );
232
		$post->shortlink               = wp_get_shortlink( $post->ID );
233
234
		return $post;
235
	}
236
237
	public function save_published( $new_status, $old_status, $post ) {
238
		if ( 'publish' === $new_status && 'publish' !== $old_status ) {
239
			$this->just_published[] = $post->ID;
240
		}
241
	}
242
243
	public function send_published() {
244
245
		while ( ! is_null( $post_ID = array_shift( $this->just_published ) ) ) {
246
			$post = get_post( $post_ID );
247
248
			// Post revisions cause race conditions where this send_published add the action before the actual post gets synced
249
			if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) || empty( $post ) ) {
250
				continue;
251
			}
252
253
			$this->send_published_post( $post_ID, $post );
254
		}
255
	}
256
257
	public function send_published_post( $post_ID, $post ) {
258
		/**
259
		 * Filter that is used to add to the post flags ( meta data ) when a post gets published
260
		 *
261
		 * @since 4.4.0
262
		 *
263
		 * @param mixed array post flags that are added to the post
264
		 * @param mixed $post WP_POST object
265
		 */
266
		$flags = apply_filters( 'jetpack_published_post_flags', array(), $post );
267
268
		/**
269
		 * Action that gets synced when a post type gets published.
270
		 *
271
		 * @since 4.4.0
272
		 *
273
		 * @param int, post_id
274
		 * @param mixed array post flags that are added to the post
275
		 */
276
		do_action( 'jetpack_published_post', $post_ID, $flags );
277
278
		if ( ( $post_ID = array_search( 'strawberry', $this->just_published ) ) !== false) {
279
			unset( $this->just_published[ $post_ID ] );
280
		}
281
	}
282
283
	public function expand_post_ids( $args ) {
284
		$post_ids = $args[0];
285
286
		$posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
287
		$posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
288
		$posts = array_values( $posts ); // reindex in case posts were deleted
289
290
		return array(
291
			$posts,
292
			$this->get_metadata( $post_ids, 'post', Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ),
293
			$this->get_term_relationships( $post_ids ),
294
		);
295
	}
296
}
297