Enforce leading and trailing slashes on paths updated with update_blog_details()

In multisite, core expects the stored value for a site's path to have leading and trailing slashes. When these slashes are missing, it becomes impossible to visit the site.

This enforces proper `/path/` creation in `update_blog_details()`, most likely used when updating an existing site through `site-info.php`.

Props earnjam, simonwheatley.

Fixes #18117. Fixes #23865.


git-svn-id: https://develop.svn.wordpress.org/trunk@31155 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt 2015-01-12 01:42:46 +00:00
parent 0b993ffe35
commit f9e9287875
2 changed files with 82 additions and 3 deletions

View File

@ -296,8 +296,14 @@ function update_blog_details( $blog_id, $details = array() ) {
$update_details = array(); $update_details = array();
$fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'); $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
$update_details[$field] = $details[$field]; if ( 'path' === $field ) {
$details[ $field ] = array_filter( explode( '/', $details[ $field ] ) );
$details[ $field ] = trailingslashit( '/' . implode( '/', $details[ $field ] ) );
}
$update_details[ $field ] = $details[ $field ];
}
$result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) ); $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );

View File

@ -343,7 +343,7 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
$blog = get_blog_details( $blog_id ); $blog = get_blog_details( $blog_id );
$this->assertEquals( 'example.com', $blog->domain ); $this->assertEquals( 'example.com', $blog->domain );
$this->assertEquals( 'my_path/', $blog->path ); $this->assertEquals( '/my_path/', $blog->path );
$this->assertEquals( '0', $blog->spam ); $this->assertEquals( '0', $blog->spam );
} }
@ -542,6 +542,79 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
$test_action_counter++; $test_action_counter++;
} }
/**
* When the path for a site is updated with update_blog_details(), the final
* path should have a leading and trailing slash. When multiple directories
* are part of the path, only one slash should separate each directory.
*
* @ticket 18117
*/
function test_update_blog_details_single_path_no_slashes() {
update_blog_details( 1, array( 'path' => 'my_path' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/my_path/', $blog->path );
}
function test_update_blog_details_single_path_double_trailing_slashes() {
update_blog_details( 1, array( 'path' => 'my_path//' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/my_path/', $blog->path );
}
function test_update_blog_details_single_path_double_leading_slashes() {
update_blog_details( 1, array( 'path' => '//my_path' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/my_path/', $blog->path );
}
function test_update_blog_details_single_path_single_trailing_slash() {
update_blog_details( 1, array( 'path' => 'my_path/' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/my_path/', $blog->path );
}
function test_update_blog_details_single_path_single_leading_slashes() {
update_blog_details( 1, array( 'path' => '/my_path' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/my_path/', $blog->path );
}
function test_update_blog_details_single_path_both_slashes() {
update_blog_details( 1, array( 'path' => '/my_path/' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/my_path/', $blog->path );
}
function test_update_blog_details_multiple_paths_no_slashes() {
update_blog_details( 1, array( 'path' => 'multiple/dirs' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/multiple/dirs/', $blog->path );
}
function test_update_blog_details_multiple_paths_middle_slashes() {
update_blog_details( 1, array( 'path' => 'multiple///dirs' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/multiple/dirs/', $blog->path );
}
function test_update_blog_details_multiple_paths_leading_slash() {
update_blog_details( 1, array( 'path' => '/multiple/dirs' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/multiple/dirs/', $blog->path );
}
function test_update_blog_details_multiple_paths_trailing_slash() {
update_blog_details( 1, array( 'path' => 'multiple/dirs/' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/multiple/dirs/', $blog->path );
}
function test_update_blog_details_multiple_paths_both_slashes() {
update_blog_details( 1, array( 'path' => '/multiple/dirs/' ) );
$blog = get_blog_details( 1 );
$this->assertEquals( '/multiple/dirs/', $blog->path );
}
/** /**
* Test cached data for a site that does not exist and then again after it exists. * Test cached data for a site that does not exist and then again after it exists.
* *