Taxonomy: Revert Light-weight/partial term counts.
Partial revert of [49141], [49171], [49316]. All functional changes are removed, appropriate term counting unit tests are retained. See #40351. git-svn-id: https://develop.svn.wordpress.org/trunk@49451 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
bba3c98cec
commit
088fd3cd39
@ -180,14 +180,6 @@ final class WP_Taxonomy {
|
||||
*/
|
||||
public $update_count_callback;
|
||||
|
||||
/**
|
||||
* Function that will be called when the count is modified by an amount.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @var callable
|
||||
*/
|
||||
public $update_count_by_callback;
|
||||
|
||||
/**
|
||||
* Whether this taxonomy should appear in the REST API.
|
||||
*
|
||||
@ -302,7 +294,6 @@ final class WP_Taxonomy {
|
||||
'rewrite' => true,
|
||||
'query_var' => $this->name,
|
||||
'update_count_callback' => '',
|
||||
'update_count_by_callback' => '',
|
||||
'show_in_rest' => false,
|
||||
'rest_base' => false,
|
||||
'rest_controller_class' => false,
|
||||
@ -420,17 +411,6 @@ final class WP_Taxonomy {
|
||||
);
|
||||
}
|
||||
|
||||
// If generic update callback is defined but increment/decrement callback is not.
|
||||
if (
|
||||
! empty( $args['update_count_callback'] ) &&
|
||||
is_callable( $args['update_count_callback'] ) &&
|
||||
empty( $args['update_count_by_callback'] )
|
||||
) {
|
||||
$args['update_count_by_callback'] = function( $tt_ids, $taxonomy ) use ( $args ) {
|
||||
return call_user_func( $args['update_count_callback'], $tt_ids, $taxonomy );
|
||||
};
|
||||
}
|
||||
|
||||
foreach ( $args as $property_name => $property_value ) {
|
||||
$this->$property_name = $property_value;
|
||||
}
|
||||
|
@ -4074,8 +4074,6 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
|
||||
clean_post_cache( $post_ID );
|
||||
}
|
||||
|
||||
// Allow term counts to be handled by transitioning post type.
|
||||
_wp_prevent_term_counting( true );
|
||||
if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
|
||||
wp_set_post_categories( $post_ID, $post_category );
|
||||
}
|
||||
@ -4132,8 +4130,6 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )
|
||||
}
|
||||
}
|
||||
}
|
||||
// Restore term counting.
|
||||
_wp_prevent_term_counting( false );
|
||||
|
||||
if ( ! empty( $postarr['meta_input'] ) ) {
|
||||
foreach ( $postarr['meta_input'] as $field => $value ) {
|
||||
@ -4450,9 +4446,7 @@ function wp_publish_post( $post ) {
|
||||
if ( ! $default_term_id ) {
|
||||
continue;
|
||||
}
|
||||
_wp_prevent_term_counting( true );
|
||||
wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy );
|
||||
_wp_prevent_term_counting( false );
|
||||
}
|
||||
|
||||
$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
|
||||
@ -7360,91 +7354,11 @@ 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 ) {
|
||||
if ( 'inherit' === $new_status ) {
|
||||
$new_status = get_post_status( $post->post_parent );
|
||||
}
|
||||
|
||||
if ( 'inherit' === $old_status ) {
|
||||
$old_status = get_post_status( $post->post_parent );
|
||||
}
|
||||
|
||||
$count_new = 'publish' === $new_status;
|
||||
$count_old = 'publish' === $old_status;
|
||||
|
||||
if ( $count_new === $count_old ) {
|
||||
// Nothing to do.
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Update counts for the post's terms.
|
||||
*
|
||||
* Term counting is deferred while incrementing/decrementing the counts to
|
||||
* reduce the number of database queries required. Once the counts are
|
||||
* complete the updates are performed if term counting wasn't previously
|
||||
* deferred.
|
||||
*/
|
||||
$previous_deferred_setting = wp_defer_term_counting();
|
||||
wp_defer_term_counting( true );
|
||||
// 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' ) );
|
||||
|
||||
if ( empty( $tt_ids ) ) {
|
||||
// No terms for this taxonomy on object.
|
||||
continue;
|
||||
wp_update_term_count( $tt_ids, $taxonomy );
|
||||
}
|
||||
|
||||
$object_types = (array) get_taxonomy( $taxonomy )->object_type;
|
||||
|
||||
foreach ( $object_types as &$object_type ) {
|
||||
list( $object_type ) = explode( ':', $object_type );
|
||||
}
|
||||
|
||||
$object_types = array_unique( $object_types );
|
||||
|
||||
if ( ! in_array( $post->post_type, $object_types, true ) ) {
|
||||
$modify_by = 0;
|
||||
} elseif ( $count_new && ! $count_old ) {
|
||||
$modify_by = 1;
|
||||
} elseif ( $count_old && ! $count_new ) {
|
||||
$modify_by = -1;
|
||||
}
|
||||
|
||||
if ( 'attachment' === $post->post_type ) {
|
||||
wp_modify_term_count_by( $tt_ids, $taxonomy, $modify_by );
|
||||
continue;
|
||||
}
|
||||
|
||||
$check_attachments = array_search( 'attachment', $object_types, true );
|
||||
if ( false !== $check_attachments ) {
|
||||
unset( $object_types[ $check_attachments ] );
|
||||
$check_attachments = true;
|
||||
}
|
||||
|
||||
wp_modify_term_count_by( $tt_ids, $taxonomy, $modify_by );
|
||||
if ( ! $check_attachments ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* For non-attachments, check if there are any attachment children
|
||||
* with 'inherited' post status -- if so those will need to be counted.
|
||||
*/
|
||||
$attachments = get_children(
|
||||
array(
|
||||
'post_parent' => $post->ID,
|
||||
'post_status' => 'inherit',
|
||||
'post_type' => 'attachment',
|
||||
'update_post_meta_cache' => false,
|
||||
'update_post_term_cache' => true,
|
||||
)
|
||||
);
|
||||
|
||||
foreach ( $attachments as $attachment ) {
|
||||
_update_term_count_on_transition_post_status( $new_status, $old_status, $attachment );
|
||||
}
|
||||
}
|
||||
wp_defer_term_counting( $previous_deferred_setting );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -336,7 +336,6 @@ function is_taxonomy_hierarchical( $taxonomy ) {
|
||||
* @since 5.1.0 Introduced `meta_box_sanitize_cb` argument.
|
||||
* @since 5.4.0 Added the registered taxonomy object as a return value.
|
||||
* @since 5.5.0 Introduced `default_term` argument.
|
||||
* @since 5.6.0 Introduced `update_count_by_callback` argument.
|
||||
*
|
||||
* @global array $wp_taxonomies Registered taxonomies.
|
||||
*
|
||||
@ -408,9 +407,6 @@ function is_taxonomy_hierarchical( $taxonomy ) {
|
||||
* to post types, which confirms that the objects are published before
|
||||
* counting them. Default _update_generic_term_count() for taxonomies
|
||||
* attached to other object types, such as users.
|
||||
* @type callable $update_count_by_callback Works much like a hook, in that it will be called when the count is
|
||||
* incremented or decremented. Defaults to the value of `$update_count_callback` if
|
||||
* a custom callack is defined, otherwise uses wp_modify_term_count_by().
|
||||
* @type string|array $default_term {
|
||||
* Default term to be used for the taxonomy.
|
||||
*
|
||||
@ -2565,25 +2561,6 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
|
||||
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
|
||||
}
|
||||
|
||||
$taxonomy_object = get_taxonomy( $taxonomy );
|
||||
|
||||
$object_types = (array) $taxonomy_object->object_type;
|
||||
foreach ( $object_types as &$object_type ) {
|
||||
if ( 0 === strpos( $object_type, 'attachment:' ) ) {
|
||||
list( $object_type ) = explode( ':', $object_type );
|
||||
}
|
||||
}
|
||||
|
||||
if ( array_filter( $object_types, 'post_type_exists' ) !== $object_types ) {
|
||||
// This taxonomy applies to non-posts, count changes now.
|
||||
$do_recount = ! _wp_prevent_term_counting();
|
||||
} elseif ( 'publish' === get_post_status( $object_id ) ) {
|
||||
// Published post, count changes now.
|
||||
$do_recount = ! _wp_prevent_term_counting();
|
||||
} else {
|
||||
$do_recount = false;
|
||||
}
|
||||
|
||||
if ( ! is_array( $terms ) ) {
|
||||
$terms = array( $terms );
|
||||
}
|
||||
@ -2669,8 +2646,8 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
|
||||
$new_tt_ids[] = $tt_id;
|
||||
}
|
||||
|
||||
if ( $new_tt_ids && $do_recount ) {
|
||||
wp_increment_term_count( $new_tt_ids, $taxonomy );
|
||||
if ( $new_tt_ids ) {
|
||||
wp_update_term_count( $new_tt_ids, $taxonomy );
|
||||
}
|
||||
|
||||
if ( ! $append ) {
|
||||
@ -2688,7 +2665,9 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $append && isset( $taxonomy_object->sort ) && $taxonomy_object->sort ) {
|
||||
$t = get_taxonomy( $taxonomy );
|
||||
|
||||
if ( ! $append && isset( $t->sort ) && $t->sort ) {
|
||||
$values = array();
|
||||
$term_order = 0;
|
||||
|
||||
@ -2769,31 +2748,6 @@ function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
|
||||
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
|
||||
}
|
||||
|
||||
$taxonomy_object = get_taxonomy( $taxonomy );
|
||||
|
||||
$object_types = (array) $taxonomy_object->object_type;
|
||||
foreach ( $object_types as &$object_type ) {
|
||||
if ( 0 === strpos( $object_type, 'attachment:' ) ) {
|
||||
list( $object_type ) = explode( ':', $object_type );
|
||||
}
|
||||
}
|
||||
|
||||
if ( array_filter( $object_types, 'post_type_exists' ) !== $object_types ) {
|
||||
// This taxonomy applies to non-posts, count changes now.
|
||||
$do_recount = ! _wp_prevent_term_counting();
|
||||
} elseif (
|
||||
'publish' === get_post_status( $object_id ) ||
|
||||
(
|
||||
'inherit' === get_post_status( $object_id ) &&
|
||||
'publish' === get_post_status( wp_get_post_parent_id( $object_id ) )
|
||||
)
|
||||
) {
|
||||
// Published post, count changes now.
|
||||
$do_recount = ! _wp_prevent_term_counting();
|
||||
} else {
|
||||
$do_recount = false;
|
||||
}
|
||||
|
||||
if ( ! is_array( $terms ) ) {
|
||||
$terms = array( $terms );
|
||||
}
|
||||
@ -2852,9 +2806,7 @@ function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
|
||||
*/
|
||||
do_action( 'deleted_term_relationships', $object_id, $tt_ids, $taxonomy );
|
||||
|
||||
if ( $do_recount ) {
|
||||
wp_decrement_term_count( $tt_ids, $taxonomy );
|
||||
}
|
||||
wp_update_term_count( $tt_ids, $taxonomy );
|
||||
|
||||
return (bool) $deleted;
|
||||
}
|
||||
@ -3274,7 +3226,6 @@ function wp_defer_term_counting( $defer = null ) {
|
||||
$_defer = $defer;
|
||||
// Flush any deferred counts.
|
||||
if ( ! $defer ) {
|
||||
wp_modify_term_count_by( null, null, null, true );
|
||||
wp_update_term_count( null, null, true );
|
||||
}
|
||||
}
|
||||
@ -3282,193 +3233,6 @@ function wp_defer_term_counting( $defer = null ) {
|
||||
return $_defer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents add/removing a term from modifying a term count.
|
||||
*
|
||||
* This is used by functions calling wp_transition_post_status() to indicate the
|
||||
* term count will be handled during the post's transition.
|
||||
*
|
||||
* @private
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param bool $new_setting The new setting for preventing term counts.
|
||||
* @return bool Whether term count prevention is enabled or disabled.
|
||||
*/
|
||||
function _wp_prevent_term_counting( $new_setting = null ) {
|
||||
static $prevent = false;
|
||||
|
||||
if ( is_bool( $new_setting ) ) {
|
||||
$prevent = $new_setting;
|
||||
}
|
||||
|
||||
return $prevent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the amount of terms in taxonomy.
|
||||
*
|
||||
* If there is a taxonomy callback applied, then it will be called for updating
|
||||
* the count.
|
||||
*
|
||||
* The default action is to increment the count by one and update the database.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param int|array $tt_ids The term_taxonomy_id of the terms.
|
||||
* @param string $taxonomy The context of the term.
|
||||
* @param int $increment_by By how many the term count is to be incremented. Default 1.
|
||||
* @param bool $do_deferred Whether to flush the deferred term counts too. Default false.
|
||||
* @return bool If no terms will return false, and if successful will return true.
|
||||
*/
|
||||
function wp_increment_term_count( $tt_ids, $taxonomy, $increment_by = 1, $do_deferred = false ) {
|
||||
return wp_modify_term_count_by( $tt_ids, $taxonomy, $increment_by, $do_deferred );
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the amount of terms in taxonomy.
|
||||
*
|
||||
* If there is a taxonomy callback applied, then it will be called for updating
|
||||
* the count.
|
||||
*
|
||||
* The default action is to decrement the count by one and update the database.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param int|array $tt_ids The term_taxonomy_id of the terms.
|
||||
* @param string $taxonomy The context of the term.
|
||||
* @param int $decrement_by By how many the term count is to be decremented. Default 1.
|
||||
* @param bool $do_deferred Whether to flush the deferred term counts too. Default false.
|
||||
* @return bool If no terms will return false, and if successful will return true.
|
||||
*/
|
||||
function wp_decrement_term_count( $tt_ids, $taxonomy, $decrement_by = 1, $do_deferred = false ) {
|
||||
return wp_modify_term_count_by( $tt_ids, $taxonomy, $decrement_by * -1, $do_deferred );
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the amount of terms in taxonomy.
|
||||
*
|
||||
* If there is a taxonomy callback applied, then it will be called for updating
|
||||
* the count.
|
||||
*
|
||||
* The default action is to decrement the count by one and update the database.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @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.
|
||||
* @param bool $do_deferred Whether to flush the deferred term counts too. Default false.
|
||||
* @return bool If no terms will return false, and if successful will return true.
|
||||
*/
|
||||
function wp_modify_term_count_by( $tt_ids, $taxonomy, $modify_by, $do_deferred = false ) {
|
||||
static $_deferred = array();
|
||||
|
||||
if ( $do_deferred ) {
|
||||
foreach ( (array) $_deferred as $taxonomy_name => $modifications ) {
|
||||
$tax_by_count = array_reduce(
|
||||
array_keys( $modifications ),
|
||||
function( $by_count, $tt_id ) use ( $modifications ) {
|
||||
if ( ! isset( $by_count[ $modifications[ $tt_id ] ] ) ) {
|
||||
$by_count[ $modifications[ $tt_id ] ] = array();
|
||||
}
|
||||
$by_count[ $modifications[ $tt_id ] ][] = $tt_id;
|
||||
return $by_count;
|
||||
},
|
||||
array()
|
||||
);
|
||||
|
||||
foreach ( $tax_by_count as $_modify_by => $_tt_ids ) {
|
||||
wp_modify_term_count_by_now( $_tt_ids, $taxonomy_name, $_modify_by );
|
||||
}
|
||||
unset( $_deferred[ $taxonomy_name ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $tt_ids ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! is_array( $tt_ids ) ) {
|
||||
$tt_ids = array( $tt_ids );
|
||||
}
|
||||
|
||||
if ( wp_defer_term_counting() ) {
|
||||
foreach ( $tt_ids as $tt_id ) {
|
||||
if ( ! isset( $_deferred[ $taxonomy ][ $tt_id ] ) ) {
|
||||
$_deferred[ $taxonomy ][ $tt_id ] = 0;
|
||||
}
|
||||
$_deferred[ $taxonomy ][ $tt_id ] += $modify_by;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return wp_modify_term_count_by_now( $tt_ids, $taxonomy, $modify_by );
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies the amount of terms in taxonomy immediately
|
||||
*
|
||||
* If there is a taxonomy callback applied, then it will be called for updating
|
||||
* the count.
|
||||
*
|
||||
* The default action is to decrement the count by one and update the database.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @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.
|
||||
* @return bool If no terms will return false, and if successful will return true.
|
||||
*/
|
||||
function wp_modify_term_count_by_now( $tt_ids, $taxonomy, $modify_by ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( 0 === $modify_by ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$tt_ids = array_filter( array_map( 'intval', (array) $tt_ids ) );
|
||||
|
||||
if ( empty( $tt_ids ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$taxonomy = get_taxonomy( $taxonomy );
|
||||
if ( ! empty( $taxonomy->update_count_by_callback ) ) {
|
||||
call_user_func( $taxonomy->update_count_by_callback, $tt_ids, $taxonomy, $modify_by );
|
||||
clean_term_cache( $tt_ids, '', false );
|
||||
return true;
|
||||
}
|
||||
|
||||
$tt_ids_string = '(' . implode( ',', $tt_ids ) . ')';
|
||||
|
||||
foreach ( $tt_ids as $tt_id ) {
|
||||
/** This action is documented in wp-includes/taxonomy.php */
|
||||
do_action( 'edit_term_taxonomy', $tt_id, $taxonomy->name );
|
||||
}
|
||||
|
||||
$result = $wpdb->query(
|
||||
$wpdb->prepare(
|
||||
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
||||
"UPDATE {$wpdb->term_taxonomy} AS tt SET tt.count = GREATEST( 0, tt.count + %d ) WHERE tt.term_taxonomy_id IN $tt_ids_string",
|
||||
$modify_by
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! $result ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( $tt_ids as $tt_id ) {
|
||||
/** This action is documented in wp-includes/taxonomy.php */
|
||||
do_action( 'edited_term_taxonomy', $tt_id, $taxonomy->name );
|
||||
}
|
||||
|
||||
clean_term_cache( $tt_ids, '', false );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the amount of terms in taxonomy.
|
||||
*
|
||||
|
@ -4,21 +4,6 @@
|
||||
* @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' ) );
|
||||
}
|
||||
@ -1067,101 +1052,4 @@ class Tests_Taxonomy extends WP_UnitTestCase {
|
||||
$this->assertContains( $tax1, $taxonomies );
|
||||
$this->assertContains( $tax2, $taxonomies );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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++;
|
||||
}
|
||||
}
|
||||
|
@ -79,11 +79,10 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Term counts are not double incremented when post created.
|
||||
* Term counts are incremented when post created.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @dataProvider data_term_count_changes_for_post_statuses
|
||||
* @ticket 40351
|
||||
*
|
||||
* @param string $post_status New post status.
|
||||
* @param int $change Expected change.
|
||||
@ -122,9 +121,8 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
* Term counts increments correctly when post status becomes published.
|
||||
*
|
||||
* @covers ::wp_publish_post
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @dataProvider data_term_counts_incremented_on_publish
|
||||
* @ticket 40351
|
||||
* @ticket 51292
|
||||
*
|
||||
* @param string $original_post_status Post status prior to change to publish.
|
||||
@ -164,9 +162,8 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
/**
|
||||
* Test post status transition update term counts correctly.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @dataProvider data_term_count_transitions_update_term_counts
|
||||
* @ticket 40351
|
||||
*
|
||||
* @param string $original_post_status Post status upon create.
|
||||
* @param string $new_post_status Post status after update.
|
||||
@ -228,11 +225,10 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Term counts are not double incremented when post created.
|
||||
* Term counts incremented correctly for posts with attachment.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @dataProvider data_term_count_changes_for_post_statuses_with_attachments
|
||||
* @ticket 40351
|
||||
*
|
||||
* @param string $post_status New post status.
|
||||
* @param int $change Expected change.
|
||||
@ -277,12 +273,11 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Term counts increments correctly when post status becomes published.
|
||||
* Term counts increments correctly when post with attachment becomes published.
|
||||
*
|
||||
* @covers ::wp_publish_post
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @dataProvider data_term_counts_incremented_on_publish_with_attachments
|
||||
* @ticket 40351
|
||||
* @ticket 51292
|
||||
*
|
||||
* @param string $original_post_status Post status prior to change to publish.
|
||||
@ -329,11 +324,10 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test post status transition update term counts correctly.
|
||||
* Test post status transition update term counts correctly for posts with attachments.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @dataProvider data_term_count_transitions_update_term_counts_with_attachments
|
||||
* @ticket 40351
|
||||
*
|
||||
* @param string $original_post_status Post status upon create.
|
||||
* @param string $new_post_status Post status after update.
|
||||
@ -404,60 +398,11 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Term counts are not double incremented when post created.
|
||||
* Term counts increments correctly when post with attachment becomes published.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @dataProvider data_term_count_changes_for_post_statuses_with_untermed_attachments
|
||||
* @ticket 40351
|
||||
*
|
||||
* @param string $post_status New post status.
|
||||
* @param int $change Expected change.
|
||||
*/
|
||||
public function test_term_count_changes_for_post_statuses_with_untermed_attachments( $post_status, $change ) {
|
||||
$term_count = get_term( self::$attachment_term )->count;
|
||||
// Do not use shared fixture for this test as it relies on a new post.
|
||||
$post_id = $this->factory()->post->create( array( 'post_status' => $post_status ) );
|
||||
wp_add_object_terms( $post_id, self::$attachment_term, 'wp_test_tax_counts' );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
array(
|
||||
'file' => 'image.jpg',
|
||||
'post_parent' => $post_id,
|
||||
'post_status' => 'inherit',
|
||||
)
|
||||
);
|
||||
|
||||
$expected = $term_count + $change;
|
||||
$this->assertSame( $expected, get_term( self::$attachment_term )->count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_term_count_changes_for_post_statuses_with_untermed_attachments.
|
||||
*
|
||||
* @return array[] {
|
||||
* @type string $post_status New post status.
|
||||
* @type int $change Expected change.
|
||||
* }
|
||||
*/
|
||||
function data_term_count_changes_for_post_statuses_with_untermed_attachments() {
|
||||
return array(
|
||||
// 0. Published post
|
||||
array( 'publish', 1 ),
|
||||
// 1. Auto draft
|
||||
array( 'auto-draft', 0 ),
|
||||
// 2. Draft
|
||||
array( 'draft', 0 ),
|
||||
// 3. Private post
|
||||
array( 'private', 0 ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Term counts increments correctly when post status becomes published.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @covers ::wp_publish_post
|
||||
* @dataProvider data_term_counts_incremented_on_publish_with_untermed_attachments
|
||||
* @ticket 40351
|
||||
* @ticket 51292
|
||||
*
|
||||
* @param string $original_post_status Post status prior to change to publish.
|
||||
@ -503,11 +448,10 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test post status transition update term counts correctly.
|
||||
* Test post status transition update term counts correctly on post with attachment.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @dataProvider data_term_count_transitions_update_term_counts_with_untermed_attachments
|
||||
* @ticket 40351
|
||||
*
|
||||
* @param string $original_post_status Post status upon create.
|
||||
* @param string $new_post_status Post status after update.
|
||||
@ -579,7 +523,7 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
/**
|
||||
* User taxonomy term counts increments when added to an account.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @ticket 51292
|
||||
*/
|
||||
public function test_term_counts_user_adding_term() {
|
||||
@ -593,7 +537,7 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
/**
|
||||
* User taxonomy term counts decrement when term deleted from user.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @ticket 51292
|
||||
*/
|
||||
public function test_term_counts_user_removing_term() {
|
||||
@ -604,92 +548,4 @@ class Tests_Term_termCount extends WP_UnitTestCase {
|
||||
$expected = $term_count - 1;
|
||||
$this->assertSame( $expected, get_term( self::$user_term )->count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure DB queries for deferred counts are nullified for net zero gain.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_defer_term_counting
|
||||
* @ticket 51292
|
||||
*/
|
||||
public function test_counts_after_deferral_net_zero() {
|
||||
$post_one = self::$post_ids['publish'];
|
||||
$post_two = self::$post_ids['publish_two'];
|
||||
$terms = self::$tag_ids;
|
||||
|
||||
wp_set_object_terms( $post_one, $terms[0], 'post_tag', true );
|
||||
|
||||
// Net gain 0;
|
||||
wp_defer_term_counting( true );
|
||||
wp_remove_object_terms( $post_one, $terms[0], 'post_tag' );
|
||||
wp_set_object_terms( $post_two, $terms[0], 'post_tag', true );
|
||||
$num_queries = get_num_queries();
|
||||
wp_defer_term_counting( false );
|
||||
// Ensure number of queries unchanged.
|
||||
$this->assertSame( $num_queries, get_num_queries() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure full recounts follow modify by X recounts to avoid miscounts.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_update_term_count
|
||||
* @covers ::wp_defer_term_counting
|
||||
* @ticket 51292
|
||||
*/
|
||||
public function test_counts_after_deferral_full_before_partial() {
|
||||
$post_one = self::$post_ids['publish'];
|
||||
$terms = self::$tag_ids;
|
||||
$term_count = get_term( $terms[0] )->count;
|
||||
|
||||
// Net gain 1;
|
||||
wp_defer_term_counting( true );
|
||||
wp_set_object_terms( $post_one, $terms[0], 'post_tag', true );
|
||||
wp_update_term_count( get_term( $terms[0] )->term_taxonomy_id, 'post_tag' );
|
||||
wp_defer_term_counting( false );
|
||||
|
||||
// Ensure term count is correct.
|
||||
$expected = $term_count + 1;
|
||||
$this->assertSame( $expected, get_term( $terms[0] )->count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure DB queries for deferred counts are combined.
|
||||
*
|
||||
* @covers ::wp_modify_term_count_by
|
||||
* @covers ::wp_defer_term_counting
|
||||
* @ticket 51292
|
||||
*/
|
||||
public function test_counts_after_deferral_matching_changes() {
|
||||
$post_one = self::$post_ids['publish'];
|
||||
$post_two = self::$post_ids['publish_two'];
|
||||
$terms = self::$tag_ids;
|
||||
|
||||
wp_set_object_terms( $post_one, $terms[0], 'post_tag', true );
|
||||
|
||||
// Net gain 0:
|
||||
wp_defer_term_counting( true );
|
||||
wp_remove_object_terms( $post_one, $terms[0], 'post_tag' );
|
||||
wp_set_object_terms( $post_two, $terms[0], 'post_tag', true );
|
||||
|
||||
// Net gain 1:
|
||||
wp_set_object_terms( $post_one, $terms[1], 'post_tag', true );
|
||||
wp_set_object_terms( $post_two, $terms[2], 'post_tag', true );
|
||||
|
||||
// Net gain 2:
|
||||
wp_set_object_terms( $post_one, array( $terms[3], $terms[4] ), 'post_tag', true );
|
||||
wp_set_object_terms( $post_two, array( $terms[3], $terms[4] ), 'post_tag', true );
|
||||
|
||||
$num_queries = get_num_queries();
|
||||
wp_defer_term_counting( false );
|
||||
|
||||
/*
|
||||
* Each count is expected to produce two queries:
|
||||
* 1) The count update
|
||||
* 2) The SELECT in `clean_term_cache()`.
|
||||
*/
|
||||
$expected = $num_queries + ( 2 * 2 );
|
||||
// Ensure number of queries correct.
|
||||
$this->assertSame( $expected, get_num_queries() );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user