Make WordPress Core

Changeset 31155


Ignore:
Timestamp:
01/12/2015 01:42:46 AM (10 years ago)
Author:
jeremyfelt
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-blogs.php

    r30656 r31155  
    297297    $update_details = array();
    298298    $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
    299     foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
    300         $update_details[$field] = $details[$field];
     299    foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
     300        if ( 'path' === $field ) {
     301            $details[ $field ] = array_filter( explode( '/', $details[ $field ] ) );
     302            $details[ $field ] = trailingslashit( '/' . implode( '/', $details[ $field ] ) );
     303        }
     304
     305        $update_details[ $field ] = $details[ $field ];
     306    }
    301307
    302308    $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
  • trunk/tests/phpunit/tests/multisite/site.php

    r30786 r31155  
    344344
    345345        $this->assertEquals( 'example.com', $blog->domain );
    346         $this->assertEquals( 'my_path/', $blog->path );
     346        $this->assertEquals( '/my_path/', $blog->path );
    347347        $this->assertEquals( '0', $blog->spam );
    348348    }
     
    541541        global $test_action_counter;
    542542        $test_action_counter++;
     543    }
     544
     545    /**
     546     * When the path for a site is updated with update_blog_details(), the final
     547     * path should have a leading and trailing slash. When multiple directories
     548     * are part of the path, only one slash should separate each directory.
     549     *
     550     * @ticket 18117
     551     */
     552    function test_update_blog_details_single_path_no_slashes() {
     553        update_blog_details( 1, array( 'path' => 'my_path' ) );
     554        $blog = get_blog_details( 1 );
     555        $this->assertEquals( '/my_path/', $blog->path );
     556    }
     557
     558    function test_update_blog_details_single_path_double_trailing_slashes() {
     559        update_blog_details( 1, array( 'path' => 'my_path//' ) );
     560        $blog = get_blog_details( 1 );
     561        $this->assertEquals( '/my_path/', $blog->path );
     562    }
     563
     564    function test_update_blog_details_single_path_double_leading_slashes() {
     565        update_blog_details( 1, array( 'path' => '//my_path' ) );
     566        $blog = get_blog_details( 1 );
     567        $this->assertEquals( '/my_path/', $blog->path );
     568    }
     569
     570    function test_update_blog_details_single_path_single_trailing_slash() {
     571        update_blog_details( 1, array( 'path' => 'my_path/' ) );
     572        $blog = get_blog_details( 1 );
     573        $this->assertEquals( '/my_path/', $blog->path );
     574    }
     575
     576    function test_update_blog_details_single_path_single_leading_slashes() {
     577        update_blog_details( 1, array( 'path' => '/my_path' ) );
     578        $blog = get_blog_details( 1 );
     579        $this->assertEquals( '/my_path/', $blog->path );
     580    }
     581
     582    function test_update_blog_details_single_path_both_slashes() {
     583        update_blog_details( 1, array( 'path' => '/my_path/' ) );
     584        $blog = get_blog_details( 1 );
     585        $this->assertEquals( '/my_path/', $blog->path );
     586    }
     587
     588    function test_update_blog_details_multiple_paths_no_slashes() {
     589        update_blog_details( 1, array( 'path' => 'multiple/dirs' ) );
     590        $blog = get_blog_details( 1 );
     591        $this->assertEquals( '/multiple/dirs/', $blog->path );
     592    }
     593
     594    function test_update_blog_details_multiple_paths_middle_slashes() {
     595        update_blog_details( 1, array( 'path' => 'multiple///dirs' ) );
     596        $blog = get_blog_details( 1 );
     597        $this->assertEquals( '/multiple/dirs/', $blog->path );
     598    }
     599
     600    function test_update_blog_details_multiple_paths_leading_slash() {
     601        update_blog_details( 1, array( 'path' => '/multiple/dirs' ) );
     602        $blog = get_blog_details( 1 );
     603        $this->assertEquals( '/multiple/dirs/', $blog->path );
     604    }
     605
     606    function test_update_blog_details_multiple_paths_trailing_slash() {
     607        update_blog_details( 1, array( 'path' => 'multiple/dirs/' ) );
     608        $blog = get_blog_details( 1 );
     609        $this->assertEquals( '/multiple/dirs/', $blog->path );
     610    }
     611
     612    function test_update_blog_details_multiple_paths_both_slashes() {
     613        update_blog_details( 1, array( 'path' => '/multiple/dirs/' ) );
     614        $blog = get_blog_details( 1 );
     615        $this->assertEquals( '/multiple/dirs/', $blog->path );
    543616    }
    544617
Note: See TracChangeset for help on using the changeset viewer.