WordPress.org

Make WordPress Core

Changeset 38681


Ignore:
Timestamp:
09/30/2016 04:09:45 AM (4 years ago)
Author:
jeremyfelt
Message:

Multisite: Ensure a consistent WP_Site return from get_site_by_path().

It is possible to short circuit get_site_by_path() using the pre_get_site_by_path filter. When this happens and a standard site object is provided, we can make sure it is upgraded to a proper WP_Site object before passing it on.

Props flixos90.
Fixes #37053.

Location:
trunk
Files:
2 edited

Legend:

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

    r38515 r38681  
    138138 *
    139139 * @since 3.9.0
     140 * @since 4.7.0 Updated to always return a `WP_Site` object.
    140141 *
    141142 * @global wpdb $wpdb WordPress database abstraction object.
     
    144145 * @param string   $path     Path to check.
    145146 * @param int|null $segments Path segments to use. Defaults to null, or the full path.
    146  * @return object|false Site object if successful. False when no site is found.
     147 * @return WP_Site|false Site object if successful. False when no site is found.
    147148 */
    148149function get_site_by_path( $domain, $path, $segments = null ) {
     
    187188     * @since 3.9.0
    188189     *
    189      * @param null|bool|object $site     Site value to return by path.
    190      * @param string           $domain   The requested domain.
    191      * @param string           $path     The requested path, in full.
    192      * @param int|null         $segments The suggested number of paths to consult.
    193      *                                   Default null, meaning the entire path was to be consulted.
    194      * @param array            $paths    The paths to search for, based on $path and $segments.
     190     * @param null|bool|WP_Site $site     Site value to return by path.
     191     * @param string            $domain   The requested domain.
     192     * @param string            $path     The requested path, in full.
     193     * @param int|null          $segments The suggested number of paths to consult.
     194     *                                    Default null, meaning the entire path was to be consulted.
     195     * @param array             $paths    The paths to search for, based on $path and $segments.
    195196     */
    196197    $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
    197198    if ( null !== $pre ) {
     199        if ( false !== $pre && ! $pre instanceof WP_Site ) {
     200            $pre = new WP_Site( $pre );
     201        }
    198202        return $pre;
    199203    }
  • trunk/tests/phpunit/tests/multisite/bootstrap.php

    r37475 r38681  
    209209    }
    210210
     211    /**
     212     * @ticket 37053
     213     */
     214    public function test_get_site_by_path_returns_wp_site() {
     215        add_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10, 3 );
     216
     217        $site = get_site_by_path( 'example.com', '/foo/' );
     218
     219        remove_filter( 'pre_get_site_by_path', array( $this, 'filter_pre_get_site_by_path' ), 10 );
     220
     221        $this->assertInstanceOf( 'WP_Site', $site );
     222    }
     223
    211224    public function filter_path_segments_to_two() {
    212225        return 2;
    213226    }
     227
     228    public function filter_pre_get_site_by_path( $site, $domain, $path ) {
     229        $site = new stdClass();
     230        $site->blog_id = 100;
     231        $site->domain = $domain;
     232        $site->path = $path;
     233        $site->site_id = 1;
     234
     235        return $site;
     236    }
    214237}
    215238
Note: See TracChangeset for help on using the changeset viewer.