Ticket #32592: 32592.diff
File 32592.diff, 5.5 KB (added by , 9 years ago) |
---|
-
src/wp-includes/meta.php
diff --git src/wp-includes/meta.php src/wp-includes/meta.php index ead2004..d1b74d9 100644
class WP_Meta_Query { 954 954 protected $clauses = array(); 955 955 956 956 /** 957 * Whether the query contains any OR relations. 958 * 959 * @since 4.3.0 960 * @access protected 961 * @var bool 962 */ 963 protected $has_or_relation = false; 964 965 /** 957 966 * Constructor. 958 967 * 959 968 * @since 3.2.0 … … class WP_Meta_Query { 1046 1055 // Sanitize the 'relation' key provided in the query. 1047 1056 if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) { 1048 1057 $clean_queries['relation'] = 'OR'; 1058 $this->has_or_relation = true; 1049 1059 1050 1060 /* 1051 1061 * If there is only a single clause, call the relation 'OR'. … … class WP_Meta_Query { 1578 1588 */ 1579 1589 return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ) ; 1580 1590 } 1591 1592 /** 1593 * Check whether the current query has any OR relations. 1594 * 1595 * In some cases, the presence of an OR relation somewhere in the query will require the use of a DISTINCT or 1596 * GROUP BY keyword in the SELECT clause. The current method can be used in these cases to determine whether 1597 * such a clause is necessary. 1598 * 1599 * @since 4.3.0 1600 * 1601 * @return bool True if the query contains any OR relations, otherwise false. 1602 */ 1603 public function has_or_relation() { 1604 return $this->has_or_relation; 1605 } 1581 1606 } 1582 1607 1583 1608 /** -
src/wp-includes/user.php
diff --git src/wp-includes/user.php src/wp-includes/user.php index 2599ce0..cb9695c 100644
class WP_User_Query { 709 709 $this->query_from .= $clauses['join']; 710 710 $this->query_where .= $clauses['where']; 711 711 712 if ( 'OR' == $this->meta_query->relation) {712 if ( $this->meta_query->has_or_relation() ) { 713 713 $this->query_fields = 'DISTINCT ' . $this->query_fields; 714 714 } 715 715 } -
tests/phpunit/tests/meta/query.php
diff --git tests/phpunit/tests/meta/query.php tests/phpunit/tests/meta/query.php index 47fe7a6..d3b315a 100644
class Tests_Meta_Query extends WP_UnitTestCase { 806 806 $this->assertRegExp( "/{$wpdb->postmeta}\.meta_key = \'exclude\'\s+OR/", $sql['where'] ); 807 807 $this->assertNotContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] ); 808 808 } 809 810 /** 811 * @group 32592 812 */ 813 public function test_has_or_relation_should_return_false() { 814 $q = new WP_Meta_Query( array( 815 'relation' => 'AND', 816 array( 817 'key' => 'foo', 818 'value' => 'bar', 819 ), 820 array( 821 'relation' => 'AND', 822 array( 823 'key' => 'foo1', 824 'value' => 'bar', 825 ), 826 array( 827 'key' => 'foo2', 828 'value' => 'bar', 829 ), 830 ), 831 ) ); 832 833 $this->assertFalse( $q->has_or_relation() ); 834 } 835 836 /** 837 * @group 32592 838 */ 839 public function test_has_or_relation_should_return_true_for_top_level_or() { 840 $q = new WP_Meta_Query( array( 841 'relation' => 'OR', 842 array( 843 'key' => 'foo', 844 'value' => 'bar', 845 ), 846 array( 847 'relation' => 'AND', 848 array( 849 'key' => 'foo1', 850 'value' => 'bar', 851 ), 852 array( 853 'key' => 'foo2', 854 'value' => 'bar', 855 ), 856 ), 857 ) ); 858 859 $this->assertTrue( $q->has_or_relation() ); 860 } 861 862 /** 863 * @group 32592 864 */ 865 public function test_has_or_relation_should_return_true_for_nested_or() { 866 $q = new WP_Meta_Query( array( 867 'relation' => 'AND', 868 array( 869 'key' => 'foo', 870 'value' => 'bar', 871 ), 872 array( 873 'relation' => 'OR', 874 array( 875 'key' => 'foo1', 876 'value' => 'bar', 877 ), 878 array( 879 'key' => 'foo2', 880 'value' => 'bar', 881 ), 882 ), 883 ) ); 884 885 $this->assertTrue( $q->has_or_relation() ); 886 } 809 887 } -
tests/phpunit/tests/user/query.php
diff --git tests/phpunit/tests/user/query.php tests/phpunit/tests/user/query.php index 2b22f1e..79a6fb1 100644
class Tests_User_Query extends WP_UnitTestCase { 789 789 790 790 $this->assertEqualSets( $expected, $found ); 791 791 } 792 793 /** 794 * @ticket 32592 795 */ 796 public function test_top_level_or_meta_query_should_eliminate_duplicate_matches() { 797 $users = $this->factory->user->create_many( 3 ); 798 799 add_user_meta( $users[0], 'foo', 'bar' ); 800 add_user_meta( $users[1], 'foo', 'bar' ); 801 add_user_meta( $users[0], 'foo2', 'bar2' ); 802 803 $q = new WP_User_Query( array( 804 'meta_query' => array( 805 'relation' => 'OR', 806 array( 807 'key' => 'foo', 808 'value' => 'bar', 809 ), 810 array( 811 'key' => 'foo2', 812 'value' => 'bar2', 813 ), 814 ), 815 ) ); 816 817 $found = wp_list_pluck( $q->get_results(), 'ID' ); 818 $expected = array( $users[0], $users[1] ); 819 820 $this->assertEqualSets( $expected, $found ); 821 } 822 823 /** 824 * @ticket 32592 825 */ 826 public function test_nested_or_meta_query_should_eliminate_duplicate_matches() { 827 $users = $this->factory->user->create_many( 3 ); 828 829 add_user_meta( $users[0], 'foo', 'bar' ); 830 add_user_meta( $users[1], 'foo', 'bar' ); 831 add_user_meta( $users[0], 'foo2', 'bar2' ); 832 add_user_meta( $users[1], 'foo3', 'bar3' ); 833 834 $q = new WP_User_Query( array( 835 'meta_query' => array( 836 'relation' => 'AND', 837 array( 838 'key' => 'foo', 839 'value' => 'bar', 840 ), 841 array( 842 'relation' => 'OR', 843 array( 844 'key' => 'foo', 845 'value' => 'bar', 846 ), 847 array( 848 'key' => 'foo2', 849 'value' => 'bar2', 850 ), 851 ), 852 ), 853 ) ); 854 855 $found = wp_list_pluck( $q->get_results(), 'ID' ); 856 $expected = array( $users[0], $users[1] ); 857 858 $this->assertEqualSets( $expected, $found ); 859 } 792 860 }