diff options
Diffstat (limited to 'plugins/jetpack/sync/class.jetpack-sync-module-posts.php')
-rw-r--r-- | plugins/jetpack/sync/class.jetpack-sync-module-posts.php | 112 |
1 files changed, 61 insertions, 51 deletions
diff --git a/plugins/jetpack/sync/class.jetpack-sync-module-posts.php b/plugins/jetpack/sync/class.jetpack-sync-module-posts.php index be31c761..3c31bbf7 100644 --- a/plugins/jetpack/sync/class.jetpack-sync-module-posts.php +++ b/plugins/jetpack/sync/class.jetpack-sync-module-posts.php @@ -5,6 +5,7 @@ require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { private $just_published = array(); + private $action_handler; public function name() { return 'posts'; @@ -22,10 +23,15 @@ class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { } public function init_listeners( $callable ) { - add_action( 'wp_insert_post', $callable, 10, 3 ); - add_action( 'wp_insert_post', array( $this, 'send_published'), 11, 3 ); + $this->action_handler = $callable; + + // Core < 4.7 doesn't deal with nested wp_insert_post calls very well + global $wp_version; + $priority = version_compare( $wp_version, '4.7-alpha', '<' ) ? 0 : 11; + + add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), $priority, 3 ); + add_action( 'deleted_post', $callable, 10 ); - add_action( 'jetpack_publicize_post', $callable ); add_action( 'jetpack_published_post', $callable, 10, 2 ); add_action( 'transition_post_status', array( $this, 'save_published' ), 10, 3 ); add_filter( 'jetpack_sync_before_enqueue_wp_insert_post', array( $this, 'filter_blacklisted_post_types' ) ); @@ -66,7 +72,7 @@ class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { // config is a list of post IDs to sync if ( is_array( $config ) ) { - $where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; + $where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; } return $where_sql; @@ -78,8 +84,11 @@ class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { /** * Process content before send + * + * @param array $args wp_insert_post arguments + * + * @return array */ - function expand_wp_insert_post( $args ) { return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] ); } @@ -110,6 +119,7 @@ class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { function is_post_type_allowed( $post_id ) { $post = get_post( $post_id ); + return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ); } @@ -138,13 +148,13 @@ class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { // return non existant post $post_type = get_post_type_object( $post->post_type ); - if ( empty( $post_type) || ! is_object( $post_type ) ) { + if ( empty( $post_type ) || ! is_object( $post_type ) ) { $non_existant_post = new stdClass(); $non_existant_post->ID = $post->ID; $non_existant_post->post_modified = $post->post_modified; $non_existant_post->post_modified_gmt = $post->post_modified_gmt; $non_existant_post->post_status = 'jetpack_sync_non_registered_post_type'; - + return $non_existant_post; } /** @@ -179,9 +189,8 @@ class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { } /** This filter is already documented in core. wp-includes/post-template.php */ - if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { + if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) { global $shortcode_tags; - $shortcodes_and_callbacks_to_remove = array(); /** * Filter prevents some shortcodes from expanding. * @@ -192,22 +201,25 @@ class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { * * @param array of shortcode tags to remove. */ - $shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( 'gallery', 'slideshow' ) ); + $shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( + 'gallery', + 'slideshow' + ) ); $removed_shortcode_callbacks = array(); foreach ( $shortcodes_to_remove as $shortcode ) { - if ( isset ( $shortcode_tags[ $shortcode ] ) ) { - $removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ]; + if ( isset ( $shortcode_tags[ $shortcode ] ) ) { + $removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ]; } } - array_map( 'remove_shortcode' , array_keys( $removed_shortcode_callbacks ) ); + array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) ); - $post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); - $post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); + $post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); + $post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt ); foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) { - add_shortcode( $shortcode, $callback ); - } + add_shortcode( $shortcode, $callback ); + } } $this->add_embed(); @@ -219,8 +231,8 @@ class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { } } - $post->permalink = get_permalink( $post->ID ); - $post->shortlink = wp_get_shortlink( $post->ID ); + $post->permalink = get_permalink( $post->ID ); + $post->shortlink = wp_get_shortlink( $post->ID ); return $post; } @@ -231,44 +243,42 @@ class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module { } } - public function send_published( $post_ID, $post, $update ) { + public function wp_insert_post( $post_ID, $post, $update ) { + call_user_func( $this->action_handler, $post_ID, $post, $update ); + $this->send_published( $post_ID, $post ); + } + + public function send_published( $post_ID, $post ) { + if ( ! in_array( $post_ID, $this->just_published ) ) { + return; + } + // Post revisions cause race conditions where this send_published add the action before the actual post gets synced if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { return; } - if ( ! empty( $this->just_published ) && in_array( $post_ID, $this->just_published ) ) { - $published = array_reverse( array_unique( $this->just_published ) ); - - // Pre 4.7 WP does not have run though send_published for every save_published call - // So lets clear out any just_published that we recorded - foreach ( $published as $just_published_post_ID ) { - if ( $post_ID !== $just_published_post_ID ) { - $post = get_post( $just_published_post_ID ); - } + /** + * Filter that is used to add to the post flags ( meta data ) when a post gets published + * + * @since 4.4.0 + * + * @param mixed array post flags that are added to the post + * @param mixed $post WP_POST object + */ + $flags = apply_filters( 'jetpack_published_post_flags', array(), $post ); - /** - * Filter that is used to add to the post flags ( meta data ) when a post gets published - * - * @since 4.4.0 - * - * @param mixed array post flags that are added to the post - * @param mixed $post WP_POST object - */ - $flags = apply_filters( 'jetpack_published_post_flags', array(), $post ); - - /** - * Action that gets synced when a post type gets published. - * - * @since 4.4.0 - * - * @param int post_id - * @param mixed array post flags that are added to the post - */ - do_action( 'jetpack_published_post', $just_published_post_ID, $flags ); - } - $this->just_published = array(); - } + /** + * Action that gets synced when a post type gets published. + * + * @since 4.4.0 + * + * @param int $post_ID + * @param mixed array $flags post flags that are added to the post + */ + do_action( 'jetpack_published_post', $post_ID, $flags ); + + $this->just_published = array_diff( $this->just_published, array( $post_ID ) ); } public function expand_post_ids( $args ) { |