Skip to:
Content

BuddyPress.org

Changeset 13991


Ignore:
Timestamp:
07/27/2024 08:16:17 PM (9 months 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/

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r13983 r13991  
    48244824 */
    48254825function bp_add_optout( $args = array() ) {
    4826     $optout = new BP_Optout();
    4827     $r      = bp_parse_args(
    4828         $args, array(
     4826    $r = bp_parse_args(
     4827        $args,
     4828        array(
    48294829            'email_address' => '',
    48304830            'user_id'       => 0,
     
    48394839        return false;
    48404840    }
     4841
     4842    $optout = new BP_Optout();
    48414843
    48424844    // Avoid creating duplicate opt-outs.
     
    48734875 */
    48744876function bp_get_optouts( $args = array() ) {
    4875     $optout_class = new BP_Optout();
    4876     return $optout_class::get( $args );
     4877    return BP_Optout::get( $args );
    48774878}
    48784879
  • 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        }
  • trunk/tests/phpunit/includes/factory.php

    r13980 r13991  
    1414        $this->signup         = new BP_UnitTest_Factory_For_Signup( $this );
    1515        $this->friendship     = new BP_UnitTest_Factory_For_Friendship( $this );
     16        $this->optout         = new BP_UnitTest_Factory_For_Optout( $this );
    1617    }
    1718}
     
    7071    }
    7172
    72     public function update_object( $activity_id, $fields ) {
    73         $activity = new BP_Activity_Activity( $activity_id );
     73    public function update_object( $object_id, $fields ) {
     74        $activity = new BP_Activity_Activity( $object_id );
    7475
    7576        foreach ( $fields as $field_name => $value ) {
     
    8485    }
    8586
    86     public function get_object_by_id( $user_id ) {
    87         return new BP_Activity_Activity( $user_id );
     87    public function get_object_by_id( $object_id ) {
     88        return new BP_Activity_Activity( $object_id );
    8889    }
    8990}
     
    138139    }
    139140
    140     public function update_object( $group_id, $fields ) {
    141         $group = new BP_Groups_Group( $group_id );
     141    public function update_object( $object_id, $fields ) {
     142        $group = new BP_Groups_Group( $object_id );
    142143
    143144        foreach ( $fields as $field_name => $value ) {
     
    152153    }
    153154
    154     public function get_object_by_id( $group_id ) {
    155         return new BP_Groups_Group( $group_id );
     155    public function get_object_by_id( $object_id ) {
     156        return new BP_Groups_Group( $object_id );
    156157    }
    157158}
     
    187188    }
    188189
    189     public function update_object( $message_id, $fields ) {}
    190 
    191     public function get_object_by_id( $message_id ) {
    192         return new BP_Messages_Message( $message_id );
     190    public function update_object( $object_id, $fields ) {}
     191
     192    public function get_object_by_id( $object_id ) {
     193        return new BP_Messages_Message( $object_id );
    193194    }
    194195}
     
    210211    }
    211212
    212     public function update_object( $group_id, $fields ) {}
    213 
    214     public function get_object_by_id( $group_id ) {
    215         return new BP_XProfile_Group( $group_id );
     213    public function update_object( $object_id, $fields ) {}
     214
     215    public function get_object_by_id( $object_id ) {
     216        return new BP_XProfile_Group( $object_id );
    216217    }
    217218}
     
    233234    }
    234235
    235     public function update_object( $field_id, $fields ) {}
    236 
    237     public function get_object_by_id( $field_id ) {
    238         return new BP_XProfile_Field( $field_id );
     236    public function update_object( $object_id, $fields ) {}
     237
     238    public function get_object_by_id( $object_id ) {
     239        return new BP_XProfile_Field( $object_id );
    239240    }
    240241}
     
    245246    }
    246247
    247     public function update_object( $id, $fields ) {}
    248 
    249     public function get_object_by_id( $id ) {
    250         return new BP_Notifications_Notification( $id );
     248    public function update_object( $object_id, $fields ) {}
     249
     250    public function get_object_by_id( $object_id ) {
     251        return new BP_Notifications_Notification( $object_id );
    251252    }
    252253}
     
    257258    }
    258259
    259     public function update_object( $id, $fields ) {}
    260 
    261     public function get_object_by_id( $id ) {
    262         return new BP_Signup( $id );
     260    public function update_object( $object_id, $fields ) {}
     261
     262    public function get_object_by_id( $object_id ) {
     263        return new BP_Signup( $object_id );
    263264    }
    264265}
     
    293294    }
    294295
    295     public function update_object( $id, $fields ) {}
    296 
    297     public function get_object_by_id( $id ) {
    298         return new BP_Friends_Friendship( $id );
    299     }
    300 }
     296    public function update_object( $object_id, $fields ) {}
     297
     298    public function get_object_by_id( $object_id ) {
     299        return new BP_Friends_Friendship( $object_id );
     300    }
     301}
     302
     303/**
     304 * Factory for optout.
     305 *
     306 * @since 15.0.0
     307 */
     308class BP_UnitTest_Factory_For_Optout extends WP_UnitTest_Factory_For_Thing {
     309
     310    public function __construct( $factory = null ) {
     311        parent::__construct( $factory );
     312
     313        $this->default_generation_definitions = array(
     314            'email_address' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
     315            'user_id'       => 0,
     316            'email_type'    => '',
     317            'date_modified' => bp_core_current_time(),
     318        );
     319    }
     320
     321    public function create_object( $args ) {
     322        $optout = new BP_Optout();
     323
     324        $optout->email_address = $args['email_address'];
     325        $optout->user_id       = $args['user_id'];
     326        $optout->email_type    = $args['email_type'];
     327        $optout->date_modified = $args['date_modified'];
     328
     329        return $optout->save();
     330    }
     331
     332    public function update_object( $object_id, $fields ) {}
     333
     334    public function get_object_by_id( $object_id ) {
     335        return new BP_Optout( $object_id );
     336    }
     337}
  • trunk/tests/phpunit/testcases/core/optouts.php

    r13980 r13991  
    55 */
    66 class BP_Tests_Optouts extends BP_UnitTestCase {
     7
     8     /**
     9      * @ticket BP8552
     10      */
     11     public function test_bp_optouts_query_cache_results() {
     12         global $wpdb;
     13
     14         self::factory()->optout->create_many( 2 );
     15
     16         // Reset.
     17         $wpdb->num_queries = 0;
     18
     19         $first_query = BP_Optout::get(
     20             array( 'cache_results' => true )
     21         );
     22
     23         $queries_before = get_num_queries();
     24
     25         $second_query = BP_Optout::get(
     26             array( 'cache_results' => false )
     27         );
     28
     29         $queries_after = get_num_queries();
     30
     31         $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' );
     32         $this->assertSame( 3, $queries_after, 'Assert that the uncached query was run' );
     33         $this->assertEquals( $first_query, $second_query, 'Results of the query are expected to match.' );
     34     }
     35
    736    public function test_bp_optouts_add_optout_vanilla() {
    837        $old_current_user = get_current_user_id();
     
    1746            'email_type'        => 'annoyance'
    1847        );
    19         $i1 = bp_add_optout( $args );
     48        $i1 = self::factory()->optout->create( $args );
    2049        $args['email_address'] = 'two@wp.org';
    21         $i2 = bp_add_optout( $args );
     50        $i2 = self::factory()->optout->create( $args );
    2251
    2352        $get_args = array(
     
    6291            'email_type'        => 'annoyance'
    6392        );
    64         $i1 = bp_add_optout( $args );
     93        $i1 = self::factory()->optout->create( $args );
    6594        bp_delete_optout_by_id( $i1 );
    6695
     
    7099        );
    71100        $optouts = bp_get_optouts( $get_args );
    72         $this->assertTrue( empty( $optouts ) );
     101        $this->assertEmpty( $optouts );
    73102
    74103        self::set_current_user( $old_current_user );
     
    87116            'email_type'        => 'annoyance'
    88117        );
    89         $i1 = bp_add_optout( $args );
     118        $i1 = self::factory()->optout->create( $args );
    90119        $args['email_address'] = 'two@wp.org';
    91         $i2 = bp_add_optout( $args );
     120        self::factory()->optout->create( $args );
    92121
    93122        $get_args = array(
     
    113142            'email_type'        => 'annoyance'
    114143        );
    115         $i1 = bp_add_optout( $args );
     144        $i1 = self::factory()->optout->create( $args );
    116145        $args['email_address'] = 'two@WP.org';
    117         $i2 = bp_add_optout( $args );
     146        self::factory()->optout->create( $args );
    118147
    119148        $get_args = array(
     
    139168            'email_type'        => 'annoyance'
    140169        );
    141         $i1 = bp_add_optout( $args );
     170        $i1 = self::factory()->optout->create( $args );
    142171        $args['email_address'] = 'two@WP.org';
    143         $i2 = bp_add_optout( $args );
     172        self::factory()->optout->create( $args );
    144173
    145174        $get_args = array(
     
    166195            'email_type'        => 'annoyance'
    167196        );
    168         $i1 = bp_add_optout( $args );
     197        $i1 = self::factory()->optout->create( $args );
    169198        // Update it.
    170199        $oo_class                = new BP_Optout( $i1 );
     
    193222            'email_type'        => 'annoyance'
    194223        );
    195         $i1 = bp_add_optout( $args );
     224        self::factory()->optout->create( $args );
    196225        $email = new BP_Email( 'activity-at-message' );
    197226        $email->set_from( 'test1@example.com' )->set_to( 'test2@example.com' )->set_subject( 'testing' );
Note: See TracChangeset for help on using the changeset viewer.