Ticket #36624: 36624.2.patch
File 36624.2.patch, 12.7 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-user-query.php
124 124 'fields' => 'all', 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 129 135 return wp_parse_args( $args, $defaults ); … … 140 146 * @since 4.4.0 Added 'paged', 'role__in', and 'role__not_in' parameters. The 'role' parameter was updated to 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', and 'login__not_in' parameters. 143 150 * 144 151 * @access public 145 152 * … … 173 180 * an array of values, or a multi-dimensional array with fields as 174 181 * keys and orders ('ASC' or 'DESC') as values. Accepted values are 175 182 * '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'.183 * (or 'login'), 'login__in', 'user_nicename' (or 'nicename'), 184 * 'nicename__in', 'user_email (or 'email'), 'user_url' (or 'url'), 185 * 'user_registered' (or 'registered'), 'post_count', 'meta_value', 186 * 'meta_value_num', the value of `$meta_key`, or an array key of 187 * `$meta_query`. To use 'meta_value' or 'meta_value_num', `$meta_key` 188 * must be also be defined. Default 'user_login'. 182 189 * @type string $order Designates ascending or descending order of users. Order values 183 190 * passed as part of an `$orderby` array take precedence over this 184 191 * parameter. Accepts 'ASC', 'DESC'. Default 'ASC'. … … 203 210 * @type bool|array $has_published_posts Pass an array of post types to filter results to users who have 204 211 * published posts in those post types. `true` is an alias for all 205 212 * public post types. 213 * @type string $nicename The user nicename. Default empty. 214 * @type array $nicename__in An array of nicenames to include. Users matching one of these 215 * nicenames will be included in results. Default empty array. 216 * @type array $nicename__not_in An array of nicenames to exclude. Users matching one of these 217 * nicenames will not be included in results. Default empty array. 218 * @type string $login The user login. Default empty. 219 * @type array $login__in An array of logins to include. Users matching one of these 220 * logins will be included in results. Default empty array. 221 * @type array $login__not_in An array of logins to exclude. Users matching one of these 222 * logins will not be included in results. Default empty array. 206 223 * } 207 224 */ 208 225 public function prepare_query( $query = array() ) { … … 276 293 $this->query_where .= " AND {$this->db->users}.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )"; 277 294 } 278 295 296 // nicename 297 if ( '' !== $qv['nicename']) { 298 $this->query_where .= $this->db->prepare( ' AND user_nicename = %s', $qv['nicename'] ); 299 } 300 301 if ( ! empty( $qv['nicename__in'] ) ) { 302 $sanitized_nicename__in = array_map( 'esc_sql', $qv['nicename__in'] ); 303 $nicename__in = implode( "','", $sanitized_nicename__in ); 304 $this->query_where .= " AND user_nicename IN ( '$nicename__in' )"; 305 } 306 307 if ( ! empty( $qv['nicename__not_in'] ) ) { 308 $sanitized_nicename__not_in = array_map( 'esc_sql', $qv['nicename__not_in'] ); 309 $nicename__not_in = implode( "','", $sanitized_nicename__not_in ); 310 $this->query_where .= " AND user_nicename NOT IN ( '$nicename__not_in' )"; 311 } 312 313 // login 314 if ( '' !== $qv['login']) { 315 $this->query_where .= $this->db->prepare( ' AND user_login = %s', $qv['login'] ); 316 } 317 318 if ( ! empty( $qv['login__in'] ) ) { 319 $sanitized_login__in = array_map( 'esc_sql', $qv['login__in'] ); 320 $login__in = implode( "','", $sanitized_login__in ); 321 $this->query_where .= " AND user_login IN ( '$login__in' )"; 322 } 323 324 if ( ! empty( $qv['login__not_in'] ) ) { 325 $sanitized_login__not_in = array_map( 'esc_sql', $qv['login__not_in'] ); 326 $login__not_in = implode( "','", $sanitized_login__not_in ); 327 $this->query_where .= " AND user_login NOT IN ( '$login__not_in' )"; 328 } 329 279 330 // Meta query. 280 331 $this->meta_query = new WP_Meta_Query(); 281 332 $this->meta_query->parse_query_vars( $qv ); … … 434 485 continue; 435 486 } 436 487 488 if ( 'nicename__in' === $_orderby || 'login__in' === $_orderby) { 489 $orderby_array[] = $parsed; 490 continue; 491 } 492 437 493 $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); 438 494 } 439 495 … … 700 756 $include = wp_parse_id_list( $this->query_vars['include'] ); 701 757 $include_sql = implode( ',', $include ); 702 758 $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )"; 703 } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) { 759 } elseif ( 'nicename__in' === $orderby ) { 760 $nicename__in = implode( "','", $this->query_vars['nicename__in']); 761 $_orderby = "FIELD( user_nicename, '$nicename__in' )"; 762 } elseif ( 'login__in' === $orderby ) { 763 $login__in = implode( "','", $this->query_vars['login__in']); 764 $_orderby = "FIELD( user_login, '$login__in' )"; 765 } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) { 704 766 $meta_clause = $meta_query_clauses[ $orderby ]; 705 767 $_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) ); 706 768 } -
tests/phpunit/tests/user/query.php
878 878 } 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 */ 883 1070 public function test_paged() {