Changeset 13991
- Timestamp:
- 07/27/2024 08:16:17 PM (9 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-functions.php
r13983 r13991 4824 4824 */ 4825 4825 function 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( 4829 4829 'email_address' => '', 4830 4830 'user_id' => 0, … … 4839 4839 return false; 4840 4840 } 4841 4842 $optout = new BP_Optout(); 4841 4843 4842 4844 // Avoid creating duplicate opt-outs. … … 4873 4875 */ 4874 4876 function bp_get_optouts( $args = array() ) { 4875 $optout_class = new BP_Optout(); 4876 return $optout_class::get( $args ); 4877 return BP_Optout::get( $args ); 4877 4878 } 4878 4879 -
trunk/src/bp-core/classes/class-bp-optout.php
r13888 r13991 102 102 * @global wpdb $wpdb WordPress database object. 103 103 * 104 * @return bool104 * @return false|int 105 105 */ 106 106 public function save() { 107 global $wpdb; 107 108 108 109 // Return value. … … 110 111 111 112 // Default data and format. 112 $data 113 $data = array( 113 114 'email_address_hash' => $this->email_address, 114 115 'user_id' => $this->user_id, … … 116 117 'date_modified' => $this->date_modified, 117 118 ); 119 118 120 $data_format = array( '%s', '%d', '%s', '%s' ); 119 121 … … 123 125 * @since 8.0.0 124 126 * 125 * @param BP_Optout object $thisCharacteristics of the opt-out to be saved.127 * @param BP_Optout $bp_optout Characteristics of the opt-out to be saved. 126 128 */ 127 129 do_action_ref_array( 'bp_optout_before_save', array( &$this ) ); … … 137 139 // Set the opt-out ID if successful. 138 140 if ( ! empty( $result ) && ! is_wp_error( $result ) ) { 139 global $wpdb;140 141 141 $this->id = $wpdb->insert_id; 142 142 $retval = $wpdb->insert_id; … … 505 505 * 506 506 * @since 8.0.0 507 * @since 15.0.0 Introduced the `cache_results` parameter. 507 508 * 508 509 * @global wpdb $wpdb WordPress database object. … … 526 527 * Default: false (no pagination, 527 528 * all items). 529 * @type bool $cache_results Optional. Whether to cache the optout information. Default: true. 528 530 * @type int $per_page Number of items to show per page. 529 531 * Default: false (no pagination, … … 541 543 public static function get( $args = array() ) { 542 544 global $wpdb; 545 543 546 $optouts_table_name = self::get_table_name(); 544 547 … … 556 559 'page' => false, 557 560 'per_page' => false, 561 'cache_results' => true, 558 562 'fields' => 'all', 559 563 ), … … 562 566 563 567 $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", 570 570 ); 571 571 … … 612 612 * @since 8.0.0 613 613 * 614 * @param string $ valueConcatenated 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. 617 617 */ 618 618 $paged_optouts_sql = apply_filters( 'bp_optouts_get_paged_optouts_sql', $paged_optouts_sql, $sql, $r ); 619 619 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 { 622 629 $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;626 630 } 627 631 … … 634 638 } 635 639 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 } 642 648 } 643 649 } -
trunk/tests/phpunit/includes/factory.php
r13980 r13991 14 14 $this->signup = new BP_UnitTest_Factory_For_Signup( $this ); 15 15 $this->friendship = new BP_UnitTest_Factory_For_Friendship( $this ); 16 $this->optout = new BP_UnitTest_Factory_For_Optout( $this ); 16 17 } 17 18 } … … 70 71 } 71 72 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 ); 74 75 75 76 foreach ( $fields as $field_name => $value ) { … … 84 85 } 85 86 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 ); 88 89 } 89 90 } … … 138 139 } 139 140 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 ); 142 143 143 144 foreach ( $fields as $field_name => $value ) { … … 152 153 } 153 154 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 ); 156 157 } 157 158 } … … 187 188 } 188 189 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 ); 193 194 } 194 195 } … … 210 211 } 211 212 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 ); 216 217 } 217 218 } … … 233 234 } 234 235 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 ); 239 240 } 240 241 } … … 245 246 } 246 247 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 ); 251 252 } 252 253 } … … 257 258 } 258 259 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 ); 263 264 } 264 265 } … … 293 294 } 294 295 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 */ 308 class 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 5 5 */ 6 6 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 7 36 public function test_bp_optouts_add_optout_vanilla() { 8 37 $old_current_user = get_current_user_id(); … … 17 46 'email_type' => 'annoyance' 18 47 ); 19 $i1 = bp_add_optout( $args );48 $i1 = self::factory()->optout->create( $args ); 20 49 $args['email_address'] = 'two@wp.org'; 21 $i2 = bp_add_optout( $args );50 $i2 = self::factory()->optout->create( $args ); 22 51 23 52 $get_args = array( … … 62 91 'email_type' => 'annoyance' 63 92 ); 64 $i1 = bp_add_optout( $args );93 $i1 = self::factory()->optout->create( $args ); 65 94 bp_delete_optout_by_id( $i1 ); 66 95 … … 70 99 ); 71 100 $optouts = bp_get_optouts( $get_args ); 72 $this->assert True( empty( $optouts ));101 $this->assertEmpty( $optouts ); 73 102 74 103 self::set_current_user( $old_current_user ); … … 87 116 'email_type' => 'annoyance' 88 117 ); 89 $i1 = bp_add_optout( $args );118 $i1 = self::factory()->optout->create( $args ); 90 119 $args['email_address'] = 'two@wp.org'; 91 $i2 = bp_add_optout( $args );120 self::factory()->optout->create( $args ); 92 121 93 122 $get_args = array( … … 113 142 'email_type' => 'annoyance' 114 143 ); 115 $i1 = bp_add_optout( $args );144 $i1 = self::factory()->optout->create( $args ); 116 145 $args['email_address'] = 'two@WP.org'; 117 $i2 = bp_add_optout( $args );146 self::factory()->optout->create( $args ); 118 147 119 148 $get_args = array( … … 139 168 'email_type' => 'annoyance' 140 169 ); 141 $i1 = bp_add_optout( $args );170 $i1 = self::factory()->optout->create( $args ); 142 171 $args['email_address'] = 'two@WP.org'; 143 $i2 = bp_add_optout( $args );172 self::factory()->optout->create( $args ); 144 173 145 174 $get_args = array( … … 166 195 'email_type' => 'annoyance' 167 196 ); 168 $i1 = bp_add_optout( $args );197 $i1 = self::factory()->optout->create( $args ); 169 198 // Update it. 170 199 $oo_class = new BP_Optout( $i1 ); … … 193 222 'email_type' => 'annoyance' 194 223 ); 195 $i1 = bp_add_optout( $args );224 self::factory()->optout->create( $args ); 196 225 $email = new BP_Email( 'activity-at-message' ); 197 226 $email->set_from( 'test1@example.com' )->set_to( 'test2@example.com' )->set_subject( 'testing' );
Note: See TracChangeset
for help on using the changeset viewer.