From 7688fdefe9b2325330996ffe4a0315487038c09a Mon Sep 17 00:00:00 2001 From: paul bearne Date: Wed, 12 Jun 2024 17:53:56 -0400 Subject: [PATCH 1/8] Optimize term count update during post status transition The modification ensures that term counts are only updated when the post's status has truly changed. This reduces unnecessary operations, improving performance when there is no status change on a post. --- src/wp-includes/post.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index b0967dbad82ba..0a51d0c4c4a3b 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -7898,10 +7898,12 @@ function wp_queue_posts_for_term_meta_lazyload( $posts ) { * @param WP_Post $post Post object. */ function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { - // Update counts for the post's terms. - foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { - $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); - wp_update_term_count( $tt_ids, $taxonomy ); + if ( $new_status !== $old_status ) { + // Update counts for the post's terms. + foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { + $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); + wp_update_term_count( $tt_ids, $taxonomy ); + } } } From 8c2fa57c1f26d27b79d6da5372aee3ec02f38ecb Mon Sep 17 00:00:00 2001 From: paul bearne Date: Fri, 14 Jun 2024 11:22:41 -0400 Subject: [PATCH 2/8] Add unit tests for _update_term_count_on_transition_post_status function The commit adds various PHPUnit tests for the "_update_term_count_on_transition_post_status" function. The tests cover multiple scenarios including updates in term count when a post is published, moved to the trash, restored, permanently deleted, or removed from a term. --- .../UpdateTermCountOnTransitionPostStatus.php | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php diff --git a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php new file mode 100644 index 0000000000000..24f9b86211aaa --- /dev/null +++ b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php @@ -0,0 +1,217 @@ +post_type, array( 'public' => true ) ); + register_taxonomy( $this->taxonomy, $this->post_type, array( 'public' => true ) ); + + $this->post_id = self::factory()->post->create( + array( + 'post_type' => $this->post_type, + 'post_status' => 'publish', + ) + ); + + $this->term_id = self::factory()->term->create( + array( + 'taxonomy' => $this->taxonomy, + 'name' => 'Test Category', + ) + ); + + wp_set_object_terms( $this->post_id, $this->term_id, $this->taxonomy ); + } + + /** + * Tear down. + */ + public function tearDown(): void { + wp_delete_post( $this->post_id, true ); + wp_delete_term( $this->term_id, $this->taxonomy ); + unregister_post_type( $this->post_type ); + unregister_taxonomy( $this->taxonomy ); + + parent::tearDown(); + } + + /** + * Test that the term count is updated when a post is published. + * + * @ticket 42522 + */ + public function test_update_term_count_on_publish() { + $this->assertTermCount( 1, $this->term_id ); + + // Change post status to draft. + wp_update_post( + array( + 'ID' => $this->post_id, + 'post_status' => 'draft', + ) + ); + + $this->assertTermCount( 0, $this->term_id ); + + // Change post status back to publish. + wp_update_post( + array( + 'ID' => $this->post_id, + 'post_status' => 'publish', + ) + ); + + $this->assertTermCount( 1, $this->term_id ); + } + + /** + * Test that the term count is updated when a post is moved to trash. + * + * @ticket 42522 + */ + public function test_update_term_count_on_trash() { + $this->assertTermCount( 1, $this->term_id ); + + // Move post to trash. + wp_trash_post( $this->post_id ); + + $this->assertTermCount( 0, $this->term_id ); + } + + /** + * Test that the term count is updated when a post is restored from trash. + * + * @ticket 42522 + */ + public function test_update_term_count_on_restore() { + $this->assertTermCount( 1, $this->term_id ); + + // Move post to trash. + wp_trash_post( $this->post_id ); + + $this->assertTermCount( 0, $this->term_id, 'Post is in trash.' ); + + // Restore post from trash. + wp_untrash_post( $this->post_id ); + + $this->assertTermCount( 0, $this->term_id, 'Post is in draft after untrashing.' ); + + // re-publish post. + wp_publish_post( $this->post_id ); + + $this->assertTermCount( 1, $this->term_id, 'Post is in publish after publishing.' ); + } + + /** + * Test that the term count is updated when a post is deleted permanently. + * + * @ticket 42522 + */ + public function test_update_term_count_on_delete() { + $this->assertTermCount( 1, $this->term_id ); + + // Delete post permanently. + wp_delete_post( $this->post_id, true ); + + $this->assertTermCount( 0, $this->term_id ); + } + + /** + * Test that the term count is updated when a post is removed from a term. + * + * @ticket 42522 + */ + public function test_update_term_count_on_remove_term() { + $this->assertTermCount( 1, $this->term_id ); + + // Remove post from term. + wp_set_object_terms( $this->post_id, array(), $this->taxonomy ); + + $this->assertTermCount( 0, $this->term_id ); + } + + /** + * Test that the term count is updated when a post is added to a term. + * + * @ticket 42522 + */ + public function test_update_term_count_on_add_term() { + $this->assertTermCount( 1, $this->term_id ); + + // Add post to another term. + $term_id2 = self::factory()->term->create( + array( + 'taxonomy' => $this->taxonomy, + 'name' => 'Test Category 2', + ) + ); + + wp_set_object_terms( $this->post_id, array( $this->term_id, $term_id2 ), $this->taxonomy ); + + $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, $term_id2 ); + } + + /** + * Test that the term count is updated when a post is added to a term. + * + * @ticket 42522 + */ + public function test_update_term_count_on_add_new_post_with_term() { + $this->assertTermCount( 1, $this->term_id ); + + $post_id = self::factory()->post->create( + array( + 'post_type' => $this->post_type, + 'post_status' => 'publish', + ) + ); + + wp_set_object_terms( $post_id, $this->term_id, $this->taxonomy ); + + $this->assertTermCount( 2, $this->term_id ); + } + + /** + * Assert that the term count is correct. + * + * @param int $expected_count Expected term count. + * @param int $term_id Term ID. + */ + protected function assertTermCount( $expected_count, $term_id, $message = '' ) { + $term = get_term( $term_id ); + $this->assertSame( $expected_count, $term->count, $message ); + } +} From 11a3b5512535047611b397a89593a77c49962d24 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 17 Jun 2024 14:27:07 -0400 Subject: [PATCH 3/8] Update tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- .../post/UpdateTermCountOnTransitionPostStatus.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php index 24f9b86211aaa..418bc8471f5e7 100644 --- a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php +++ b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php @@ -55,18 +55,6 @@ public function setUp(): void { wp_set_object_terms( $this->post_id, $this->term_id, $this->taxonomy ); } - /** - * Tear down. - */ - public function tearDown(): void { - wp_delete_post( $this->post_id, true ); - wp_delete_term( $this->term_id, $this->taxonomy ); - unregister_post_type( $this->post_type ); - unregister_taxonomy( $this->taxonomy ); - - parent::tearDown(); - } - /** * Test that the term count is updated when a post is published. * From 74426358e4a2e81d5cd0b455bed3373a00877967 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 17 Jun 2024 14:27:16 -0400 Subject: [PATCH 4/8] Update tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- .../tests/post/UpdateTermCountOnTransitionPostStatus.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php index 418bc8471f5e7..07a06aef8c429 100644 --- a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php +++ b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php @@ -32,11 +32,12 @@ class Tests_Taxonomy_UpdateTermCountOnTransitionPostStatus extends WP_UnitTestCa /** * Set up. */ - public function setUp(): void { - parent::setUp(); + public function set_up(): void { + parent::set_up(); register_post_type( $this->post_type, array( 'public' => true ) ); register_taxonomy( $this->taxonomy, $this->post_type, array( 'public' => true ) ); + } $this->post_id = self::factory()->post->create( array( From a9b6c24fe11d5735ee1cdf8354cec2d655bece02 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 17 Jun 2024 14:27:33 -0400 Subject: [PATCH 5/8] Update tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- .../post/UpdateTermCountOnTransitionPostStatus.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php index 07a06aef8c429..cbdbb770196fd 100644 --- a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php +++ b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php @@ -39,21 +39,25 @@ public function set_up(): void { register_taxonomy( $this->taxonomy, $this->post_type, array( 'public' => true ) ); } - $this->post_id = self::factory()->post->create( + /** + * Create shared fixtures. + */ + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { + self::$post_id = $factory->post->create( array( - 'post_type' => $this->post_type, + 'post_type' => self::$post_type, 'post_status' => 'publish', ) ); - $this->term_id = self::factory()->term->create( + self::$term_id = $factory->term->create( array( - 'taxonomy' => $this->taxonomy, + 'taxonomy' => self::$taxonomy, 'name' => 'Test Category', ) ); - wp_set_object_terms( $this->post_id, $this->term_id, $this->taxonomy ); + wp_set_object_terms( self::$post_id, self::$term_id, self::$taxonomy ); } /** From f0219a92f31d07394e88422b03ecb82594653414 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 17 Jun 2024 14:27:48 -0400 Subject: [PATCH 6/8] Update tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- .../tests/post/UpdateTermCountOnTransitionPostStatus.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php index cbdbb770196fd..f09cbf53b447e 100644 --- a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php +++ b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php @@ -12,12 +12,12 @@ class Tests_Taxonomy_UpdateTermCountOnTransitionPostStatus extends WP_UnitTestCa /** * @var int Post ID. */ - protected $post_id; + protected static $post_id; /** * @var int Term ID. */ - protected $term_id; + protected static $term_id; /** * @var string Post type. From aeae28a5ec2ad1e909169dce0c43192f74f739f5 Mon Sep 17 00:00:00 2001 From: paul bearne Date: Tue, 18 Jun 2024 14:22:38 -0400 Subject: [PATCH 7/8] Refactor test variables to static in post status transitions The commit substitutes instance variables for static ones in tests related to post status transitions. This refactor improves consistency in referencing these values and optimizes performance by invoking these values directly instead of through instance references. --- .../UpdateTermCountOnTransitionPostStatus.php | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php index f09cbf53b447e..5bf25f62007ef 100644 --- a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php +++ b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php @@ -22,12 +22,12 @@ class Tests_Taxonomy_UpdateTermCountOnTransitionPostStatus extends WP_UnitTestCa /** * @var string Post type. */ - protected $post_type = 'post'; + protected static $post_type = 'post'; /** * @var string Taxonomy name. */ - protected $taxonomy = 'category'; + protected static $taxonomy = 'category'; /** * Set up. @@ -35,8 +35,8 @@ class Tests_Taxonomy_UpdateTermCountOnTransitionPostStatus extends WP_UnitTestCa public function set_up(): void { parent::set_up(); - register_post_type( $this->post_type, array( 'public' => true ) ); - register_taxonomy( $this->taxonomy, $this->post_type, array( 'public' => true ) ); + register_post_type( self::$post_type, array( 'public' => true ) ); + register_taxonomy( self::$taxonomy, self::$post_type, array( 'public' => true ) ); } /** @@ -66,27 +66,27 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { * @ticket 42522 */ public function test_update_term_count_on_publish() { - $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, self::$term_id ); // Change post status to draft. wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_status' => 'draft', ) ); - $this->assertTermCount( 0, $this->term_id ); + $this->assertTermCount( 0, self::$term_id ); // Change post status back to publish. wp_update_post( array( - 'ID' => $this->post_id, + 'ID' => self::$post_id, 'post_status' => 'publish', ) ); - $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, self::$term_id ); } /** @@ -95,12 +95,12 @@ public function test_update_term_count_on_publish() { * @ticket 42522 */ public function test_update_term_count_on_trash() { - $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, self::$term_id ); // Move post to trash. - wp_trash_post( $this->post_id ); + wp_trash_post( self::$post_id ); - $this->assertTermCount( 0, $this->term_id ); + $this->assertTermCount( 0, self::$term_id ); } /** @@ -109,22 +109,22 @@ public function test_update_term_count_on_trash() { * @ticket 42522 */ public function test_update_term_count_on_restore() { - $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, self::$term_id ); // Move post to trash. - wp_trash_post( $this->post_id ); + wp_trash_post( self::$post_id ); - $this->assertTermCount( 0, $this->term_id, 'Post is in trash.' ); + $this->assertTermCount( 0, self::$term_id, 'Post is in trash.' ); // Restore post from trash. - wp_untrash_post( $this->post_id ); + wp_untrash_post( self::$post_id ); - $this->assertTermCount( 0, $this->term_id, 'Post is in draft after untrashing.' ); + $this->assertTermCount( 0, self::$term_id, 'Post is in draft after untrashing.' ); // re-publish post. - wp_publish_post( $this->post_id ); + wp_publish_post( self::$post_id ); - $this->assertTermCount( 1, $this->term_id, 'Post is in publish after publishing.' ); + $this->assertTermCount( 1, self::$term_id, 'Post is in publish after publishing.' ); } /** @@ -133,12 +133,12 @@ public function test_update_term_count_on_restore() { * @ticket 42522 */ public function test_update_term_count_on_delete() { - $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, self::$term_id ); // Delete post permanently. - wp_delete_post( $this->post_id, true ); + wp_delete_post( self::$post_id, true ); - $this->assertTermCount( 0, $this->term_id ); + $this->assertTermCount( 0, self::$term_id ); } /** @@ -147,12 +147,12 @@ public function test_update_term_count_on_delete() { * @ticket 42522 */ public function test_update_term_count_on_remove_term() { - $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, self::$term_id ); // Remove post from term. - wp_set_object_terms( $this->post_id, array(), $this->taxonomy ); + wp_set_object_terms( self::$post_id, array(), self::$taxonomy ); - $this->assertTermCount( 0, $this->term_id ); + $this->assertTermCount( 0, self::$term_id ); } /** @@ -161,19 +161,19 @@ public function test_update_term_count_on_remove_term() { * @ticket 42522 */ public function test_update_term_count_on_add_term() { - $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, self::$term_id ); // Add post to another term. $term_id2 = self::factory()->term->create( array( - 'taxonomy' => $this->taxonomy, + 'taxonomy' => self::$taxonomy, 'name' => 'Test Category 2', ) ); - wp_set_object_terms( $this->post_id, array( $this->term_id, $term_id2 ), $this->taxonomy ); + wp_set_object_terms( self::$post_id, array( self::$term_id, $term_id2 ), self::$taxonomy ); - $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, self::$term_id ); $this->assertTermCount( 1, $term_id2 ); } @@ -183,18 +183,18 @@ public function test_update_term_count_on_add_term() { * @ticket 42522 */ public function test_update_term_count_on_add_new_post_with_term() { - $this->assertTermCount( 1, $this->term_id ); + $this->assertTermCount( 1, self::$term_id ); $post_id = self::factory()->post->create( array( - 'post_type' => $this->post_type, + 'post_type' => self::$post_type, 'post_status' => 'publish', ) ); - wp_set_object_terms( $post_id, $this->term_id, $this->taxonomy ); + wp_set_object_terms( $post_id, self::$term_id, self::$taxonomy ); - $this->assertTermCount( 2, $this->term_id ); + $this->assertTermCount( 2, self::$term_id ); } /** From d197d20d9963ba6abb02d2962bd93c1930c534bf Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 20 Jun 2024 09:25:42 -0400 Subject: [PATCH 8/8] Update tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- .../tests/post/UpdateTermCountOnTransitionPostStatus.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php index 5bf25f62007ef..f61532c87c99a 100644 --- a/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php +++ b/tests/phpunit/tests/post/UpdateTermCountOnTransitionPostStatus.php @@ -32,7 +32,7 @@ class Tests_Taxonomy_UpdateTermCountOnTransitionPostStatus extends WP_UnitTestCa /** * Set up. */ - public function set_up(): void { + public function set_up() { parent::set_up(); register_post_type( self::$post_type, array( 'public' => true ) );