Changeset 13990
- Timestamp:
- 07/27/2024 08:14:28 PM (8 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/classes/class-bp-invitation.php
r13890 r13990 358 358 * Assemble the WHERE clause of a get() SQL statement. 359 359 * 360 * Used by BP_Invitation::get() to create its WHERE361 * clause.362 *363 360 * @since 5.0.0 364 361 * … … 439 436 440 437 // 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}"; 446 441 } 447 442 … … 689 684 * 690 685 * @since 5.0.0 686 * @since 15.0.0 Introduced the `cache_results` parameter. 691 687 * 692 688 * @global wpdb $wpdb WordPress database object. … … 730 726 * @type string $order_by Field to order results by. 731 727 * @type string $sort_order ASC or DESC. 728 * @type bool $cache_results Optional. Whether to cache invitation information. Default true. 732 729 * @type int $page Number of the current page of results. 733 730 * Default: false (no pagination, … … 745 742 public static function get( $args = array() ) { 746 743 global $wpdb; 744 747 745 $invites_table_name = BP_Invitation_Manager::get_table_name(); 748 746 … … 764 762 'order_by' => false, 765 763 'sort_order' => false, 764 'cache_results' => true, 766 765 'page' => false, 767 766 'per_page' => false, … … 772 771 773 772 $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', 780 776 ); 781 777 … … 786 782 } elseif ( 'inviter_ids' === $r['fields'] ) { 787 783 $sql['fields'] = 'DISTINCT i.inviter_id'; 788 } else {789 $sql['fields'] = 'DISTINCT i.id';790 784 } 791 785 … … 830 824 * @since 5.0.0 831 825 * 832 * @param string $ valueConcatenated 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. 835 829 */ 836 830 $paged_invites_sql = apply_filters( 'bp_invitations_get_paged_invitations_sql', $paged_invites_sql, $sql, $r ); 837 831 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 { 840 841 $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;844 842 } 845 843 … … 850 848 } 851 849 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 } 858 858 } 859 859 } … … 882 882 public static function get_total_count( $args ) { 883 883 global $wpdb; 884 884 885 $invites_table_name = BP_Invitation_Manager::get_table_name(); 885 886 … … 914 915 915 916 // Return the queried results. 916 return $wpdb->get_var( $sql );917 return (int) $wpdb->get_var( $sql ); 917 918 } 918 919 -
trunk/tests/phpunit/testcases/core/invitations.php
r13980 r13990 8 8 */ 9 9 class BP_Tests_Invitations extends BP_UnitTestCase { 10 11 /** 12 * @ticket BP8552 13 * @group cache 14 */ 15 public function test_invitation_query_with_ids_cache_results() { 16 global $wpdb; 17 18 $u1 = self::factory()->user->create(); 19 $u2 = self::factory()->user->create(); 20 $u3 = self::factory()->user->create(); 21 22 $invites_class = new BPTest_Invitation_Manager_Extension(); 23 24 // Create a couple of invitations. 25 $invite_args = array( 26 'user_id' => $u3, 27 'inviter_id' => $u1, 28 'item_id' => 1, 29 'send_invite' => 'sent', 30 ); 31 32 $invites_class->add_invitation( $invite_args ); 33 34 $invite_args['inviter_id'] = $u2; 35 36 $invites_class->add_invitation( $invite_args ); 37 38 $wpdb->num_queries = 0; 39 40 $first_query = BP_Invitation::get( 41 array( 42 'cache_results' => true, 43 'fields' => 'ids', 44 ) 45 ); 46 47 $queries_before = get_num_queries(); 48 49 $second_query = BP_Invitation::get( 50 array( 51 'cache_results' => false, 52 'fields' => 'ids', 53 ) 54 ); 55 56 $queries_after = get_num_queries(); 57 58 $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' ); 59 $this->assertSame( 2, $queries_after, 'Assert that the uncached query was run' ); 60 $this->assertSameSets( $first_query, $second_query, 'Results of the query are expected to match.' ); 61 } 62 63 /** 64 * @ticket BP8552 65 * @group cache 66 */ 67 public function test_invitation_query_with_all_cache_results() { 68 global $wpdb; 69 70 $u1 = self::factory()->user->create(); 71 $u2 = self::factory()->user->create(); 72 $u3 = self::factory()->user->create(); 73 74 $invites_class = new BPTest_Invitation_Manager_Extension(); 75 76 // Create a couple of invitations. 77 $invite_args = array( 78 'user_id' => $u3, 79 'inviter_id' => $u1, 80 'item_id' => 1, 81 'send_invite' => 'sent', 82 ); 83 84 $invites_class->add_invitation( $invite_args ); 85 86 $invite_args['inviter_id'] = $u2; 87 88 $invites_class->add_invitation( $invite_args ); 89 90 $wpdb->num_queries = 0; 91 92 $first_query = BP_Invitation::get( 93 array( 'cache_results' => true ) 94 ); 95 96 $queries_before = get_num_queries(); 97 98 $second_query = BP_Invitation::get( 99 array( 'cache_results' => false ) 100 ); 101 102 $queries_after = get_num_queries(); 103 104 $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' ); 105 $this->assertSame( 3, $queries_after, 'Assert that the uncached query was run' ); 106 $this->assertEquals( $first_query, $second_query, 'Results of the query are expected to match.' ); 107 } 108 10 109 public function test_bp_invitations_add_invitation_vanilla() { 11 110 $old_current_user = get_current_user_id();
Note: See TracChangeset
for help on using the changeset viewer.