[wp-trac] [WordPress Trac] #62003: User_Query cache triggers fatal error by ignoring 'fields' query var
WordPress Trac
noreply at wordpress.org
Fri Sep 6 20:40:11 UTC 2024
#62003: User_Query cache triggers fatal error by ignoring 'fields' query var
--------------------------+-----------------------------
Reporter: sermitr | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Users | Version: 6.6.1
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
Method `WP_User_Query::query()` generates different results depending on
whether `$this->query_vars['fields']` an array (array of objects) or a
string (array of scalar values).
However, if it contains only one field, the generated SQL query will be
the same.
Function `generate_cache_key()` doesn't take into account the `fields`
query var, and relies on the generated SQL query instead. This leads to
the unpredictability of the returned data format (array of objects or
array of scalar values).
How to reproduce:
1. Clean the cache: `wp cache flush`.
2. Fetch the users passing `fields` as an array:
{{{#!php
<?php
$wp_user_search = new WP_User_Query( array( 'fields' => array( 'ID' ),
'count_total' => false ) );
$users = $wp_user_search->get_results();
var_dump( $users );
// Result is an array of objects, which is now cached: array(1) { [0]=>
object(stdClass)#1268 (2) { ["ID"]=> string(1) "1" ["id"]=> string(1) "1"
} }
}}}
3. Run the same request with the `fields` being a string: `'fields' =>
'ID'`. The result will be an array of objects pulled from cache, although
array of strings is expected.
3. Fetch the users by CLI command: `wp user list`. The result is expected
to be an array of strings, so the fatal error is triggered:
{{{
Fatal error: Uncaught TypeError: Illegal offset type in wp-includes/class-
wp-user-query.php:892
}}}
4. Clean the cache: `wp cache flush`
5. Run the request from step 3, where `fields` is a string:
{{{#!php
<?php
$wp_user_search = new WP_User_Query(array('fields' => 'ID', 'count_total'
=> false));
$users = $wp_user_search->get_results();
var_dump( $users);
// Result is an array of strings, which is now cached: array(1) { [0]=>
string(1) "1" }
}}}
6. Run the request from step 2, where `fields` is an array. The result be
the same array of strings pulled from cache, which is different from what
we had on step 2.
7. Run the CLI command `wp user list`. There will be no fatal error,
because the data pulled from cache is an array of strings as expected.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/62003>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list