Make WordPress Core


Ignore:
Timestamp:
09/13/2015 11:39:20 PM (6 years ago)
Author:
jeremyfelt
Message:

Multisite: Implement the get_by_path method in WP_Network.

Move the internals of get_network_by_path() to WP_Network() and allow network objects to be retrieved by passing a requested domain and path.

Props johnjamesjacoby, jeremyfelt, drewapicture, wonderboymusic.
See #31985.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-network.php

    r34097 r34099  
    152152        }
    153153    }
     154
     155    /**
     156     * Retrieve a network by its domain and path.
     157     *
     158     * @since 4.4.0
     159     * @access public
     160     * @static
     161     *
     162     * @param string   $domain   Domain to check.
     163     * @param string   $path     Path to check.
     164     * @param int|null $segments Path segments to use. Defaults to null, or the full path.
     165     * @return WP_Network|bool Network object if successful. False when no network is found.
     166     */
     167    public static function get_by_path( $domain = '', $path = '', $segments = null ) {
     168        global $wpdb;
     169
     170        $domains = array( $domain );
     171        $pieces  = explode( '.', $domain );
     172
     173        /*
     174         * It's possible one domain to search is 'com', but it might as well
     175         * be 'localhost' or some other locally mapped domain.
     176         */
     177        while ( array_shift( $pieces ) ) {
     178            if ( ! empty( $pieces ) ) {
     179                $domains[] = implode( '.', $pieces );
     180            }
     181        }
     182
     183        /*
     184         * If we've gotten to this function during normal execution, there is
     185         * more than one network installed. At this point, who knows how many
     186         * we have. Attempt to optimize for the situation where networks are
     187         * only domains, thus meaning paths never need to be considered.
     188         *
     189         * This is a very basic optimization; anything further could have
     190         * drawbacks depending on the setup, so this is best done per-install.
     191         */
     192        $using_paths = true;
     193        if ( wp_using_ext_object_cache() ) {
     194            $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
     195            if ( false === $using_paths ) {
     196                $using_paths = $wpdb->get_var( "SELECT id FROM {$wpdb->site} WHERE path <> '/' LIMIT 1" );
     197                wp_cache_add( 'networks_have_paths', (int) $using_paths, 'site-options'  );
     198            }
     199        }
     200
     201        $paths = array();
     202        if ( true === $using_paths ) {
     203            $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
     204
     205            /**
     206             * Filter the number of path segments to consider when searching for a site.
     207             *
     208             * @since 3.9.0
     209             *
     210             * @param int|null $segments The number of path segments to consider. WordPress by default looks at
     211             *                           one path segment. The function default of null only makes sense when you
     212             *                           know the requested path should match a network.
     213             * @param string   $domain   The requested domain.
     214             * @param string   $path     The requested path, in full.
     215             */
     216            $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
     217
     218            if ( ( null !== $segments ) && count( $path_segments ) > $segments ) {
     219                $path_segments = array_slice( $path_segments, 0, $segments );
     220            }
     221
     222            while ( count( $path_segments ) ) {
     223                $paths[] = '/' . implode( '/', $path_segments ) . '/';
     224                array_pop( $path_segments );
     225            }
     226
     227            $paths[] = '/';
     228        }
     229
     230        /**
     231         * Determine a network by its domain and path.
     232         *
     233         * This allows one to short-circuit the default logic, perhaps by
     234         * replacing it with a routine that is more optimal for your setup.
     235         *
     236         * Return null to avoid the short-circuit. Return false if no network
     237         * can be found at the requested domain and path. Otherwise, return
     238         * an object from wp_get_network().
     239         *
     240         * @since 3.9.0
     241         *
     242         * @param null|bool|object $network  Network value to return by path.
     243         * @param string           $domain   The requested domain.
     244         * @param string           $path     The requested path, in full.
     245         * @param int|null         $segments The suggested number of paths to consult.
     246         *                                   Default null, meaning the entire path was to be consulted.
     247         * @param array            $paths    The paths to search for, based on $path and $segments.
     248         */
     249        $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
     250        if ( null !== $pre ) {
     251            return $pre;
     252        }
     253
     254        // @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
     255        // We already have paths covered. What about how far domains should be drilled down (including www)?
     256
     257        $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
     258
     259        if ( false === $using_paths ) {
     260            $network = $wpdb->get_row( "
     261                SELECT * FROM {$wpdb->site}
     262                WHERE domain IN ({$search_domains})
     263                ORDER BY CHAR_LENGTH(domain)
     264                DESC LIMIT 1
     265            " );
     266
     267            if ( ! empty( $network ) && ! is_wp_error( $network ) ) {
     268                return new WP_Network( $network );
     269            }
     270
     271            return false;
     272
     273        } else {
     274            $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     275            $networks = $wpdb->get_results( "
     276                SELECT * FROM {$wpdb->site}
     277                WHERE domain IN ({$search_domains})
     278                AND path IN ({$search_paths})
     279                ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC
     280            " );
     281        }
     282
     283        /*
     284         * Domains are sorted by length of domain, then by length of path.
     285         * The domain must match for the path to be considered. Otherwise,
     286         * a network with the path of / will suffice.
     287         */
     288        $found = false;
     289        foreach ( $networks as $network ) {
     290            if ( ( $network->domain === $domain ) || ( "www.{$network->domain}" === $domain ) ) {
     291                if ( in_array( $network->path, $paths, true ) ) {
     292                    $found = true;
     293                    break;
     294                }
     295            }
     296            if ( $network->path === '/' ) {
     297                $found = true;
     298                break;
     299            }
     300        }
     301
     302        if ( true === $found ) {
     303            return new WP_Network( $network );
     304        }
     305
     306        return false;
     307    }
    154308}
Note: See TracChangeset for help on using the changeset viewer.