Completed
Push — update/jetpack_published_post_... ( 8caf9c...189895 )
by
unknown
25:56 queued 13:41
created

Jetpack_Sync_Module_Posts::send_published()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 40
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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