diff --git a/projects/packages/sync/changelog/update-sync-add-links-when-expanding-posts-in-before-send b/projects/packages/sync/changelog/update-sync-add-links-when-expanding-posts-in-before-send new file mode 100644 index 0000000000000..dd77887df297a --- /dev/null +++ b/projects/packages/sync/changelog/update-sync-add-links-when-expanding-posts-in-before-send @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Sync:Moved the add links part of the expand posts functionality in dedicated sync requests to before_send insetad of before_enqueue diff --git a/projects/packages/sync/src/class-package-version.php b/projects/packages/sync/src/class-package-version.php index 83ee4058665aa..9a90505729922 100644 --- a/projects/packages/sync/src/class-package-version.php +++ b/projects/packages/sync/src/class-package-version.php @@ -12,7 +12,7 @@ */ class Package_Version { - const PACKAGE_VERSION = '3.1.3'; + const PACKAGE_VERSION = '3.1.4-alpha'; const PACKAGE_SLUG = 'sync'; diff --git a/projects/packages/sync/src/modules/class-posts.php b/projects/packages/sync/src/modules/class-posts.php index 0c087ceea6ded..1fd15afd5c2b9 100644 --- a/projects/packages/sync/src/modules/class-posts.php +++ b/projects/packages/sync/src/modules/class-posts.php @@ -227,6 +227,10 @@ public function init_full_sync_listeners( $callable ) { * @access public */ public function init_before_send() { + + add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'filter_jetpack_sync_before_send_jetpack_sync_save_post' ) ); + add_filter( 'jetpack_sync_before_send_jetpack_published_post', array( $this, 'filter_jetpack_sync_before_send_jetpack_published_post' ) ); + // meta. add_filter( 'jetpack_sync_before_send_added_post_meta', array( $this, 'trim_post_meta' ) ); add_filter( 'jetpack_sync_before_send_updated_post_meta', array( $this, 'trim_post_meta' ) ); @@ -324,18 +328,6 @@ public function trim_post_meta( $args ) { return array( $meta_id, $object_id, $meta_key, $meta_value ); } - /** - * Process content before send. - * - * @param array $args Arguments of the `wp_insert_post` hook. - * - * @return array - */ - public function expand_jetpack_sync_save_post( $args ) { - list( $post_id, $post, $update, $previous_state ) = $args; - return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state ); - } - /** * Filter all blacklisted post types and add filtered post content. * @@ -349,7 +341,7 @@ public function filter_jetpack_sync_before_enqueue_jetpack_sync_save_post( $args return false; } - return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state ); + return array( $post_id, $this->filter_post_content( $post ), $update, $previous_state ); } /** @@ -360,8 +352,32 @@ public function filter_jetpack_sync_before_enqueue_jetpack_sync_save_post( $args */ public function filter_jetpack_sync_before_enqueue_jetpack_published_post( $args ) { list( $post_id, $flags, $post ) = $args; + if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) { + return false; + } + return array( $post_id, $flags, $this->filter_post_content( $post ) ); + } + + /** + * Add filtered post content. + * + * @param array $args Hook arguments. + * @return array Hook arguments. + */ + public function filter_jetpack_sync_before_send_jetpack_published_post( $args ) { + list( $post_id, $flags, $filtered_post ) = $args; + return array( $post_id, $flags, $this->add_links( $filtered_post ) ); + } - return array( $post_id, $flags, $this->filter_post_content_and_add_links( $post ) ); + /** + * Add filtered post content. + * + * @param array $args Hook arguments. + * @return array Hook arguments. + */ + public function filter_jetpack_sync_before_send_jetpack_sync_save_post( $args ) { + list( $post_id, $filtered_post, $update, $previous_state ) = $args; + return array( $post_id, $this->add_links( $filtered_post ), $update, $previous_state ); } /** @@ -451,11 +467,21 @@ public function add_embed() { } /** - * Expands wp_insert_post to include filtered content + * Expands wp_insert_post to include filtered content andd add links * * @param \WP_Post $post_object Post object. */ public function filter_post_content_and_add_links( $post_object ) { + $filtered_post = $this->filter_post_content( $post_object ); + return $this->add_links( $filtered_post ); + } + + /** + * Expands wp_insert_post to include filtered content + * + * @param \WP_Post $post_object Post object. + */ + public function filter_post_content( $post_object ) { global $post; // Used to restore the post global. @@ -572,6 +598,27 @@ public function filter_post_content_and_add_links( $post_object ) { $this->add_embed(); + $filtered_post = $post; + + // Restore global post. + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $post = $current_post; + + return $filtered_post; + } + + /** + * Add links to the post object. + * + * @param \WP_Post $post Post object. + * @return \WP_Post Post object with added links. + */ + public function add_links( $post ) { + + if ( isset( $post->post_status ) && in_array( $post->post_status, array( 'jetpack_sync_non_registered_post_type', 'jetpack_sync_blocked' ), true ) ) { + return $post; + } + if ( has_post_thumbnail( $post->ID ) ) { $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { @@ -585,14 +632,7 @@ public function filter_post_content_and_add_links( $post_object ) { if ( function_exists( 'amp_get_permalink' ) ) { $post->amp_permalink = amp_get_permalink( $post->ID ); } - - $filtered_post = $post; - - // Restore global post. - // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited - $post = $current_post; - - return $filtered_post; + return $post; } /** @@ -742,7 +782,6 @@ public function send_published( $post_ID, $post ) { // Only Send Pulished Post event if post_type is not blacklisted. if ( ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) { - /** * Action that gets synced when a post type gets published. * diff --git a/projects/plugins/jetpack/changelog/update-sync-add-links-when-expanding-posts-in-before-send b/projects/plugins/jetpack/changelog/update-sync-add-links-when-expanding-posts-in-before-send new file mode 100644 index 0000000000000..dfd0a85dcdb68 --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-sync-add-links-when-expanding-posts-in-before-send @@ -0,0 +1,4 @@ +Significance: patch +Type: major + +Sync:Moved the add links part of the expand posts functionality in dedicated sync requests to before_send instead of before_enqueue diff --git a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-base.php b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-base.php index 3f8dc2dd57a2e..36a9898f0569d 100644 --- a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-base.php +++ b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-base.php @@ -138,7 +138,14 @@ protected function assertDataIsSynced() { $local_posts = array_map( array( $posts_sync_module, - 'filter_post_content_and_add_links', + 'filter_post_content', + ), + $local->get_posts() + ); + $local_posts = array_map( + array( + $posts_sync_module, + 'add_links', ), $local->get_posts() ); diff --git a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-posts.php b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-posts.php index 024801a6ac248..b6067215149ad 100644 --- a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-posts.php +++ b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-posts.php @@ -51,11 +51,11 @@ public function test_post_content_limit() { '@phan-var \Automattic\Jetpack\Sync\Modules\Posts $post_sync_module'; $this->post->post_content = str_repeat( 'X', Automattic\Jetpack\Sync\Modules\Posts::MAX_POST_CONTENT_LENGTH - 1 ); - $filtered_post = $post_sync_module->filter_post_content_and_add_links( $this->post ); + $filtered_post = $post_sync_module->filter_post_content( $this->post ); $this->assertNotEmpty( $filtered_post->post_content, 'Filtered post content is empty for stings of allowed length.' ); $this->post->post_content = str_repeat( 'X', Automattic\Jetpack\Sync\Modules\Posts::MAX_POST_CONTENT_LENGTH ); - $filtered_post = $post_sync_module->filter_post_content_and_add_links( $this->post ); + $filtered_post = $post_sync_module->filter_post_content( $this->post ); $this->assertEmpty( $filtered_post->post_content, 'Filtered post content is not truncated (empty) for stings larger than allowed length.' ); } @@ -523,13 +523,13 @@ public function test_sync_post_filter_restores_global_post() { $post_sync_module = Modules::get_module( 'posts' ); '@phan-var \Automattic\Jetpack\Sync\Modules\Posts $post_sync_module'; - $post_sync_module->filter_post_content_and_add_links( $this->post ); + $post_sync_module->filter_post_content( $this->post ); $this->assertSame( $post_id, $post->ID ); // Test with post global not set. $post = null; - $post_sync_module->filter_post_content_and_add_links( $this->post ); + $post_sync_module->filter_post_content( $this->post ); $this->assertNull( $post ); } @@ -666,6 +666,8 @@ public function test_do_not_sync_non_existant_post_types() { $this->assertSame( '', $synced_post->post_content_filtered ); $this->assertSame( '', $synced_post->post_excerpt_filtered ); $this->assertEquals( 'does_not_exist', $synced_post->post_type ); + $this->assertObjectNotHasProperty( 'permalink', $synced_post ); + $this->assertObjectNotHasProperty( 'shortlink', $synced_post ); } /** @@ -721,6 +723,8 @@ public function test_sync_post_jetpack_sync_prevent_sending_post_data_filter() { $this->assertTrue( strtotime( $this->post->post_modified_gmt ) <= strtotime( $post->post_modified_gmt ) ); $this->assertEquals( 'jetpack_sync_blocked', $post->post_status ); $this->assertEquals( 'post', $post->post_type ); + $this->assertObjectNotHasProperty( 'permalink', $post ); + $this->assertObjectNotHasProperty( 'shortlink', $post ); // Since the filter is not there any more the sync should happen as expected. $this->post->post_content = 'foo bar'; @@ -730,6 +734,8 @@ public function test_sync_post_jetpack_sync_prevent_sending_post_data_filter() { $synced_post = $this->server_replica_storage->get_post( $this->post->ID ); // no we sync the content and it looks like what we expect to be. $this->assertEquals( $this->post->post_content, $synced_post->post_content ); + $this->assertObjectHasProperty( 'permalink', $synced_post ); + $this->assertObjectHasProperty( 'shortlink', $synced_post ); } /**