Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/27/2024 08:16:17 PM (2 years ago)
Author:
espellcaste
Message:

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

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

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

File:
1 edited

Legend:

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

    r13888 r13991  
    102102         * @global wpdb $wpdb WordPress database object.
    103103         *
    104          * @return bool
     104         * @return false|int
    105105         */
    106106        public function save() {
     107                global $wpdb;
    107108
    108109                // Return value.
     
    110111
    111112                // Default data and format.
    112                 $data        = array(
     113                $data = array(
    113114                        'email_address_hash' => $this->email_address,
    114115                        'user_id'            => $this->user_id,
     
    116117                        'date_modified'      => $this->date_modified,
    117118                );
     119
    118120                $data_format = array( '%s', '%d', '%s', '%s' );
    119121
     
    123125                 * @since 8.0.0
    124126                 *
    125                  * @param BP_Optout object $this Characteristics of the opt-out to be saved.
     127                 * @param BP_Optout $bp_optout Characteristics of the opt-out to be saved.
    126128                 */
    127129                do_action_ref_array( 'bp_optout_before_save', array( &$this ) );
     
    137139                // Set the opt-out ID if successful.
    138140                if ( ! empty( $result ) && ! is_wp_error( $result ) ) {
    139                         global $wpdb;
    140 
    141141                        $this->id = $wpdb->insert_id;
    142142                        $retval   = $wpdb->insert_id;
     
    505505         *
    506506         * @since 8.0.0
     507         * @since 15.0.0 Introduced the `cache_results` parameter.
    507508         *
    508509         * @global wpdb $wpdb WordPress database object.
     
    526527         *                                           Default: false (no pagination,
    527528         *                                           all items).
     529         *     @type bool         $cache_results     Optional. Whether to cache the optout information. Default: true.
    528530         *     @type int          $per_page          Number of items to show per page.
    529531         *                                           Default: false (no pagination,
     
    541543        public static function get( $args = array() ) {
    542544                global $wpdb;
     545
    543546                $optouts_table_name = self::get_table_name();
    544547
     
    556559                                'page'          => false,
    557560                                'per_page'      => false,
     561                                'cache_results' => true,
    558562                                'fields'        => 'all',
    559563                        ),
     
    562566
    563567                $sql = array(
    564                         'select'     => 'SELECT',
    565                         'fields'     => '',
    566                         'from'       => "FROM {$optouts_table_name} o",
    567                         'where'      => '',
    568                         'orderby'    => '',
    569                         'pagination' => '',
     568                        'select' => 'SELECT',
     569                        'from'   => "FROM {$optouts_table_name} o",
    570570                );
    571571
     
    612612                 * @since 8.0.0
    613613                 *
    614                  * @param string $value Concatenated SQL statement.
    615                  * @param array  $sql   Array of SQL parts before concatenation.
    616                  * @param array  $r     Array of parsed arguments for the get method.
     614                 * @param string $paged_optouts_sql Concatenated SQL statement.
     615                 * @param array  $sql               Array of SQL parts before concatenation.
     616                 * @param array  $r                 Array of parsed arguments for the get method.
    617617                 */
    618618                $paged_optouts_sql = apply_filters( 'bp_optouts_get_paged_optouts_sql', $paged_optouts_sql, $sql, $r );
    619619
    620                 $cached = bp_core_get_incremented_cache( $paged_optouts_sql, 'bp_optouts' );
    621                 if ( false === $cached ) {
     620                if ( $r['cache_results'] ) {
     621                        $cached = bp_core_get_incremented_cache( $paged_optouts_sql, 'bp_optouts' );
     622                        if ( false === $cached ) {
     623                                $paged_optout_ids = $wpdb->get_col( $paged_optouts_sql );
     624                                bp_core_set_incremented_cache( $paged_optouts_sql, 'bp_optouts', $paged_optout_ids );
     625                        } else {
     626                                $paged_optout_ids = $cached;
     627                        }
     628                } else {
    622629                        $paged_optout_ids = $wpdb->get_col( $paged_optouts_sql );
    623                         bp_core_set_incremented_cache( $paged_optouts_sql, 'bp_optouts', $paged_optout_ids );
    624                 } else {
    625                         $paged_optout_ids = $cached;
    626630                }
    627631
     
    634638                }
    635639
    636                 $uncached_ids = bp_get_non_cached_ids( $paged_optout_ids, 'bp_optouts' );
    637                 if ( $uncached_ids ) {
    638                         $ids_sql      = implode( ',', array_map( 'intval', $uncached_ids ) );
    639                         $data_objects = $wpdb->get_results( "SELECT o.* FROM {$optouts_table_name} o WHERE o.id IN ({$ids_sql})" );
    640                         foreach ( $data_objects as $data_object ) {
    641                                 wp_cache_set( $data_object->id, $data_object, 'bp_optouts' );
     640                if ( $r['cache_results'] ) {
     641                        $uncached_ids = bp_get_non_cached_ids( $paged_optout_ids, 'bp_optouts' );
     642                        if ( $uncached_ids ) {
     643                                $ids_sql      = implode( ',', array_map( 'intval', $uncached_ids ) );
     644                                $data_objects = $wpdb->get_results( "SELECT o.* FROM {$optouts_table_name} o WHERE o.id IN ({$ids_sql})" );
     645                                foreach ( $data_objects as $data_object ) {
     646                                        wp_cache_set( $data_object->id, $data_object, 'bp_optouts' );
     647                                }
    642648                        }
    643649                }
Note: See TracChangeset for help on using the changeset viewer.