From a6ffb75725a81fa23f5cb264f5c48810548d28e4 Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Tue, 18 Jun 2024 14:04:35 +0200 Subject: [PATCH 01/10] Move the add links part when expanding posts to before send --- .../packages/sync/src/modules/class-posts.php | 77 +++++++++++++------ .../php/sync/test_class.jetpack-sync-base.php | 9 ++- .../sync/test_class.jetpack-sync-posts.php | 14 ++-- 3 files changed, 68 insertions(+), 32 deletions(-) diff --git a/projects/packages/sync/src/modules/class-posts.php b/projects/packages/sync/src/modules/class-posts.php index 0c087ceea6ded..ca364cec43379 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,34 @@ 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; + } + $filtered_post = $this->filter_post_content( $post ); + $filtered_post_with_links = $this->add_links( $filtered_post ); + return array( $post_id, $flags, $filtered_post_with_links ); + } - 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_published_post( $args ) { + list( $post_id, $flags, $filtered_post ) = $args; + return array( $post_id, $flags, $this->add_links( $filtered_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 ); } /** @@ -455,7 +473,7 @@ public function add_embed() { * * @param \WP_Post $post_object Post object. */ - public function filter_post_content_and_add_links( $post_object ) { + public function filter_post_content( $post_object ) { global $post; // Used to restore the post global. @@ -572,6 +590,22 @@ 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 ( 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 +619,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 +769,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. * @@ -883,7 +909,8 @@ public function get_next_chunk( $config, $status, $chunk_size ) { */ private function expand_posts( $post_ids ) { $posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) ); - $posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts ); + $posts = array_map( array( $this, 'filter_post_content' ), $posts ); + $posts = array_map( array( $this, 'add_links' ), $posts ); $posts = array_values( $posts ); // Reindex in case posts were deleted. return $posts; } 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..f670dbf65efd4 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.' ); } @@ -67,7 +67,8 @@ public function test_add_post_syncs_event() { $post_sync_module = Modules::get_module( 'posts' ); '@phan-var \Automattic\Jetpack\Sync\Modules\Posts $post_sync_module'; - $this->post = $post_sync_module->filter_post_content_and_add_links( $this->post ); + $this->post = $post_sync_module->filter_post_content( $this->post ); + $this->post = $post_sync_module->add_links( $this->post ); $this->assertEqualsObject( $this->post, $event->args[1], 'Synced post does not match local post.' ); } @@ -78,7 +79,8 @@ public function test_add_post_syncs_post_data() { $post_sync_module = Modules::get_module( 'posts' ); '@phan-var \Automattic\Jetpack\Sync\Modules\Posts $post_sync_module'; - $this->post = $post_sync_module->filter_post_content_and_add_links( $this->post ); + $this->post = $post_sync_module->filter_post_content( $this->post ); + $this->post = $post_sync_module->add_links( $this->post ); $this->assertEquals( $this->post, $this->server_replica_storage->get_post( $this->post->ID ) ); } @@ -523,13 +525,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 ); } From 4ffa8cd8729df0d8ecf8ba45b7f5c85145bb2eb1 Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Tue, 18 Jun 2024 14:12:45 +0200 Subject: [PATCH 02/10] Add changelog --- .../update-sync-add-links-when-expanding-posts-in-before-send | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/packages/sync/changelog/update-sync-add-links-when-expanding-posts-in-before-send 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 From d8bb23c048e1f224138104059dd4f7d1a7bd0812 Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Tue, 18 Jun 2024 14:13:15 +0200 Subject: [PATCH 03/10] Use the new implementation, with two separated methods --- projects/packages/sync/src/modules/class-posts.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/projects/packages/sync/src/modules/class-posts.php b/projects/packages/sync/src/modules/class-posts.php index ca364cec43379..9e7a7197bfe83 100644 --- a/projects/packages/sync/src/modules/class-posts.php +++ b/projects/packages/sync/src/modules/class-posts.php @@ -129,7 +129,9 @@ public function get_object_by_id( $object_type, $id ) { if ( 'post' === $object_type ) { $post = get_post( (int) $id ); if ( $post ) { - return $this->filter_post_content_and_add_links( $post ); + $filtered_post = $this->filter_post_content( $post ); + $filtered_post_with_links = $this->add_links( $filtered_post ); + return $filtered_post_with_links; } } From 3f7b264d2d22ed0f7626f8913bac671e9414a725 Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Tue, 18 Jun 2024 14:28:06 +0200 Subject: [PATCH 04/10] Version bump --- projects/packages/sync/src/class-package-version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/sync/src/class-package-version.php b/projects/packages/sync/src/class-package-version.php index 87cc451e60912..ba4f842163c04 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.1'; + const PACKAGE_VERSION = '3.1.2-alpha'; const PACKAGE_SLUG = 'sync'; From 53cee2e859ac8b3b62858f461641f1b3584e0a49 Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Tue, 18 Jun 2024 14:31:22 +0200 Subject: [PATCH 05/10] Changelog for jetpack --- .../update-sync-add-links-when-expanding-posts-in-before-send | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/jetpack/changelog/update-sync-add-links-when-expanding-posts-in-before-send 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 From 10362440c71db1318c43bedf4226bcfa34c44bb3 Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Thu, 20 Jun 2024 10:44:28 +0200 Subject: [PATCH 06/10] Got back filter_post_content_and_add_links so we don't have it as a breaking change and we can still use it for expand_posts and get_object_by_id --- .../packages/sync/src/modules/class-posts.php | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/projects/packages/sync/src/modules/class-posts.php b/projects/packages/sync/src/modules/class-posts.php index 9e7a7197bfe83..dcf2b6b329b36 100644 --- a/projects/packages/sync/src/modules/class-posts.php +++ b/projects/packages/sync/src/modules/class-posts.php @@ -129,9 +129,7 @@ public function get_object_by_id( $object_type, $id ) { if ( 'post' === $object_type ) { $post = get_post( (int) $id ); if ( $post ) { - $filtered_post = $this->filter_post_content( $post ); - $filtered_post_with_links = $this->add_links( $filtered_post ); - return $filtered_post_with_links; + return $this->filter_post_content_and_add_links( $post ); } } @@ -357,9 +355,7 @@ public function filter_jetpack_sync_before_enqueue_jetpack_published_post( $args if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) { return false; } - $filtered_post = $this->filter_post_content( $post ); - $filtered_post_with_links = $this->add_links( $filtered_post ); - return array( $post_id, $flags, $filtered_post_with_links ); + return array( $post_id, $flags, $this->filter_post_content( $post ) ); } /** @@ -470,6 +466,16 @@ public function add_embed() { add_filter( 'the_content', array( $wp_embed, 'autoembed' ), 8 ); } + /** + * 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 * @@ -608,6 +614,11 @@ public function filter_post_content( $post_object ) { * @return \WP_Post Post object with added links. */ public function add_links( $post ) { + + if ( ! ( $post instanceof \WP_Post ) || $post->post_status === 'jetpack_sync_blocked' || $post->post_status === 'jetpack_sync_non_registered_post_type' ) { + 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] ) ) { @@ -911,8 +922,7 @@ public function get_next_chunk( $config, $status, $chunk_size ) { */ private function expand_posts( $post_ids ) { $posts = array_filter( array_map( array( 'WP_Post', 'get_instance' ), $post_ids ) ); - $posts = array_map( array( $this, 'filter_post_content' ), $posts ); - $posts = array_map( array( $this, 'add_links' ), $posts ); + $posts = array_map( array( $this, 'filter_post_content_and_add_links' ), $posts ); $posts = array_values( $posts ); // Reindex in case posts were deleted. return $posts; } From 256c41ef6aadebc6f7a537e663882f88a0e466d0 Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Thu, 20 Jun 2024 10:45:34 +0200 Subject: [PATCH 07/10] Added couple of asserts in existing unit tests compatible with trunk to make sure we are behaving in the same way --- .../tests/php/sync/test_class.jetpack-sync-posts.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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 f670dbf65efd4..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 @@ -67,8 +67,7 @@ public function test_add_post_syncs_event() { $post_sync_module = Modules::get_module( 'posts' ); '@phan-var \Automattic\Jetpack\Sync\Modules\Posts $post_sync_module'; - $this->post = $post_sync_module->filter_post_content( $this->post ); - $this->post = $post_sync_module->add_links( $this->post ); + $this->post = $post_sync_module->filter_post_content_and_add_links( $this->post ); $this->assertEqualsObject( $this->post, $event->args[1], 'Synced post does not match local post.' ); } @@ -79,8 +78,7 @@ public function test_add_post_syncs_post_data() { $post_sync_module = Modules::get_module( 'posts' ); '@phan-var \Automattic\Jetpack\Sync\Modules\Posts $post_sync_module'; - $this->post = $post_sync_module->filter_post_content( $this->post ); - $this->post = $post_sync_module->add_links( $this->post ); + $this->post = $post_sync_module->filter_post_content_and_add_links( $this->post ); $this->assertEquals( $this->post, $this->server_replica_storage->get_post( $this->post->ID ) ); } @@ -668,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 ); } /** @@ -723,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'; @@ -732,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 ); } /** From 61b72e8bae3a8113948c6740abe270e42817336d Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Thu, 20 Jun 2024 10:48:35 +0200 Subject: [PATCH 08/10] Cosmetic changes to the check --- projects/packages/sync/src/modules/class-posts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/sync/src/modules/class-posts.php b/projects/packages/sync/src/modules/class-posts.php index dcf2b6b329b36..1fd15afd5c2b9 100644 --- a/projects/packages/sync/src/modules/class-posts.php +++ b/projects/packages/sync/src/modules/class-posts.php @@ -615,7 +615,7 @@ public function filter_post_content( $post_object ) { */ public function add_links( $post ) { - if ( ! ( $post instanceof \WP_Post ) || $post->post_status === 'jetpack_sync_blocked' || $post->post_status === 'jetpack_sync_non_registered_post_type' ) { + if ( isset( $post->post_status ) && in_array( $post->post_status, array( 'jetpack_sync_non_registered_post_type', 'jetpack_sync_blocked' ), true ) ) { return $post; } From 5506999056b63d5abea9e52bc9afdaaf4435c5db Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Tue, 25 Jun 2024 14:08:17 +0200 Subject: [PATCH 09/10] Version bump --- projects/packages/sync/src/class-package-version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/sync/src/class-package-version.php b/projects/packages/sync/src/class-package-version.php index ba4f842163c04..6e59ed409788b 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.2-alpha'; + const PACKAGE_VERSION = '3.1.3-alpha'; const PACKAGE_SLUG = 'sync'; From 381def6861de9fb3a45429f7a91daf6964df798e Mon Sep 17 00:00:00 2001 From: Juanma Rodriguez Escriche Date: Wed, 26 Jun 2024 09:58:49 +0200 Subject: [PATCH 10/10] Version bump --- projects/packages/sync/src/class-package-version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/sync/src/class-package-version.php b/projects/packages/sync/src/class-package-version.php index 6e59ed409788b..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-alpha'; + const PACKAGE_VERSION = '3.1.4-alpha'; const PACKAGE_SLUG = 'sync';