Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) use ( $args ) {
return call_user_func( $args['update_count_callback'], $tt_ids, $taxonomy );
};
}
Expand Down
112 changes: 112 additions & 0 deletions tests/phpunit/tests/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
Expand Down Expand Up @@ -1030,4 +1045,101 @@ 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;

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