<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[55657] trunk: Users: Cache database queries within `WP_User_Query` class.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/55657">55657</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/55657","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spacedmonkey</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-04-18 11:48:46 +0000 (Tue, 18 Apr 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Users: Cache database queries within `WP_User_Query` class.
Cache the results of database queries within `WP_User_Query` class. Only cache queries that are requesting 3 or less fields so that caches are not storing full user objects. Cache results are stored in a new global cache group named `users-queries`. Add a new parameter to `WP_User_Query` called `cache_results` to allow developers to opt out of a receiving cached results. `cache_results` parameter defaults to true. Also add a new helper function called `wp_cache_set_users_last_changed`, similar to `wp_cache_set_posts_last_changed` that incroments last changed value in cache group `users`. Ensure that `wp_cache_set_users_last_changed` is called whenever user / user meta is modified for proper cache invalidation.
Props johnjamesjacoby, spacedmonkey, westi, dd32, strategio, srikanthmeenakshi, OllieJones, khoipro, rjasdfiii, flixos90, mukesh27, peterwilsoncc.
Fixes <a href="https://core.trac.wordpress.org/ticket/40613">#40613</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpuserqueryphp">trunk/src/wp-includes/class-wp-user-query.php</a></li>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludesloadphp">trunk/src/wp-includes/load.php</a></li>
<li><a href="#trunksrcwpincludesmsblogsphp">trunk/src/wp-includes/ms-blogs.php</a></li>
<li><a href="#trunksrcwpincludesmsfunctionsphp">trunk/src/wp-includes/ms-functions.php</a></li>
<li><a href="#trunksrcwpincludesuserphp">trunk/src/wp-includes/user.php</a></li>
<li><a href="#trunktestsphpunitincludesabstracttestcasephp">trunk/tests/phpunit/includes/abstract-testcase.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsuserqueryCachephp">trunk/tests/phpunit/tests/user/queryCache.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpuserqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-user-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-user-query.php 2023-04-18 08:35:11 UTC (rev 55656)
+++ trunk/src/wp-includes/class-wp-user-query.php 2023-04-18 11:48:46 UTC (rev 55657)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -119,6 +119,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'login' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'login__in' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'login__not_in' => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'cache_results' => true,
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return wp_parse_args( $args, $defaults );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -140,6 +141,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.1.0 Introduced the 'meta_compare_key' parameter.
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.3.0 Introduced the 'meta_type_key' parameter.
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.9.0 Added 'capability', 'capability__in', and 'capability__not_in' parameters.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.3.0 Added 'cache_results' parameter.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="cx" style="display: block; padding: 0 10px"> * @global WP_Roles $wp_roles WordPress role management object.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -254,6 +256,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * logins will be included in results. Default empty array.
</span><span class="cx" style="display: block; padding: 0 10px"> * @type string[] $login__not_in An array of logins to exclude. Users matching one of these
</span><span class="cx" style="display: block; padding: 0 10px"> * logins will not be included in results. Default empty array.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type bool $cache_results Whether to cache user information. Default true.
</ins><span class="cx" style="display: block; padding: 0 10px"> * }
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function prepare_query( $query = array() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -790,6 +793,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $qv =& $this->query_vars;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Do not cache results if more than 3 fields are requested.
+ if ( is_array( $qv['fields'] ) && count( $qv['fields'] ) > 3 ) {
+ $qv['cache_results'] = false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the users array before the query takes place.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -816,28 +824,47 @@
</span><span class="cx" style="display: block; padding: 0 10px"> {$this->query_orderby}
</span><span class="cx" style="display: block; padding: 0 10px"> {$this->query_limit}
</span><span class="cx" style="display: block; padding: 0 10px"> ";
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- if ( is_array( $qv['fields'] ) ) {
- $this->results = $wpdb->get_results( $this->request );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $cache_value = false;
+ $cache_key = $this->generate_cache_key( $qv, $this->request );
+ $cache_group = 'users-queries';
+ if ( $qv['cache_results'] ) {
+ $cache_value = wp_cache_get( $cache_key, $cache_group );
+ }
+ if ( false !== $cache_value ) {
+ $this->results = $cache_value['user_data'];
+ $this->total_users = $cache_value['total_users'];
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->results = $wpdb->get_col( $this->request );
- }
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
- /**
- * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance.
- *
- * @since 3.2.0
- * @since 5.1.0 Added the `$this` parameter.
- *
- * @global wpdb $wpdb WordPress database abstraction object.
- *
- * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
- * @param WP_User_Query $query The current WP_User_Query instance.
- */
- $found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_array( $qv['fields'] ) ) {
+ $this->results = $wpdb->get_results( $this->request );
+ } else {
+ $this->results = $wpdb->get_col( $this->request );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->total_users = (int) $wpdb->get_var( $found_users_query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
+ /**
+ * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance.
+ *
+ * @since 3.2.0
+ * @since 5.1.0 Added the `$this` parameter.
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
+ * @param WP_User_Query $query The current WP_User_Query instance.
+ */
+ $found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this );
+
+ $this->total_users = (int) $wpdb->get_var( $found_users_query );
+ }
+
+ if ( $qv['cache_results'] ) {
+ $cache_value = array(
+ 'user_data' => $this->results,
+ 'total_users' => $this->total_users,
+ );
+ wp_cache_add( $cache_key, $cache_value, $cache_group );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1011,6 +1038,54 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Generate cache key.
+ *
+ * @since 6.3.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param array $args Query arguments.
+ * @param string $sql SQL statement.
+ * @return string Cache key.
+ */
+ protected function generate_cache_key( array $args, $sql ) {
+ global $wpdb;
+
+ // Replace wpdb placeholder in the SQL statement used by the cache key.
+ $sql = $wpdb->remove_placeholder_escape( $sql );
+
+ $key = md5( $sql );
+ $last_changed = wp_cache_get_last_changed( 'users' );
+
+ if ( empty( $args['orderby'] ) ) {
+ // Default order is by 'user_login'.
+ $ordersby = array( 'user_login' => '' );
+ } elseif ( is_array( $args['orderby'] ) ) {
+ $ordersby = $args['orderby'];
+ } else {
+ // 'orderby' values may be a comma- or space-separated list.
+ $ordersby = preg_split( '/[,\s]+/', $args['orderby'] );
+ }
+
+ $blog_id = 0;
+ if ( isset( $args['blog_id'] ) ) {
+ $blog_id = absint( $args['blog_id'] );
+ }
+ if ( ( $args['has_published_posts'] && $blog_id ) || in_array( 'post_count', $ordersby, true ) ) {
+ $switch = get_current_blog_id() !== $blog_id;
+ if ( $switch ) {
+ switch_to_blog( $blog_id );
+ }
+ $last_changed .= wp_cache_get_last_changed( 'posts' );
+ if ( $switch ) {
+ restore_current_blog();
+ }
+ }
+
+ return "get_users:$key:$last_changed";
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Parses an 'order' query variable and casts it to ASC or DESC as necessary.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.2.0
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2023-04-18 08:35:11 UTC (rev 55656)
+++ trunk/src/wp-includes/default-filters.php 2023-04-18 11:48:46 UTC (rev 55657)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -115,6 +115,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// User meta.
+add_action( 'added_user_meta', 'wp_cache_set_users_last_changed' );
+add_action( 'updated_user_meta', 'wp_cache_set_users_last_changed' );
+add_action( 'deleted_user_meta', 'wp_cache_set_users_last_changed' );
+add_action( 'add_user_role', 'wp_cache_set_users_last_changed' );
+add_action( 'set_user_role', 'wp_cache_set_users_last_changed' );
+add_action( 'remove_user_role', 'wp_cache_set_users_last_changed' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Term meta.
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'added_term_meta', 'wp_cache_set_terms_last_changed' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'updated_term_meta', 'wp_cache_set_terms_last_changed' );
</span></span></pre></div>
<a id="trunksrcwpincludesloadphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/load.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/load.php 2023-04-18 08:35:11 UTC (rev 55656)
+++ trunk/src/wp-includes/load.php 2023-04-18 11:48:46 UTC (rev 55657)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -776,6 +776,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'usermeta',
</span><span class="cx" style="display: block; padding: 0 10px"> 'user_meta',
</span><span class="cx" style="display: block; padding: 0 10px"> 'userslugs',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'users-queries',
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesmsblogsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-blogs.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-blogs.php 2023-04-18 08:35:11 UTC (rev 55656)
+++ trunk/src/wp-includes/ms-blogs.php 2023-04-18 11:48:46 UTC (rev 55657)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -573,6 +573,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'usermeta',
</span><span class="cx" style="display: block; padding: 0 10px"> 'user_meta',
</span><span class="cx" style="display: block; padding: 0 10px"> 'userslugs',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'users-queries',
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -666,6 +667,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'usermeta',
</span><span class="cx" style="display: block; padding: 0 10px"> 'user_meta',
</span><span class="cx" style="display: block; padding: 0 10px"> 'userslugs',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'users-queries',
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-functions.php 2023-04-18 08:35:11 UTC (rev 55656)
+++ trunk/src/wp-includes/ms-functions.php 2023-04-18 11:48:46 UTC (rev 55657)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -295,6 +295,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ clean_user_cache( $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> restore_current_blog();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span></span></pre></div>
<a id="trunksrcwpincludesuserphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/user.php 2023-04-18 08:35:11 UTC (rev 55656)
+++ trunk/src/wp-includes/user.php 2023-04-18 11:48:46 UTC (rev 55657)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1907,6 +1907,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( $user->ID, 'user_meta' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_cache_set_users_last_changed();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires immediately after the given user's cache is cleaned.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5016,3 +5017,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Sets the last changed time for the 'users' cache group.
+ *
+ * @since 6.3.0
+ */
+function wp_cache_set_users_last_changed() {
+ wp_cache_set( 'last_changed', microtime(), 'users' );
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesabstracttestcasephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/abstract-testcase.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/abstract-testcase.php 2023-04-18 08:35:11 UTC (rev 55656)
+++ trunk/tests/phpunit/includes/abstract-testcase.php 2023-04-18 11:48:46 UTC (rev 55657)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -412,6 +412,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'usermeta',
</span><span class="cx" style="display: block; padding: 0 10px"> 'user_meta',
</span><span class="cx" style="display: block; padding: 0 10px"> 'userslugs',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'users-queries',
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsuserqueryCachephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/user/queryCache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/queryCache.php (rev 0)
+++ trunk/tests/phpunit/tests/user/queryCache.php 2023-04-18 11:48:46 UTC (rev 55657)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,781 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Test WP_User Query, in wp-includes/user.php
+ *
+ * @group user
+ *
+ * @coversDefaultClass WP_User_Query
+ */
+class Tests_User_Query_Cache extends WP_UnitTestCase {
+ /**
+ * @var int[]
+ */
+ protected static $author_ids;
+
+ /**
+ * @var int[]
+ */
+ protected static $sub_ids;
+
+ /**
+ * @var int[]
+ */
+ protected static $editor_ids;
+
+ /**
+ * @var int[]
+ */
+ protected static $contrib_id;
+
+ /**
+ * @var int[]
+ */
+ protected static $admin_ids;
+
+ /**
+ * @var int[]
+ */
+ protected $user_id;
+
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$author_ids = $factory->user->create_many(
+ 4,
+ array(
+ 'role' => 'author',
+ )
+ );
+
+ self::$sub_ids = $factory->user->create_many(
+ 2,
+ array(
+ 'role' => 'subscriber',
+ )
+ );
+
+ self::$editor_ids = $factory->user->create_many(
+ 3,
+ array(
+ 'role' => 'editor',
+ )
+ );
+
+ self::$contrib_id = $factory->user->create(
+ array(
+ 'role' => 'contributor',
+ )
+ );
+
+ self::$admin_ids = $factory->user->create_many(
+ 2,
+ array(
+ 'role' => 'administrator',
+ )
+ );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_query_cache_different_count() {
+ $args = array(
+ 'count_total' => true,
+ );
+
+ $query1 = new WP_User_Query( $args );
+ $users1 = wp_list_pluck( $query1->get_results(), 'ID' );
+ $users_total1 = $query1->get_total();
+
+ $queries_before = get_num_queries();
+
+ $args = array(
+ 'count_total' => false,
+ );
+
+ $query2 = new WP_User_Query( $args );
+ $users2 = wp_list_pluck( $query2->get_results(), 'ID' );
+ $users_total2 = $query2->get_total();
+ $queries_after = get_num_queries();
+
+ $this->assertNotSame( $queries_before, $queries_after, 'Assert that the number of queries is not equal' );
+ $this->assertNotSame( $users_total1, $users_total2, 'Assert that totals do not match' );
+ $this->assertSameSets( $users1, $users2, 'Results of the query are expected to match.' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_query_cache_results() {
+ $args = array(
+ 'cache_results' => true,
+ );
+
+ $query1 = new WP_User_Query( $args );
+ $users1 = wp_list_pluck( $query1->get_results(), 'ID' );
+
+ $queries_before = get_num_queries();
+
+ $args = array(
+ 'cache_results' => false,
+ );
+
+ $query2 = new WP_User_Query( $args );
+ $users2 = wp_list_pluck( $query2->get_results(), 'ID' );
+ $queries_after = get_num_queries();
+
+ $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' );
+ $this->assertSameSets( $users1, $users2, 'Results of the query are expected to match.' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ * @expectedDeprecated WP_User_Query
+ */
+ public function test_query_cache_who() {
+ $args = array(
+ 'who' => 'authors',
+ 'fields' => array( 'ID' ),
+ );
+
+ $query1 = new WP_User_Query( $args );
+ $users1 = $query1->get_results();
+ $users_total1 = $query1->get_total();
+
+ $queries_before = get_num_queries();
+ $query2 = new WP_User_Query( $args );
+ $users2 = $query2->get_results();
+ $users_total2 = $query2->get_total();
+ $queries_after = get_num_queries();
+
+ $this->assertSame( $queries_before, $queries_after, 'No queries are expected run.' );
+ $this->assertSame( $users_total1, $users_total2, 'Number of users returned us expected to match.' );
+ $this->assertSameSets( $users1, $users2, 'Results of the query are expected to match.' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ * @dataProvider data_query_cache
+ * @param array $args Optional. See WP_User_Query::prepare_query()
+ */
+ public function test_query_cache( array $args ) {
+ $query1 = new WP_User_Query( $args );
+ $users1 = $query1->get_results();
+ $users_total1 = $query1->get_total();
+
+ $queries_before = get_num_queries();
+ $query2 = new WP_User_Query( $args );
+ $users2 = $query2->get_results();
+ $users_total2 = $query2->get_total();
+ $queries_after = get_num_queries();
+
+ $this->assertSame( 0, $queries_after - $queries_before, 'Assert that no queries are run' );
+ $this->assertSame( $users_total1, $users_total2, 'Assert that totals do match' );
+ $this->assertSameSets( $users1, $users2, 'Asset that results of query match' );
+ }
+
+ /**
+ * Data provider
+ *
+ * @return array
+ */
+ public function data_query_cache() {
+ $data = array(
+ 'id' => array(
+ 'args' => array( 'fields' => array( 'id' ) ),
+
+ ),
+ 'ID' => array(
+ 'args' => array( 'fields' => array( 'ID' ) ),
+ ),
+ 'user_login' => array(
+ 'args' => array( 'fields' => array( 'user_login' ) ),
+ ),
+ 'user_nicename' => array(
+ 'args' => array( 'fields' => array( 'user_nicename' ) ),
+ ),
+ 'user_email' => array(
+ 'args' => array( 'fields' => array( 'user_email' ) ),
+ ),
+ 'user_url' => array(
+ 'args' => array( 'fields' => array( 'user_url' ) ),
+ ),
+ 'user_status' => array(
+ 'args' => array( 'fields' => array( 'user_status' ) ),
+ ),
+ 'display_name' => array(
+ 'args' => array( 'fields' => array( 'display_name' ) ),
+ ),
+ 'invalid_field' => array(
+ 'args' => array( 'fields' => array( 'invalid_field' ) ),
+ ),
+ 'valid array inc id' => array(
+ 'args' => array( 'fields' => array( 'display_name', 'user_email', 'id' ) ),
+ ),
+ 'valid array inc ID' => array(
+ 'args' => array( 'fields' => array( 'display_name', 'user_email', 'ID' ) ),
+ ),
+ 'partly valid array' => array(
+ 'args' => array( 'fields' => array( 'display_name', 'invalid_field' ) ),
+ ),
+ 'orderby' => array(
+ 'args' => array(
+ 'fields' => array( 'ID' ),
+ 'orderby' => array( 'login', 'nicename' ),
+ ),
+ ),
+ 'meta query' => array(
+ 'args' => array(
+ 'fields' => array( 'ID' ),
+ 'meta_query' => array(
+ 'foo_key' => array(
+ 'key' => 'foo',
+ 'compare' => 'EXISTS',
+ ),
+ ),
+ 'orderby' => 'foo_key',
+ 'order' => 'DESC',
+ ),
+ ),
+ 'meta query LIKE' => array(
+ 'args' => array(
+ 'fields' => array( 'ID' ),
+ 'meta_query' => array(
+ array(
+ 'key' => 'foo',
+ 'value' => '00',
+ 'compare' => 'LIKE',
+ ),
+ ),
+ 'orderby' => 'foo_key',
+ 'order' => 'DESC',
+ ),
+ ),
+ 'published posts' => array(
+ 'args' => array(
+ 'has_published_posts' => true,
+ 'fields' => array( 'ID' ),
+ ),
+ ),
+ 'published posts order' => array(
+ 'args' => array(
+ 'orderby' => 'post_count',
+ 'fields' => array( 'ID' ),
+ ),
+ ),
+ 'published count_total' => array(
+ 'args' => array(
+
+ 'count_total' => false,
+ 'fields' => array( 'ID' ),
+ ),
+ ),
+ 'capability' => array(
+ 'args' => array(
+ 'capability' => 'install_plugins',
+ 'fields' => array( 'ID' ),
+ ),
+ ),
+ 'include' => array(
+ 'args' => array(
+ 'includes' => self::$author_ids,
+ 'fields' => array( 'ID' ),
+ ),
+ ),
+ 'exclude' => array(
+ 'args' => array(
+ 'exclude' => self::$author_ids,
+ 'fields' => array( 'ID' ),
+ ),
+ ),
+ 'search' => array(
+ 'args' => array(
+ 'search' => 'User',
+ 'fields' => array( 'ID' ),
+ ),
+ ),
+ );
+
+ if ( is_multisite() ) {
+ $data['spam'] = array(
+ 'args' => array( 'fields' => array( 'spam' ) ),
+ );
+ $data['deleted'] = array(
+ 'args' => array( 'fields' => array( 'deleted' ) ),
+ );
+ }
+
+ return $data;
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_query_cache_remove_user_role() {
+ $user_id = self::factory()->user->create( array( 'role' => 'author' ) );
+
+ $q1 = new WP_User_Query(
+ array(
+ 'role' => 'author',
+ )
+ );
+
+ $found = wp_list_pluck( $q1->get_results(), 'ID' );
+
+ $this->assertContains( $user_id, $found, 'Expected to find author in returned values.' );
+
+ $user = get_user_by( 'id', $user_id );
+ $user->remove_role( 'author' );
+
+ $q2 = new WP_User_Query(
+ array(
+ 'role' => 'author',
+ )
+ );
+
+ $found = wp_list_pluck( $q2->get_results(), 'ID' );
+ $this->assertNotContains( $user_id, $found, 'Expected not to find author in returned values.' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_query_cache_set_user_role() {
+ $user_id = self::factory()->user->create( array( 'role' => 'author' ) );
+
+ $q1 = new WP_User_Query(
+ array(
+ 'role' => 'author',
+ )
+ );
+
+ $found = wp_list_pluck( $q1->get_results(), 'ID' );
+
+ $this->assertContains( $user_id, $found, 'Expected to find author in returned values.' );
+
+ $user = get_user_by( 'id', $user_id );
+ $user->set_role( 'editor' );
+
+ $q2 = new WP_User_Query(
+ array(
+ 'role' => 'author',
+ )
+ );
+
+ $found = wp_list_pluck( $q2->get_results(), 'ID' );
+ $this->assertNotContains( $user_id, $found, 'Expected not to find author in returned values.' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_query_cache_delete_user() {
+ $user_id = self::factory()->user->create();
+
+ $q1 = new WP_User_Query(
+ array(
+ 'include' => array( $user_id ),
+ )
+ );
+
+ $found = wp_list_pluck( $q1->get_results(), 'ID' );
+ $expected = array( $user_id );
+
+ $this->assertSameSets( $expected, $found, 'Find author in returned values' );
+
+ wp_delete_user( $user_id );
+
+ $q2 = new WP_User_Query(
+ array(
+ 'include' => array( $user_id ),
+ )
+ );
+
+ $found = wp_list_pluck( $q2->get_results(), 'ID' );
+ $this->assertNotContains( $user_id, $found, 'Expected not to find author in returned values.' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_query_cache_do_not_cache() {
+ $user_id = self::factory()->user->create();
+
+ $args = array(
+ 'fields' => array(
+ 'user_login',
+ 'user_nicename',
+ 'user_email',
+ 'user_url',
+ 'user_status',
+ 'display_name',
+ ),
+ 'include' => array( $user_id ),
+ );
+
+ $q1 = new WP_User_Query( $args );
+ $found1 = $q1->get_results();
+ $callback = static function( $user ) {
+ return (array) $user;
+ };
+
+ $found1 = array_map( $callback, $found1 );
+
+ $queries_before = get_num_queries();
+ $q2 = new WP_User_Query( $args );
+ $found2 = $q2->get_results();
+ $found2 = array_map( $callback, $found2 );
+ $queries_after = get_num_queries();
+
+ $this->assertSame( $queries_after - $queries_before, 2, 'Ensure that query is not cached' );
+ $this->assertSameSets( $found1, $found2, 'Expected results to match.', 'Ensure that to results match' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_query_cache_update_user() {
+ $user_id = end( self::$admin_ids );
+
+ wp_update_user(
+ array(
+ 'ID' => $user_id,
+ 'user_nicename' => 'paul',
+ )
+ );
+
+ $args = array(
+ 'nicename__in' => array( 'paul' ),
+ );
+
+ $q1 = new WP_User_Query( $args );
+
+ $found = wp_list_pluck( $q1->get_results(), 'ID' );
+ $expected = array( $user_id );
+
+ $this->assertSameSets( $expected, $found, 'Find author in returned values' );
+
+ wp_update_user(
+ array(
+ 'ID' => $user_id,
+ 'user_nicename' => 'linda',
+ )
+ );
+
+ $q2 = new WP_User_Query( $args );
+
+ $found = wp_list_pluck( $q2->get_results(), 'ID' );
+ $this->assertNotContains( $user_id, $found, 'Expected not to find author in returned values.' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_query_cache_create_user() {
+ $user_id = end( self::$admin_ids );
+
+ $args = array( 'blog_id' => get_current_blog_id() );
+
+ $q1 = new WP_User_Query( $args );
+
+ $found = wp_list_pluck( $q1->get_results(), 'ID' );
+
+ $this->assertContains( $user_id, $found, 'Expected to find author in returned values.' );
+
+ $user_id_2 = self::factory()->user->create();
+
+ $q2 = new WP_User_Query( $args );
+
+ $found = wp_list_pluck( $q2->get_results(), 'ID' );
+ $this->assertContains( $user_id_2, $found, 'Find author in returned values' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_has_published_posts_delete_post() {
+ register_post_type( 'wptests_pt_public', array( 'public' => true ) );
+
+ $post_id = self::factory()->post->create(
+ array(
+ 'post_author' => self::$author_ids[2],
+ 'post_status' => 'publish',
+ 'post_type' => 'wptests_pt_public',
+ )
+ );
+
+ $q1 = new WP_User_Query(
+ array(
+ 'has_published_posts' => true,
+ )
+ );
+
+ $found = wp_list_pluck( $q1->get_results(), 'ID' );
+ $expected = array( self::$author_ids[2] );
+
+ $this->assertSameSets( $expected, $found, 'Find author in returned values' );
+
+ wp_delete_post( $post_id, true );
+
+ $q2 = new WP_User_Query(
+ array(
+ 'has_published_posts' => true,
+ )
+ );
+
+ $found = wp_list_pluck( $q2->get_results(), 'ID' );
+ $this->assertSameSets( array(), $found, 'Not to find author in returned values' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_has_published_posts_delete_post_order() {
+ register_post_type( 'wptests_pt_public', array( 'public' => true ) );
+
+ $user_id = self::factory()->user->create();
+
+ $post_id = self::factory()->post->create(
+ array(
+ 'post_author' => $user_id,
+ 'post_status' => 'publish',
+ 'post_type' => 'wptests_pt_public',
+ )
+ );
+
+ $q1 = new WP_User_Query(
+ array(
+ 'orderby' => 'post_count',
+ )
+ );
+
+ $found1 = wp_list_pluck( $q1->get_results(), 'ID' );
+ $this->assertContains( $user_id, $found1, 'Find author in returned values in first run of WP_User_Query' );
+
+ wp_delete_post( $post_id, true );
+
+ $q2 = new WP_User_Query(
+ array(
+ 'orderby' => 'post_count',
+ )
+ );
+
+ $found2 = wp_list_pluck( $q2->get_results(), 'ID' );
+ $this->assertContains( $user_id, $found1, 'Find author in returned values in second run of WP_User_Query' );
+ $this->assertSameSets( $found1, $found2, 'Not same order' );
+ }
+
+ /**
+ * @ticket 40613
+ * @covers ::query
+ */
+ public function test_meta_query_cache_invalidation() {
+ add_user_meta( self::$author_ids[0], 'foo', 'bar' );
+ add_user_meta( self::$author_ids[1], 'foo', 'bar' );
+
+ $q1 = new WP_User_Query(
+ array(
+ 'meta_query' => array(
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ ),
+ )
+ );
+
+ $found = wp_list_pluck( $q1->get_results(), 'ID' );
+ $expected = array( self::$author_ids[0], self::$author_ids[1] );
+
+ $this->assertSameSets( $expected, $found, 'Asset that results contain authors' );
+
+ delete_user_meta( self::$author_ids[1], 'foo' );
+
+ $q2 = new WP_User_Query(
+ array(
+ 'meta_query' => array(
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ ),
+ )
+ );
+
+ $found = wp_list_pluck( $q2->get_results(), 'ID' );
+ $expected = array( self::$author_ids[0] );
+
+ $this->assertSameSets( $expected, $found, 'Asset that results do not contain author without meta' );
+ }
+
+ /**
+ * @ticket 40613
+ * @group ms-required
+ * @covers ::query
+ */
+ public function test_get_single_capability_multisite_blog_id() {
+ $blog_id = self::factory()->blog->create();
+
+ add_user_to_blog( $blog_id, self::$author_ids[0], 'subscriber' );
+ add_user_to_blog( $blog_id, self::$author_ids[1], 'author' );
+ add_user_to_blog( $blog_id, self::$author_ids[2], 'editor' );
+
+ $q1 = new WP_User_Query(
+ array(
+ 'capability' => 'publish_posts',
+ 'blog_id' => $blog_id,
+ )
+ );
+
+ $found = wp_list_pluck( $q1->get_results(), 'ID' );
+
+ $this->assertNotContains( self::$author_ids[0], $found, 'Asset that results do not contain author 0 without capability on site on first run' );
+ $this->assertContains( self::$author_ids[1], $found, 'Asset that results do contain author with capability on site on first run' );
+ $this->assertContains( self::$author_ids[2], $found, 'Asset that results do contain author with capability on site on first run' );
+
+ remove_user_from_blog( self::$author_ids[2], $blog_id );
+
+ $q2 = new WP_User_Query(
+ array(
+ 'capability' => 'publish_posts',
+ 'blog_id' => $blog_id,
+ )
+ );
+
+ $found = wp_list_pluck( $q2->get_results(), 'ID' );
+ $this->assertNotContains( self::$author_ids[0], $found, 'Asset that results do not contain author 0 without capability on site on second run' );
+ $this->assertContains( self::$author_ids[1], $found, 'Asset that results do contain author with capability on site on second run' );
+ $this->assertNotContains( self::$author_ids[2], $found, 'Asset that results do not contain author 1 without capability on site on second run' );
+ }
+
+ /**
+ * @ticket 40613
+ * @group ms-required
+ * @covers ::query
+ */
+ public function test_query_should_respect_blog_id() {
+ $blogs = self::factory()->blog->create_many( 2 );
+
+ add_user_to_blog( $blogs[0], self::$author_ids[0], 'author' );
+ add_user_to_blog( $blogs[0], self::$author_ids[1], 'author' );
+ add_user_to_blog( $blogs[1], self::$author_ids[0], 'author' );
+ add_user_to_blog( $blogs[1], self::$author_ids[1], 'author' );
+ add_user_to_blog( $blogs[1], self::$author_ids[2], 'author' );
+
+ $q = new WP_User_Query(
+ array(
+ 'fields' => 'ids',
+ 'blog_id' => $blogs[0],
+ )
+ );
+
+ $expected = array( (string) self::$author_ids[0], (string) self::$author_ids[1] );
+
+ $this->assertSameSets( $expected, $q->get_results(), 'Asset that expected users return' );
+
+ $q = new WP_User_Query(
+ array(
+ 'fields' => 'ids',
+ 'blog_id' => $blogs[1],
+ )
+ );
+
+ $expected = array( (string) self::$author_ids[0], (string) self::$author_ids[1], (string) self::$author_ids[2] );
+
+ $this->assertSameSets( $expected, $q->get_results(), 'Asset that expected users return from different blog' );
+ }
+
+ /**
+ * @ticket 40613
+ * @group ms-required
+ * @covers ::query
+ */
+ public function test_has_published_posts_should_respect_blog_id() {
+ $blogs = self::factory()->blog->create_many( 2 );
+
+ add_user_to_blog( $blogs[0], self::$author_ids[0], 'author' );
+ add_user_to_blog( $blogs[0], self::$author_ids[1], 'author' );
+ add_user_to_blog( $blogs[1], self::$author_ids[0], 'author' );
+ add_user_to_blog( $blogs[1], self::$author_ids[1], 'author' );
+
+ switch_to_blog( $blogs[0] );
+ self::factory()->post->create(
+ array(
+ 'post_author' => self::$author_ids[0],
+ 'post_status' => 'publish',
+ 'post_type' => 'post',
+ )
+ );
+ restore_current_blog();
+
+ switch_to_blog( $blogs[1] );
+ $post_id = self::factory()->post->create(
+ array(
+ 'post_author' => self::$author_ids[1],
+ 'post_status' => 'publish',
+ 'post_type' => 'post',
+ )
+ );
+ restore_current_blog();
+
+ $q = new WP_User_Query(
+ array(
+ 'has_published_posts' => array( 'post' ),
+ 'blog_id' => $blogs[1],
+ )
+ );
+
+ $found = wp_list_pluck( $q->get_results(), 'ID' );
+ $expected = array( self::$author_ids[1] );
+
+ $this->assertSameSets( $expected, $found, 'Asset that expected users returned with posts on this site' );
+ switch_to_blog( $blogs[1] );
+ wp_delete_post( $post_id, true );
+ restore_current_blog();
+
+ $q = new WP_User_Query(
+ array(
+ 'has_published_posts' => array( 'post' ),
+ 'blog_id' => $blogs[1],
+ )
+ );
+
+ $found = wp_list_pluck( $q->get_results(), 'ID' );
+
+ $this->assertSameSets( array(), $found, 'Asset that no users returned with posts on this site as posts have been deleted' );
+ }
+
+ /**
+ * Ensure cache keys are generated without WPDB placeholders.
+ *
+ * @ticket 40613
+ *
+ * @covers ::generate_cache_key
+ */
+ public function test_generate_cache_key_placeholder() {
+ global $wpdb;
+ $query1 = new WP_User_Query( array( 'capability' => 'edit_posts' ) );
+
+ $query_vars = $query1->query_vars;
+ $request_with_placeholder = $query1->request;
+ $request_without_placeholder = $wpdb->remove_placeholder_escape( $query1->request );
+
+ $reflection = new ReflectionMethod( $query1, 'generate_cache_key' );
+ $reflection->setAccessible( true );
+
+ $cache_key_1 = $reflection->invoke( $query1, $query_vars, $request_with_placeholder );
+ $cache_key_2 = $reflection->invoke( $query1, $query_vars, $request_without_placeholder );
+
+ $this->assertSame( $cache_key_1, $cache_key_2, 'Cache key differs when using wpdb placeholder.' );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/user/queryCache.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>