Skip to:
Content

BuddyPress.org

Changeset 8949


Ignore:
Timestamp:
08/20/2014 06:32:30 PM (12 years ago)
Author:
boonebgorges
Message:

Simplify logic that transforms WP_Meta_Query SQL for Groups queries

This simplified logic fixes a bug related to the use of the 'compare'
operator in certain cases.

Fixes #5824

Props imath

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-classes.php

    r8934 r8949  
    892892
    893893                        if ( ! empty( $matches_a[1] ) && ! empty( $matches_b[1] ) ) {
    894                                 $sql_array['join']  = implode( ',', $matches_a[1] ). ', ';
    895 
    896                                 $sql_array['where'] = '';
    897 
    898                                 $meta_query_where_clauses = explode( "\n", $meta_sql['where'] );
    899 
    900                                 // Trim empties
    901                                 $meta_query_where_clauses = array_values( array_filter( $meta_query_where_clauses ) );
    902 
    903                                 foreach( $matches_b[1] as $key => $group_id_clause ) {
    904                                         $sql_array['where'] .= ' ' . preg_replace( '/^(AND\s+[\(\s]+)/', '$1' . $group_id_clause . ' AND ', ltrim( $meta_query_where_clauses[ $key ] ) );
    905                                 }
    906 
     894                                $sql_array['join']  = implode( ',', $matches_a[1] ) . ', ';
     895                                $sql_array['where'] = $meta_sql['where'] . ' AND ' . implode ( ' AND ', $matches_b[1] );
    907896                        }
    908897                }
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r8829 r8949  
    137137        /**
    138138         * @group get
     139         * @group group_meta_query
     140         */
     141        public function test_get_with_meta_query_multiple_clauses_relation_or() {
     142                $now = time();
     143                $g1 = $this->factory->group->create( array(
     144                        'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60 ),
     145                ) );
     146                $g2 = $this->factory->group->create( array(
     147                        'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*2 ),
     148                ) );
     149                $g3 = $this->factory->group->create( array(
     150                        'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*3 ),
     151                ) );
     152                groups_update_groupmeta( $g1, 'foo', 'bar' );
     153                groups_update_groupmeta( $g2, 'foo', 'baz' );
     154                groups_update_groupmeta( $g3, 'bar', 'barry' );
     155
     156                $groups = BP_Groups_Group::get( array(
     157                        'meta_query' => array(
     158                                'relation' => 'OR',
     159                                array(
     160                                        'key' => 'foo',
     161                                        'value' => 'bar',
     162                                ),
     163                                array(
     164                                        'key' => 'bar',
     165                                        'value' => 'barry',
     166                                ),
     167                        ),
     168                ) );
     169                $ids = wp_list_pluck( $groups['groups'], 'id' );
     170                $this->assertEquals( array( $g1, $g3 ), $ids );
     171                $this->assertEquals( 2, $groups['total'] );
     172        }
     173
     174        /**
     175         * @group get
     176         * @group group_meta_query
     177         * @ticket BP5824
     178         */
     179        public function test_get_with_meta_query_multiple_keys_with_same_value() {
     180                $now = time();
     181                $g1 = $this->factory->group->create( array(
     182                        'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60 ),
     183                ) );
     184                $g2 = $this->factory->group->create( array(
     185                        'last_activity' => date( 'Y-m-d H:i:s', $now - 60*60*2 ),
     186                ) );
     187                groups_update_groupmeta( $g1, 'foo', 'bar' );
     188                groups_update_groupmeta( $g2, 'foo2', 'bar' );
     189
     190                $groups = BP_Groups_Group::get( array(
     191                        'meta_query' => array(
     192                                array(
     193                                        'key' => 'foo',
     194                                        'value' => 'bar',
     195                                        'compare' => '=',
     196                                ),
     197                        ),
     198                ) );
     199                $ids = wp_list_pluck( $groups['groups'], 'id' );
     200                $this->assertEquals( $ids, array( $g1 ) );
     201                $this->assertEquals( 1, $groups['total'] );
     202        }
     203
     204        /**
     205         * @group get
    139206         */
    140207        public function test_get_normal_search() {
Note: See TracChangeset for help on using the changeset viewer.