<!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>[43010] trunk: Multisite: Add meta query functionality to `WP_Site_Query`.</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/43010">43010</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/43010","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-04-27 11:40:35 +0000 (Fri, 27 Apr 2018)</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'>Multisite: Add meta query functionality to `WP_Site_Query`.

After the introduction of site metadata in <a href="https://core.trac.wordpress.org/changeset/42836">[42836]</a>, it should be possible to query sites by that data.

Fixes <a href="https://core.trac.wordpress.org/ticket/40229">#40229</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpsitequeryphp">trunk/src/wp-includes/class-wp-site-query.php</a></li>
<li><a href="#trunktestsphpunittestsmultisitesiteMetaphp">trunk/tests/phpunit/tests/multisite/siteMeta.php</a></li>
<li><a href="#trunktestsphpunittestsmultisitesiteQueryphp">trunk/tests/phpunit/tests/multisite/siteQuery.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpsitequeryphp"></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-site-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-site-query.php     2018-04-27 11:36:47 UTC (rev 43009)
+++ trunk/src/wp-includes/class-wp-site-query.php       2018-04-27 11:40:35 UTC (rev 43010)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -40,6 +40,22 @@
</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">+         * Metadata query container.
+        *
+        * @since 5.0.0
+        * @var WP_Meta_Query
+        */
+       public $meta_query = false;
+
+       /**
+        * Metadata query clauses.
+        *
+        * @since 5.0.0
+        * @var array
+        */
+       protected $meta_query_clauses;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Date query container.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.6.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -92,7 +108,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.6.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 5.0.0 Introduced the 'update_site_meta_cache' parameter.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 5.0.0 Introduced the 'update_site_meta_cache', 'meta_query', 'meta_key',
+        *              'meta_value', 'meta_type' and 'meta_compare' parameters.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string|array $query {
</span><span class="cx" style="display: block; padding: 0 10px">         *     Optional. Array or query string of site query parameters. Default empty.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -139,6 +156,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *                                                Default empty array.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type bool         $update_site_cache      Whether to prime the cache for found sites. Default true.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type bool         $update_site_meta_cache Whether to prime the metadata cache for found sites. Default true.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *     @type array        $meta_query             Meta query clauses to limit retrieved sites by. See `WP_Meta_Query`.
+        *                                                Default empty.
+        *     @type string       $meta_key               Limit sites to those matching a specific metadata key.
+        *                                                Can be used in conjunction with `$meta_value`. Default empty.
+        *     @type string       $meta_value             Limit sites to those matching a specific metadata value.
+        *                                                Usually used in conjunction with `$meta_key`. Default empty.
+        *     @type string       $meta_type              Data type that the `$meta_value` column will be CAST to for
+        *                                                comparisons. Default empty.
+        *     @type string       $meta_compare           Comparison operator to test the `$meta_value`. Default empty.
</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 __construct( $query = '' ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,6 +201,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'date_query'             => null, // See WP_Date_Query
</span><span class="cx" style="display: block; padding: 0 10px">                        'update_site_cache'      => true,
</span><span class="cx" style="display: block; padding: 0 10px">                        'update_site_meta_cache' => true,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'meta_query'             => '',
+                       'meta_key'               => '',
+                       'meta_value'             => '',
+                       'meta_type'              => '',
+                       'meta_compare'           => '',
</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">                if ( ! empty( $query ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -228,12 +259,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.6.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @global wpdb $wpdb WordPress database abstraction object.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return array|int List of WP_Site objects, a list of site ids when 'fields' is set to 'ids',
</span><span class="cx" style="display: block; padding: 0 10px">         *                   or the number of sites when 'count' is passed as a query var.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function get_sites() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                global $wpdb;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->parse_query();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Parse meta query.
+               $this->meta_query = new WP_Meta_Query();
+               $this->meta_query->parse_query_vars( $this->query_vars );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Fires before sites are retrieved.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -243,6 +282,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                do_action_ref_array( 'pre_get_sites', array( &$this ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Reparse query vars, in case they were modified in a 'pre_get_sites' callback.
+               $this->meta_query->parse_query_vars( $this->query_vars );
+               if ( ! empty( $this->meta_query->queries ) ) {
+                       $this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
</span><span class="cx" style="display: block; padding: 0 10px">                $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -370,7 +415,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $orderby = implode( ', ', $orderby_array );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $orderby = "blog_id $order";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $orderby = "{$wpdb->blogs}.blog_id $order";
</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">                $number = absint( $this->query_vars['number'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -387,23 +432,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $this->query_vars['count'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $fields = 'COUNT(*)';
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $fields = 'blog_id';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $fields = "{$wpdb->blogs}.blog_id";
</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">                // Parse site IDs for an IN clause.
</span><span class="cx" style="display: block; padding: 0 10px">                $site_id = absint( $this->query_vars['ID'] );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $site_id ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->sql_clauses['where']['ID'] = $wpdb->prepare( 'blog_id = %d', $site_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->sql_clauses['where']['ID'] = $wpdb->prepare( "{$wpdb->blogs}.blog_id = %d", $site_id );
</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">                // Parse site IDs for an IN clause.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $this->query_vars['site__in'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->sql_clauses['where']['site__in'] = 'blog_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->sql_clauses['where']['site__in'] = "{$wpdb->blogs}.blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
</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">                // Parse site IDs for a NOT IN clause.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $this->query_vars['site__not_in'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->sql_clauses['where']['site__not_in'] = 'blog_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->sql_clauses['where']['site__not_in'] = "{$wpdb->blogs}.blog_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )';
</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">                $network_id = absint( $this->query_vars['network_id'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -526,6 +571,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $join = '';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( ! empty( $this->meta_query_clauses ) ) {
+                       $join .= $this->meta_query_clauses['join'];
+
+                       // Strip leading 'AND'.
+                       $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
+
+                       if ( ! $this->query_vars['count'] ) {
+                               $groupby = "{$wpdb->blogs}.blog_id";
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $where = implode( ' AND ', $this->sql_clauses['where'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -675,10 +731,42 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $parsed = 'CHAR_LENGTH(path)';
</span><span class="cx" style="display: block; padding: 0 10px">                                break;
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'id':
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $parsed = 'blog_id';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $parsed = "{$wpdb->blogs}.blog_id";
</ins><span class="cx" style="display: block; padding: 0 10px">                                 break;
</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">+                if ( ! empty( $parsed ) || empty( $this->meta_query_clauses ) ) {
+                       return $parsed;
+               }
+
+               $meta_clauses = $this->meta_query->get_clauses();
+               if ( empty( $meta_clauses ) ) {
+                       return $parsed;
+               }
+
+               $primary_meta_query = reset( $meta_clauses );
+               if ( ! empty( $primary_meta_query['key'] ) && $primary_meta_query['key'] === $orderby ) {
+                       $orderby = 'meta_value';
+               }
+
+               switch ( $orderby ) {
+                       case 'meta_value':
+                               if ( ! empty( $primary_meta_query['type'] ) ) {
+                                       $parsed = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
+                               } else {
+                                       $parsed = "{$primary_meta_query['alias']}.meta_value";
+                               }
+                               break;
+                       case 'meta_value_num':
+                               $parsed = "{$primary_meta_query['alias']}.meta_value+0";
+                               break;
+                       default:
+                               if ( isset( $meta_clauses[ $orderby ] ) ) {
+                                       $meta_clause = $meta_clauses[ $orderby ];
+                                       $parsed      = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
+                               }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $parsed;
</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="trunktestsphpunittestsmultisitesiteMetaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/multisite/siteMeta.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/multisite/siteMeta.php  2018-04-27 11:36:47 UTC (rev 43009)
+++ trunk/tests/phpunit/tests/multisite/siteMeta.php    2018-04-27 11:40:35 UTC (rev 43010)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -256,6 +256,122 @@
</span><span class="cx" style="display: block; padding: 0 10px">                get_site_meta( self::$site_id, 'foo', true );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $num_queries + 1, $wpdb->num_queries);
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 40229
+        */
+       public function test_add_site_meta_should_bust_get_sites_cache() {
+               if ( ! is_site_meta_supported() ) {
+                       $this->markTestSkipped( 'Tests only runs with the blogmeta database table installed' );
+               }
+
+               add_site_meta( self::$site_id, 'foo', 'bar' );
+
+               // Prime cache.
+               $found = get_sites( array(
+                       'fields' => 'ids',
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+               ) );
+
+               $this->assertEqualSets( array( self::$site_id ), $found );
+
+               add_site_meta( self::$site_id2, 'foo', 'bar' );
+
+               $found = get_sites( array(
+                       'fields' => 'ids',
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+               ) );
+
+               $this->assertEqualSets( array( self::$site_id, self::$site_id2 ), $found );
+       }
+
+       /**
+        * @ticket 40229
+        */
+       public function test_update_site_meta_should_bust_get_sites_cache() {
+               if ( ! is_site_meta_supported() ) {
+                       $this->markTestSkipped( 'Tests only runs with the blogmeta database table installed' );
+               }
+
+               add_site_meta( self::$site_id, 'foo', 'bar' );
+               add_site_meta( self::$site_id2, 'foo', 'baz' );
+
+               // Prime cache.
+               $found = get_sites( array(
+                       'fields' => 'ids',
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+               ) );
+
+               $this->assertEqualSets( array( self::$site_id ), $found );
+
+               update_site_meta( self::$site_id2, 'foo', 'bar' );
+
+               $found = get_sites( array(
+                       'fields' => 'ids',
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+               ) );
+
+               $this->assertEqualSets( array( self::$site_id, self::$site_id2 ), $found );
+       }
+
+       /**
+        * @ticket 40229
+        */
+       public function test_delete_site_meta_should_bust_get_sites_cache() {
+               if ( ! is_site_meta_supported() ) {
+                       $this->markTestSkipped( 'Tests only runs with the blogmeta database table installed' );
+               }
+
+               add_site_meta( self::$site_id, 'foo', 'bar' );
+               add_site_meta( self::$site_id2, 'foo', 'bar' );
+
+               // Prime cache.
+               $found = get_sites( array(
+                       'fields' => 'ids',
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+               ) );
+
+               $this->assertEqualSets( array( self::$site_id, self::$site_id2 ), $found );
+
+               delete_site_meta( self::$site_id2, 'foo', 'bar' );
+
+               $found = get_sites( array(
+                       'fields' => 'ids',
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+               ) );
+
+               $this->assertEqualSets( array( self::$site_id ), $found );
+       }
</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"> endif;
</span></span></pre></div>
<a id="trunktestsphpunittestsmultisitesiteQueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/multisite/siteQuery.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/multisite/siteQuery.php 2018-04-27 11:36:47 UTC (rev 43009)
+++ trunk/tests/phpunit/tests/multisite/siteQuery.php   2018-04-27 11:40:35 UTC (rev 43010)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -878,6 +878,193 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->assertEquals( $number_of_queries + 1, $wpdb->num_queries );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               /**
+                * @ticket 40229
+                * @dataProvider data_wp_site_query_meta_query
+                */
+               public function test_wp_site_query_meta_query( $query, $expected, $strict ) {
+                       if ( ! is_site_meta_supported() ) {
+                               $this->markTestSkipped( 'Tests only runs with the blogmeta database table installed' );
+                       }
+
+                       add_site_meta( self::$site_ids['wordpress.org/'], 'foo', 'foo' );
+                       add_site_meta( self::$site_ids['wordpress.org/foo/'], 'foo', 'bar' );
+                       add_site_meta( self::$site_ids['wordpress.org/foo/bar/'], 'foo', 'baz' );
+                       add_site_meta( self::$site_ids['make.wordpress.org/'], 'bar', 'baz' );
+                       add_site_meta( self::$site_ids['wordpress.org/'], 'numberfoo', 1 );
+                       add_site_meta( self::$site_ids['wordpress.org/foo/'], 'numberfoo', 2 );
+
+                       $query['fields'] = 'ids';
+
+                       $q     = new WP_Site_Query();
+                       $found = $q->query( $query );
+
+                       foreach ( $expected as $index => $domain_path ) {
+                               $expected[ $index ] = self::$site_ids[ $domain_path ];
+                       }
+
+                       if ( $strict ) {
+                               $this->assertEquals( $expected, $found );
+                       } else {
+                               $this->assertEqualSets( $expected, $found );
+                       }
+               }
+
+               public function data_wp_site_query_meta_query() {
+                       return array(
+                               array(
+                                       array(
+                                               'meta_key' => 'foo',
+                                       ),
+                                       array(
+                                               'wordpress.org/',
+                                               'wordpress.org/foo/',
+                                               'wordpress.org/foo/bar/',
+                                       ),
+                                       false,
+                               ),
+                               array(
+                                       array(
+                                               'meta_key'   => 'foo',
+                                               'meta_value' => 'bar',
+                                       ),
+                                       array(
+                                               'wordpress.org/foo/',
+                                       ),
+                                       false,
+                               ),
+                               array(
+                                       array(
+                                               'meta_key'     => 'foo',
+                                               'meta_value'   => array( 'bar', 'baz' ),
+                                               'meta_compare' => 'IN',
+                                       ),
+                                       array(
+                                               'wordpress.org/foo/',
+                                               'wordpress.org/foo/bar/',
+                                       ),
+                                       false,
+                               ),
+                               array(
+                                       array(
+                                               'meta_query' => array(
+                                                       array(
+                                                               'key'   => 'foo',
+                                                               'value' => 'bar',
+                                                       ),
+                                                       array(
+                                                               'key'   => 'numberfoo',
+                                                               'value' => 2,
+                                                               'type'  => 'NUMERIC',
+                                                       ),
+                                               ),
+                                       ),
+                                       array(
+                                               'wordpress.org/foo/',
+                                       ),
+                                       false,
+                               ),
+                               array(
+                                       array(
+                                               'meta_key' => 'foo',
+                                               'orderby'  => 'meta_value',
+                                               'order'    => 'ASC',
+                                       ),
+                                       array(
+                                               'wordpress.org/foo/',
+                                               'wordpress.org/foo/bar/',
+                                               'wordpress.org/',
+                                       ),
+                                       true,
+                               ),
+                               array(
+                                       array(
+                                               'meta_key' => 'foo',
+                                               'orderby'  => 'foo',
+                                               'order'    => 'ASC',
+                                       ),
+                                       array(
+                                               'wordpress.org/foo/',
+                                               'wordpress.org/foo/bar/',
+                                               'wordpress.org/',
+                                       ),
+                                       true,
+                               ),
+                               array(
+                                       array(
+                                               'meta_key' => 'numberfoo',
+                                               'orderby'  => 'meta_value_num',
+                                               'order'    => 'DESC',
+                                       ),
+                                       array(
+                                               'wordpress.org/foo/',
+                                               'wordpress.org/',
+                                       ),
+                                       true,
+                               ),
+                               array(
+                                       array(
+                                               'meta_query' => array(
+                                                       array(
+                                                               'key'     => 'foo',
+                                                               'value'   => array( 'foo', 'bar' ),
+                                                               'compare' => 'IN',
+                                                       ),
+                                                       array(
+                                                               'key' => 'numberfoo',
+                                                       ),
+                                               ),
+                                               'orderby'    => array( 'meta_value' => 'ASC' ),
+                                       ),
+                                       array(
+                                               'wordpress.org/foo/',
+                                               'wordpress.org/',
+                                       ),
+                                       true,
+                               ),
+                               array(
+                                       array(
+                                               'meta_query' => array(
+                                                       array(
+                                                               'key'     => 'foo',
+                                                               'value'   => array( 'foo', 'bar' ),
+                                                               'compare' => 'IN',
+                                                       ),
+                                                       array(
+                                                               'key' => 'numberfoo',
+                                                       ),
+                                               ),
+                                               'orderby'    => array( 'foo' => 'ASC' ),
+                                       ),
+                                       array(
+                                               'wordpress.org/foo/',
+                                               'wordpress.org/',
+                                       ),
+                                       true,
+                               ),
+                               array(
+                                       array(
+                                               'meta_query' => array(
+                                                       array(
+                                                               'key'     => 'foo',
+                                                               'value'   => array( 'foo', 'bar' ),
+                                                               'compare' => 'IN',
+                                                       ),
+                                                       'my_subquery' => array(
+                                                               'key' => 'numberfoo',
+                                                       ),
+                                               ),
+                                               'orderby'    => array( 'my_subquery' => 'DESC' ),
+                                       ),
+                                       array(
+                                               'wordpress.org/foo/',
+                                               'wordpress.org/',
+                                       ),
+                                       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"> endif;
</span></span></pre>
</div>
</div>

</body>
</html>