Changeset 13989
- Timestamp:
- 07/27/2024 08:12:32 PM (7 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-members/classes/class-bp-signup.php
r13890 r13989 259 259 * 260 260 * @since 2.0.0 261 * @since 6.0.0 Added a list of allowed orderby parameters. 261 * @since 6.0.0 Added a list of allowed orderby parameters. 262 * @since 15.0.0 Introduced the `cache_results` parameter. 262 263 * 263 264 * @global wpdb $wpdb The WordPress database object. … … 267 268 * @type int $offset Offset amount. Default 0. 268 269 * @type int $number How many to fetch. Pass -1 to fetch all. Default 1. 269 * @type bool|string $usersearch Whether or notto search for a username. Default false.270 * @type string $orderby Order By parameter. Possible values are `signup_id`, `login`, `email`,270 * @type bool|string $usersearch Whether to search for a username. Default false. 271 * @type string $orderby Order by parameter. Possible values are `signup_id`, `login`, `email`, 271 272 * `registered`, `activated`. Default `signup_id`. 272 273 * @type string $order Order direction. Default 'DESC'. 273 * @type bool $include Whether or notto include more specific query params.274 * @type bool $include Whether to include more specific query params. 274 275 * @type string $activation_key Activation key to search for. If specified, all other 275 276 * parameters will be ignored. 276 277 * @type int|bool $active Pass 0 for inactive signups, 1 for active signups, 277 278 * and `false` to ignore. 279 * @type bool $cache_results Optional. Whether to cache signup information. Default true. 278 280 * @type string $user_login Specific user login to return. 279 281 * @type string $fields Which fields to return. Specify 'ids' to fetch a list of signups IDs. … … 302 304 'user_email' => '', 303 305 'user_login' => '', 306 'cache_results' => true, 304 307 'fields' => 'all', 305 308 ), … … 319 322 320 323 $sql = array( 321 'select' => "SELECT DISTINCT signup_id",322 'from' 323 'where' 324 'orderby' 325 'limit' 324 'select' => 'SELECT DISTINCT signup_id', 325 'from' => "{$bp->members->table_name_signups}", 326 'where' => array(), 327 'orderby' => '', 328 'limit' => '', 326 329 ); 327 330 328 331 // Activation key trumps other parameters because it should be unique. 329 332 if ( ! empty( $r['activation_key'] ) ) { 330 $sql['where'][] = $wpdb->prepare( "activation_key = %s", $r['activation_key'] );333 $sql['where'][] = $wpdb->prepare( 'activation_key = %s', $r['activation_key'] ); 331 334 332 335 // `Include` finds signups by ID. 333 } else 336 } elseif ( ! empty( $r['include'] ) ) { 334 337 335 338 $in = implode( ',', wp_parse_id_list( $r['include'] ) ); … … 343 346 // Active. 344 347 if ( false !== $r['active'] ) { 345 $sql['where'][] = $wpdb->prepare( "active = %d", absint( $r['active'] ) );348 $sql['where'][] = $wpdb->prepare( 'active = %d', absint( $r['active'] ) ); 346 349 } 347 350 … … 349 352 if ( ! empty( $r['usersearch'] ) ) { 350 353 $search_terms_like = '%' . bp_esc_like( $r['usersearch'] ) . '%'; 351 $sql['where'][] = $wpdb->prepare( "( user_login LIKE %s OR user_email LIKE %s OR meta LIKE %s )", $search_terms_like, $search_terms_like, $search_terms_like );354 $sql['where'][] = $wpdb->prepare( '( user_login LIKE %s OR user_email LIKE %s OR meta LIKE %s )', $search_terms_like, $search_terms_like, $search_terms_like ); 352 355 } 353 356 354 357 // User email. 355 358 if ( ! empty( $r['user_email'] ) ) { 356 $sql['where'][] = $wpdb->prepare( "user_email = %s", $r['user_email'] );359 $sql['where'][] = $wpdb->prepare( 'user_email = %s', $r['user_email'] ); 357 360 } 358 361 359 362 // User login. 360 363 if ( ! empty( $r['user_login'] ) ) { 361 $sql['where'][] = $wpdb->prepare( "user_login = %s", $r['user_login'] );362 } 363 364 $order 364 $sql['where'][] = $wpdb->prepare( 'user_login = %s', $r['user_login'] ); 365 } 366 367 $order = bp_esc_sql_order( $r['order'] ); 365 368 $sql['orderby'] = "ORDER BY {$r['orderby']} {$order}"; 366 369 367 370 $number = intval( $r['number'] ); 368 371 if ( -1 !== $number ) { 369 $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", absint( $r['offset'] ), $number );372 $sql['limit'] = $wpdb->prepare( 'LIMIT %d, %d', absint( $r['offset'] ), $number ); 370 373 } 371 374 } … … 381 384 * @since 2.0.0 382 385 * 383 * @param string $ valueSQL statement.384 * @param array $sql Array of SQL statement parts.385 * @param array $args Array of original arguments for get() method.386 * @param array $r Array of parsed arguments for get() method.386 * @param string $paged_signups_sql SQL statement. 387 * @param array $sql Array of SQL statement parts. 388 * @param array $args Array of original arguments for get() method. 389 * @param array $r Array of parsed arguments for get() method. 387 390 */ 388 391 $paged_signups_sql = apply_filters( 'bp_members_signups_paged_query', $paged_signups_sql, $sql, $args, $r ); 389 392 390 $cached = bp_core_get_incremented_cache( $paged_signups_sql, 'bp_signups' ); 391 if ( false === $cached ) { 393 if ( $r['cache_results'] ) { 394 $cached = bp_core_get_incremented_cache( $paged_signups_sql, 'bp_signups' ); 395 if ( false === $cached ) { 396 $paged_signup_ids = $wpdb->get_col( $paged_signups_sql ); 397 bp_core_set_incremented_cache( $paged_signups_sql, 'bp_signups', $paged_signup_ids ); 398 } else { 399 $paged_signup_ids = $cached; 400 } 401 } else { 392 402 $paged_signup_ids = $wpdb->get_col( $paged_signups_sql ); 393 bp_core_set_incremented_cache( $paged_signups_sql, 'bp_signups', $paged_signup_ids );394 } else {395 $paged_signup_ids = $cached;396 403 } 397 404 … … 399 406 if ( 'ids' === $r['fields'] ) { 400 407 $paged_signups = array_map( 'intval', $paged_signup_ids ); 401 402 408 } else { 403 $uncached_signup_ids = bp_get_non_cached_ids( $paged_signup_ids, 'bp_signups' ); 404 if ( $uncached_signup_ids ) { 405 $signup_ids_sql = implode( ',', array_map( 'intval', $uncached_signup_ids ) ); 406 $signup_data_objects = $wpdb->get_results( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id IN ({$signup_ids_sql})" ); 407 foreach ( $signup_data_objects as $signup_data_object ) { 408 wp_cache_set( $signup_data_object->signup_id, $signup_data_object, 'bp_signups' ); 409 if ( $r['cache_results'] ) { 410 $uncached_signup_ids = bp_get_non_cached_ids( $paged_signup_ids, 'bp_signups' ); 411 if ( $uncached_signup_ids ) { 412 $signup_ids_sql = implode( ',', array_map( 'intval', $uncached_signup_ids ) ); 413 $signup_data_objects = $wpdb->get_results( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id IN ({$signup_ids_sql})" ); 414 foreach ( $signup_data_objects as $signup_data_object ) { 415 wp_cache_set( $signup_data_object->signup_id, $signup_data_object, 'bp_signups' ); 416 } 409 417 } 410 418 } … … 420 428 421 429 /** 422 * Filters the Signups count query.423 * 424 * @since 2.0.0 425 * 426 * @param string $ valueSQL statement.427 * @param array $sql Array of SQL statement parts.428 * @param array $args Array of original arguments for get() method.429 * @param array $r Array of parsed arguments for get() method.430 * Filters the signups count query. 431 * 432 * @since 2.0.0 433 * 434 * @param string $total_signups_sql SQL statement. 435 * @param array $sql Array of SQL statement parts. 436 * @param array $args Array of original arguments for get() method. 437 * @param array $r Array of parsed arguments for get() method. 430 438 */ 431 439 $total_signups_sql = apply_filters( 'bp_members_signups_count_query', $total_signups_sql, $sql, $args, $r ); 432 440 433 $cached = bp_core_get_incremented_cache( $total_signups_sql, 'bp_signups' ); 434 if ( false === $cached ) { 441 if ( $r['cache_results'] ) { 442 $cached = bp_core_get_incremented_cache( $total_signups_sql, 'bp_signups' ); 443 if ( false === $cached ) { 444 $total_signups = (int) $wpdb->get_var( $total_signups_sql ); 445 bp_core_set_incremented_cache( $total_signups_sql, 'bp_signups', $total_signups ); 446 } else { 447 $total_signups = (int) $cached; 448 } 449 } else { 435 450 $total_signups = (int) $wpdb->get_var( $total_signups_sql ); 436 bp_core_set_incremented_cache( $total_signups_sql, 'bp_signups', $total_signups );437 } else { 438 $total_signups = (int) $cached;439 }440 441 return array( 'signups' => $paged_signups, 'total' => $total_signups);451 } 452 453 return array( 454 'signups' => $paged_signups, 455 'total' => $total_signups, 456 ); 442 457 } 443 458 -
trunk/tests/phpunit/testcases/members/class-bp-signup.php
r13980 r13989 359 359 360 360 /** 361 * @ticket BP8552 362 * @group cache 363 */ 364 public function test_signup_query_with_ids_cache_results() { 365 global $wpdb; 366 367 self::factory()->signup->create_many( 2 ); 368 369 // Reset. 370 $wpdb->num_queries = 0; 371 372 $first_query = BP_Signup::get( 373 array( 374 'cache_results' => true, 375 'fields' => 'ids', 376 ) 377 ); 378 379 $queries_before = get_num_queries(); 380 381 $second_query = BP_Signup::get( 382 array( 383 'cache_results' => false, 384 'fields' => 'ids', 385 ) 386 ); 387 388 $queries_after = get_num_queries(); 389 390 $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' ); 391 $this->assertSame( 4, $queries_after, 'Assert that the uncached query was run' ); 392 $this->assertSameSets( $first_query['signups'], $second_query['signups'], 'Results of the query are expected to match.' ); 393 $this->assertSame( $first_query['total'], $second_query['total'], 'Results of the query are expected to match.' ); 394 } 395 396 /** 397 * @ticket BP8552 398 * @group cache 399 */ 400 public function test_signup_query_with_all_cache_results() { 401 global $wpdb; 402 403 self::factory()->signup->create_many( 2 ); 404 405 // Reset. 406 $wpdb->num_queries = 0; 407 408 $first_query = BP_Signup::get( 409 array( 'cache_results' => true ) 410 ); 411 412 $queries_before = get_num_queries(); 413 414 $second_query = BP_Signup::get( 415 array( 'cache_results' => false ) 416 ); 417 418 $queries_after = get_num_queries(); 419 420 $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' ); 421 $this->assertSame( 5, $queries_after, 'Assert that the uncached query was run' ); 422 $this->assertSame( $first_query['total'], $second_query['total'], 'Results of the query are expected to match.' ); 423 } 424 425 /** 361 426 * @group cache 362 427 */ … … 364 429 global $wpdb; 365 430 366 $s = self::factory()->signup->create();367 368 $found1 = BP_Signup::get(369 array(370 'fields' => 'ids', 371 )372 ); 373 374 $num_queries = $wpdb->num_queries; 375 376 $found2 = BP_Signup::get( 377 array(378 'fields' => 'ids', 379 )380 ); 381 382 $this->assert EqualSets( $found1, $found2);383 $this->assertSame ( $num_queries, $wpdb->num_queries);431 self::factory()->signup->create_many( 2 ); 432 433 // Reset. 434 $wpdb->num_queries = 0; 435 436 $args = array( 'fields' => 'ids' ); 437 438 $first_query = BP_Signup::get( $args ); 439 440 $queries_before = get_num_queries(); 441 442 $second_query = BP_Signup::get( $args ); 443 444 $queries_after = get_num_queries(); 445 446 $this->assertSame( $queries_before, $queries_after, 'Assert that queries are run' ); 447 $this->assertSame( 2, $queries_after, 'Assert that the uncached query was run' ); 448 $this->assertSameSets( $first_query['signups'], $second_query['signups'], 'Results of the query are expected to match.' ); 384 449 } 385 450 … … 388 453 */ 389 454 public function test_get_query_caches_should_be_busted_by_add() { 390 $s1 = self::factory()->signup->create(); 391 392 $found1 = BP_Signup::get( 393 array( 394 'fields' => 'ids', 395 ) 396 ); 455 $s1 = self::factory()->signup->create(); 456 $args = array( 'fields' => 'ids' ); 457 458 $found1 = BP_Signup::get( $args ); 397 459 $this->assertEqualSets( array( $s1 ), $found1['signups'] ); 398 460 399 461 $s2 = self::factory()->signup->create(); 400 $found2 = BP_Signup::get( 401 array( 402 'fields' => 'ids', 403 ) 404 ); 462 $found2 = BP_Signup::get( $args ); 405 463 $this->assertEqualSets( array( $s2 ), $found2['signups'] ); 406 464 } … … 521 579 $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] ); 522 580 523 $activate =BP_Signup::activate( (array) $s2 );581 BP_Signup::activate( (array) $s2 ); 524 582 525 583 $found2 = BP_Signup::get( … … 570 628 $this->assertFalse( $found1->active ); 571 629 572 $activate =BP_Signup::activate( (array) $s1 );630 BP_Signup::activate( (array) $s1 ); 573 631 574 632 $found2 = new BP_Signup( $s1 );
Note: See TracChangeset
for help on using the changeset viewer.