From 263fb23dc099a1d41e0626dc8e4f5a8a4bb6b409 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Thu, 15 Oct 2020 11:10:47 +1100 Subject: [PATCH 1/5] Test to ensure custom callbacks work as expected. --- tests/phpunit/tests/taxonomy.php | 111 +++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index f2f023dbad734..1dcee06fffeec 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -4,6 +4,21 @@ * @group taxonomy */ class Tests_Taxonomy extends WP_UnitTestCase { + + /** + * Number of times full count callback has been called. + * + * @var int + */ + public $full_count_cb_called = 0; + + /** + * Number of times partial count callback has been called. + * + * @var int + */ + public $partial_count_cb_called = 0; + function test_get_post_taxonomies() { $this->assertSame( array( 'category', 'post_tag', 'post_format' ), get_object_taxonomies( 'post' ) ); } @@ -1030,4 +1045,100 @@ function test_default_term_for_custom_taxonomy() { unregister_taxonomy( $tax ); $this->assertSame( get_option( 'default_term_' . $tax ), false ); } + + /** + * Ensure custom callbacks are used when registered. + * + * @covers register_taxonomy + * @ticket 40351 + */ + function test_register_taxonomy_counting_callbacks() { + $post_id = self::factory()->post->create(); + + register_taxonomy( + 'wp_tax_40351_full_only', + 'post', + array( + 'update_count_callback' => array( $this, 'cb_register_taxonomy_full_count_callback' ), + ) + ); + $full_term = self::factory()->term->create_and_get( + array( + 'taxonomy' => 'wp_tax_40351_full_only', + ) + ); + $full_term_id = $full_term->term_id; + $full_term_ttid = $full_term->term_taxonomy_id; + + register_taxonomy( + 'wp_tax_40351_partial_only', + 'post', + array( + 'update_count_by_callback' => array( $this, 'cb_register_taxonomy_partial_count_callback' ), + ) + ); + $partial_term = self::factory()->term->create_and_get( + array( + 'taxonomy' => 'wp_tax_40351_partial_only', + ) + ); + $partial_term_id = $partial_term->term_id; + $partial_term_ttid = $partial_term->term_taxonomy_id; + + register_taxonomy( + 'wp_tax_40351_both', + 'post', + array( + 'update_count_callback' => array( $this, 'cb_register_taxonomy_full_count_callback' ), + 'update_count_by_callback' => array( $this, 'cb_register_taxonomy_partial_count_callback' ), + ) + ); + $both_term = self::factory()->term->create_and_get( + array( + 'taxonomy' => 'wp_tax_40351_both', + ) + ); + $both_term_id = $both_term->term_id; + $both_term_ttid = $both_term->term_taxonomy_id; + + wp_set_post_terms( $post_id, $full_term_id, 'wp_tax_40351_full_only' ); + $this->assertSame( 0, $this->partial_count_cb_called ); + $this->assertSame( 1, $this->full_count_cb_called ); + + wp_set_post_terms( $post_id, $partial_term_id, 'wp_tax_40351_partial_only' ); + $this->assertSame( 1, $this->partial_count_cb_called ); + $this->assertSame( 1, $this->full_count_cb_called ); + + wp_set_post_terms( $post_id, $both_term_id, 'wp_tax_40351_both' ); + $this->assertSame( 2, $this->partial_count_cb_called ); + $this->assertSame( 1, $this->full_count_cb_called ); + + // Force a full recount `$both_term` to ensure callback is called. + wp_update_term_count( $both_term_ttid, 'wp_tax_40351_both' ); + $this->assertSame( 2, $this->full_count_cb_called ); + } + + /** + * Custom full count callback for `test_register_taxonomy_counting_callbacks()`. + * + * For the purpose of this test no database modifications are required. + * + * @param int|array $tt_ids The term_taxonomy_id of the terms. + * @param string $taxonomy The context of the term. + */ + function cb_register_taxonomy_full_count_callback( $tt_ids, $taxonomy ) { + $this->full_count_cb_called++; + } + + /** + * Custom partial count callback for `test_register_taxonomy_counting_callbacks()`. + * + * For the purpose of this test no database modifications are required. + * + * @param int|array $tt_ids The term_taxonomy_id of the terms. + * @param string $taxonomy The context of the term. + */ + function cb_register_taxonomy_partial_count_callback( $tt_ids, $taxonomy, $modify_by ) { + $this->partial_count_cb_called++; + } } From 51408f954f2a8917d22ac6b6020e3359720f3ad7 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Thu, 15 Oct 2020 11:11:01 +1100 Subject: [PATCH 2/5] Use args to avoid errors. --- src/wp-includes/class-wp-taxonomy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-taxonomy.php b/src/wp-includes/class-wp-taxonomy.php index 21f3d4aec9128..c51f76dc8717e 100644 --- a/src/wp-includes/class-wp-taxonomy.php +++ b/src/wp-includes/class-wp-taxonomy.php @@ -426,7 +426,7 @@ public function set_props( $object_type, $args ) { is_callable( $args['update_count_callback'] ) && empty( $args['update_count_by_callback'] ) ) { - $args['update_count_by_callback'] = function( $tt_ids, $taxonomy, $modify_by ) { + $args['update_count_by_callback'] = function( $tt_ids, $taxonomy, $modify_by ) use ( $args ) { return call_user_func( $args['update_count_callback'], $tt_ids, $taxonomy ); }; } From 958f4a4dcece8a5dbdd78ff0980f9e2d33de773d Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Thu, 15 Oct 2020 11:14:31 +1100 Subject: [PATCH 3/5] Remove unused vars. --- tests/phpunit/tests/taxonomy.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index 1dcee06fffeec..c3064dbb54a72 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -1062,13 +1062,12 @@ function test_register_taxonomy_counting_callbacks() { 'update_count_callback' => array( $this, 'cb_register_taxonomy_full_count_callback' ), ) ); - $full_term = self::factory()->term->create_and_get( + $full_term = self::factory()->term->create_and_get( array( 'taxonomy' => 'wp_tax_40351_full_only', ) ); - $full_term_id = $full_term->term_id; - $full_term_ttid = $full_term->term_taxonomy_id; + $full_term_id = $full_term->term_id; register_taxonomy( 'wp_tax_40351_partial_only', @@ -1077,13 +1076,12 @@ function test_register_taxonomy_counting_callbacks() { 'update_count_by_callback' => array( $this, 'cb_register_taxonomy_partial_count_callback' ), ) ); - $partial_term = self::factory()->term->create_and_get( + $partial_term = self::factory()->term->create_and_get( array( 'taxonomy' => 'wp_tax_40351_partial_only', ) ); - $partial_term_id = $partial_term->term_id; - $partial_term_ttid = $partial_term->term_taxonomy_id; + $partial_term_id = $partial_term->term_id; register_taxonomy( 'wp_tax_40351_both', From 4356adb8db60afefd76948c952e1b494e3f7e994 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Thu, 15 Oct 2020 11:16:34 +1100 Subject: [PATCH 4/5] Docblocks. --- tests/phpunit/tests/taxonomy.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index c3064dbb54a72..b697b17096c6a 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -1119,7 +1119,8 @@ function test_register_taxonomy_counting_callbacks() { /** * Custom full count callback for `test_register_taxonomy_counting_callbacks()`. * - * For the purpose of this test no database modifications are required. + * For the purpose of this test no database modifications are required, therefore + * the parameters passed are unused. * * @param int|array $tt_ids The term_taxonomy_id of the terms. * @param string $taxonomy The context of the term. @@ -1131,10 +1132,12 @@ function cb_register_taxonomy_full_count_callback( $tt_ids, $taxonomy ) { /** * Custom partial count callback for `test_register_taxonomy_counting_callbacks()`. * - * For the purpose of this test no database modifications are required. + * For the purpose of this test no database modifications are required, therefore + * the parameters passed are unused. * - * @param int|array $tt_ids The term_taxonomy_id of the terms. - * @param string $taxonomy The context of the term. + * @param int|array $tt_ids The term_taxonomy_id of the terms. + * @param string $taxonomy The context of the term. + * @param int $modify_by By how many the term count is to be modified. */ function cb_register_taxonomy_partial_count_callback( $tt_ids, $taxonomy, $modify_by ) { $this->partial_count_cb_called++; From 97f966e538a119eee43f00419f9c0d44c7e5d54f Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Thu, 15 Oct 2020 13:22:26 +1100 Subject: [PATCH 5/5] Remove uneeded argument. --- src/wp-includes/class-wp-taxonomy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-taxonomy.php b/src/wp-includes/class-wp-taxonomy.php index c51f76dc8717e..daedb66f8231b 100644 --- a/src/wp-includes/class-wp-taxonomy.php +++ b/src/wp-includes/class-wp-taxonomy.php @@ -426,7 +426,7 @@ public function set_props( $object_type, $args ) { is_callable( $args['update_count_callback'] ) && empty( $args['update_count_by_callback'] ) ) { - $args['update_count_by_callback'] = function( $tt_ids, $taxonomy, $modify_by ) use ( $args ) { + $args['update_count_by_callback'] = function( $tt_ids, $taxonomy ) use ( $args ) { return call_user_func( $args['update_count_callback'], $tt_ids, $taxonomy ); }; }