Skip to:
Content

BuddyPress.org

Changeset 13990


Ignore:
Timestamp:
07/27/2024 08:14:28 PM (8 months ago)
Author:
espellcaste
Message:

Core: Add cache_results flag to the BP_Invitation::get getter.

When performing a request with cache_results, it stops the invite information retrieved from being added to the cache.

See #8552
Closes https://github.com/buddypress/buddypress/pull/346/

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-invitation.php

    r13890 r13990  
    358358     * Assemble the WHERE clause of a get() SQL statement.
    359359     *
    360      * Used by BP_Invitation::get() to create its WHERE
    361      * clause.
    362      *
    363360     * @since 5.0.0
    364361     *
     
    439436
    440437        // Type.
    441         if ( ! empty( $args['type'] ) && 'all' !== $args['type'] ) {
    442             if ( 'invite' === $args['type'] || 'request' === $args['type'] ) {
    443                 $type_clean               = $wpdb->prepare( '%s', $args['type'] );
    444                 $where_conditions['type'] = "type = {$type_clean}";
    445             }
     438        if ( ! empty( $args['type'] ) && ( 'invite' === $args['type'] || 'request' === $args['type'] ) ) {
     439            $type_clean               = $wpdb->prepare( '%s', $args['type'] );
     440            $where_conditions['type'] = "type = {$type_clean}";
    446441        }
    447442
     
    689684     *
    690685     * @since 5.0.0
     686     * @since 15.0.0 Introduced the `cache_results` parameter.
    691687     *
    692688     * @global wpdb $wpdb WordPress database object.
     
    730726     *     @type string       $order_by          Field to order results by.
    731727     *     @type string       $sort_order        ASC or DESC.
     728     *     @type bool         $cache_results     Optional. Whether to cache invitation information. Default true.
    732729     *     @type int          $page              Number of the current page of results.
    733730     *                                           Default: false (no pagination,
     
    745742    public static function get( $args = array() ) {
    746743        global $wpdb;
     744
    747745        $invites_table_name = BP_Invitation_Manager::get_table_name();
    748746
     
    764762                'order_by'          => false,
    765763                'sort_order'        => false,
     764                'cache_results'     => true,
    766765                'page'              => false,
    767766                'per_page'          => false,
     
    772771
    773772        $sql = array(
    774             'select'     => 'SELECT',
    775             'fields'     => '',
    776             'from'       => "FROM {$invites_table_name} i",
    777             'where'      => '',
    778             'orderby'    => '',
    779             'pagination' => '',
     773            'select' => 'SELECT',
     774            'from'   => "FROM {$invites_table_name} i",
     775            'fields' => 'DISTINCT i.id',
    780776        );
    781777
     
    786782        } elseif ( 'inviter_ids' === $r['fields'] ) {
    787783            $sql['fields'] = 'DISTINCT i.inviter_id';
    788         } else {
    789             $sql['fields'] = 'DISTINCT i.id';
    790784        }
    791785
     
    830824         * @since 5.0.0
    831825         *
    832          * @param string $value Concatenated SQL statement.
    833          * @param array  $sql   Array of SQL parts before concatenation.
    834          * @param array  $r     Array of parsed arguments for the get method.
     826         * @param string $paged_invites_sql Concatenated SQL statement.
     827         * @param array  $sql               Array of SQL parts before concatenation.
     828         * @param array  $r                 Array of parsed arguments for the get method.
    835829         */
    836830        $paged_invites_sql = apply_filters( 'bp_invitations_get_paged_invitations_sql', $paged_invites_sql, $sql, $r );
    837831
    838         $cached = bp_core_get_incremented_cache( $paged_invites_sql, 'bp_invitations' );
    839         if ( false === $cached ) {
     832        if ( $r['cache_results'] ) {
     833            $cached = bp_core_get_incremented_cache( $paged_invites_sql, 'bp_invitations' );
     834            if ( false === $cached ) {
     835                $paged_invite_ids = $wpdb->get_col( $paged_invites_sql );
     836                bp_core_set_incremented_cache( $paged_invites_sql, 'bp_invitations', $paged_invite_ids );
     837            } else {
     838                $paged_invite_ids = $cached;
     839            }
     840        } else {
    840841            $paged_invite_ids = $wpdb->get_col( $paged_invites_sql );
    841             bp_core_set_incremented_cache( $paged_invites_sql, 'bp_invitations', $paged_invite_ids );
    842         } else {
    843             $paged_invite_ids = $cached;
    844842        }
    845843
     
    850848        }
    851849
    852         $uncached_ids = bp_get_non_cached_ids( $paged_invite_ids, 'bp_invitations' );
    853         if ( $uncached_ids ) {
    854             $ids_sql      = implode( ',', array_map( 'intval', $uncached_ids ) );
    855             $data_objects = $wpdb->get_results( "SELECT i.* FROM {$invites_table_name} i WHERE i.id IN ({$ids_sql})" );
    856             foreach ( $data_objects as $data_object ) {
    857                 wp_cache_set( $data_object->id, $data_object, 'bp_invitations' );
     850        if ( $r['cache_results'] ) {
     851            $uncached_ids = bp_get_non_cached_ids( $paged_invite_ids, 'bp_invitations' );
     852            if ( $uncached_ids ) {
     853                $ids_sql      = implode( ',', array_map( 'intval', $uncached_ids ) );
     854                $data_objects = $wpdb->get_results( "SELECT i.* FROM {$invites_table_name} i WHERE i.id IN ({$ids_sql})" );
     855                foreach ( $data_objects as $data_object ) {
     856                    wp_cache_set( $data_object->id, $data_object, 'bp_invitations' );
     857                }
    858858            }
    859859        }
     
    882882    public static function get_total_count( $args ) {
    883883        global $wpdb;
     884
    884885        $invites_table_name = BP_Invitation_Manager::get_table_name();
    885886
     
    914915
    915916        // Return the queried results.
    916         return $wpdb->get_var( $sql );
     917        return (int) $wpdb->get_var( $sql );
    917918    }
    918919
  • trunk/tests/phpunit/testcases/core/invitations.php

    r13980 r13990  
    88 */
    99 class BP_Tests_Invitations extends BP_UnitTestCase {
     10
     11     /**
     12      * @ticket BP8552
     13      * @group cache
     14      */
     15     public function test_invitation_query_with_ids_cache_results() {
     16         global $wpdb;
     17
     18         $u1 = self::factory()->user->create();
     19         $u2 = self::factory()->user->create();
     20         $u3 = self::factory()->user->create();
     21
     22         $invites_class = new BPTest_Invitation_Manager_Extension();
     23
     24         // Create a couple of invitations.
     25         $invite_args = array(
     26             'user_id'           => $u3,
     27             'inviter_id'       => $u1,
     28             'item_id'           => 1,
     29             'send_invite'       => 'sent',
     30         );
     31
     32         $invites_class->add_invitation( $invite_args );
     33
     34         $invite_args['inviter_id'] = $u2;
     35
     36         $invites_class->add_invitation( $invite_args );
     37
     38         $wpdb->num_queries = 0;
     39
     40         $first_query = BP_Invitation::get(
     41             array(
     42                 'cache_results' => true,
     43                 'fields'        => 'ids',
     44             )
     45         );
     46
     47         $queries_before = get_num_queries();
     48
     49         $second_query = BP_Invitation::get(
     50             array(
     51                 'cache_results' => false,
     52                 'fields'        => 'ids',
     53             )
     54         );
     55
     56         $queries_after = get_num_queries();
     57
     58         $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' );
     59         $this->assertSame( 2, $queries_after, 'Assert that the uncached query was run' );
     60         $this->assertSameSets( $first_query, $second_query, 'Results of the query are expected to match.' );
     61     }
     62
     63     /**
     64      * @ticket BP8552
     65      * @group cache
     66      */
     67     public function test_invitation_query_with_all_cache_results() {
     68         global $wpdb;
     69
     70         $u1 = self::factory()->user->create();
     71         $u2 = self::factory()->user->create();
     72         $u3 = self::factory()->user->create();
     73
     74         $invites_class = new BPTest_Invitation_Manager_Extension();
     75
     76         // Create a couple of invitations.
     77         $invite_args = array(
     78             'user_id'     => $u3,
     79             'inviter_id'  => $u1,
     80             'item_id'     => 1,
     81             'send_invite' => 'sent',
     82         );
     83
     84         $invites_class->add_invitation( $invite_args );
     85
     86         $invite_args['inviter_id'] = $u2;
     87
     88         $invites_class->add_invitation( $invite_args );
     89
     90         $wpdb->num_queries = 0;
     91
     92         $first_query = BP_Invitation::get(
     93             array( 'cache_results' => true )
     94         );
     95
     96         $queries_before = get_num_queries();
     97
     98         $second_query = BP_Invitation::get(
     99             array( 'cache_results' => false )
     100         );
     101
     102         $queries_after = get_num_queries();
     103
     104         $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' );
     105         $this->assertSame( 3, $queries_after, 'Assert that the uncached query was run' );
     106         $this->assertEquals( $first_query, $second_query, 'Results of the query are expected to match.' );
     107     }
     108
    10109    public function test_bp_invitations_add_invitation_vanilla() {
    11110        $old_current_user = get_current_user_id();
Note: See TracChangeset for help on using the changeset viewer.