Changeset 53362
- Timestamp:
- 05/07/2022 03:30:51 AM (2 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-user-query.php
r53327 r53362 231 231 * - 'user_url' 232 232 * - 'user_registered' 233 * - 'user_pass' 234 * - 'user_activation_key' 235 * - 'user_status' 236 * - 'spam' (only available on multisite installs) 237 * - 'deleted' (only available on multisite installs) 233 238 * - 'all' for all fields 234 239 * - 'all_with_meta' to include meta fields. … … 276 281 277 282 $allowed_fields = array( 278 'ID', 279 'display_name', 283 'id', 280 284 'user_login', 285 'user_pass', 281 286 'user_nicename', 282 287 'user_email', 283 288 'user_url', 284 289 'user_registered', 290 'user_activation_key', 291 'user_status', 292 'display_name', 285 293 ); 294 if ( is_multisite() ) { 295 $allowed_fields[] = 'spam'; 296 $allowed_fields[] = 'deleted'; 297 } 286 298 287 299 if ( is_array( $qv['fields'] ) ) { 300 $qv['fields'] = array_map( 'strtolower', $qv['fields'] ); 288 301 $qv['fields'] = array_intersect( array_unique( $qv['fields'] ), $allowed_fields ); 289 302 290 303 if ( empty( $qv['fields'] ) ) { 291 $qv['fields'] = array( ' ID' );304 $qv['fields'] = array( 'id' ); 292 305 } 293 306 294 307 $this->query_fields = array(); 295 308 foreach ( $qv['fields'] as $field ) { 296 $field = ' ID' === $field ? 'ID' : sanitize_key( $field );309 $field = 'id' === $field ? 'ID' : sanitize_key( $field ); 297 310 $this->query_fields[] = "$wpdb->users.$field"; 298 311 } … … 303 316 $this->query_fields = "$wpdb->users.ID"; 304 317 } else { 305 $field = ' ID' === $qv['fields']? 'ID' : sanitize_key( $qv['fields'] );318 $field = 'id' === strtolower( $qv['fields'] ) ? 'ID' : sanitize_key( $qv['fields'] ); 306 319 $this->query_fields = "$wpdb->users.$field"; 307 320 } … … 823 836 return; 824 837 } 825 826 if ( 'all_with_meta' === $qv['fields'] ) { 838 if ( 839 is_array( $qv['fields'] ) && 840 isset( $this->results[0]->ID ) 841 ) { 842 foreach ( $this->results as $result ) { 843 $result->id = $result->ID; 844 } 845 } elseif ( 'all_with_meta' === $qv['fields'] ) { 827 846 cache_users( $this->results ); 828 847 -
trunk/tests/phpunit/tests/user/query.php
r53331 r53362 1970 1970 /** 1971 1971 * @ticket 53177 1972 * @dataProvider data_returning_field s1972 * @dataProvider data_returning_field_subset_as_string 1973 1973 * 1974 * @covers WP_User_Query::prepare_query 1975 * 1976 * @param $field 1977 * @param $expected 1978 */ 1979 public function test_returning_fields( $field, $expected_values ) { 1974 * @param string $field 1975 * @param mixed $expected 1976 */ 1977 public function test_returning_field_subset_as_string( $field, $expected ) { 1980 1978 $q = new WP_User_Query( 1981 1979 array( … … 1986 1984 $results = $q->get_results(); 1987 1985 1988 if ( 'all_with_meta' === $field ) { 1989 $data = array_shift( $results )->data; 1990 1991 } else { 1992 $data = ( isset( $results[0]->data ) ) ? $results[0]->data : $results[0]; 1993 } 1994 1995 foreach ( $expected_values as $key => $expected_value ) { 1996 if ( ! is_array( $results ) ) { 1997 $this->assertEquals( array_shift( $results ), $expected_value ); 1998 } else { 1999 $value = ( isset( $data->$key ) ) ? $data->$key : $data; 2000 $this->assertEquals( $value, $expected_value ); 2001 } 2002 } 2003 } 2004 2005 public function data_returning_fields() { 2006 return array( 2007 'all' => array( 2008 'field' => 'all', 1986 $this->assertSameSets( $expected, $results ); 1987 } 1988 1989 /** 1990 * Data provider 1991 * 1992 * @return array 1993 */ 1994 function data_returning_field_subset_as_string() { 1995 $data = array( 1996 'id' => array( 1997 'fields' => 'id', 1998 'expected' => array( '1' ), 1999 ), 2000 'ID' => array( 2001 'fields' => 'ID', 2002 'expected' => array( '1' ), 2003 ), 2004 'user_login' => array( 2005 'fields' => 'user_login', 2006 'expected' => array( 'admin' ), 2007 ), 2008 'user_nicename' => array( 2009 'fields' => 'user_nicename', 2010 'expected' => array( 'admin' ), 2011 ), 2012 'user_email' => array( 2013 'fields' => 'user_email', 2014 'expected' => array( WP_TESTS_EMAIL ), 2015 ), 2016 'user_url' => array( 2017 'fields' => 'user_url', 2018 'expected' => array( wp_guess_url() ), 2019 ), 2020 'user_status' => array( 2021 'fields' => 'user_status', 2022 'expected' => array( '0' ), 2023 ), 2024 'display_name' => array( 2025 'fields' => 'display_name', 2026 'expected' => array( 'admin' ), 2027 ), 2028 'invalid_field' => array( 2029 'fields' => 'invalid_field', 2030 'expected' => array( '1' ), 2031 ), 2032 ); 2033 2034 if ( is_multisite() ) { 2035 $data['spam'] = array( 2036 'fields' => 'spam', 2037 'expected' => array( '0' ), 2038 ); 2039 $data['deleted'] = array( 2040 'fields' => 'deleted', 2041 'expected' => array( '0' ), 2042 ); 2043 } 2044 2045 return $data; 2046 } 2047 2048 /** 2049 * @ticket 53177 2050 * @dataProvider data_returning_field_subset_as_array 2051 * 2052 * @param array $field 2053 * @param mixed $expected 2054 */ 2055 public function test_returning_field_subset_as_array( $field, $expected ) { 2056 $q = new WP_User_Query( 2057 array( 2058 'fields' => $field, 2059 'include' => array( '1' ), 2060 ) 2061 ); 2062 $results = $q->get_results(); 2063 2064 if ( isset( $results[0] ) && is_object( $results[0] ) ) { 2065 $results = (array) $results[0]; 2066 } 2067 2068 $this->assertSameSetsWithIndex( $expected, $results ); 2069 } 2070 2071 /** 2072 * Data provider 2073 * 2074 * @return array 2075 */ 2076 function data_returning_field_subset_as_array() { 2077 $data = array( 2078 'id' => array( 2079 'fields' => array( 'id' ), 2009 2080 'expected' => array( 2010 'ID' => '1', 2011 'user_login' => 'admin', 2012 'user_nicename' => 'admin', 2013 'user_email' => WP_TESTS_EMAIL, 2014 'user_url' => wp_guess_url(), 2015 'user_activation_key' => '', 2016 'user_status' => '0', 2017 'display_name' => 'admin', 2018 ), 2019 ), 2020 'all_with_meta' => array( 2021 'field' => 'all_with_meta', 2022 'expected' => array( 2023 'ID' => '1', 2024 'user_login' => 'admin', 2025 'user_nicename' => 'admin', 2026 'user_email' => WP_TESTS_EMAIL, 2027 'user_url' => wp_guess_url(), 2028 'user_activation_key' => '', 2029 'user_status' => '0', 2030 'display_name' => 'admin', 2031 ), 2032 ), 2033 'ID' => array( 2034 'field' => 'ID', 2035 'expected' => array( 2081 'id' => '1', 2036 2082 'ID' => '1', 2037 2083 ), 2038 2084 ), 2039 ' id'=> array(2040 'field ' => 'id',2085 'ID' => array( 2086 'fields' => array( 'ID' ), 2041 2087 'expected' => array( 2042 2088 'ID' => '1', 2089 'id' => '1', 2043 2090 ), 2044 2091 ), 2045 'display_name' => array( 2046 'field' => 'display_name', 2092 'user_login' => array( 2093 'fields' => array( 'user_login' ), 2094 'expected' => array( 'user_login' => 'admin' ), 2095 ), 2096 'user_nicename' => array( 2097 'fields' => array( 'user_nicename' ), 2098 'expected' => array( 'user_nicename' => 'admin' ), 2099 ), 2100 'user_email' => array( 2101 'fields' => array( 'user_email' ), 2102 'expected' => array( 'user_email' => WP_TESTS_EMAIL ), 2103 ), 2104 'user_url' => array( 2105 'fields' => array( 'user_url' ), 2106 'expected' => array( 'user_url' => wp_guess_url() ), 2107 ), 2108 'user_status' => array( 2109 'fields' => array( 'user_status' ), 2110 'expected' => array( 'user_status' => '0' ), 2111 ), 2112 'display_name' => array( 2113 'fields' => array( 'display_name' ), 2114 'expected' => array( 'display_name' => 'admin' ), 2115 ), 2116 'invalid_field' => array( 2117 'fields' => array( 'invalid_field' ), 2118 'expected' => array( 2119 'ID' => '1', 2120 'id' => '1', 2121 ), 2122 ), 2123 'valid array inc id' => array( 2124 'fields' => array( 'display_name', 'user_email', 'id' ), 2047 2125 'expected' => array( 2048 2126 'display_name' => 'admin', 2127 'user_email' => WP_TESTS_EMAIL, 2128 'id' => '1', 2129 'ID' => '1', 2049 2130 ), 2050 2131 ), 2051 ' user_login'=> array(2052 'field ' => 'user_login',2132 'valid array inc ID' => array( 2133 'fields' => array( 'display_name', 'user_email', 'ID' ), 2053 2134 'expected' => array( 2054 'user_login' => 'admin', 2135 'display_name' => 'admin', 2136 'user_email' => WP_TESTS_EMAIL, 2137 'id' => '1', 2138 'ID' => '1', 2055 2139 ), 2056 2140 ), 2057 'user_nicename' => array( 2058 'field' => 'user_nicename', 2059 'expected' => array( 2060 'user_nicename' => 'admin', 2061 ), 2141 'partly valid array' => array( 2142 'fields' => array( 'display_name', 'invalid_field' ), 2143 'expected' => array( 'display_name' => 'admin' ), 2062 2144 ), 2063 'user_email' => array( 2064 'field' => 'user_email', 2065 'expected' => array( 2066 'user_email' => WP_TESTS_EMAIL, 2067 ), 2068 ), 2069 'invalid_field' => array( 2070 'field' => 'invalid_field', 2071 'expected' => array( 2072 '0' => '1', 2073 ), 2074 ), 2075 'valid_array' => array( 2076 'field' => array( 'ID', 'display_name' ), 2077 'expected' => array( 2078 'ID' => '1', 2079 'display_name' => 'admin', 2080 ), 2081 ), 2082 'semivalid_array' => array( 2083 'field' => array( 'ID', 'display_name', 'invalid_field' ), 2084 'expected' => array( 2085 'ID' => '1', 2086 'display_name' => 'admin', 2087 ), 2088 ), 2089 'invalid_array' => array( 2090 'field' => array( 'invalid_field' ), 2091 'expected' => array( 2092 'ID' => '1', 2093 ), 2094 ), 2095 ); 2145 ); 2146 2147 if ( is_multisite() ) { 2148 $data['spam'] = array( 2149 'fields' => array( 'spam' ), 2150 'expected' => array( 'spam' => '0' ), 2151 ); 2152 $data['deleted'] = array( 2153 'fields' => array( 'deleted' ), 2154 'expected' => array( 'deleted' => '0' ), 2155 ); 2156 } 2157 2158 return $data; 2159 } 2160 2161 /** 2162 * @ticket 53177 2163 */ 2164 public function test_returning_field_all() { 2165 $q = new WP_User_Query( 2166 array( 2167 'fields' => 'all', 2168 'include' => array( '1' ), 2169 ) 2170 ); 2171 $results = $q->get_results(); 2172 $user_data = (array) $results[0]->data; 2173 2174 $expected_results = array( 2175 'ID' => '1', 2176 'user_login' => 'admin', 2177 'user_nicename' => 'admin', 2178 'user_url' => wp_guess_url(), 2179 'user_email' => WP_TESTS_EMAIL, 2180 'user_activation_key' => '', 2181 'user_status' => '0', 2182 'display_name' => 'admin', 2183 ); 2184 2185 if ( is_multisite() ) { 2186 $expected_results['spam'] = '0'; 2187 $expected_results['deleted'] = '0'; 2188 } 2189 2190 // These change for each run. 2191 unset( $user_data['user_pass'], $user_data['user_registered'] ); 2192 2193 $this->assertSameSetsWithIndex( $expected_results, $user_data ); 2194 $this->assertInstanceOf( 'WP_User', $results[0] ); 2096 2195 } 2097 2196
Note: See TracChangeset
for help on using the changeset viewer.