From 3965584a044961b5959cb4a84c1ae97f5333d054 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 16 Oct 2020 02:41:15 +0000 Subject: [PATCH] Taxonomy: Fix warnings thrown by custom term count callbacks. Add a `use` to a closure to avoid an undefined variable throwing a warning. Adds unit tests to ensure the custom callbacks run as expected when defined. Follow up to [49141]. Props ocean90, dd32. Fixes #40351. git-svn-id: https://develop.svn.wordpress.org/trunk@49171 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-taxonomy.php | 2 +- tests/phpunit/tests/taxonomy.php | 112 ++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-taxonomy.php b/src/wp-includes/class-wp-taxonomy.php index 21f3d4aec9..daedb66f82 100644 --- a/src/wp-includes/class-wp-taxonomy.php +++ b/src/wp-includes/class-wp-taxonomy.php @@ -426,7 +426,7 @@ final class WP_Taxonomy { 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 ) use ( $args ) { return call_user_func( $args['update_count_callback'], $tt_ids, $taxonomy ); }; } diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index f2f023dbad..b697b17096 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,101 @@ class Tests_Taxonomy extends WP_UnitTestCase { 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; + + 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; + + 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, 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. + */ + 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, 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 $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++; + } }