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
This commit is contained in:
Peter Wilson
2020-10-16 02:41:15 +00:00
parent 901edbe510
commit 3965584a04
2 changed files with 113 additions and 1 deletions

View File

@@ -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 );
};
}

View File

@@ -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++;
}
}