<!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>[28280] trunk: Multisite: Treat 'www' as a special subdomain, reversing 3.9 regression.</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">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/28280">28280</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-05-06 05:31:12 +0000 (Tue, 06 May 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Multisite: Treat 'www' as a special subdomain, reversing 3.9 regression.

props jeremyfelt.
fixes <a href="http://core.trac.wordpress.org/ticket/27927">#27927</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmsloadphp">trunk/src/wp-includes/ms-load.php</a></li>
<li><a href="#trunktestsphpunittestsmsphp">trunk/tests/phpunit/tests/ms.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmsloadphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/ms-load.php (28279 => 28280)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/ms-load.php        2014-05-06 05:31:07 UTC (rev 28279)
+++ trunk/src/wp-includes/ms-load.php   2014-05-06 05:31:12 UTC (rev 28280)
</span><span class="lines">@@ -348,13 +348,30 @@
</span><span class="cx">   * then cache whether we can just always ignore paths.
</span><span class="cx">   */
</span><span class="cx"> 
</span><ins>+       // Either www or non-www is supported, not both. If a www domain is requested,
+       // query for both to provide the proper redirect.
+       $domains = array( $domain );
+       if ( 'www.' === substr( $domain, 0, 4 ) ) {
+               $domains[] = substr( $domain, 4 );
+               $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
+       }
+
</ins><span class="cx">   if ( count( $paths ) > 1 ) {
</span><del>-               $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
-               $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domain );
-               $sql .= " AND path IN ($paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1";
</del><ins>+                $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
+       }
+
+       if ( count( $domains ) > 1 && count( $paths ) > 1 ) {
+               $site = $wpdb->get_row( "SELECT * FROM $wpdb->blogs WHERE domain IN ($search_domains) AND path IN ($search_paths) ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC LIMIT 1" );
+       } elseif ( count( $domains ) > 1 ) {
+               $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE path = %s", $paths[0] );
+               $sql .= " AND domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1";
</ins><span class="cx">           $site = $wpdb->get_row( $sql );
</span><ins>+       } elseif ( count( $paths ) > 1 ) {
+               $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domains[0] );
+               $sql .= " AND path IN ($search_paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1";
+               $site = $wpdb->get_row( $sql );
</ins><span class="cx">   } else {
</span><del>-               $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s", $domain, $paths[0] ) );
</del><ins>+                $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domains[0], $paths[0] ) );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  if ( $site ) {
</span></span></pre></div>
<a id="trunktestsphpunittestsmsphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/ms.php (28279 => 28280)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/ms.php 2014-05-06 05:31:07 UTC (rev 28279)
+++ trunk/tests/phpunit/tests/ms.php    2014-05-06 05:31:12 UTC (rev 28280)
</span><span class="lines">@@ -1225,6 +1225,7 @@
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="cx">   * @ticket 27003
</span><ins>+        * @ticket 27927
</ins><span class="cx">    */
</span><span class="cx">  function test_get_site_by_path() {
</span><span class="cx">          $ids = array(
</span><span class="lines">@@ -1233,6 +1234,9 @@
</span><span class="cx">                  'wordpress.org/foo/bar/'      => array( 'domain' => 'wordpress.org',      'path' => '/foo/bar/' ),
</span><span class="cx">                  'make.wordpress.org/'         => array( 'domain' => 'make.wordpress.org', 'path' => '/' ),
</span><span class="cx">                  'make.wordpress.org/foo/'     => array( 'domain' => 'make.wordpress.org', 'path' => '/foo/' ),
</span><ins>+                       'www.w.org/'                  => array( 'domain' => 'www.w.org',          'path' => '/' ),
+                       'www.w.org/foo/'              => array( 'domain' => 'www.w.org',          'path' => '/foo/' ),
+                       'www.w.org/foo/bar/'          => array( 'domain' => 'www.w.org',          'path' => '/foo/bar/' ),
</ins><span class="cx">           );
</span><span class="cx"> 
</span><span class="cx">          foreach ( $ids as &$id ) {
</span><span class="lines">@@ -1243,23 +1247,66 @@
</span><span class="cx">          $this->assertEquals( $ids['wordpress.org/'],
</span><span class="cx">                  get_site_by_path( 'wordpress.org', '/notapath/' )->blog_id );
</span><span class="cx"> 
</span><ins>+               $this->assertEquals( $ids['wordpress.org/'],
+                       get_site_by_path( 'www.wordpress.org', '/notapath/' )->blog_id );
+
</ins><span class="cx">           $this->assertEquals( $ids['wordpress.org/foo/bar/'],
</span><span class="cx">                  get_site_by_path( 'wordpress.org', '/foo/bar/baz/' )->blog_id );
</span><span class="cx"> 
</span><span class="cx">          $this->assertEquals( $ids['wordpress.org/foo/bar/'],
</span><ins>+                       get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/' )->blog_id );
+
+               $this->assertEquals( $ids['wordpress.org/foo/bar/'],
</ins><span class="cx">                   get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 3 )->blog_id );
</span><span class="cx"> 
</span><span class="cx">          $this->assertEquals( $ids['wordpress.org/foo/bar/'],
</span><ins>+                       get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 3 )->blog_id );
+
+               $this->assertEquals( $ids['wordpress.org/foo/bar/'],
</ins><span class="cx">                   get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 2 )->blog_id );
</span><span class="cx"> 
</span><ins>+               $this->assertEquals( $ids['wordpress.org/foo/bar/'],
+                       get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 2 )->blog_id );
+
</ins><span class="cx">           $this->assertEquals( $ids['wordpress.org/foo/'],
</span><span class="cx">                  get_site_by_path( 'wordpress.org', '/foo/bar/baz/', 1 )->blog_id );
</span><span class="cx"> 
</span><ins>+               $this->assertEquals( $ids['wordpress.org/foo/'],
+                       get_site_by_path( 'www.wordpress.org', '/foo/bar/baz/', 1 )->blog_id );
+
</ins><span class="cx">           $this->assertEquals( $ids['wordpress.org/'],
</span><span class="cx">                  get_site_by_path( 'wordpress.org', '/', 0 )->blog_id );
</span><span class="cx"> 
</span><ins>+               $this->assertEquals( $ids['wordpress.org/'],
+                       get_site_by_path( 'www.wordpress.org', '/', 0 )->blog_id );
+
</ins><span class="cx">           $this->assertEquals( $ids['make.wordpress.org/foo/'],
</span><span class="cx">                  get_site_by_path( 'make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id );
</span><ins>+
+               $this->assertEquals( $ids['make.wordpress.org/foo/'],
+                       get_site_by_path( 'www.make.wordpress.org', '/foo/bar/baz/qux/', 4 )->blog_id );
+
+               $this->assertEquals( $ids['www.w.org/'],
+                       get_site_by_path( 'www.w.org', '/', 0 )->blog_id );
+
+               $this->assertEquals( $ids['www.w.org/'],
+                       get_site_by_path( 'www.w.org', '/notapath/' )->blog_id );
+
+               $this->assertEquals( $ids['www.w.org/foo/bar/'],
+                       get_site_by_path( 'www.w.org', '/foo/bar/baz/' )->blog_id );
+
+               $this->assertEquals( $ids['www.w.org/foo/'],
+                       get_site_by_path( 'www.w.org', '/foo/bar/baz/', 1 )->blog_id );
+
+               // A site installed with www will not be found by the root domain.
+               $this->assertFalse( get_site_by_path( 'w.org', '/' ) );
+               $this->assertFalse( get_site_by_path( 'w.org', '/notapath/' ) );
+               $this->assertFalse( get_site_by_path( 'w.org', '/foo/bar/baz/' ) );
+               $this->assertFalse( get_site_by_path( 'w.org', '/foo/bar/baz/', 1 ) );
+
+               // A site will not be found by its root domain when an invalid subdomain is requested.
+               $this->assertFalse( get_site_by_path( 'invalid.wordpress.org', '/' ) );
+               $this->assertFalse( get_site_by_path( 'invalid.wordpress.org', '/foo/bar/' ) );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  /**
</span></span></pre>
</div>
</div>

</body>
</html>