Changeset 38715
- Timestamp:
- 10/04/2016 02:26:26 AM (8 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-user-query.php
r38457 r38715 125 125 'who' => '', 126 126 'has_published_posts' => null, 127 'nicename' => '', 128 'nicename__in' => array(), 129 'nicename__not_in' => array(), 130 'login' => '', 131 'login__in' => array(), 132 'login__not_in' => array() 127 133 ); 128 134 … … 141 147 * permit an array or comma-separated list of values. The 'number' parameter was updated to support 142 148 * querying for all users with using -1. 149 * @since 4.7.0 Added 'nicename', 'nicename__in', 'nicename__not_in', 'login', 'login__in', 150 * and 'login__not_in' parameters. 143 151 * 144 152 * @access public … … 174 182 * keys and orders ('ASC' or 'DESC') as values. Accepted values are 175 183 * 'ID', 'display_name' (or 'name'), 'include', 'user_login' 176 * (or 'login'), ' user_nicename' (or 'nicename'), 'user_email'177 * (or 'email'), 'user_url' (or 'url'), 'user_registered'178 * or 'registered'), 'post_count', 'meta_value', 'meta_value_num',179 * the value of `$meta_key`, or an array key of `$meta_query`. To use180 * 'meta_value' or 'meta_value_num', `$meta_key` must be also be181 * defined. Default 'user_login'.184 * (or 'login'), 'login__in', 'user_nicename' (or 'nicename'), 185 * 'nicename__in', 'user_email (or 'email'), 'user_url' (or 'url'), 186 * 'user_registered' (or 'registered'), 'post_count', 'meta_value', 187 * 'meta_value_num', the value of `$meta_key`, or an array key of 188 * `$meta_query`. To use 'meta_value' or 'meta_value_num', `$meta_key` 189 * must be also be defined. Default 'user_login'. 182 190 * @type string $order Designates ascending or descending order of users. Order values 183 191 * passed as part of an `$orderby` array take precedence over this … … 204 212 * published posts in those post types. `true` is an alias for all 205 213 * public post types. 214 * @type string $nicename The user nicename. Default empty. 215 * @type array $nicename__in An array of nicenames to include. Users matching one of these 216 * nicenames will be included in results. Default empty array. 217 * @type array $nicename__not_in An array of nicenames to exclude. Users matching one of these 218 * nicenames will not be included in results. Default empty array. 219 * @type string $login The user login. Default empty. 220 * @type array $login__in An array of logins to include. Users matching one of these 221 * logins will be included in results. Default empty array. 222 * @type array $login__not_in An array of logins to exclude. Users matching one of these 223 * logins will not be included in results. Default empty array. 206 224 * } 207 225 */ … … 277 295 } 278 296 297 // nicename 298 if ( '' !== $qv['nicename']) { 299 $this->query_where .= $this->db->prepare( ' AND user_nicename = %s', $qv['nicename'] ); 300 } 301 302 if ( ! empty( $qv['nicename__in'] ) ) { 303 $sanitized_nicename__in = array_map( 'esc_sql', $qv['nicename__in'] ); 304 $nicename__in = implode( "','", $sanitized_nicename__in ); 305 $this->query_where .= " AND user_nicename IN ( '$nicename__in' )"; 306 } 307 308 if ( ! empty( $qv['nicename__not_in'] ) ) { 309 $sanitized_nicename__not_in = array_map( 'esc_sql', $qv['nicename__not_in'] ); 310 $nicename__not_in = implode( "','", $sanitized_nicename__not_in ); 311 $this->query_where .= " AND user_nicename NOT IN ( '$nicename__not_in' )"; 312 } 313 314 // login 315 if ( '' !== $qv['login']) { 316 $this->query_where .= $this->db->prepare( ' AND user_login = %s', $qv['login'] ); 317 } 318 319 if ( ! empty( $qv['login__in'] ) ) { 320 $sanitized_login__in = array_map( 'esc_sql', $qv['login__in'] ); 321 $login__in = implode( "','", $sanitized_login__in ); 322 $this->query_where .= " AND user_login IN ( '$login__in' )"; 323 } 324 325 if ( ! empty( $qv['login__not_in'] ) ) { 326 $sanitized_login__not_in = array_map( 'esc_sql', $qv['login__not_in'] ); 327 $login__not_in = implode( "','", $sanitized_login__not_in ); 328 $this->query_where .= " AND user_login NOT IN ( '$login__not_in' )"; 329 } 330 279 331 // Meta query. 280 332 $this->meta_query = new WP_Meta_Query(); … … 435 487 } 436 488 437 $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); 489 if ( 'nicename__in' === $_orderby || 'login__in' === $_orderby ) { 490 $orderby_array[] = $parsed; 491 } else { 492 $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); 493 } 438 494 } 439 495 … … 701 757 $include_sql = implode( ',', $include ); 702 758 $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )"; 759 } elseif ( 'nicename__in' === $orderby ) { 760 $sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] ); 761 $nicename__in = implode( "','", $sanitized_nicename__in ); 762 $_orderby = "FIELD( user_nicename, '$nicename__in' )"; 763 } elseif ( 'login__in' === $orderby ) { 764 $sanitized_login__in = array_map( 'esc_sql', $this->query_vars['login__in'] ); 765 $login__in = implode( "','", $sanitized_login__in ); 766 $_orderby = "FIELD( user_login, '$login__in' )"; 703 767 } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) { 704 768 $meta_clause = $meta_query_clauses[ $orderby ]; -
trunk/tests/phpunit/tests/user/query.php
r38398 r38715 879 879 880 880 /** 881 * @ticket 36624 882 */ 883 public function test_nicename_returns_user_with_nicename() { 884 wp_update_user( array( 885 'ID' => self::$author_ids[0], 886 'user_nicename' => 'peter' 887 ) ); 888 889 $q = new WP_User_Query( array ( 890 'nicename' => 'peter' 891 ) ); 892 893 $found = wp_list_pluck( $q->get_results(), 'ID' ); 894 $expected = array( self::$author_ids[0] ); 895 896 $this->assertContains( "AND user_nicename = 'peter'", $q->query_where); 897 $this->assertEqualSets( $expected, $found); 898 } 899 900 /** 901 * @ticket 36624 902 */ 903 public function test_nicename__in_returns_users_with_included_nicenames() { 904 wp_update_user( array( 905 'ID' => self::$author_ids[0], 906 'user_nicename' => 'peter' 907 ) ); 908 909 wp_update_user( array( 910 'ID' => self::$author_ids[1], 911 'user_nicename' => 'paul' 912 ) ); 913 914 wp_update_user( array( 915 'ID' => self::$author_ids[2], 916 'user_nicename' => 'mary' 917 ) ); 918 919 $q = new WP_User_Query( array ( 920 'nicename__in' => array( 'peter', 'paul', 'mary' ) 921 ) ); 922 923 $found = wp_list_pluck( $q->get_results(), 'ID' ); 924 $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] ); 925 926 $this->assertContains( "AND user_nicename IN ( 'peter','paul','mary' )", $q->query_where); 927 $this->assertEqualSets( $expected, $found ); 928 } 929 930 /** 931 * @ticket 36624 932 */ 933 public function test_nicename__not_in_returns_users_without_included_nicenames() { 934 wp_update_user( array( 935 'ID' => self::$author_ids[0], 936 'user_nicename' => 'peter' 937 ) ); 938 939 wp_update_user( array( 940 'ID' => self::$author_ids[1], 941 'user_nicename' => 'paul' 942 ) ); 943 944 wp_update_user( array( 945 'ID' => self::$author_ids[2], 946 'user_nicename' => 'mary' 947 ) ); 948 949 $q = new WP_User_Query( array ( 950 'nicename__not_in' => array( 'peter', 'paul', 'mary' ) 951 ) ); 952 953 $foundCount = count($q->get_results()); 954 $expectedCount = 10; // 13 total users minus 3 from query 955 956 $this->assertContains( "AND user_nicename NOT IN ( 'peter','paul','mary' )", $q->query_where); 957 $this->assertEquals( $expectedCount, $foundCount ); 958 } 959 960 /** 961 * @ticket 36624 962 */ 963 public function test_orderby_nicename__in() { 964 wp_update_user( array( 965 'ID' => self::$author_ids[0], 966 'user_nicename' => 'peter' 967 ) ); 968 969 wp_update_user( array( 970 'ID' => self::$author_ids[1], 971 'user_nicename' => 'paul' 972 ) ); 973 974 wp_update_user( array( 975 'ID' => self::$author_ids[2], 976 'user_nicename' => 'mary' 977 ) ); 978 979 $q = new WP_User_Query( array ( 980 'nicename__in' => array( 'mary', 'peter', 'paul' ), 981 'orderby' => 'nicename__in' 982 ) ); 983 984 $found = wp_list_pluck( $q->get_results(), 'ID' ); 985 $expected = array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] ); 986 987 $this->assertContains( "FIELD( user_nicename, 'mary','peter','paul' )", $q->query_orderby); 988 $this->assertSame( $expected, $found ); 989 } 990 991 /** 992 * @ticket 36624 993 */ 994 public function test_login_returns_user_with_login() { 995 996 $user_login = get_userdata( self::$author_ids[0] )->user_login; 997 998 $q = new WP_User_Query( array ( 999 'login' => $user_login 1000 ) ); 1001 1002 $found = wp_list_pluck( $q->get_results(), 'ID' ); 1003 $expected = array( self::$author_ids[0] ); 1004 1005 $this->assertContains( "AND user_login = '$user_login'", $q->query_where); 1006 $this->assertEqualSets( $expected, $found); 1007 } 1008 1009 /** 1010 * @ticket 36624 1011 */ 1012 public function test_login__in_returns_users_with_included_logins() { 1013 $user_login1 = get_userdata( self::$author_ids[0] )->user_login; 1014 $user_login2 = get_userdata( self::$author_ids[1] )->user_login; 1015 $user_login3 = get_userdata( self::$author_ids[2] )->user_login; 1016 1017 $q = new WP_User_Query( array ( 1018 'login__in' => array( $user_login1, $user_login2, $user_login3 ) 1019 ) ); 1020 1021 $found = wp_list_pluck( $q->get_results(), 'ID' ); 1022 $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] ); 1023 1024 $this->assertContains( "AND user_login IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where); 1025 $this->assertEqualSets( $expected, $found ); 1026 } 1027 1028 /** 1029 * @ticket 36624 1030 */ 1031 public function test_login__not_in_returns_users_without_included_logins() { 1032 $user_login1 = get_userdata( self::$author_ids[0] )->user_login; 1033 $user_login2 = get_userdata( self::$author_ids[1] )->user_login; 1034 $user_login3 = get_userdata( self::$author_ids[2] )->user_login; 1035 1036 $q = new WP_User_Query( array ( 1037 'login__not_in' => array( $user_login1, $user_login2, $user_login3 ) 1038 ) ); 1039 1040 $foundCount = count($q->get_results()); 1041 $expectedCount = 10; // 13 total users minus 3 from query 1042 1043 $this->assertContains( "AND user_login NOT IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where); 1044 $this->assertEquals( $expectedCount, $foundCount ); 1045 } 1046 1047 /** 1048 * @ticket 36624 1049 */ 1050 public function test_orderby_login__in() { 1051 $user_login1 = get_userdata( self::$author_ids[0] )->user_login; 1052 $user_login2 = get_userdata( self::$author_ids[1] )->user_login; 1053 $user_login3 = get_userdata( self::$author_ids[2] )->user_login; 1054 1055 $q = new WP_User_Query( array ( 1056 'login__in' => array( $user_login2, $user_login3, $user_login1 ), 1057 'orderby' => 'login__in' 1058 ) ); 1059 1060 $found = wp_list_pluck( $q->get_results(), 'ID' ); 1061 $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ); 1062 1063 $this->assertContains( "FIELD( user_login, '$user_login2','$user_login3','$user_login1' )", $q->query_orderby); 1064 $this->assertSame( $expected, $found ); 1065 } 1066 1067 /** 881 1068 * @ticket 25145 882 1069 */
Note: See TracChangeset
for help on using the changeset viewer.