Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/27/2024 08:14:28 PM (9 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/

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.