<!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>[41698] trunk: Multisite: Introduce `get_site_by()`.</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 { 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/41698">41698</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/41698","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jeremyfelt</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-10-03 04:09:50 +0000 (Tue, 03 Oct 2017)</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: Introduce `get_site_by()`.

`get_site_by()` is a replacement for `get_blog_details()` that uses `WP_Site_Query` to retrieve specific sites based on a given field and value.

Props flixos90, spacedmonkey.
Fixes <a href="https://core.trac.wordpress.org/ticket/40180">#40180</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmsblogsphp">trunk/src/wp-includes/ms-blogs.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsmultisitegetSiteByphp">trunk/tests/phpunit/tests/multisite/getSiteBy.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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        2017-10-03 03:43:01 UTC (rev 41697)
+++ trunk/src/wp-includes/ms-blogs.php  2017-10-03 04:09:50 UTC (rev 41698)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -536,6 +536,118 @@
</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">+ * Retrieves a site by a given field and value.
+ *
+ * @since 4.9.0
+ *
+ * @param string     $field      Name of a field to query against. Accepts 'id', 'slug', 'url',
+ *                               'domain' (if a subdomain install) or 'path' (if a subdirectory install).
+ * @param string|int $value      The search value for $field.
+ * @param int|null   $network_id Optional. ID of the network. Default is the current network.
+ * @return WP_Site|null The site object or null if not found.
+ */
+function get_site_by( $field, $value, $network_id = null ) {
+       $args = array();
+
+       // `get_sites()` will return unexpected results if empty strings are passed as arguments.
+       if ( 'slug' === $field || 'url' === $field || 'domain' === $field || 'path' === $field ) {
+               if ( ! is_string( $value ) && ! is_numeric( $value ) ) {
+                       return null;
+               }
+
+               $value = trim( $value );
+
+               if ( 0 === strlen( $value ) ) {
+                       return null;
+               }
+       }
+
+       switch ( $field ) {
+               case 'id':
+                       if ( ! is_numeric( $value ) ) {
+                               return null;
+                       }
+                       $args['site__in'][] = intval( $value );
+                       break;
+               case 'slug':
+                       $network = get_network( $network_id );
+                       if ( ! $network ) {
+                               return null;
+                       }
+
+                       if ( is_subdomain_install() ) {
+                               $args['domain'] = trim( $value, '/' ) . '.' . preg_replace( '|^www\.|', '', $network->domain );
+                               $args['path'] = $network->path;
+                       } else {
+                               $args['domain'] = $network->domain;
+                               $args['path'] = $network->path . trim( $value, '/' ) . '/';
+                       }
+                       break;
+               case 'url':
+                       if ( 0 !== strpos( $value, 'http://' ) && 0 !== strpos( $value, 'https://' ) ) {
+                               $value = 'http://' . $value;
+                       }
+
+                       $parts = wp_parse_url( $value );
+                       if ( ! $parts ) {
+                               return null;
+                       }
+
+                       $args['domain'] = $parts['host'];
+                       if ( ! empty( $parts['path'] ) ) {
+                               $args['path'] = '/' . trim( $parts['path'], '/' ) . '/';
+                       } else {
+                               $args['path'] = '/';
+                       }
+                       break;
+               case 'domain':
+                       if ( ! is_subdomain_install() ) {
+                               return null;
+                       }
+
+                       $args['domain'] = $value;
+                       break;
+               case 'path':
+                       if ( is_subdomain_install() ) {
+                               return null;
+                       }
+
+                       $args['path'] = '/' . trim( $value, '/' ) . '/';
+                       break;
+               default:
+                       return null;
+       }
+
+       if ( isset( $args['domain'] ) && substr( $args['domain'], 0, 4 ) === 'www.' ) {
+               $nowww = substr( $args['domain'], 4 );
+
+               $args['domain__in'] = array( $nowww, $args['domain'] );
+               unset( $args['domain'] );
+
+               $args['orderby'] = 'domain_length';
+               $args['order']   = 'DESC';
+       }
+
+       if ( isset( $args['path'] ) ) {
+               $args['path'] = str_replace( '//', '/', $args['path'] );
+       }
+
+       if ( ! empty( $network_id ) ) {
+               $args['network_id'] = (int) $network_id;
+       }
+
+       $args['number'] = 1;
+
+       $sites = get_sites( $args );
+
+       if ( empty( $sites ) ) {
+               return null;
+       }
+
+       return array_shift( $sites );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Adds any sites from the given ids to the cache that do not already exist in cache.
</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></pre></div>
<a id="trunktestsphpunittestsmultisitegetSiteByphp"></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/multisite/getSiteBy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/multisite/getSiteBy.php                         (rev 0)
+++ trunk/tests/phpunit/tests/multisite/getSiteBy.php   2017-10-03 04:09:50 UTC (rev 41698)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,252 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+if ( is_multisite() ) :
+/**
+ * Test get_site_by() in multisite.
+ *
+ * @ticket 40180
+ * @group ms-site
+ * @group multisite
+ */
+class Tests_Multisite_Get_Site_By extends WP_UnitTestCase {
+       protected static $network_ids;
+       protected static $site_ids;
+
+       public static function wpSetUpBeforeClass( $factory ) {
+               self::$network_ids = array(
+                       'wordpress.org/'         => array( 'domain' => 'wordpress.org',     'path' => '/' ),
+                       'www.wordpress.net/'     => array( 'domain' => 'www.wordpress.net', 'path' => '/' ),
+               );
+
+               foreach ( self::$network_ids as &$id ) {
+                       $id = $factory->network->create( $id );
+               }
+               unset( $id );
+
+               self::$site_ids = array(
+                       'wordpress.org/'              => array( 'domain' => 'wordpress.org',     'path' => '/',     'site_id' => self::$network_ids['wordpress.org/'] ),
+                       'foo.wordpress.org/'          => array( 'domain' => 'foo.wordpress.org', 'path' => '/',     'site_id' => self::$network_ids['wordpress.org/'] ),
+                       'wordpress.org/foo/'          => array( 'domain' => 'wordpress.org',     'path' => '/foo/', 'site_id' => self::$network_ids['wordpress.org/'] ),
+                       'www.wordpress.org/'          => array( 'domain' => 'www.wordpress.org', 'path' => '/',     'site_id' => self::$network_ids['wordpress.org/'] ),
+                       'www.wordpress.net/'          => array( 'domain' => 'www.wordpress.net', 'path' => '/',     'site_id' => self::$network_ids['www.wordpress.net/'] ),
+                       'foo.wordpress.net/'          => array( 'domain' => 'foo.wordpress.net', 'path' => '/',     'site_id' => self::$network_ids['www.wordpress.net/'] ),
+                       'www.wordpress.net/foo/'      => array( 'domain' => 'www.wordpress.net', 'path' => '/foo/', 'site_id' => self::$network_ids['www.wordpress.net/'] ),
+               );
+
+               foreach ( self::$site_ids as &$id ) {
+                       $id = $factory->blog->create( $id );
+               }
+               unset( $id );
+       }
+
+       public static function wpTearDownAfterClass() {
+               global $wpdb;
+
+               foreach( self::$site_ids as $id ) {
+                       wpmu_delete_blog( $id, true );
+               }
+
+               foreach( self::$network_ids as $id ) {
+                       $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $id ) );
+                       $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", $id ) );
+               }
+
+               wp_update_network_site_counts();
+       }
+
+       public function test_get_site_by_id_with_valid_id() {
+               $result = get_site_by( 'id', self::$site_ids['wordpress.org/'] );
+
+               $this->assertEquals( self::$site_ids['wordpress.org/'], $result->id );
+       }
+
+       public function test_get_site_by_id_with_invalid_id() {
+               $result = get_site_by( 'id', 'wp.org' );
+
+               $this->assertNull( $result );
+       }
+
+       public function test_get_site_by_slug_subdomain() {
+               if ( ! is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' );
+               }
+
+               $result = get_site_by( 'slug', 'foo', self::$network_ids['wordpress.org/'] );
+
+               $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id );
+       }
+
+       public function test_get_site_by_slug_with_www_subdomain() {
+               if ( ! is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' );
+               }
+
+               $result = get_site_by( 'slug', 'foo', self::$network_ids['www.wordpress.net/'] );
+
+               $this->assertEquals( self::$site_ids['foo.wordpress.net/'], $result->id );
+       }
+
+       public function test_get_site_by_slug_subdirectory() {
+               if ( is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' );
+               }
+
+               $result = get_site_by( 'slug', 'foo', self::$network_ids['wordpress.org/'] );
+
+               $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id );
+       }
+
+       public function test_get_site_by_slug_with_www_subdirectory() {
+               if ( is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' );
+               }
+
+               $result = get_site_by( 'slug', 'foo', self::$network_ids['www.wordpress.net/'] );
+
+               $this->assertEquals( self::$site_ids['www.wordpress.net/foo/'], $result->id );
+       }
+
+       public function test_get_site_by_slug_with_first_network() {
+               $result = get_site_by( 'slug', 'foo', self::$network_ids['wordpress.org/'] );
+
+               if ( is_subdomain_install() ) {
+                       $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id );
+               } else {
+                       $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id );
+               }
+       }
+
+       public function test_get_site_by_slug_with_second_network() {
+               $result = get_site_by( 'slug', 'foo', self::$network_ids['www.wordpress.net/'] );
+
+               if ( is_subdomain_install() ) {
+                       $this->assertEquals( self::$site_ids['foo.wordpress.net/'], $result->id );
+               } else {
+                       $this->assertEquals( self::$site_ids['www.wordpress.net/foo/'], $result->id );
+               }
+       }
+
+       public function test_get_site_by_slug_with_invalid_network() {
+               $result = get_site_by( 'slug', 'foo', 444 );
+
+               $this->assertNull( $result );
+       }
+
+       public function test_get_site_by_slug_with_empty_string_returns_null() {
+               $result = get_site_by( 'slug', '' );
+
+               $this->assertNull( $result );
+       }
+
+       /**
+        * @dataProvider data_get_site_by_url
+        */
+       public function test_get_site_by_url( $url, $expected ) {
+               $result = get_site_by( 'url', $url );
+
+               $this->assertEquals( self::$site_ids[ $expected ], $result->id );
+       }
+
+       public function data_get_site_by_url() {
+               return array(
+                       array(
+                               'wordpress.org/foo/',
+                               'wordpress.org/foo/',
+                       ),
+                       array(
+                               'wordpress.org/foo',
+                               'wordpress.org/foo/',
+                       ),
+                       array(
+                               'foo.wordpress.org/',
+                               'foo.wordpress.org/',
+                       ),
+                       array(
+                               'foo.wordpress.org',
+                               'foo.wordpress.org/',
+                       ),
+                       array(
+                               'www.wordpress.net/',
+                               'www.wordpress.net/',
+                       ),
+                       array(
+                               'www.wordpress.org/',
+                               'www.wordpress.org/',
+                       ),
+               );
+       }
+
+       public function test_get_site_by_url_with_empty_string_returns_null() {
+               $result = get_site_by( 'url', '' );
+
+               $this->assertNull( $result );
+       }
+
+       public function test_get_site_by_url_with_invalid_url() {
+               $result = get_site_by( 'url', 'not a url' );
+
+               $this->assertNull( $result );
+       }
+
+       public function test_get_site_by_domain_subdomain() {
+               if ( ! is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' );
+               }
+
+               $result = get_site_by( 'domain', 'foo.wordpress.org' );
+
+               $this->assertEquals( self::$site_ids['foo.wordpress.org/'], $result->id );
+       }
+
+       public function test_get_site_by_domain_subdirectory() {
+               if ( is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' );
+               }
+
+               $result = get_site_by( 'domain', 'foo.wordpress.org' );
+
+               $this->assertNull( $result );
+       }
+
+       public function test_get_site_by_domain_with_empty_string_returns_null() {
+               if ( ! is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' );
+               }
+
+               $result = get_site_by( 'domain', '' );
+
+               $this->assertNull( $result );
+       }
+
+       public function test_get_site_by_path_subdomain() {
+               if ( ! is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdomain configuration.' );
+               }
+
+               $result = get_site_by( 'path', '/foo/' );
+
+               $this->assertNull( $result );
+       }
+
+       public function test_get_site_by_path_subdirectory() {
+               if ( is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' );
+               }
+
+               $result = get_site_by( 'path', '/foo/' );
+
+               $this->assertEquals( self::$site_ids['wordpress.org/foo/'], $result->id );
+       }
+
+       public function test_get_site_by_path_with_empty_string_returns_null() {
+               if ( is_subdomain_install() ) {
+                       $this->markTestSkipped( 'This test is only valid in a subdirectory configuration.' );
+               }
+
+               $result = get_site_by( 'path', '' );
+
+               $this->assertNull( $result );
+       }
+}
+
+endif;
</ins></span></pre>
</div>
</div>

</body>
</html>