PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.1.4
Jetpack – WP Security, Backup, Speed, & Growth v7.1.4
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / sync / class.jetpack-sync-module-posts.php
class.jetpack-sync-module-posts.php
434 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $previous_status = array();
9 private $action_handler;
10 private $import_end = false;
11
12 const DEFAULT_PREVIOUS_STATE = 'new';
13
14 public function name() {
15 return 'posts';
16 }
17
18 public function get_object_by_id( $object_type, $id ) {
19 if ( $object_type === 'post' && $post = get_post( intval( $id ) ) ) {
20 return $this->filter_post_content_and_add_links( $post );
21 }
22
23 return false;
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 add_action( 'jetpack_sync_save_post', $callable, 10, 4 );
35
36 add_action( 'deleted_post', $callable, 10 );
37 add_action( 'jetpack_published_post', $callable, 10, 2 );
38
39 add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 );
40 add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_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
46 add_action( 'jetpack_daily_akismet_meta_cleanup_before', array( $this, 'daily_akismet_meta_cleanup_before' ) );
47 add_action( 'jetpack_daily_akismet_meta_cleanup_after', array( $this, 'daily_akismet_meta_cleanup_after' ) );
48 add_action( 'jetpack_post_meta_batch_delete', $callable, 10, 2 );
49
50 }
51
52
53 public function daily_akismet_meta_cleanup_before( $feedback_ids ) {
54 remove_action( 'deleted_post_meta', $this->action_handler );
55 /**
56 * Used for syncing deletion of batch post meta
57 *
58 * @since 6.1.0
59 *
60 * @module sync
61 *
62 * @param array $feedback_ids feedback post IDs
63 * @param string $meta_key to be deleted
64 */
65 do_action( 'jetpack_post_meta_batch_delete', $feedback_ids, '_feedback_akismet_values' );
66 }
67
68 public function daily_akismet_meta_cleanup_after( $feedback_ids ) {
69 add_action( 'deleted_post_meta', $this->action_handler );
70 }
71
72 public function init_full_sync_listeners( $callable ) {
73 add_action( 'jetpack_full_sync_posts', $callable ); // also sends post meta
74 }
75
76 public function init_before_send() {
77 add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'expand_jetpack_sync_save_post' ) );
78
79 // full sync
80 add_filter( 'jetpack_sync_before_send_jetpack_full_sync_posts', array( $this, 'expand_post_ids' ) );
81 }
82
83 public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
84 global $wpdb;
85
86 return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_posts', $wpdb->posts, 'ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
87 }
88
89 public function estimate_full_sync_actions( $config ) {
90 global $wpdb;
91
92 $query = "SELECT count(*) FROM $wpdb->posts WHERE " . $this->get_where_sql( $config );
93 $count = $wpdb->get_var( $query );
94
95 return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
96 }
97
98 private function get_where_sql( $config ) {
99 $where_sql = Jetpack_Sync_Settings::get_blacklisted_post_types_sql();
100
101 // config is a list of post IDs to sync
102 if ( is_array( $config ) ) {
103 $where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
104 }
105
106 return $where_sql;
107 }
108
109 function get_full_sync_actions() {
110 return array( 'jetpack_full_sync_posts' );
111 }
112
113 /**
114 * Process content before send
115 *
116 * @param array $args wp_insert_post arguments
117 *
118 * @return array
119 */
120 function expand_jetpack_sync_save_post( $args ) {
121 list( $post_id, $post, $update, $previous_state ) = $args;
122 return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state );
123 }
124
125 function filter_blacklisted_post_types( $args ) {
126 $post = $args[1];
127
128 if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
129 return false;
130 }
131
132 return $args;
133 }
134
135 // Meta
136 function filter_meta( $args ) {
137 if ( $this->is_post_type_allowed( $args[1] ) && $this->is_whitelisted_post_meta( $args[2] ) ) {
138 return $args;
139 }
140
141 return false;
142 }
143
144 function is_whitelisted_post_meta( $meta_key ) {
145 // _wpas_skip_ is used by publicize
146 return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ) || wp_startswith( $meta_key, '_wpas_skip_' );
147 }
148
149 function is_post_type_allowed( $post_id ) {
150 $post = get_post( intval( $post_id ) );
151 if ( $post->post_type ) {
152 return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) );
153 }
154 return false;
155 }
156
157 function remove_embed() {
158 global $wp_embed;
159 remove_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
160 // remove the embed shortcode since we would do the part later.
161 remove_shortcode( 'embed' );
162 // Attempts to embed all URLs in a post
163 remove_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
164 }
165
166 function add_embed() {
167 global $wp_embed;
168 add_filter( 'the_content', array( $wp_embed, 'run_shortcode' ), 8 );
169 // Shortcode placeholder for strip_shortcodes()
170 add_shortcode( 'embed', '__return_false' );
171 // Attempts to embed all URLs in a post
172 add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 );
173 }
174
175 // Expands wp_insert_post to include filtered content
176 function filter_post_content_and_add_links( $post_object ) {
177 global $post;
178 $post = $post_object;
179
180 // return non existant post
181 $post_type = get_post_type_object( $post->post_type );
182 if ( empty( $post_type ) || ! is_object( $post_type ) ) {
183 $non_existant_post = new stdClass();
184 $non_existant_post->ID = $post->ID;
185 $non_existant_post->post_modified = $post->post_modified;
186 $non_existant_post->post_modified_gmt = $post->post_modified_gmt;
187 $non_existant_post->post_status = 'jetpack_sync_non_registered_post_type';
188
189 return $non_existant_post;
190 }
191 /**
192 * Filters whether to prevent sending post data to .com
193 *
194 * Passing true to the filter will prevent the post data from being sent
195 * to the WordPress.com.
196 * Instead we pass data that will still enable us to do a checksum against the
197 * Jetpacks data but will prevent us from displaying the data on in the API as well as
198 * other services.
199 *
200 * @since 4.2.0
201 *
202 * @param boolean false prevent post data from being synced to WordPress.com
203 * @param mixed $post WP_POST object
204 */
205 if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
206 // We only send the bare necessary object to be able to create a checksum.
207 $blocked_post = new stdClass();
208 $blocked_post->ID = $post->ID;
209 $blocked_post->post_modified = $post->post_modified;
210 $blocked_post->post_modified_gmt = $post->post_modified_gmt;
211 $blocked_post->post_status = 'jetpack_sync_blocked';
212
213 return $blocked_post;
214 }
215
216 // lets not do oembed just yet.
217 $this->remove_embed();
218
219 if ( 0 < strlen( $post->post_password ) ) {
220 $post->post_password = 'auto-' . wp_generate_password( 10, false );
221 }
222
223 /** This filter is already documented in core. wp-includes/post-template.php */
224 if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) {
225 global $shortcode_tags;
226 /**
227 * Filter prevents some shortcodes from expanding.
228 *
229 * Since we can can expand some type of shortcode better on the .com side and make the
230 * expansion more relevant to contexts. For example [galleries] and subscription emails
231 *
232 * @since 4.5.0
233 *
234 * @param array of shortcode tags to remove.
235 */
236 $shortcodes_to_remove = apply_filters(
237 'jetpack_sync_do_not_expand_shortcodes',
238 array(
239 'gallery',
240 'slideshow',
241 )
242 );
243 $removed_shortcode_callbacks = array();
244 foreach ( $shortcodes_to_remove as $shortcode ) {
245 if ( isset( $shortcode_tags[ $shortcode ] ) ) {
246 $removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ];
247 }
248 }
249
250 array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) );
251
252 $post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
253 $post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt );
254
255 foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) {
256 add_shortcode( $shortcode, $callback );
257 }
258 }
259
260 $this->add_embed();
261
262 if ( has_post_thumbnail( $post->ID ) ) {
263 $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
264 if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
265 $post->featured_image = $image_attributes[0];
266 }
267 }
268
269 $post->permalink = get_permalink( $post->ID );
270 $post->shortlink = wp_get_shortlink( $post->ID );
271
272 if ( function_exists( 'amp_get_permalink' ) ) {
273 $post->amp_permalink = amp_get_permalink( $post->ID );
274 }
275
276 return $post;
277 }
278
279 public function save_published( $new_status, $old_status, $post ) {
280 if ( 'publish' === $new_status && 'publish' !== $old_status ) {
281 $this->just_published[ $post->ID ] = true;
282 }
283
284 $this->previous_status[ $post->ID ] = $old_status;
285 }
286
287 /*
288 * When publishing or updating a post, the Gutenberg editor sends two requests:
289 * 1. sent to WP REST API endpoint `wp-json/wp/v2/posts/$id`
290 * 2. sent to wp-admin/post.php `?post=$id&action=edit&classic-editor=1&meta_box=1`
291 *
292 * The 2nd request is to update post meta, which is not supported on WP REST API.
293 * When syncing post data, we will include if this was a meta box update.
294 */
295 public function is_gutenberg_meta_box_update() {
296 return (
297 isset( $_POST['action'], $_GET['classic-editor'], $_GET['meta_box'] ) &&
298 'editpost' === $_POST['action'] &&
299 '1' === $_GET['classic-editor'] &&
300 '1' === $_GET['meta_box'] &&
301 Jetpack_Gutenberg::is_gutenberg_available()
302 );
303 }
304
305 public function wp_insert_post( $post_ID, $post = null, $update = null ) {
306 if ( ! is_numeric( $post_ID ) || is_null( $post ) ) {
307 return;
308 }
309
310 // workaround for https://github.com/woocommerce/woocommerce/issues/18007
311 if ( $post && 'shop_order' === $post->post_type ) {
312 $post = get_post( $post_ID );
313 }
314
315 $previous_status = isset( $this->previous_status[ $post_ID ] ) ?
316 $this->previous_status[ $post_ID ] :
317 self::DEFAULT_PREVIOUS_STATE;
318
319 $just_published = isset( $this->just_published[ $post_ID ] ) ?
320 $this->just_published[ $post_ID ] :
321 false;
322
323 $state = array(
324 'is_auto_save' => (bool) Jetpack_Constants::get_constant( 'DOING_AUTOSAVE' ),
325 'previous_status' => $previous_status,
326 'just_published' => $just_published,
327 'is_gutenberg_meta_box_update' => $this->is_gutenberg_meta_box_update(),
328 );
329 /**
330 * Filter that is used to add to the post flags ( meta data ) when a post gets published
331 *
332 * @since 5.8.0
333 *
334 * @param int $post_ID the post ID
335 * @param mixed $post WP_POST object
336 * @param bool $update Whether this is an existing post being updated or not.
337 * @param mixed $state state
338 *
339 * @module sync
340 */
341 do_action( 'jetpack_sync_save_post', $post_ID, $post, $update, $state );
342 unset( $this->previous_status[ $post_ID ] );
343 $this->send_published( $post_ID, $post );
344 }
345
346 public function send_published( $post_ID, $post ) {
347 if ( ! isset( $this->just_published[ $post_ID ] ) ) {
348 return;
349 }
350
351 // Post revisions cause race conditions where this send_published add the action before the actual post gets synced
352 if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
353 return;
354 }
355
356 $post_flags = array(
357 'post_type' => $post->post_type,
358 );
359
360 $author_user_object = get_user_by( 'id', $post->post_author );
361 if ( $author_user_object ) {
362 $post_flags['author'] = array(
363 'id' => $post->post_author,
364 'wpcom_user_id' => get_user_meta( $post->post_author, 'wpcom_user_id', true ),
365 'display_name' => $author_user_object->display_name,
366 'email' => $author_user_object->user_email,
367 'translated_role' => Jetpack::translate_user_to_role( $author_user_object ),
368 );
369 }
370
371 /**
372 * Filter that is used to add to the post flags ( meta data ) when a post gets published
373 *
374 * @since 4.4.0
375 *
376 * @param mixed array post flags that are added to the post
377 * @param mixed $post WP_POST object
378 */
379 $flags = apply_filters( 'jetpack_published_post_flags', $post_flags, $post );
380
381 /**
382 * Action that gets synced when a post type gets published.
383 *
384 * @since 4.4.0
385 *
386 * @param int $post_ID
387 * @param mixed array $flags post flags that are added to the post
388 */
389 do_action( 'jetpack_published_post', $post_ID, $flags );
390 unset( $this->just_published[ $post_ID ] );
391
392 /**
393 * Send additional sync action for Activity Log when post is a Customizer publish
394 */
395 if ( 'customize_changeset' == $post->post_type ) {
396 $post_content = json_decode( $post->post_content, true );
397 foreach ( $post_content as $key => $value ) {
398 // Skip if it isn't a widget
399 if ( 'widget_' != substr( $key, 0, strlen( 'widget_' ) ) ) {
400 continue;
401 }
402 // Change key from "widget_archives[2]" to "archives-2"
403 $key = str_replace( 'widget_', '', $key );
404 $key = str_replace( '[', '-', $key );
405 $key = str_replace( ']', '', $key );
406
407 global $wp_registered_widgets;
408 if ( isset( $wp_registered_widgets[ $key ] ) ) {
409 $widget_data = array(
410 'name' => $wp_registered_widgets[ $key ]['name'],
411 'id' => $key,
412 'title' => $value['value']['title'],
413 );
414 do_action( 'jetpack_widget_edited', $widget_data );
415 }
416 }
417 }
418 }
419
420 public function expand_post_ids( $args ) {
421 $post_ids = $args[0];
422
423 $posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) );
424 $posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts );
425 $posts = array_values( $posts ); // reindex in case posts were deleted
426
427 return array(
428 $posts,
429 $this->get_metadata( $post_ids, 'post', Jetpack_Sync_Settings::get_setting( 'post_meta_whitelist' ) ),
430 $this->get_term_relationships( $post_ids ),
431 );
432 }
433 }
434