Skip to:
Content

BuddyPress.org

Changeset 11074


Ignore:
Timestamp:
09/09/2016 03:54:32 PM (5 years ago)
Author:
boonebgorges
Message:

Groups: Bust incrementor cache when modifying group taxonomy terms.

Group taxonomy terms, such as those belonging to bp_group_type, can
affect the results of BP_Groups_Group::get() queries. As such,
changing a group taxonomy term must invalidate the query cache.

WP's taxonomy API doesn't provide a direct method to determine whether
the object whose terms are being modified is a BuddyPress group. So
we infer the object type by looking at the object types that are
registered for the specified taxonomy; if bp_group is one of them, we
invalidate. This could result in cases where the cache is being purged
unnecessarily, but better safe than sorry.

See #5451.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-cache.php

    r11072 r11074  
    275275add_action( 'deleted_group_meta',      'bp_groups_reset_cache_incrementor' );
    276276add_action( 'added_group_meta',        'bp_groups_reset_cache_incrementor' );
     277
     278/**
     279 * Reset cache incrementor for Groups component when a group's taxonomy terms change.
     280 *
     281 * We infer that a group is being affected by looking at the objects belonging
     282 * to the taxonomy being affected.
     283 *
     284 * @since 2.7.0
     285 *
     286 * @param int    $object_id ID of the item whose terms are being modified.
     287 * @param array  $terms     Array of object terms.
     288 * @param array  $tt_ids    Array of term taxonomy IDs.
     289 * @param string $taxonomy  Taxonomy slug.
     290 * @return bool True on success, false on failure.
     291 */
     292function bp_groups_reset_cache_incrementor_on_group_term_change( $object_id, $terms, $tt_ids, $taxonomy ) {
     293    $tax_object = get_taxonomy( $taxonomy );
     294    if ( $tax_object && in_array( 'bp_group', $tax_object->object_type ) ) {
     295        return bp_groups_reset_cache_incrementor();
     296    }
     297
     298    return false;
     299}
     300add_action( 'bp_set_object_terms', 'bp_groups_reset_cache_incrementor_on_group_term_change', 10, 4 );
     301
     302/**
     303 * Reset cache incrementor for Groups component when a group's taxonomy terms are removed.
     304 *
     305 * We infer that a group is being affected by looking at the objects belonging
     306 * to the taxonomy being affected.
     307 *
     308 * @since 2.7.0
     309 *
     310 * @param int    $object_id ID of the item whose terms are being modified.
     311 * @param array  $terms     Array of object terms.
     312 * @param string $taxonomy  Taxonomy slug.
     313 * @return bool True on success, false on failure.
     314 */
     315function bp_groups_reset_cache_incrementor_on_group_term_remove( $object_id, $terms, $taxonomy ) {
     316    $tax_object = get_taxonomy( $taxonomy );
     317    if ( $tax_object && in_array( 'bp_group', $tax_object->object_type ) ) {
     318        return bp_groups_reset_cache_incrementor();
     319    }
     320
     321    return false;
     322}
     323add_action( 'bp_remove_object_terms', 'bp_groups_reset_cache_incrementor_on_group_term_remove', 10, 3 );
    277324
    278325/* List actions to clear super cached pages on, if super cache is installed */
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r11072 r11074  
    567567    /**
    568568     * @group cache
     569     * @group group_types
     570     * @ticket BP5451
     571     * @ticket BP6643
     572     */
     573    public function test_get_query_caches_should_be_busted_by_group_term_change() {
     574        global $wpdb;
     575
     576        bp_groups_register_group_type( 'foo' );
     577        bp_groups_register_group_type( 'bar' );
     578
     579        $groups = $this->factory->group->create_many( 2 );
     580        bp_groups_set_group_type( $groups[0], 'foo' );
     581        bp_groups_set_group_type( $groups[1], 'bar' );
     582
     583        $found1 = BP_Groups_Group::get( array(
     584            'group_type' => 'foo',
     585        ) );
     586
     587        $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found1['groups'], 'id' ) );
     588
     589        bp_groups_set_group_type( $groups[1], 'foo' );
     590
     591        $found2 = BP_Groups_Group::get( array(
     592            'group_type' => 'foo',
     593        ) );
     594
     595        $this->assertEqualSets( array( $groups[0], $groups[1] ), wp_list_pluck( $found2['groups'], 'id' ) );
     596    }
     597
     598    /**
     599     * @group cache
     600     * @group group_types
     601     * @ticket BP5451
     602     * @ticket BP6643
     603     */
     604    public function test_get_query_caches_should_be_busted_by_group_term_removal() {
     605        global $wpdb;
     606
     607        bp_groups_register_group_type( 'foo' );
     608
     609        $groups = $this->factory->group->create_many( 2 );
     610        bp_groups_set_group_type( $groups[0], 'foo' );
     611        bp_groups_set_group_type( $groups[1], 'foo' );
     612
     613        $found1 = BP_Groups_Group::get( array(
     614            'group_type' => 'foo',
     615        ) );
     616
     617        $this->assertEqualSets( array( $groups[0], $groups[1] ), wp_list_pluck( $found1['groups'], 'id' ) );
     618
     619        bp_groups_remove_group_type( $groups[1], 'foo' );
     620
     621        $found2 = BP_Groups_Group::get( array(
     622            'group_type' => 'foo',
     623        ) );
     624
     625        $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found2['groups'], 'id' ) );
     626    }
     627
     628    /**
     629     * @group cache
    569630     * @ticket BP5451
    570631     * @ticket BP6643
Note: See TracChangeset for help on using the changeset viewer.