Make WordPress Core

Ticket #35791: 35791.1.patch

File 35791.1.patch, 122.7 KB (added by spacedmonkey, 9 years ago)
  • wp-content/themes/twentyfifteen/header.php

     
    5050        </div><!-- .sidebar -->
    5151
    5252        <div id="content" class="site-content">
     53<?php wp_update_network_site_counts();
     54 No newline at end of file
  • wp-includes/class-wp-site-query.php

     
     1<?php
     2
     3/**
     4 * Site API: WP_Site_Query class
     5 *
     6 * @package WordPress
     7 * @subpackage Sites
     8 * @since 4.6.0
     9 */
     10
     11/**
     12 * Core class used for querying sites.
     13 *
     14 * @since 4.6.0
     15 *
     16 * @see WP_Site_Query::__construct() for accepted arguments.
     17 */
     18class WP_Site_Query {
     19
     20        /**
     21         * SQL for database query.
     22         *
     23         * @since 4.6.0
     24         * @access public
     25         * @var string
     26         */
     27        public $request;
     28
     29        /**
     30         * SQL query clauses.
     31         *
     32         * @since 4.6.0
     33         * @access protected
     34         * @var array
     35         */
     36        protected $sql_clauses = array(
     37                'select'  => '',
     38                'from'    => '',
     39                'where'   => array(),
     40                'groupby' => '',
     41                'orderby' => '',
     42                'limits'  => '',
     43        );
     44
     45        /**
     46         * SQL WHERE clause.
     47         *
     48         * Stored after the 'sites_clauses' filter is run on the compiled WHERE sub-clauses.
     49         *
     50         * @since 4.6.0
     51         * @access protected
     52         * @var string
     53         */
     54        protected $filtered_where_clause;
     55
     56        /**
     57         * Date query container
     58         *
     59         * @since 4.6.0
     60         * @access public
     61         * @var object WP_Date_Query
     62         */
     63        public $date_query = false;
     64
     65        /**
     66         * Query vars set by the user.
     67         *
     68         * @since 4.6.0
     69         * @access public
     70         * @var array
     71         */
     72        public $query_vars;
     73
     74        /**
     75         * Default values for query vars.
     76         *
     77         * @since 4.6.0
     78         * @access public
     79         * @var array
     80         */
     81        public $query_var_defaults;
     82
     83        /**
     84         * List of sites located by the query.
     85         *
     86         * @since 4.6.0
     87         * @access public
     88         * @var array
     89         */
     90        public $sites;
     91
     92        /**
     93         * The amount of found sites for the current query.
     94         *
     95         * @since 4.6.0
     96         * @access public
     97         * @var int
     98         */
     99        public $found_sites = 0;
     100
     101        /**
     102         * The number of pages.
     103         *
     104         * @since 4.6.0
     105         * @access public
     106         * @var int
     107         */
     108        public $max_num_pages = 0;
     109
     110        /**
     111         * Make private/protected methods readable for backwards compatibility.
     112         *
     113         * @since 4.6.0
     114         * @access public
     115         *
     116         * @param callable $name Method to call.
     117         * @param array $arguments Arguments to pass when calling.
     118         *
     119         * @return mixed|false Return value of the callback, false otherwise.
     120         */
     121        public function __call( $name, $arguments ) {
     122                if ( 'get_search_sql' === $name ) {
     123                        return call_user_func_array( array( $this, $name ), $arguments );
     124                }
     125
     126                return false;
     127        }
     128
     129        /**
     130         * Constructor.
     131         *
     132         * Sets up the site query, based on the query vars passed.
     133         *
     134         * @since 4.6.0
     135         * @access public
     136         *
     137         * @param string|array $query {
     138         *     Optional. Array or query string of site query parameters. Default empty.
     139         *
     140         * @type array $site__in Array of site IDs to include. Default empty.
     141         * @type array $site__not_in Array of site IDs to exclude. Default empty.
     142         * @type bool $count Whether to return a site count (true) or array of
     143         *                                                   site objects (false). Default false.
     144         * @type array $date_query Date query clauses to limit sites by. See WP_Date_Query.
     145         *                                                   Default null.
     146         * @type string $fields Site fields to return. Accepts 'ids' for site IDs
     147         *                                                   only or empty for all fields. Default empty.
     148         * @type int $ID Currently unused.
     149         * @type int $number Maximum number of sites to retrieve.
     150         *                                                   Default null (no limit).
     151         * @type int $offset Number of sites to offset the query. Used to build
     152         *                                                   LIMIT clause. Default 0.
     153         * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query.
     154         *                                                   Default: true.
     155         * @type string|array $orderby Site status or array of statuses. To use 'meta_value'
     156         *                                                   or 'meta_value_num', `$meta_key` must also be defined.
     157         *                                                   To sort by a specific `$meta_query` clause, use that
     158         *                                                   clause's array key. Accepts 'site_agent',
     159         *                                                   'site_approved', 'site_author',
     160         *                                                   'site_author_email', 'site_author_IP',
     161         *                                                   'site_author_url', 'site_content', 'site_date',
     162         *                                                   'site_date_gmt', 'blog_id', 'site_karma',
     163         *                                                   'site_parent', 'site_id', 'site_type',
     164         *                                                   'user_id', 'site__in', 'meta_value', 'meta_value_num',
     165         *                                                   the value of $meta_key, and the array keys of
     166         *                                                   `$meta_query`. Also accepts false, an empty array, or
     167         *                                                   'none' to disable `ORDER BY` clause.
     168         *                                                   Default: 'site_date_gmt'.
     169         * @type string $order How to order retrieved sites. Accepts 'ASC', 'DESC'.
     170         *                                                   Default: 'DESC'.
     171         * @type string $domain Limit results to those affiliated with a given network ID.
     172         *                                                   Default current network id.
     173         * @type array $domain__in Array of domains to include affiliated sites for.
     174         *                                                   Default empty.
     175         * @type array $domain__not_in Array of domains to exclude affiliated sites for.
     176         *                                                   Default empty.
     177         * @type string $path Limit results to those affiliated with a given network ID.
     178         *                                                   Default current network id.
     179         * @type array $path__in Array of paths to include affiliated sites for.
     180         *                                                   Default empty.
     181         * @type array $path__not_in Array of paths to exclude affiliated sites for.
     182         *                                                   Default empty.
     183         * @type int $network_id Limit results to those affiliated with a given network ID.
     184         *                                                   Default current network id.
     185         * @type array $network__in Array of network IDs to include affiliated sites for.
     186         *                                                   Default empty.
     187         * @type array $network__not_in Array of network IDs to exclude affiliated sites for.
     188         *                                                   Default empty.
     189         * @type string $search Search term(s) to retrieve matching sites for.
     190         *                                                   Default empty.
     191         * @type bool $update_site_cache Whether to prime the cache for site networks.
     192         *                                                   Default false.
     193         * }
     194         */
     195        public function __construct( $query = '' ) {
     196                $this->query_var_defaults = array(
     197                        'fields'            => '',
     198                        'ID'                => '',
     199                        'site__in'          => '',
     200                        'site__not_in'      => '',
     201                        'number'            => '',
     202                        'offset'            => '',
     203                        'no_found_rows'     => true,
     204                        'orderby'           => '',
     205                        'order'             => 'DESC',
     206                        'network_id'        => 0,
     207                        'network__in'       => '',
     208                        'network__not_in'   => '',
     209                        'domain'            => '',
     210                        'domain__in'        => '',
     211                        'domain__not_in'    => '',
     212                        'path'              => '',
     213                        'path__in'          => '',
     214                        'path__not_in'      => '',
     215                        'public'            => 'all',
     216                        'archived'          => 'all',
     217                        'mature'            => 'all',
     218                        'spam'              => 'all',
     219                        'deleted'           => 'all',
     220                        'search'            => '',
     221                        'count'             => false,
     222                        'date_query'        => null, // See WP_Date_Query
     223                        'update_site_cache' => true,
     224                );
     225
     226                if ( ! empty( $query ) ) {
     227                        $this->query( $query );
     228                }
     229        }
     230
     231        /**
     232         * Parse arguments passed to the site query with default query parameters.
     233         *
     234         * @since  4.6.0 Extracted from WP_Site_Query::query().
     235         *
     236         * @access public
     237         *
     238         * @param string|array $query WP_Site_Query arguments. See WP_Site_Query::__construct()
     239         */
     240        public function parse_query( $query = '' ) {
     241                if ( empty( $query ) ) {
     242                        $query = $this->query_vars;
     243                }
     244
     245                $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
     246                do_action_ref_array( 'parse_site_query', array( &$this ) );
     247        }
     248
     249        /**
     250         * Sets up the WordPress query for retrieving sites.
     251         *
     252         * @since 4.6.0
     253         * @access public
     254         *
     255         * @param string|array $query Array or URL query string of parameters.
     256         *
     257         * @return array|int List of sites, or number of sites when 'count' is passed as a query var.
     258         */
     259        public function query( $query ) {
     260                $this->query_vars = wp_parse_args( $query );
     261
     262                return $this->get_sites();
     263        }
     264
     265        /**
     266         * Get a list of sites matching the query vars.
     267         *
     268         * @since 4.6.0
     269         * @access public
     270         *
     271         * @global wpdb $wpdb WordPress database abstraction object.
     272         *
     273         * @return int|array The list of sites.
     274         */
     275        public function get_sites() {
     276                global $wpdb;
     277
     278                $this->parse_query();
     279
     280
     281                /**
     282                 * Fires before sites are retrieved.
     283                 *
     284                 * @since 4.6.0
     285                 *
     286                 * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference.
     287                 */
     288                do_action_ref_array( 'pre_get_sites', array( &$this ) );
     289
     290                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
     291                $key          = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
     292                $last_changed = wp_cache_get( 'last_changed', 'site' );
     293                if ( ! $last_changed ) {
     294                        $last_changed = microtime();
     295                        wp_cache_set( 'last_changed', $last_changed, 'site' );
     296                }
     297                $cache_key = "get_site_ids:$key:$last_changed";
     298
     299                $site_ids = wp_cache_get( $cache_key, 'site' );
     300                if ( false === $site_ids ) {
     301                        $site_ids = $this->get_site_ids();
     302                        wp_cache_add( $cache_key, $site_ids, 'site' );
     303                }
     304
     305                // If querying for a count only, there's nothing more to do.
     306                if ( $this->query_vars['count'] ) {
     307                        // $site_ids is actually a count in this case.
     308                        return intval( $site_ids );
     309                }
     310
     311                $site_ids = array_map( 'intval', $site_ids );
     312
     313                $this->site_count = count( $this->sites );
     314
     315                if ( $site_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
     316                        /**
     317                         * Filter the query used to retrieve found site count.
     318                         *
     319                         * @since 4.6.0
     320                         *
     321                         * @param string $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'.
     322                         * @param WP_Site_Query $site_query The `WP_Site_Query` instance.
     323                         */
     324                        $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this );
     325                        $this->found_sites = (int) $wpdb->get_var( $found_sites_query );
     326
     327                        $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
     328                }
     329
     330                if ( 'ids' == $this->query_vars['fields'] ) {
     331                        $this->sites = $site_ids;
     332
     333                        return $this->sites;
     334                }
     335
     336                // Fetch full site objects from the primed cache.
     337                $_sites = array();
     338                foreach ( $site_ids as $site_id ) {
     339                        if ( $_site = get_site( $site_id ) ) {
     340                                $_sites[] = $_site;
     341                        }
     342                }
     343
     344                // Prime site network caches.
     345                if ( $this->query_vars['update_site_cache'] ) {
     346                        $site_ids = array();
     347                        foreach ( $_sites as $_site ) {
     348                                $site_ids[] = $_site->site_id;
     349                        }
     350                        _prime_site_caches( $site_ids );
     351                }
     352
     353                /**
     354                 * Filter the site query results.
     355                 *
     356                 * @since 4.6.0
     357                 *
     358                 * @param array $results An array of sites.
     359                 * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference.
     360                 */
     361                $_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) );
     362
     363                // Convert to WP_Site instances
     364                $sites = array_map( 'get_site', $_sites );
     365
     366                $this->sites = $sites;
     367
     368                return $this->sites;
     369        }
     370
     371        /**
     372         * Used internally to get a list of site IDs matching the query vars.
     373         *
     374         * @since 4.6.0
     375         * @access protected
     376         *
     377         * @global wpdb $wpdb WordPress database abstraction object.
     378         */
     379        protected function get_site_ids() {
     380                global $wpdb;
     381
     382                $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
     383
     384                // Disable ORDER BY with 'none', an empty array, or boolean false.
     385                if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
     386                        $orderby = '';
     387                } elseif ( ! empty( $this->query_vars['orderby'] ) ) {
     388                        $ordersby = is_array( $this->query_vars['orderby'] ) ?
     389                                $this->query_vars['orderby'] :
     390                                preg_split( '/[,\s]/', $this->query_vars['orderby'] );
     391
     392                        $orderby_array         = array();
     393                        $found_orderby_blog_id = false;
     394                        foreach ( $ordersby as $_key => $_value ) {
     395                                if ( ! $_value ) {
     396                                        continue;
     397                                }
     398
     399                                if ( is_int( $_key ) ) {
     400                                        $_orderby = $_value;
     401                                        $_order   = $order;
     402                                } else {
     403                                        $_orderby = $_key;
     404                                        $_order   = $_value;
     405                                }
     406
     407                                if ( ! $found_orderby_blog_id && in_array( $_orderby, array( 'blog_id', 'site__in' ) ) ) {
     408                                        $found_orderby_blog_id = true;
     409                                }
     410
     411                                $parsed = $this->parse_orderby( $_orderby );
     412
     413                                if ( ! $parsed ) {
     414                                        continue;
     415                                }
     416
     417                                if ( 'site__in' === $_orderby ) {
     418                                        $orderby_array[] = $parsed;
     419                                        continue;
     420                                }
     421
     422                                $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
     423                        }
     424
     425                        $orderby = implode( ', ', $orderby_array );
     426                } else {
     427                        $orderby = "blog_id $order";
     428                }
     429
     430                $number = absint( $this->query_vars['number'] );
     431                $offset = absint( $this->query_vars['offset'] );
     432
     433                if ( ! empty( $number ) ) {
     434                        if ( $offset ) {
     435                                $limits = 'LIMIT ' . $offset . ',' . $number;
     436                        } else {
     437                                $limits = 'LIMIT ' . $number;
     438                        }
     439                }
     440
     441                if ( $this->query_vars['count'] ) {
     442                        $fields = 'COUNT(*)';
     443                } else {
     444                        $fields = "blog_id";
     445                }
     446
     447                // Parse site IDs for an IN clause.
     448                $site_id = absint( $this->query_vars['ID'] );
     449                if ( ! empty( $site_id ) ) {
     450                        $this->sql_clauses['where']['ID'] = $wpdb->prepare( 'blog_id = %d', $site_id );
     451                }
     452
     453                // Parse site IDs for an IN clause.
     454                if ( ! empty( $this->query_vars['site__in'] ) ) {
     455                        $this->sql_clauses['where']['site__in'] = "blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
     456                }
     457
     458                // Parse site IDs for a NOT IN clause.
     459                if ( ! empty( $this->query_vars['site__not_in'] ) ) {
     460                        $this->sql_clauses['where']['site__not_in'] = "blog_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )';
     461                }
     462
     463                $network_id = absint( $this->query_vars['network_id'] );
     464                if ( ! empty( $network_id ) ) {
     465                        $this->sql_clauses['where']['network_id'] = $wpdb->prepare( 'site_id = %d', $network_id );
     466                }
     467
     468                // Parse site network IDs for an IN clause.
     469                if ( ! empty( $this->query_vars['network__in'] ) ) {
     470                        $this->sql_clauses['where']['network__in'] = 'site_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )';
     471                }
     472
     473                // Parse site network IDs for a NOT IN clause.
     474                if ( ! empty( $this->query_vars['network__not_in'] ) ) {
     475                        $this->sql_clauses['where']['network__not_in'] = 'site_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )';
     476                }
     477
     478
     479                if ( ! empty( $this->query_vars['domain'] ) ) {
     480                        $this->sql_clauses['where']['domain'] = $wpdb->prepare( 'domain = %s', $this->query_vars['domain'] );
     481                }
     482
     483                // Parse site domain for an IN clause.
     484                if ( is_array( $this->query_vars['domain__in'] ) ) {
     485                        $this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
     486                }
     487
     488                // Parse site domain for a NOT IN clause.
     489                if ( is_array( $this->query_vars['domain__not_in'] ) ) {
     490                        $this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
     491                }
     492
     493
     494                if ( ! empty( $this->query_vars['path'] ) ) {
     495                        $this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] );
     496                }
     497
     498                // Parse site path for an IN clause.
     499                if ( is_array( $this->query_vars['path__in'] ) ) {
     500                        $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
     501                }
     502
     503                // Parse site path for a NOT IN clause.
     504                if ( is_array( $this->query_vars['path__not_in'] ) ) {
     505                        $this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
     506                }
     507
     508
     509                if ( is_numeric( $this->query_vars['archived'] ) ) {
     510                        $archived                               = absint( $this->query_vars['archived'] );
     511                        $this->sql_clauses['where']['archived'] = $wpdb->prepare( "archived = %d ", $archived );
     512                }
     513
     514                if ( is_numeric( $this->query_vars['mature'] ) ) {
     515                        $mature                               = absint( $this->query_vars['mature'] );
     516                        $this->sql_clauses['where']['mature'] = $wpdb->prepare( "mature = %d ", $mature );
     517                }
     518
     519                if ( is_numeric( $this->query_vars['spam'] ) ) {
     520                        $spam                               = absint( $this->query_vars['spam'] );
     521                        $this->sql_clauses['where']['spam'] = $wpdb->prepare( "spam = %d ", $spam );
     522                }
     523
     524                if ( is_numeric( $this->query_vars['deleted'] ) ) {
     525                        $deleted                               = absint( $this->query_vars['deleted'] );
     526                        $this->sql_clauses['where']['deleted'] = $wpdb->prepare( "deleted = %d ", $deleted );
     527                }
     528
     529                // Falsy search strings are ignored.
     530                if ( strlen( $this->query_vars['search'] ) ) {
     531                        $search_sql = $this->get_search_sql(
     532                                $this->query_vars['search'],
     533                                array( 'domain', 'path' )
     534                        );
     535
     536                        // Strip leading 'AND'.
     537                        $this->sql_clauses['where']['search'] = preg_replace( '/^\s*AND\s*/', '', $search_sql );
     538                }
     539
     540                $date_query = $this->query_vars['date_query'];
     541                if ( ! empty( $date_query ) && is_array( $date_query ) ) {
     542                        $date_query_object                        = new WP_Date_Query( $date_query, 'registered' );
     543                        $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
     544                }
     545
     546                $where = implode( ' AND ', $this->sql_clauses['where'] );
     547
     548                $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
     549                /**
     550                 * Filter the site query clauses.
     551                 *
     552                 * @since 4.6.0
     553                 *
     554                 * @param array $pieces A compacted array of site query clauses.
     555                 * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference.
     556                 */
     557                $clauses = apply_filters_ref_array( 'sites_clauses', array( compact( $pieces ), &$this ) );
     558                $fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
     559                $join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
     560                $where   = isset( $clauses['where'] ) ? $clauses['where'] : '';
     561                $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
     562                $limits  = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
     563                $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : '';
     564
     565                $this->filtered_where_clause = $where;
     566
     567                if ( $where ) {
     568                        $where = 'WHERE ' . $where;
     569                }
     570
     571                if ( $groupby ) {
     572                        $groupby = 'GROUP BY ' . $groupby;
     573                }
     574
     575                if ( $orderby ) {
     576                        $orderby = "ORDER BY $orderby";
     577                }
     578
     579                $found_rows = '';
     580                if ( ! $this->query_vars['no_found_rows'] ) {
     581                        $found_rows = 'SQL_CALC_FOUND_ROWS';
     582                }
     583
     584                $this->sql_clauses['select']  = "SELECT $found_rows $fields";
     585                $this->sql_clauses['from']    = "FROM $wpdb->blogs $join";
     586                $this->sql_clauses['groupby'] = $groupby;
     587                $this->sql_clauses['orderby'] = $orderby;
     588                $this->sql_clauses['limits']  = $limits;
     589
     590                $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
     591
     592                if ( $this->query_vars['count'] ) {
     593                        return intval( $wpdb->get_var( $this->request ) );
     594                } else {
     595                        $site_ids = $wpdb->get_col( $this->request );
     596
     597                        return array_map( 'intval', $site_ids );
     598                }
     599        }
     600
     601        /**
     602         * Used internally to generate an SQL string for searching across multiple columns
     603         *
     604         * @since 4.6.0
     605         * @access protected
     606         *
     607         * @global wpdb $wpdb WordPress database abstraction object.
     608         *
     609         * @param string $string
     610         * @param array $cols
     611         *
     612         * @return string
     613         */
     614        protected function get_search_sql( $string, $cols ) {
     615                global $wpdb;
     616
     617                $like = '%' . $wpdb->esc_like( $string ) . '%';
     618
     619                $searches = array();
     620                foreach ( $cols as $col ) {
     621                        $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
     622                }
     623
     624                return ' AND (' . implode( ' OR ', $searches ) . ')';
     625        }
     626
     627        /**
     628         * Parse and sanitize 'orderby' keys passed to the site query.
     629         *
     630         * @since 4.6.0
     631         * @access protected
     632         *
     633         * @global wpdb $wpdb WordPress database abstraction object.
     634         *
     635         * @param string $orderby Alias for the field to order by.
     636         *
     637         * @return string|false Value to used in the ORDER clause. False otherwise.
     638         */
     639        protected function parse_orderby( $orderby ) {
     640                global $wpdb;
     641
     642                switch ( $orderby ) {
     643                        case 'domain':
     644                        case 'last_updated':
     645                        case 'path':
     646                        case 'registered':
     647                                $_orderby = $orderby;
     648                                break;
     649                        case 'network_id':
     650                                $_orderby = "site_id";
     651                                break;
     652                        case 'ids':
     653                        default:
     654                                $_orderby = "blog_id";
     655                                break;
     656                }
     657
     658                $parsed = "$_orderby";
     659
     660
     661                return $parsed;
     662        }
     663
     664        /**
     665         * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
     666         *
     667         * @since 4.6.0
     668         * @access protected
     669         *
     670         * @param string $order The 'order' query variable.
     671         *
     672         * @return string The sanitized 'order' query variable.
     673         */
     674        protected function parse_order( $order ) {
     675                if ( ! is_string( $order ) || empty( $order ) ) {
     676                        return 'DESC';
     677                }
     678
     679                if ( 'ASC' === strtoupper( $order ) ) {
     680                        return 'ASC';
     681                } else {
     682                        return 'DESC';
     683                }
     684        }
     685}
  • wp-includes/date.php

    Property changes on: wp-includes/class-wp-site-query.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
    489489                global $wpdb;
    490490
    491491                $valid_columns = array(
    492                         'post_date', 'post_date_gmt', 'post_modified',
    493                         'post_modified_gmt', 'comment_date', 'comment_date_gmt',
     492                        'post_date',
     493                        'post_date_gmt',
     494                        'post_modified',
     495                        'post_modified_gmt',
     496                        'comment_date',
     497                        'comment_date_gmt',
    494498                        'user_registered',
     499                        'registered',
     500                        'last_updated',
    495501                );
    496502
    497503                // Attempt to detect a table prefix.
     
    525531                                $wpdb->users => array(
    526532                                        'user_registered',
    527533                                ),
     534                                $wpdb->blogs => array(
     535                                        'registered',
     536                                        'last_updated',
     537                                ),
    528538                        );
    529539
    530540                        // If it's a known column name, add the appropriate table prefix.
  • wp-includes/http.php

     
    607607 * @return bool
    608608 */
    609609function ms_allowed_http_request_hosts( $is_external, $host ) {
    610         global $wpdb;
    611610        static $queried = array();
    612         if ( $is_external )
     611        if ( $is_external ) {
    613612                return $is_external;
    614         if ( $host === get_current_site()->domain )
     613        }
     614        if ( $host === get_current_site()->domain ) {
    615615                return true;
    616         if ( isset( $queried[ $host ] ) )
     616        }
     617        if ( isset( $queried[ $host ] ) ) {
    617618                return $queried[ $host ];
    618         $queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) );
     619        }
     620        $query            = new WP_Site_Query();
     621        $result           = $query->query( array( 'domain' => $host, 'count' => true ) );
     622        $queried[ $host ] = (bool) $result;
     623
    619624        return $queried[ $host ];
    620625}
    621626
  • wp-includes/ms-blogs.php

     
    9999                $path = $current_site->path . $slug . '/';
    100100        }
    101101
    102         $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
     102        $query   = new WP_Site_Query();
     103        $result  = $query->query( array( 'domain' => $domain, 'path' => $path, 'fields' => 'ids' ) );
     104        $blog_id = array_shift( $result );
     105
    103106        wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
     107
    104108        return $blog_id;
    105109}
    106110
     
    126130                } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
    127131                        $key = md5( $fields['domain'] . $fields['path'] );
    128132                        $blog = wp_cache_get($key, 'blog-lookup');
    129                         if ( false !== $blog )
     133                        if ( false !== $blog ) {
    130134                                return $blog;
     135                        }
     136
     137                        $domains = array( $fields['domain'] );
    131138                        if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
    132                                 $nowww = substr( $fields['domain'], 4 );
    133                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
    134                         } else {
    135                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
     139                                $domains[] = substr( $fields['domain'], 4 );
    136140                        }
     141
     142                        $query  = new WP_Site_Query();
     143                        $args = array( 'domain__in' => $domains, 'path' => $fields['path'], 'number' => 1 );
     144                        $result = $query->query( $args );
     145                        $blog   = array_shift( $result );
     146
    137147                        if ( $blog ) {
    138148                                wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
    139149                                $blog_id = $blog->blog_id;
     
    143153                } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
    144154                        $key = md5( $fields['domain'] );
    145155                        $blog = wp_cache_get($key, 'blog-lookup');
    146                         if ( false !== $blog )
     156                        if ( false !== $blog ) {
    147157                                return $blog;
     158                        }
     159                        $domains = array($fields['domain']);
    148160                        if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
    149                                 $nowww = substr( $fields['domain'], 4 );
    150                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
    151                         } else {
    152                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
     161                                $domains[] = substr( $fields['domain'], 4 );
    153162                        }
     163
     164                        $query = new WP_Site_Query();
     165                        $args = array( 'domain__in' => $domains, 'number' => 1 );
     166                        $result = $query->query( $args );
     167                        $blog   = array_shift( $result );
     168
    154169                        if ( $blog ) {
    155170                                wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
    156171                                $blog_id = $blog->blog_id;
     
    457472}
    458473
    459474/**
     475 * Retrieves site data given a site ID or site object.
     476 *
     477 * If an object is passed then the site data will be cached and then returned
     478 * after being passed through a filter. If the site is empty, then the global
     479 * site variable will be used, if it is set.
     480 *
     481 * @since 4.6.0
     482 *
     483 * @global WP_Site $site
     484 *
     485 * @param WP_Site|string|int $site Site to retrieve.
     486 * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants.
     487 *
     488 * @return WP_Site|array|null Depends on $output value.
     489 */
     490function get_site( &$site = null, $output = OBJECT ) {
     491        global $current_blog;
     492        if ( empty( $site ) && isset( $current_blog ) ) {
     493                $site = $current_blog;
     494        }
     495
     496        if ( $site instanceof WP_Site ) {
     497                $_site = $site;
     498        } elseif ( is_object( $site ) ) {
     499                $_site = new WP_Site( $site );
     500        } else {
     501                $_site = WP_Site::get_instance( $site );
     502        }
     503
     504        if ( ! $_site ) {
     505                return null;
     506        }
     507
     508        /**
     509         * Fires after a site is retrieved.
     510         *
     511         * @since 4.6.0
     512         *
     513         * @param mixed $_site Site data.
     514         */
     515        $_site = apply_filters( 'get_site', $_site );
     516
     517        if ( $output == OBJECT ) {
     518                return $_site;
     519        } elseif ( $output == ARRAY_A ) {
     520                return $_site->to_array();
     521        } elseif ( $output == ARRAY_N ) {
     522                return array_values( $_site->to_array() );
     523        }
     524
     525        return $_site;
     526}
     527
     528/**
     529 * Adds any sites from the given ids to the cache that do not already exist in cache
     530 *
     531 * @since 4.6.0
     532 * @access private
     533 *
     534 * @see update_site_cache()
     535 *
     536 * @global wpdb $wpdb WordPress database abstraction object.
     537 *
     538 * @param array $ids ID list.
     539 */
     540function _prime_site_caches( $ids ) {
     541        global $wpdb;
     542
     543        $non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
     544        if ( ! empty( $non_cached_ids ) ) {
     545                $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", $non_cached_ids ) ) );
     546
     547                update_site_cache( $fresh_sites );
     548        }
     549}
     550
     551/**
     552 * Updates sites in cache.
     553 *
     554 * @since 4.6.0
     555 *
     556 * @param array $sites Array of site objects, passed by reference.
     557 */
     558function update_site_cache( &$sites ) {
     559        if ( ! $sites ) {
     560                return;
     561        }
     562
     563        foreach ( $sites as $site ) {
     564                wp_cache_add( $site->blog_id, $site, 'sites' );
     565        }
     566}
     567
     568/**
    460569 * Retrieve option value for a given blog id based on name of option.
    461570 *
    462571 * If the option does not exist or does not have a value, then the return value
  • wp-includes/ms-functions.php

     
    3737 * @global wpdb $wpdb WordPress database abstraction object.
    3838 *
    3939 * @param int $user_id The unique ID of the user
     40 *
    4041 * @return WP_Site|void The blog object
    4142 */
    4243function get_active_blog_for_user( $user_id ) {
    4344        global $wpdb;
    4445        $blogs = get_blogs_of_user( $user_id );
    45         if ( empty( $blogs ) )
     46        if ( empty( $blogs ) ) {
    4647                return;
     48        }
    4749
    48         if ( !is_multisite() )
    49                 return $blogs[$wpdb->blogid];
     50        if ( ! is_multisite() ) {
     51                return $blogs[ $wpdb->blogid ];
     52        }
    5053
    5154        $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
    52         $first_blog = current($blogs);
     55        $first_blog   = current( $blogs );
    5356        if ( false !== $primary_blog ) {
    5457                if ( ! isset( $blogs[ $primary_blog ] ) ) {
    5558                        update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
     
    6669
    6770        if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
    6871                $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
    69                 $ret = false;
     72                $ret   = false;
    7073                if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
    7174                        foreach ( (array) $blogs as $blog_id => $blog ) {
    72                                 if ( $blog->site_id != $wpdb->siteid )
     75                                if ( $blog->site_id != $wpdb->siteid ) {
    7376                                        continue;
     77                                }
    7478                                $details = get_blog_details( $blog_id );
    7579                                if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
    7680                                        $ret = $blog;
    77                                         if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
     81                                        if ( get_user_meta( $user_id, 'primary_blog', true ) != $blog_id ) {
    7882                                                update_user_meta( $user_id, 'primary_blog', $blog_id );
    79                                         if ( !get_user_meta($user_id , 'source_domain', true) )
     83                                        }
     84                                        if ( ! get_user_meta( $user_id, 'source_domain', true ) ) {
    8085                                                update_user_meta( $user_id, 'source_domain', $blog->domain );
     86                                        }
    8187                                        break;
    8288                                }
    8389                        }
     
    8490                } else {
    8591                        return;
    8692                }
     93
    8794                return $ret;
    8895        } else {
    8996                return $primary;
     
    111118 * @since MU 1.0
    112119 *
    113120 * @param int $network_id Deprecated, not supported.
     121 *
    114122 * @return int
    115123 */
    116124function get_blog_count( $network_id = 0 ) {
    117         if ( func_num_args() )
     125        if ( func_num_args() ) {
    118126                _deprecated_argument( __FUNCTION__, '3.1' );
     127        }
    119128
    120129        return get_site_option( 'blog_count' );
    121130}
     
    127136 *
    128137 * @param int $blog_id ID of the blog.
    129138 * @param int $post_id ID of the post you're looking for.
     139 *
    130140 * @return WP_Post|null WP_Post on success or null on failure
    131141 */
    132142function get_blog_post( $blog_id, $post_id ) {
     
    145155 *
    146156 * @since MU 1.0
    147157 *
    148  * @param int    $blog_id ID of the blog you're adding the user to.
    149  * @param int    $user_id ID of the user you're adding.
    150  * @param string $role    The role you want the user to have
     158 * @param int $blog_id ID of the blog you're adding the user to.
     159 * @param int $user_id ID of the user you're adding.
     160 * @param string $role The role you want the user to have
     161 *
    151162 * @return true|WP_Error
    152163 */
    153164function add_user_to_blog( $blog_id, $user_id, $role ) {
    154         switch_to_blog($blog_id);
     165        switch_to_blog( $blog_id );
    155166
    156167        $user = get_userdata( $user_id );
    157168
    158169        if ( ! $user ) {
    159170                restore_current_blog();
     171
    160172                return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
    161173        }
    162174
    163         if ( !get_user_meta($user_id, 'primary_blog', true) ) {
    164                 update_user_meta($user_id, 'primary_blog', $blog_id);
    165                 $details = get_blog_details($blog_id);
    166                 update_user_meta($user_id, 'source_domain', $details->domain);
     175        if ( ! get_user_meta( $user_id, 'primary_blog', true ) ) {
     176                update_user_meta( $user_id, 'primary_blog', $blog_id );
     177                $details = get_blog_details( $blog_id );
     178                update_user_meta( $user_id, 'source_domain', $details->domain );
    167179        }
    168180
    169         $user->set_role($role);
     181        $user->set_role( $role );
    170182
    171183        /**
    172184         * Fires immediately after a user is added to a site.
     
    173185         *
    174186         * @since MU
    175187         *
    176          * @param int    $user_id User ID.
    177          * @param string $role    User role.
    178          * @param int    $blog_id Blog ID.
     188         * @param int $user_id User ID.
     189         * @param string $role User role.
     190         * @param int $blog_id Blog ID.
    179191         */
    180192        do_action( 'add_user_to_blog', $user_id, $role, $blog_id );
    181193        wp_cache_delete( $user_id, 'users' );
    182194        wp_cache_delete( $blog_id . '_user_count', 'blog-details' );
    183195        restore_current_blog();
     196
    184197        return true;
    185198}
    186199
     
    197210 *
    198211 * @global wpdb $wpdb WordPress database abstraction object.
    199212 *
    200  * @param int    $user_id ID of the user you're removing.
    201  * @param int    $blog_id ID of the blog you're removing the user from.
     213 * @param int $user_id ID of the user you're removing.
     214 * @param int $blog_id ID of the blog you're removing the user from.
    202215 * @param string $reassign Optional. A user to whom to reassign posts.
     216 *
    203217 * @return true|WP_Error
    204218 */
    205 function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
     219function remove_user_from_blog( $user_id, $blog_id = '', $reassign = '' ) {
    206220        global $wpdb;
    207         switch_to_blog($blog_id);
     221        switch_to_blog( $blog_id );
    208222        $user_id = (int) $user_id;
    209223        /**
    210224         * Fires before a user is removed from a site.
     
    218232
    219233        // If being removed from the primary blog, set a new primary if the user is assigned
    220234        // to multiple blogs.
    221         $primary_blog = get_user_meta($user_id, 'primary_blog', true);
     235        $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
    222236        if ( $primary_blog == $blog_id ) {
    223                 $new_id = '';
     237                $new_id     = '';
    224238                $new_domain = '';
    225                 $blogs = get_blogs_of_user($user_id);
     239                $blogs      = get_blogs_of_user( $user_id );
    226240                foreach ( (array) $blogs as $blog ) {
    227                         if ( $blog->userblog_id == $blog_id )
     241                        if ( $blog->userblog_id == $blog_id ) {
    228242                                continue;
    229                         $new_id = $blog->userblog_id;
     243                        }
     244                        $new_id     = $blog->userblog_id;
    230245                        $new_domain = $blog->domain;
    231246                        break;
    232247                }
    233248
    234                 update_user_meta($user_id, 'primary_blog', $new_id);
    235                 update_user_meta($user_id, 'source_domain', $new_domain);
     249                update_user_meta( $user_id, 'primary_blog', $new_id );
     250                update_user_meta( $user_id, 'source_domain', $new_domain );
    236251        }
    237252
    238253        // wp_revoke_user($user_id);
     
    239254        $user = get_userdata( $user_id );
    240255        if ( ! $user ) {
    241256                restore_current_blog();
    242                 return new WP_Error('user_does_not_exist', __('That user does not exist.'));
     257
     258                return new WP_Error( 'user_does_not_exist', __( 'That user does not exist.' ) );
    243259        }
    244260
    245261        $user->remove_all_caps();
    246262
    247         $blogs = get_blogs_of_user($user_id);
    248         if ( count($blogs) == 0 ) {
    249                 update_user_meta($user_id, 'primary_blog', '');
    250                 update_user_meta($user_id, 'source_domain', '');
     263        $blogs = get_blogs_of_user( $user_id );
     264        if ( count( $blogs ) == 0 ) {
     265                update_user_meta( $user_id, 'primary_blog', '' );
     266                update_user_meta( $user_id, 'source_domain', '' );
    251267        }
    252268
    253269        if ( $reassign != '' ) {
     
    278294 *
    279295 * @param int $blog_id ID of the source blog.
    280296 * @param int $post_id ID of the desired post.
     297 *
    281298 * @return string The post's permalink
    282299 */
    283300function get_blog_permalink( $blog_id, $post_id ) {
     
    301318 * @global wpdb $wpdb WordPress database abstraction object.
    302319 *
    303320 * @param string $domain
    304  * @param string $path   Optional. Not required for subdomain installations.
     321 * @param string $path Optional. Not required for subdomain installations.
     322 *
    305323 * @return int 0 if no blog found, otherwise the ID of the matching blog
    306324 */
    307325function get_blog_id_from_url( $domain, $path = '/' ) {
     
    308326        global $wpdb;
    309327
    310328        $domain = strtolower( $domain );
    311         $path = strtolower( $path );
    312         $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
     329        $path   = strtolower( $path );
     330        $id     = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
    313331
    314         if ( $id == -1 ) // blog does not exist
     332        if ( $id == - 1 ) // blog does not exist
     333        {
    315334                return 0;
    316         elseif ( $id )
     335        } elseif ( $id ) {
    317336                return (int) $id;
     337        }
    318338
    319         $id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s and path = %s /* get_blog_id_from_url */", $domain, $path ) );
     339        $query  = new WP_Site_Query();
     340        $result = $query->query( array( 'domain' => $domain, 'path' => $path, 'fields' => 'ids' ) );
     341        $id     = array_shift( $result );
     342        if ( ! $id ) {
     343                wp_cache_set( md5( $domain . $path ), - 1, 'blog-id-cache' );
    320344
    321         if ( ! $id ) {
    322                 wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
    323345                return 0;
    324346        }
    325347
     
    341363 * @since MU
    342364 *
    343365 * @param string $user_email The email provided by the user at registration.
     366 *
    344367 * @return bool Returns true when the email address is banned.
    345368 */
    346369function is_email_address_unsafe( $user_email ) {
    347370        $banned_names = get_site_option( 'banned_email_domains' );
    348         if ( $banned_names && ! is_array( $banned_names ) )
     371        if ( $banned_names && ! is_array( $banned_names ) ) {
    349372                $banned_names = explode( "\n", $banned_names );
     373        }
    350374
    351375        $is_email_address_unsafe = false;
    352376
    353377        if ( $banned_names && is_array( $banned_names ) ) {
    354                 $banned_names = array_map( 'strtolower', $banned_names );
     378                $banned_names     = array_map( 'strtolower', $banned_names );
    355379                $normalized_email = strtolower( $user_email );
    356380
    357381                list( $email_local_part, $email_domain ) = explode( '@', $normalized_email );
    358382
    359383                foreach ( $banned_names as $banned_domain ) {
    360                         if ( ! $banned_domain )
     384                        if ( ! $banned_domain ) {
    361385                                continue;
     386                        }
    362387
    363388                        if ( $email_domain == $banned_domain ) {
    364389                                $is_email_address_unsafe = true;
     
    366391                        }
    367392
    368393                        $dotted_domain = ".$banned_domain";
    369                         if ( $dotted_domain === substr( $normalized_email, -strlen( $dotted_domain ) ) ) {
     394                        if ( $dotted_domain === substr( $normalized_email, - strlen( $dotted_domain ) ) ) {
    370395                                $is_email_address_unsafe = true;
    371396                                break;
    372397                        }
     
    378403         *
    379404         * @since 3.5.0
    380405         *
    381          * @param bool   $is_email_address_unsafe Whether the email address is "unsafe". Default false.
    382          * @param string $user_email              User email address.
     406         * @param bool $is_email_address_unsafe Whether the email address is "unsafe". Default false.
     407         * @param string $user_email User email address.
    383408         */
    384409        return apply_filters( 'is_email_address_unsafe', $is_email_address_unsafe, $user_email );
    385410}
     
    400425 *
    401426 * @global wpdb $wpdb WordPress database abstraction object.
    402427 *
    403  * @param string $user_name  The login name provided by the user.
     428 * @param string $user_name The login name provided by the user.
    404429 * @param string $user_email The email provided by the user.
     430 *
    405431 * @return array Contains username, email, and error messages.
    406432 */
    407 function wpmu_validate_user_signup($user_name, $user_email) {
     433function wpmu_validate_user_signup( $user_name, $user_email ) {
    408434        global $wpdb;
    409435
    410436        $errors = new WP_Error();
    411437
    412438        $orig_username = $user_name;
    413         $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
     439        $user_name     = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
    414440
    415441        if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
    416442                $errors->add( 'user_name', __( 'Usernames can only contain lowercase letters (a-z) and numbers.' ) );
     
    419445
    420446        $user_email = sanitize_email( $user_email );
    421447
    422         if ( empty( $user_name ) )
    423                 $errors->add('user_name', __( 'Please enter a username.' ) );
     448        if ( empty( $user_name ) ) {
     449                $errors->add( 'user_name', __( 'Please enter a username.' ) );
     450        }
    424451
    425452        $illegal_names = get_site_option( 'illegal_names' );
    426453        if ( ! is_array( $illegal_names ) ) {
    427                 $illegal_names = array(  'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
     454                $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
    428455                add_site_option( 'illegal_names', $illegal_names );
    429456        }
    430457        if ( in_array( $user_name, $illegal_names ) ) {
    431                 $errors->add( 'user_name',  __( 'Sorry, that username is not allowed.' ) );
     458                $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) );
    432459        }
    433460
    434461        /** This filter is documented in wp-includes/user.php */
     
    435462        $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
    436463
    437464        if ( in_array( strtolower( $user_name ), array_map( 'strtolower', $illegal_logins ) ) ) {
    438                 $errors->add( 'user_name',  __( 'Sorry, that username is not allowed.' ) );
     465                $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) );
    439466        }
    440467
    441         if ( is_email_address_unsafe( $user_email ) )
    442                 $errors->add('user_email',  __('You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.'));
     468        if ( is_email_address_unsafe( $user_email ) ) {
     469                $errors->add( 'user_email', __( 'You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.' ) );
     470        }
    443471
    444         if ( strlen( $user_name ) < 4 )
    445                 $errors->add('user_name',  __( 'Username must be at least 4 characters.' ) );
     472        if ( strlen( $user_name ) < 4 ) {
     473                $errors->add( 'user_name', __( 'Username must be at least 4 characters.' ) );
     474        }
    446475
    447476        if ( strlen( $user_name ) > 60 ) {
    448477                $errors->add( 'user_name', __( 'Username may not be longer than 60 characters.' ) );
     
    449478        }
    450479
    451480        // all numeric?
    452         if ( preg_match( '/^[0-9]*$/', $user_name ) )
    453                 $errors->add('user_name', __('Sorry, usernames must have letters too!'));
     481        if ( preg_match( '/^[0-9]*$/', $user_name ) ) {
     482                $errors->add( 'user_name', __( 'Sorry, usernames must have letters too!' ) );
     483        }
    454484
    455         if ( !is_email( $user_email ) )
    456                 $errors->add('user_email', __( 'Please enter a valid email address.' ) );
     485        if ( ! is_email( $user_email ) ) {
     486                $errors->add( 'user_email', __( 'Please enter a valid email address.' ) );
     487        }
    457488
    458489        $limited_email_domains = get_site_option( 'limited_email_domains' );
    459490        if ( is_array( $limited_email_domains ) && ! empty( $limited_email_domains ) ) {
    460491                $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
    461492                if ( ! in_array( $emaildomain, $limited_email_domains ) ) {
    462                         $errors->add('user_email', __('Sorry, that email address is not allowed!'));
     493                        $errors->add( 'user_email', __( 'Sorry, that email address is not allowed!' ) );
    463494                }
    464495        }
    465496
    466497        // Check if the username has been used already.
    467         if ( username_exists($user_name) )
     498        if ( username_exists( $user_name ) ) {
    468499                $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
     500        }
    469501
    470502        // Check if the email address has been used already.
    471         if ( email_exists($user_email) )
     503        if ( email_exists( $user_email ) ) {
    472504                $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
     505        }
    473506
    474507        // Has someone already signed up for this username?
    475         $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
     508        $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name ) );
    476509        if ( $signup != null ) {
    477                 $registered_at =  mysql2date('U', $signup->registered);
    478                 $now = current_time( 'timestamp', true );
    479                 $diff = $now - $registered_at;
     510                $registered_at = mysql2date( 'U', $signup->registered );
     511                $now           = current_time( 'timestamp', true );
     512                $diff          = $now - $registered_at;
    480513                // If registered more than two days ago, cancel registration and let this signup go through.
    481                 if ( $diff > 2 * DAY_IN_SECONDS )
     514                if ( $diff > 2 * DAY_IN_SECONDS ) {
    482515                        $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
    483                 else
    484                         $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
     516                } else {
     517                        $errors->add( 'user_name', __( 'That username is currently reserved but may be available in a couple of days.' ) );
     518                }
    485519        }
    486520
    487         $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
     521        $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email ) );
    488522        if ( $signup != null ) {
    489                 $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
     523                $diff = current_time( 'timestamp', true ) - mysql2date( 'U', $signup->registered );
    490524                // If registered more than two days ago, cancel registration and let this signup go through.
    491                 if ( $diff > 2 * DAY_IN_SECONDS )
     525                if ( $diff > 2 * DAY_IN_SECONDS ) {
    492526                        $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
    493                 else
    494                         $errors->add('user_email', __('That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.'));
     527                } else {
     528                        $errors->add( 'user_email', __( 'That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.' ) );
     529                }
    495530        }
    496531
    497         $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
     532        $result = array( 'user_name'     => $user_name,
     533                         'orig_username' => $orig_username,
     534                         'user_email'    => $user_email,
     535                         'errors'        => $errors
     536        );
    498537
    499538        /**
    500539         * Filter the validated user registration details.
     
    507546         * @param array $result {
    508547         *     The array of user name, email and the error messages.
    509548         *
    510          *     @type string   $user_name    Sanitized and unique username.
    511          *     @type string  $orig_username Original username.
    512          *     @type string   $user_email    User email address.
    513          *     @type WP_Error $errors        WP_Error object containing any errors found.
     549         * @type string $user_name Sanitized and unique username.
     550         * @type string $orig_username Original username.
     551         * @type string $user_email User email address.
     552         * @type WP_Error $errors WP_Error object containing any errors found.
    514553         * }
    515554         */
    516555        return apply_filters( 'wpmu_validate_user_signup', $result );
     
    532571 *
    533572 * @since MU
    534573 *
    535  * @global wpdb   $wpdb
     574 * @global wpdb $wpdb
    536575 * @global string $domain
    537576 *
    538  * @param string         $blogname   The blog name provided by the user. Must be unique.
    539  * @param string         $blog_title The blog title provided by the user.
    540  * @param WP_User|string $user       Optional. The user object to check against the new site name.
     577 * @param string $blogname The blog name provided by the user. Must be unique.
     578 * @param string $blog_title The blog title provided by the user.
     579 * @param WP_User|string $user Optional. The user object to check against the new site name.
     580 *
    541581 * @return array Contains the new site data and error messages.
    542582 */
    543583function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
     
    544584        global $wpdb, $domain;
    545585
    546586        $current_site = get_current_site();
    547         $base = $current_site->path;
     587        $base         = $current_site->path;
    548588
    549589        $blog_title = strip_tags( $blog_title );
    550590
    551         $errors = new WP_Error();
     591        $errors        = new WP_Error();
    552592        $illegal_names = get_site_option( 'illegal_names' );
    553593        if ( $illegal_names == false ) {
    554594                $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
     
    563603                $illegal_names = array_merge( $illegal_names, get_subdirectory_reserved_names() );
    564604        }
    565605
    566         if ( empty( $blogname ) )
    567                 $errors->add('blogname', __( 'Please enter a site name.' ) );
     606        if ( empty( $blogname ) ) {
     607                $errors->add( 'blogname', __( 'Please enter a site name.' ) );
     608        }
    568609
    569610        if ( preg_match( '/[^a-z0-9]+/', $blogname ) ) {
    570611                $errors->add( 'blogname', __( 'Site names can only contain lowercase letters (a-z) and numbers.' ) );
    571612        }
    572613
    573         if ( in_array( $blogname, $illegal_names ) )
    574                 $errors->add('blogname',  __( 'That name is not allowed.' ) );
     614        if ( in_array( $blogname, $illegal_names ) ) {
     615                $errors->add( 'blogname', __( 'That name is not allowed.' ) );
     616        }
    575617
    576         if ( strlen( $blogname ) < 4 && !is_super_admin() )
    577                 $errors->add('blogname',  __( 'Site name must be at least 4 characters.' ) );
     618        if ( strlen( $blogname ) < 4 && ! is_super_admin() ) {
     619                $errors->add( 'blogname', __( 'Site name must be at least 4 characters.' ) );
     620        }
    578621
    579622        // do not allow users to create a blog that conflicts with a page on the main blog.
    580         if ( !is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_site->blog_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) )
     623        if ( ! is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_site->blog_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) ) {
    581624                $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
     625        }
    582626
    583627        // all numeric?
    584         if ( preg_match( '/^[0-9]*$/', $blogname ) )
    585                 $errors->add('blogname', __('Sorry, site names must have letters too!'));
     628        if ( preg_match( '/^[0-9]*$/', $blogname ) ) {
     629                $errors->add( 'blogname', __( 'Sorry, site names must have letters too!' ) );
     630        }
    586631
    587632        /**
    588633         * Filter the new site name during registration.
     
    596641         */
    597642        $blogname = apply_filters( 'newblogname', $blogname );
    598643
    599         $blog_title = wp_unslash(  $blog_title );
     644        $blog_title = wp_unslash( $blog_title );
    600645
    601         if ( empty( $blog_title ) )
    602                 $errors->add('blog_title', __( 'Please enter a site title.' ) );
     646        if ( empty( $blog_title ) ) {
     647                $errors->add( 'blog_title', __( 'Please enter a site title.' ) );
     648        }
    603649
    604650        // Check if the domain/path has been used already.
    605651        if ( is_subdomain_install() ) {
    606652                $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
    607                 $path = $base;
     653                $path     = $base;
    608654        } else {
    609655                $mydomain = "$domain";
    610                 $path = $base.$blogname.'/';
     656                $path     = $base . $blogname . '/';
    611657        }
    612         if ( domain_exists($mydomain, $path, $current_site->id) )
     658        if ( domain_exists( $mydomain, $path, $current_site->id ) ) {
    613659                $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
     660        }
    614661
    615662        if ( username_exists( $blogname ) ) {
    616                 if ( ! is_object( $user ) || ( is_object($user) && ( $user->user_login != $blogname ) ) )
     663                if ( ! is_object( $user ) || ( is_object( $user ) && ( $user->user_login != $blogname ) ) ) {
    617664                        $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
     665                }
    618666        }
    619667
    620668        // Has someone already signed up for this domain?
    621         $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
    622         if ( ! empty($signup) ) {
    623                 $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
     669        $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path ) ); // TODO: Check email too?
     670        if ( ! empty( $signup ) ) {
     671                $diff = current_time( 'timestamp', true ) - mysql2date( 'U', $signup->registered );
    624672                // If registered more than two days ago, cancel registration and let this signup go through.
    625                 if ( $diff > 2 * DAY_IN_SECONDS )
    626                         $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
    627                 else
    628                         $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
     673                if ( $diff > 2 * DAY_IN_SECONDS ) {
     674                        $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain, 'path' => $path ) );
     675                } else {
     676                        $errors->add( 'blogname', __( 'That site is currently reserved but may be available in a couple days.' ) );
     677                }
    629678        }
    630679
    631         $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
     680        $result = array( 'domain'     => $mydomain,
     681                         'path'       => $path,
     682                         'blogname'   => $blogname,
     683                         'blog_title' => $blog_title,
     684                         'user'       => $user,
     685                         'errors'     => $errors
     686        );
    632687
    633688        /**
    634689         * Filter site details and error messages following registration.
     
    638693         * @param array $result {
    639694         *     Array of domain, path, blog name, blog title, user and error messages.
    640695         *
    641          *     @type string         $domain    Domain for the site.
    642          *     @type string         $path      Path for the site. Used in subdirectory installs.
    643          *     @type string         $blogname  The unique site name (slug).
    644          *     @type string        $blog_title Blog title.
    645          *     @type string|WP_User $user      By default, an empty string. A user object if provided.
    646          *     @type WP_Error       $errors    WP_Error containing any errors found.
     696         * @type string $domain Domain for the site.
     697         * @type string $path Path for the site. Used in subdirectory installs.
     698         * @type string $blogname The unique site name (slug).
     699         * @type string $blog_title Blog title.
     700         * @type string|WP_User $user By default, an empty string. A user object if provided.
     701         * @type WP_Error $errors WP_Error containing any errors found.
    647702         * }
    648703         */
    649704        return apply_filters( 'wpmu_validate_blog_signup', $result );
     
    656711 *
    657712 * @global wpdb $wpdb WordPress database abstraction object.
    658713 *
    659  * @param string $domain     The requested domain.
    660  * @param string $path       The requested path.
    661  * @param string $title      The requested site title.
    662  * @param string $user       The user's requested login name.
     714 * @param string $domain The requested domain.
     715 * @param string $path The requested path.
     716 * @param string $title The requested site title.
     717 * @param string $user The user's requested login name.
    663718 * @param string $user_email The user's email address.
    664  * @param array  $meta      By default, contains the requested privacy setting and lang_id.
     719 * @param array $meta By default, contains the requested privacy setting and lang_id.
    665720 */
    666 function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() )  {
     721function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() ) {
    667722        global $wpdb;
    668723
    669         $key = substr( md5( time() . rand() . $domain ), 0, 16 );
    670         $meta = serialize($meta);
     724        $key  = substr( md5( time() . rand() . $domain ), 0, 16 );
     725        $meta = serialize( $meta );
    671726
    672727        $wpdb->insert( $wpdb->signups, array(
    673                 'domain' => $domain,
    674                 'path' => $path,
    675                 'title' => $title,
    676                 'user_login' => $user,
    677                 'user_email' => $user_email,
    678                 'registered' => current_time('mysql', true),
     728                'domain'         => $domain,
     729                'path'           => $path,
     730                'title'          => $title,
     731                'user_login'     => $user,
     732                'user_email'     => $user_email,
     733                'registered'     => current_time( 'mysql', true ),
    679734                'activation_key' => $key,
    680                 'meta' => $meta
     735                'meta'           => $meta
    681736        ) );
    682737
    683738        /**
     
    685740         *
    686741         * @since 4.4.0
    687742         *
    688          * @param string $domain     The requested domain.
    689          * @param string $path       The requested path.
    690          * @param string $title      The requested site title.
    691          * @param string $user       The user's requested login name.
     743         * @param string $domain The requested domain.
     744         * @param string $path The requested path.
     745         * @param string $title The requested site title.
     746         * @param string $user The user's requested login name.
    692747         * @param string $user_email The user's email address.
    693          * @param string $key        The user's activation key
    694          * @param array  $meta      By default, contains the requested privacy setting and lang_id.
     748         * @param string $key The user's activation key
     749         * @param array $meta By default, contains the requested privacy setting and lang_id.
    695750         */
    696751        do_action( 'after_signup_site', $domain, $path, $title, $user, $user_email, $key, $meta );
    697752}
     
    706761 *
    707762 * @global wpdb $wpdb WordPress database abstraction object.
    708763 *
    709  * @param string $user       The user's requested login name.
     764 * @param string $user The user's requested login name.
    710765 * @param string $user_email The user's email address.
    711  * @param array  $meta      By default, this is an empty array.
     766 * @param array $meta By default, this is an empty array.
    712767 */
    713768function wpmu_signup_user( $user, $user_email, $meta = array() ) {
    714769        global $wpdb;
    715770
    716771        // Format data
    717         $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
     772        $user       = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
    718773        $user_email = sanitize_email( $user_email );
    719         $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
    720         $meta = serialize($meta);
     774        $key        = substr( md5( time() . rand() . $user_email ), 0, 16 );
     775        $meta       = serialize( $meta );
    721776
    722777        $wpdb->insert( $wpdb->signups, array(
    723                 'domain' => '',
    724                 'path' => '',
    725                 'title' => '',
    726                 'user_login' => $user,
    727                 'user_email' => $user_email,
    728                 'registered' => current_time('mysql', true),
     778                'domain'         => '',
     779                'path'           => '',
     780                'title'          => '',
     781                'user_login'     => $user,
     782                'user_email'     => $user_email,
     783                'registered'     => current_time( 'mysql', true ),
    729784                'activation_key' => $key,
    730                 'meta' => $meta
     785                'meta'           => $meta
    731786        ) );
    732787
    733788        /**
     
    735790         *
    736791         * @since 4.4.0
    737792         *
    738          * @param string $user       The user's requested login name.
     793         * @param string $user The user's requested login name.
    739794         * @param string $user_email The user's email address.
    740          * @param string $key        The user's activation key
    741          * @param array  $meta      Additional signup meta. By default, this is an empty array.
     795         * @param string $key The user's activation key
     796         * @param array $meta Additional signup meta. By default, this is an empty array.
    742797         */
    743798        do_action( 'after_signup_user', $user, $user_email, $key, $meta );
    744799}
     
    758813 *
    759814 * @since MU
    760815 *
    761  * @param string $domain     The new blog domain.
    762  * @param string $path       The new blog path.
    763  * @param string $title      The site title.
    764  * @param string $user       The user's login name.
     816 * @param string $domain The new blog domain.
     817 * @param string $path The new blog path.
     818 * @param string $title The site title.
     819 * @param string $user The user's login name.
    765820 * @param string $user_email The user's email address.
    766  * @param string $key        The activation key created in wpmu_signup_blog()
    767  * @param array  $meta       By default, contains the requested privacy setting and lang_id.
     821 * @param string $key The activation key created in wpmu_signup_blog()
     822 * @param array $meta By default, contains the requested privacy setting and lang_id.
     823 *
    768824 * @return bool
    769825 */
    770826function wpmu_signup_blog_notification( $domain, $path, $title, $user, $user_email, $key, $meta = array() ) {
     
    773829         *
    774830         * @since MU
    775831         *
    776          * @param string|bool $domain     Site domain.
    777          * @param string      $path      Site path.
    778          * @param string      $title      Site title.
    779          * @param string      $user      User login name.
    780          * @param string      $user_email User email address.
    781          * @param string      $key        Activation key created in wpmu_signup_blog().
    782          * @param array       $meta      By default, contains the requested privacy setting and lang_id.
     832         * @param string|bool $domain Site domain.
     833         * @param string $path Site path.
     834         * @param string $title Site title.
     835         * @param string $user User login name.
     836         * @param string $user_email User email address.
     837         * @param string $key Activation key created in wpmu_signup_blog().
     838         * @param array $meta By default, contains the requested privacy setting and lang_id.
    783839         */
    784840        if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta ) ) {
    785841                return false;
     
    786842        }
    787843
    788844        // Send email with activation link.
    789         if ( !is_subdomain_install() || get_current_site()->id != 1 )
    790                 $activate_url = network_site_url("wp-activate.php?key=$key");
    791         else
    792                 $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
     845        if ( ! is_subdomain_install() || get_current_site()->id != 1 ) {
     846                $activate_url = network_site_url( "wp-activate.php?key=$key" );
     847        } else {
     848                $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key";
     849        } // @todo use *_url() API
    793850
    794         $activate_url = esc_url($activate_url);
    795         $admin_email = get_site_option( 'admin_email' );
    796         if ( $admin_email == '' )
     851        $activate_url = esc_url( $activate_url );
     852        $admin_email  = get_site_option( 'admin_email' );
     853        if ( $admin_email == '' ) {
    797854                $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
    798         $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
    799         $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
    800         $message = sprintf(
    801                 /**
    802                  * Filter the message content of the new blog notification email.
    803                  *
    804                  * Content should be formatted for transmission via wp_mail().
    805                  *
    806                  * @since MU
    807                  *
    808                  * @param string $content    Content of the notification email.
    809                  * @param string $domain     Site domain.
    810                  * @param string $path       Site path.
    811                  * @param string $title      Site title.
    812                  * @param string $user       User login name.
    813                  * @param string $user_email User email address.
    814                  * @param string $key        Activation key created in wpmu_signup_blog().
    815                  * @param array  $meta       By default, contains the requested privacy setting and lang_id.
    816                  */
     855        }
     856        $from_name       = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
     857        $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option( 'blog_charset' ) . "\"\n";
     858        $message         = sprintf(
     859        /**
     860         * Filter the message content of the new blog notification email.
     861         *
     862         * Content should be formatted for transmission via wp_mail().
     863         *
     864         * @since MU
     865         *
     866         * @param string $content Content of the notification email.
     867         * @param string $domain Site domain.
     868         * @param string $path Site path.
     869         * @param string $title Site title.
     870         * @param string $user User login name.
     871         * @param string $user_email User email address.
     872         * @param string $key Activation key created in wpmu_signup_blog().
     873         * @param array $meta By default, contains the requested privacy setting and lang_id.
     874         */
    817875                apply_filters( 'wpmu_signup_blog_notification_email',
    818876                        __( "To activate your blog, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\nAfter you activate, you can visit your site here:\n\n%s" ),
    819877                        $domain, $path, $title, $user, $user_email, $key, $meta
     
    824882        );
    825883        // TODO: Don't hard code activation link.
    826884        $subject = sprintf(
    827                 /**
    828                 * Filter the subject of the new blog notification email.
    829                 *
    830                 * @since MU
    831                 *
    832                  * @param string $subject    Subject of the notification email.
    833                  * @param string $domain    Site domain.
    834                  * @param string $path      Site path.
    835                  * @param string $title      Site title.
    836                  * @param string $user      User login name.
    837                 * @param string $user_email User email address.
    838                  * @param string $key        Activation key created in wpmu_signup_blog().
    839                  * @param array  $meta      By default, contains the requested privacy setting and lang_id.
    840                 */
     885        /**
     886        * Filter the subject of the new blog notification email.
     887        *
     888        * @since MU
     889        *
     890         * @param string $subject Subject of the notification email.
     891         * @param string $domain Site domain.
     892         * @param string $path Site path.
     893         * @param string $title Site title.
     894         * @param string $user User login name.
     895        * @param string $user_email User email address.
     896         * @param string $key Activation key created in wpmu_signup_blog().
     897         * @param array $meta By default, contains the requested privacy setting and lang_id.
     898        */
    841899                apply_filters( 'wpmu_signup_blog_notification_subject',
    842900                        __( '[%1$s] Activate %2$s' ),
    843901                        $domain, $path, $title, $user, $user_email, $key, $meta
     
    846904                esc_url( 'http://' . $domain . $path )
    847905        );
    848906        wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
     907
    849908        return true;
    850909}
    851910
     
    864923 *
    865924 * @since MU
    866925 *
    867  * @param string $user       The user's login name.
     926 * @param string $user The user's login name.
    868927 * @param string $user_email The user's email address.
    869  * @param string $key        The activation key created in wpmu_signup_user()
    870  * @param array  $meta       By default, an empty array.
     928 * @param string $key The activation key created in wpmu_signup_user()
     929 * @param array $meta By default, an empty array.
     930 *
    871931 * @return bool
    872932 */
    873933function wpmu_signup_user_notification( $user, $user_email, $key, $meta = array() ) {
     
    876936         *
    877937         * @since MU
    878938         *
    879          * @param string $user       User login name.
     939         * @param string $user User login name.
    880940         * @param string $user_email User email address.
    881          * @param string $key        Activation key created in wpmu_signup_user().
    882          * @param array  $meta      Signup meta data.
     941         * @param string $key Activation key created in wpmu_signup_user().
     942         * @param array $meta Signup meta data.
    883943         */
    884         if ( ! apply_filters( 'wpmu_signup_user_notification', $user, $user_email, $key, $meta ) )
     944        if ( ! apply_filters( 'wpmu_signup_user_notification', $user, $user_email, $key, $meta ) ) {
    885945                return false;
     946        }
    886947
    887948        // Send email with activation link.
    888949        $admin_email = get_site_option( 'admin_email' );
    889         if ( $admin_email == '' )
     950        if ( $admin_email == '' ) {
    890951                $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
    891         $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
    892         $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
    893         $message = sprintf(
    894                 /**
    895                  * Filter the content of the notification email for new user sign-up.
    896                  *
    897                  * Content should be formatted for transmission via wp_mail().
    898                  *
    899                  * @since MU
    900                  *
    901                  * @param string $content    Content of the notification email.
    902                  * @param string $user       User login name.
    903                  * @param string $user_email User email address.
    904                  * @param string $key        Activation key created in wpmu_signup_user().
    905                  * @param array  $meta       Signup meta data.
    906                  */
     952        }
     953        $from_name       = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
     954        $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option( 'blog_charset' ) . "\"\n";
     955        $message         = sprintf(
     956        /**
     957         * Filter the content of the notification email for new user sign-up.
     958         *
     959         * Content should be formatted for transmission via wp_mail().
     960         *
     961         * @since MU
     962         *
     963         * @param string $content Content of the notification email.
     964         * @param string $user User login name.
     965         * @param string $user_email User email address.
     966         * @param string $key Activation key created in wpmu_signup_user().
     967         * @param array $meta Signup meta data.
     968         */
    907969                apply_filters( 'wpmu_signup_user_notification_email',
    908970                        __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
    909971                        $user, $user_email, $key, $meta
     
    912974        );
    913975        // TODO: Don't hard code activation link.
    914976        $subject = sprintf(
    915                 /**
    916                 * Filter the subject of the notification email of new user signup.
    917                 *
    918                 * @since MU
    919                 *
    920                  * @param string $subject    Subject of the notification email.
    921                  * @param string $user      User login name.
    922                 * @param string $user_email User email address.
    923                  * @param string $key        Activation key created in wpmu_signup_user().
    924                  * @param array  $meta      Signup meta data.
    925                 */
     977        /**
     978        * Filter the subject of the notification email of new user signup.
     979        *
     980        * @since MU
     981        *
     982         * @param string $subject Subject of the notification email.
     983         * @param string $user User login name.
     984        * @param string $user_email User email address.
     985         * @param string $key Activation key created in wpmu_signup_user().
     986         * @param array $meta Signup meta data.
     987        */
    926988                apply_filters( 'wpmu_signup_user_notification_subject',
    927989                        __( '[%1$s] Activate %2$s' ),
    928990                        $user, $user_email, $key, $meta
     
    931993                $user
    932994        );
    933995        wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
     996
    934997        return true;
    935998}
    936999
     
    9471010 * @global wpdb $wpdb WordPress database abstraction object.
    9481011 *
    9491012 * @param string $key The activation key provided to the user.
     1013 *
    9501014 * @return array|WP_Error An array containing information about the activated user and/or blog
    9511015 */
    952 function wpmu_activate_signup($key) {
     1016function wpmu_activate_signup( $key ) {
    9531017        global $wpdb;
    9541018
    955         $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
     1019        $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key ) );
    9561020
    957         if ( empty( $signup ) )
     1021        if ( empty( $signup ) ) {
    9581022                return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
     1023        }
    9591024
    9601025        if ( $signup->active ) {
    961                 if ( empty( $signup->domain ) )
     1026                if ( empty( $signup->domain ) ) {
    9621027                        return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
    963                 else
     1028                } else {
    9641029                        return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
     1030                }
    9651031        }
    9661032
    967         $meta = maybe_unserialize($signup->meta);
     1033        $meta     = maybe_unserialize( $signup->meta );
    9681034        $password = wp_generate_password( 12, false );
    9691035
    970         $user_id = username_exists($signup->user_login);
     1036        $user_id = username_exists( $signup->user_login );
    9711037
    972         if ( ! $user_id )
    973                 $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email);
    974         else
     1038        if ( ! $user_id ) {
     1039                $user_id = wpmu_create_user( $signup->user_login, $password, $signup->user_email );
     1040        } else {
    9751041                $user_already_exists = true;
     1042        }
    9761043
    977         if ( ! $user_id )
    978                 return new WP_Error('create_user', __('Could not create user'), $signup);
     1044        if ( ! $user_id ) {
     1045                return new WP_Error( 'create_user', __( 'Could not create user' ), $signup );
     1046        }
    9791047
    980         $now = current_time('mysql', true);
     1048        $now = current_time( 'mysql', true );
    9811049
    982         if ( empty($signup->domain) ) {
    983                 $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
     1050        if ( empty( $signup->domain ) ) {
     1051                $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
    9841052
    985                 if ( isset( $user_already_exists ) )
    986                         return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
     1053                if ( isset( $user_already_exists ) ) {
     1054                        return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup );
     1055                }
    9871056
    9881057                /**
    9891058                 * Fires immediately after a new user is activated.
     
    9901059                 *
    9911060                 * @since MU
    9921061                 *
    993                  * @param int   $user_id User ID.
    994                  * @param int   $password User password.
    995                  * @param array $meta     Signup meta data.
     1062                 * @param int $user_id User ID.
     1063                 * @param int $password User password.
     1064                 * @param array $meta Signup meta data.
    9961065                 */
    9971066                do_action( 'wpmu_activate_user', $user_id, $password, $meta );
     1067
    9981068                return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta );
    9991069        }
    10001070
     
    10011071        $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
    10021072
    10031073        // TODO: What to do if we create a user but cannot create a blog?
    1004         if ( is_wp_error($blog_id) ) {
     1074        if ( is_wp_error( $blog_id ) ) {
    10051075                // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
    10061076                // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
    10071077                if ( 'blog_taken' == $blog_id->get_error_code() ) {
    10081078                        $blog_id->add_data( $signup );
    1009                         $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
     1079                        $wpdb->update( $wpdb->signups, array( 'active'    => 1,
     1080                                                              'activated' => $now
     1081                        ), array( 'activation_key' => $key ) );
    10101082                }
     1083
    10111084                return $blog_id;
    10121085        }
    10131086
    1014         $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
     1087        $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
    10151088        /**
    10161089         * Fires immediately after a site is activated.
    10171090         *
    10181091         * @since MU
    10191092         *
    1020          * @param int    $blog_id      Blog ID.
    1021          * @param int    $user_id      User ID.
    1022          * @param int    $password      User password.
    1023          * @param string $signup_title  Site title.
    1024          * @param array  $meta          Signup meta data.
     1093         * @param int $blog_id Blog ID.
     1094         * @param int $user_id User ID.
     1095         * @param int $password User password.
     1096         * @param string $signup_title Site title.
     1097         * @param array $meta Signup meta data.
    10251098         */
    10261099        do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta );
    10271100
    1028         return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
     1101        return array( 'blog_id'  => $blog_id,
     1102                      'user_id'  => $user_id,
     1103                      'password' => $password,
     1104                      'title'    => $signup->title,
     1105                      'meta'     => $meta
     1106        );
    10291107}
    10301108
    10311109/**
     
    10391117 * @since MU
    10401118 *
    10411119 * @param string $user_name The new user's login name.
    1042  * @param string $password  The new user's password.
    1043  * @param string $email     The new user's email address.
     1120 * @param string $password The new user's password.
     1121 * @param string $email The new user's email address.
     1122 *
    10441123 * @return int|false Returns false on failure, or int $user_id on success
    10451124 */
    10461125function wpmu_create_user( $user_name, $password, $email ) {
     
    10471126        $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
    10481127
    10491128        $user_id = wp_create_user( $user_name, $password, $email );
    1050         if ( is_wp_error( $user_id ) )
     1129        if ( is_wp_error( $user_id ) ) {
    10511130                return false;
     1131        }
    10521132
    10531133        // Newly created users have no roles or caps until they are added to a blog.
    10541134        delete_user_option( $user_id, 'capabilities' );
     
    10801160 *
    10811161 * @since MU
    10821162 *
    1083  * @param string $domain  The new site's domain.
    1084  * @param string $path    The new site's path.
    1085  * @param string $title   The new site's title.
    1086  * @param int    $user_id The user ID of the new site's admin.
    1087  * @param array  $meta    Optional. Used to set initial site options.
    1088  * @param int    $site_id Optional. Only relevant on multi-network installs.
     1163 * @param string $domain The new site's domain.
     1164 * @param string $path The new site's path.
     1165 * @param string $title The new site's title.
     1166 * @param int $user_id The user ID of the new site's admin.
     1167 * @param array $meta Optional. Used to set initial site options.
     1168 * @param int $site_id Optional. Only relevant on multi-network installs.
     1169 *
    10891170 * @return int|WP_Error Returns WP_Error object on failure, int $blog_id on success
    10901171 */
    10911172function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $site_id = 1 ) {
    10921173        $defaults = array( 'public' => 0 );
    1093         $meta = wp_parse_args( $meta, $defaults );
     1174        $meta     = wp_parse_args( $meta, $defaults );
    10941175
    10951176        $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
    10961177
    1097         if ( is_subdomain_install() )
     1178        if ( is_subdomain_install() ) {
    10981179                $domain = str_replace( '@', '', $domain );
     1180        }
    10991181
    1100         $title = strip_tags( $title );
     1182        $title   = strip_tags( $title );
    11011183        $user_id = (int) $user_id;
    11021184
    1103         if ( empty($path) )
     1185        if ( empty( $path ) ) {
    11041186                $path = '/';
     1187        }
    11051188
    11061189        // Check if the domain has been used already. We should return an error message.
    1107         if ( domain_exists($domain, $path, $site_id) )
     1190        if ( domain_exists( $domain, $path, $site_id ) ) {
    11081191                return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
     1192        }
    11091193
    11101194        if ( ! wp_installing() ) {
    11111195                wp_installing( true );
    11121196        }
    11131197
    1114         if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
    1115                 return new WP_Error('insert_blog', __('Could not create site.'));
     1198        if ( ! $blog_id = insert_blog( $domain, $path, $site_id ) ) {
     1199                return new WP_Error( 'insert_blog', __( 'Could not create site.' ) );
     1200        }
    11161201
    1117         switch_to_blog($blog_id);
    1118         install_blog($blog_id, $title);
    1119         wp_install_defaults($user_id);
     1202        switch_to_blog( $blog_id );
     1203        install_blog( $blog_id, $title );
     1204        wp_install_defaults( $user_id );
    11201205
    1121         add_user_to_blog($blog_id, $user_id, 'administrator');
     1206        add_user_to_blog( $blog_id, $user_id, 'administrator' );
    11221207
    11231208        foreach ( $meta as $key => $value ) {
    1124                 if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) )
     1209                if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) {
    11251210                        update_blog_status( $blog_id, $key, $value );
    1126                 else
     1211                } else {
    11271212                        update_option( $key, $value );
     1213                }
    11281214        }
    11291215
    11301216        add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
    11311217        update_option( 'blog_public', (int) $meta['public'] );
    11321218
    1133         if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
     1219        if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) {
    11341220                update_user_meta( $user_id, 'primary_blog', $blog_id );
     1221        }
    11351222
    11361223        restore_current_blog();
    11371224        /**
     
    11391226         *
    11401227         * @since MU
    11411228         *
    1142          * @param int    $blog_id Blog ID.
    1143          * @param int    $user_id User ID.
    1144          * @param string $domain  Site domain.
    1145          * @param string $path    Site path.
    1146          * @param int    $site_id Site ID. Only relevant on multi-network installs.
    1147          * @param array  $meta    Meta data. Used to set initial site options.
     1229         * @param int $blog_id Blog ID.
     1230         * @param int $user_id User ID.
     1231         * @param string $domain Site domain.
     1232         * @param string $path Site path.
     1233         * @param int $site_id Site ID. Only relevant on multi-network installs.
     1234         * @param array $meta Meta data. Used to set initial site options.
    11481235         */
    11491236        do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
    11501237
     
    11591246 *
    11601247 * @since MU
    11611248 *
    1162  * @param int    $blog_id    The new site's ID.
     1249 * @param int $blog_id The new site's ID.
    11631250 * @param string $deprecated Not used.
     1251 *
    11641252 * @return bool
    11651253 */
    11661254function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
    1167         if ( get_site_option( 'registrationnotification' ) != 'yes' )
     1255        if ( get_site_option( 'registrationnotification' ) != 'yes' ) {
    11681256                return false;
     1257        }
    11691258
    11701259        $email = get_site_option( 'admin_email' );
    1171         if ( is_email($email) == false )
     1260        if ( is_email( $email ) == false ) {
    11721261                return false;
     1262        }
    11731263
    1174         $options_site_url = esc_url(network_admin_url('settings.php'));
     1264        $options_site_url = esc_url( network_admin_url( 'settings.php' ) );
    11751265
    11761266        switch_to_blog( $blog_id );
    11771267        $blogname = get_option( 'blogname' );
    1178         $siteurl = site_url();
     1268        $siteurl  = site_url();
    11791269        restore_current_blog();
    11801270
    11811271        $msg = sprintf( __( 'New Site: %1$s
     
    11821272URL: %2$s
    11831273Remote IP: %3$s
    11841274
    1185 Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
     1275Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url );
    11861276        /**
    11871277         * Filter the message body of the new site activation email sent
    11881278         * to the network administrator.
     
    11941284        $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
    11951285
    11961286        wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
     1287
    11971288        return true;
    11981289}
    11991290
     
    12061297 * @since MU
    12071298 *
    12081299 * @param int $user_id The new user's ID.
     1300 *
    12091301 * @return bool
    12101302 */
    12111303function newuser_notify_siteadmin( $user_id ) {
    1212         if ( get_site_option( 'registrationnotification' ) != 'yes' )
     1304        if ( get_site_option( 'registrationnotification' ) != 'yes' ) {
    12131305                return false;
     1306        }
    12141307
    12151308        $email = get_site_option( 'admin_email' );
    12161309
    1217         if ( is_email($email) == false )
     1310        if ( is_email( $email ) == false ) {
    12181311                return false;
     1312        }
    12191313
    12201314        $user = get_userdata( $user_id );
    12211315
    1222         $options_site_url = esc_url(network_admin_url('settings.php'));
    1223         $msg = sprintf(__('New User: %1$s
     1316        $options_site_url = esc_url( network_admin_url( 'settings.php' ) );
     1317        $msg              = sprintf( __( 'New User: %1$s
    12241318Remote IP: %2$s
    12251319
    1226 Disable these notifications: %3$s'), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
     1320Disable these notifications: %3$s' ), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url );
    12271321
    12281322        /**
    12291323         * Filter the message body of the new user activation email sent
     
    12311325         *
    12321326         * @since MU
    12331327         *
    1234          * @param string  $msg Email body.
     1328         * @param string $msg Email body.
    12351329         * @param WP_User $user WP_User instance of the new user.
    12361330         */
    12371331        $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
    1238         wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
     1332        wp_mail( $email, sprintf( __( 'New User Registration: %s' ), $user->user_login ), $msg );
     1333
    12391334        return true;
    12401335}
    12411336
     
    12491344 *
    12501345 * @global wpdb $wpdb WordPress database abstraction object.
    12511346 *
    1252  * @param string $domain  The domain to be checked.
    1253  * @param string $path    The path to be checked.
    1254  * @param int    $site_id Optional. Relevant only on multi-network installs.
     1347 * @param string $domain The domain to be checked.
     1348 * @param string $path The path to be checked.
     1349 * @param int $site_id Optional. Relevant only on multi-network installs.
     1350 *
    12551351 * @return int
    12561352 */
    1257 function domain_exists($domain, $path, $site_id = 1) {
    1258         global $wpdb;
     1353function domain_exists( $domain, $path, $site_id = 1 ) {
    12591354        $path = trailingslashit( $path );
    1260         $result = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s AND site_id = %d", $domain, $path, $site_id) );
    12611355
     1356        $query  = new WP_Site_Query();
     1357        $args   = array( 'path' => $path, 'domain' => $domain, 'network' => $site_id, 'number' => 1, 'fields' => 'ids' );
     1358        $result = $query->query( $args );
     1359        $site   = array_shift( $result );
     1360
    12621361        /**
    12631362         * Filter whether a blogname is taken.
    12641363         *
    12651364         * @since 3.5.0
    12661365         *
    1267          * @param int|null $result The blog_id if the blogname exists, null otherwise.
    1268          * @param string   $domain Domain to be checked.
    1269          * @param string   $path    Path to be checked.
    1270          * @param int      $site_id Site ID. Relevant only on multi-network installs.
     1366         * @param int|null $site The blog_id if the blogname exists, null otherwise.
     1367         * @param string $domain Domain to be checked.
     1368         * @param string $path Path to be checked.
     1369         * @param int $site_id Site ID. Relevant only on multi-network installs.
    12711370         */
    1272         return apply_filters( 'domain_exists', $result, $domain, $path, $site_id );
     1371        return apply_filters( 'domain_exists', $site, $domain, $path, $site_id );
    12731372}
    12741373
    12751374/**
     
    12821381 *
    12831382 * @global wpdb $wpdb WordPress database abstraction object.
    12841383 *
    1285  * @param string $domain  The domain of the new site.
    1286  * @param string $path    The path of the new site.
    1287  * @param int    $site_id Unless you're running a multi-network install, be sure to set this value to 1.
     1384 * @param string $domain The domain of the new site.
     1385 * @param string $path The path of the new site.
     1386 * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
     1387 *
    12881388 * @return int|false The ID of the new row
    12891389 */
    1290 function insert_blog($domain, $path, $site_id) {
     1390function insert_blog( $domain, $path, $site_id ) {
    12911391        global $wpdb;
    12921392
    1293         $path = trailingslashit($path);
     1393        $path    = trailingslashit( $path );
    12941394        $site_id = (int) $site_id;
    12951395
    1296         $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
    1297         if ( ! $result )
     1396        $result = $wpdb->insert( $wpdb->blogs, array( 'site_id'    => $site_id,
     1397                                                      'domain'     => $domain,
     1398                                                      'path'       => $path,
     1399                                                      'registered' => current_time( 'mysql' )
     1400        ) );
     1401        if ( ! $result ) {
    12981402                return false;
     1403        }
    12991404
    13001405        $blog_id = $wpdb->insert_id;
    13011406        refresh_blog_details( $blog_id );
     
    13141419 *
    13151420 * @since MU
    13161421 *
    1317  * @global wpdb     $wpdb
     1422 * @global wpdb $wpdb
    13181423 * @global WP_Roles $wp_roles
    13191424 *
    1320  * @param int    $blog_id    The value returned by insert_blog().
     1425 * @param int $blog_id The value returned by insert_blog().
    13211426 * @param string $blog_title The title of the new site.
    13221427 */
    13231428function install_blog( $blog_id, $blog_title = '' ) {
     
    13291434        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    13301435
    13311436        $suppress = $wpdb->suppress_errors();
    1332         if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
     1437        if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) {
    13331438                die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p></body></html>' );
     1439        }
    13341440        $wpdb->suppress_errors( $suppress );
    13351441
    13361442        $url = get_blogaddress_by_id( $blog_id );
     
    13471453
    13481454        if ( ! is_subdomain_install() ) {
    13491455
    1350                 if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
    1351                         $siteurl = set_url_scheme( $siteurl, 'https' );
    1352                 }
    1353                 if ( 'https' === parse_url( get_home_url( $current_site->blog_id ), PHP_URL_SCHEME ) ) {
    1354                         $home = set_url_scheme( $home, 'https' );
    1355                 }
     1456                if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
     1457                        $siteurl = set_url_scheme( $siteurl, 'https' );
     1458                }
     1459                if ( 'https' === parse_url( get_home_url( $current_site->blog_id ), PHP_URL_SCHEME ) ) {
     1460                        $home = set_url_scheme( $home, 'https' );
     1461                }
    13561462
    13571463        }
    13581464
     
    13591465        update_option( 'siteurl', $siteurl );
    13601466        update_option( 'home', $home );
    13611467
    1362         if ( get_site_option( 'ms_files_rewriting' ) )
     1468        if ( get_site_option( 'ms_files_rewriting' ) ) {
    13631469                update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
    1364         else
     1470        } else {
    13651471                update_option( 'upload_path', get_blog_option( get_current_site()->blog_id, 'upload_path' ) );
     1472        }
    13661473
    13671474        update_option( 'blogname', wp_unslash( $blog_title ) );
    13681475        update_option( 'admin_email', '' );
     
    13691476
    13701477        // remove all perms
    13711478        $table_prefix = $wpdb->get_blog_prefix();
    1372         delete_metadata( 'user', 0, $table_prefix . 'user_level',   null, true ); // delete all
     1479        delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
    13731480        delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
    13741481}
    13751482
     
    13871494 * @param int $blog_id Ignored in this function.
    13881495 * @param int $user_id
    13891496 */
    1390 function install_blog_defaults($blog_id, $user_id) {
     1497function install_blog_defaults( $blog_id, $user_id ) {
    13911498        global $wpdb;
    13921499
    13931500        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     
    13941501
    13951502        $suppress = $wpdb->suppress_errors();
    13961503
    1397         wp_install_defaults($user_id);
     1504        wp_install_defaults( $user_id );
    13981505
    13991506        $wpdb->suppress_errors( $suppress );
    14001507}
     
    14091516 *
    14101517 * @since MU
    14111518 *
    1412  * @param int    $blog_id
    1413  * @param int    $user_id
     1519 * @param int $blog_id
     1520 * @param int $user_id
    14141521 * @param string $password
    1415  * @param string $title    The new blog's title
    1416  * @param array  $meta     Optional. Not used in the default function, but is passed along to hooks for customization.
     1522 * @param string $title The new blog's title
     1523 * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
     1524 *
    14171525 * @return bool
    14181526 */
    14191527function wpmu_welcome_notification( $blog_id, $user_id, $password, $title, $meta = array() ) {
     
    14261534         *
    14271535         * @since MU
    14281536         *
    1429          * @param int|bool $blog_id  Blog ID.
    1430          * @param int      $user_id User ID.
    1431          * @param string   $password User password.
    1432          * @param string   $title    Site title.
    1433          * @param array    $meta    Signup meta data.
     1537         * @param int|bool $blog_id Blog ID.
     1538         * @param int $user_id User ID.
     1539         * @param string $password User password.
     1540         * @param string $title Site title.
     1541         * @param array $meta Signup meta data.
    14341542         */
    1435         if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) )
     1543        if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) ) {
    14361544                return false;
     1545        }
    14371546
    14381547        $welcome_email = get_site_option( 'welcome_email' );
    14391548        if ( $welcome_email == false ) {
     
    14541563--The Team @ SITE_NAME' );
    14551564        }
    14561565
    1457         $url = get_blogaddress_by_id($blog_id);
     1566        $url  = get_blogaddress_by_id( $blog_id );
    14581567        $user = get_userdata( $user_id );
    14591568
    14601569        $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
     
    14711580         * @since MU
    14721581         *
    14731582         * @param string $welcome_email Message body of the email.
    1474          * @param int    $blog_id      Blog ID.
    1475          * @param int    $user_id      User ID.
    1476          * @param string $password      User password.
    1477          * @param string $title         Site title.
    1478          * @param array  $meta          Signup meta data.
     1583         * @param int $blog_id Blog ID.
     1584         * @param int $user_id User ID.
     1585         * @param string $password User password.
     1586         * @param string $title Site title.
     1587         * @param array $meta Signup meta data.
    14791588         */
    14801589        $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta );
    1481         $admin_email = get_site_option( 'admin_email' );
     1590        $admin_email   = get_site_option( 'admin_email' );
    14821591
    1483         if ( $admin_email == '' )
     1592        if ( $admin_email == '' ) {
    14841593                $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
     1594        }
    14851595
    1486         $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
    1487         $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
    1488         $message = $welcome_email;
     1596        $from_name       = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
     1597        $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option( 'blog_charset' ) . "\"\n";
     1598        $message         = $welcome_email;
    14891599
    1490         if ( empty( $current_site->site_name ) )
     1600        if ( empty( $current_site->site_name ) ) {
    14911601                $current_site->site_name = 'WordPress';
     1602        }
    14921603
    14931604        /**
    14941605         * Filter the subject of the welcome email after site activation.
     
    14991610         */
    15001611        $subject = apply_filters( 'update_welcome_subject', sprintf( __( 'New %1$s Site: %2$s' ), $current_site->site_name, wp_unslash( $title ) ) );
    15011612        wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
     1613
    15021614        return true;
    15031615}
    15041616
     
    15121624 *
    15131625 * @since MU
    15141626 *
    1515  * @param int    $user_id
     1627 * @param int $user_id
    15161628 * @param string $password
    1517  * @param array  $meta     Optional. Not used in the default function, but is passed along to hooks for customization.
     1629 * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
     1630 *
    15181631 * @return bool
    15191632 */
    15201633function wpmu_welcome_user_notification( $user_id, $password, $meta = array() ) {
     
    15211634        $current_site = get_current_site();
    15221635
    15231636        /**
    1524          * Filter whether to bypass the welcome email after user activation.
     1637         * Filter whether to bypass the welcome email after user activation.
    15251638         *
    15261639         * Returning false disables the welcome email.
    15271640         *
    15281641         * @since MU
    15291642         *
    1530          * @param int    $user_id User ID.
     1643         * @param int $user_id User ID.
    15311644         * @param string $password User password.
    1532          * @param array  $meta    Signup meta data.
     1645         * @param array $meta Signup meta data.
    15331646         */
    1534         if ( ! apply_filters( 'wpmu_welcome_user_notification', $user_id, $password, $meta ) )
     1647        if ( ! apply_filters( 'wpmu_welcome_user_notification', $user_id, $password, $meta ) ) {
    15351648                return false;
     1649        }
    15361650
    15371651        $welcome_email = get_site_option( 'welcome_user_email' );
    15381652
     
    15461660         * @since MU
    15471661         *
    15481662         * @param string $welcome_email The message body of the account activation success email.
    1549          * @param int    $user_id      User ID.
    1550          * @param string $password      User password.
    1551          * @param array  $meta          Signup meta data.
     1663         * @param int $user_id User ID.
     1664         * @param string $password User password.
     1665         * @param array $meta Signup meta data.
    15521666         */
    15531667        $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta );
    15541668        $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
     
    15581672
    15591673        $admin_email = get_site_option( 'admin_email' );
    15601674
    1561         if ( $admin_email == '' )
     1675        if ( $admin_email == '' ) {
    15621676                $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
     1677        }
    15631678
    1564         $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
    1565         $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
    1566         $message = $welcome_email;
     1679        $from_name       = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
     1680        $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option( 'blog_charset' ) . "\"\n";
     1681        $message         = $welcome_email;
    15671682
    1568         if ( empty( $current_site->site_name ) )
     1683        if ( empty( $current_site->site_name ) ) {
    15691684                $current_site->site_name = 'WordPress';
     1685        }
    15701686
    15711687        /**
    15721688         * Filter the subject of the welcome email after user activation.
     
    15751691         *
    15761692         * @param string $subject Subject of the email.
    15771693         */
    1578         $subject = apply_filters( 'update_welcome_user_subject', sprintf( __( 'New %1$s User: %2$s' ), $current_site->site_name, $user->user_login) );
     1694        $subject = apply_filters( 'update_welcome_user_subject', sprintf( __( 'New %1$s User: %2$s' ), $current_site->site_name, $user->user_login ) );
    15791695        wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
     1696
    15801697        return true;
    15811698}
    15821699
     
    15961713 */
    15971714function get_current_site() {
    15981715        global $current_site;
     1716
    15991717        return $current_site;
    16001718}
    16011719
     
    16101728 * @global wpdb $wpdb WordPress database abstraction object.
    16111729 *
    16121730 * @param int $user_id
     1731 *
    16131732 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
    16141733 */
    16151734function get_most_recent_post_of_user( $user_id ) {
    16161735        global $wpdb;
    16171736
    1618         $user_blogs = get_blogs_of_user( (int) $user_id );
     1737        $user_blogs       = get_blogs_of_user( (int) $user_id );
    16191738        $most_recent_post = array();
    16201739
    16211740        // Walk through each blog and get the most recent post
    16221741        // published by $user_id
    16231742        foreach ( (array) $user_blogs as $blog ) {
    1624                 $prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
    1625                 $recent_post = $wpdb->get_row( $wpdb->prepare("SELECT ID, post_date_gmt FROM {$prefix}posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A);
     1743                $prefix      = $wpdb->get_blog_prefix( $blog->userblog_id );
     1744                $recent_post = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_date_gmt FROM {$prefix}posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A );
    16261745
    16271746                // Make sure we found a post
    1628                 if ( isset($recent_post['ID']) ) {
    1629                         $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
     1747                if ( isset( $recent_post['ID'] ) ) {
     1748                        $post_gmt_ts = strtotime( $recent_post['post_date_gmt'] );
    16301749
    16311750                        // If this is the first post checked or if this post is
    16321751                        // newer than the current recent post, make it the new
    16331752                        // most recent post.
    1634                         if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
     1753                        if ( ! isset( $most_recent_post['post_gmt_ts'] ) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
    16351754                                $most_recent_post = array(
    1636                                         'blog_id'               => $blog->userblog_id,
    1637                                         'post_id'               => $recent_post['ID'],
    1638                                         'post_date_gmt' => $recent_post['post_date_gmt'],
    1639                                         'post_gmt_ts'   => $post_gmt_ts
     1755                                        'blog_id'       => $blog->userblog_id,
     1756                                        'post_id'       => $recent_post['ID'],
     1757                                        'post_date_gmt' => $recent_post['post_date_gmt'],
     1758                                        'post_gmt_ts'   => $post_gmt_ts
    16401759                                );
    16411760                        }
    16421761                }
     
    16561775 * @since MU
    16571776 *
    16581777 * @param string $directory Full path of a directory.
     1778 *
    16591779 * @return int Size of the directory in MB.
    16601780 */
    16611781function get_dirsize( $directory ) {
    16621782        $dirsize = get_transient( 'dirsize_cache' );
    1663         if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
    1664                 return $dirsize[ $directory ][ 'size' ];
     1783        if ( is_array( $dirsize ) && isset( $dirsize[ $directory ]['size'] ) ) {
     1784                return $dirsize[ $directory ]['size'];
     1785        }
    16651786
    1666         if ( ! is_array( $dirsize ) )
     1787        if ( ! is_array( $dirsize ) ) {
    16671788                $dirsize = array();
     1789        }
    16681790
    16691791        // Exclude individual site directories from the total when checking the main site,
    16701792        // as they are subdirectories and should not be counted.
    16711793        if ( is_main_site() ) {
    1672                 $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory, $directory . '/sites' );
     1794                $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, $directory . '/sites' );
    16731795        } else {
    1674                 $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
     1796                $dirsize[ $directory ]['size'] = recurse_dirsize( $directory );
    16751797        }
    16761798
    16771799        set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
    1678         return $dirsize[ $directory ][ 'size' ];
     1800
     1801        return $dirsize[ $directory ]['size'];
    16791802}
    16801803
    16811804/**
     
    16881811 * @since 4.3.0 $exclude parameter added.
    16891812 *
    16901813 * @param string $directory Full path of a directory.
    1691  * @param string $exclude   Optional. Full path of a subdirectory to exclude from the total.
     1814 * @param string $exclude Optional. Full path of a subdirectory to exclude from the total.
     1815 *
    16921816 * @return int|false Size in MB if a valid directory. False if not.
    16931817 */
    16941818function recurse_dirsize( $directory, $exclude = null ) {
     
    17001824                return false;
    17011825        }
    17021826
    1703         if ($handle = opendir($directory)) {
    1704                 while(($file = readdir($handle)) !== false) {
    1705                         $path = $directory.'/'.$file;
    1706                         if ($file != '.' && $file != '..') {
    1707                                 if (is_file($path)) {
    1708                                         $size += filesize($path);
    1709                                 } elseif (is_dir($path)) {
     1827        if ( $handle = opendir( $directory ) ) {
     1828                while ( ( $file = readdir( $handle ) ) !== false ) {
     1829                        $path = $directory . '/' . $file;
     1830                        if ( $file != '.' && $file != '..' ) {
     1831                                if ( is_file( $path ) ) {
     1832                                        $size += filesize( $path );
     1833                                } elseif ( is_dir( $path ) ) {
    17101834                                        $handlesize = recurse_dirsize( $path, $exclude );
    1711                                         if ($handlesize > 0)
     1835                                        if ( $handlesize > 0 ) {
    17121836                                                $size += $handlesize;
     1837                                        }
    17131838                                }
    17141839                        }
    17151840                }
    1716                 closedir($handle);
     1841                closedir( $handle );
    17171842        }
     1843
    17181844        return $size;
    17191845}
    17201846
     
    17301856 * @since MU
    17311857 *
    17321858 * @param array $mimes
     1859 *
    17331860 * @return array
    17341861 */
    17351862function check_upload_mimes( $mimes ) {
    1736         $site_exts = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );
     1863        $site_exts  = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );
    17371864        $site_mimes = array();
    17381865        foreach ( $site_exts as $ext ) {
    17391866                foreach ( $mimes as $ext_pattern => $mime ) {
    1740                         if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
    1741                                 $site_mimes[$ext_pattern] = $mime;
     1867                        if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false ) {
     1868                                $site_mimes[ $ext_pattern ] = $mime;
     1869                        }
    17421870                }
    17431871        }
     1872
    17441873        return $site_mimes;
    17451874}
    17461875
     
    17761905function wpmu_log_new_registrations( $blog_id, $user_id ) {
    17771906        global $wpdb;
    17781907        $user = get_userdata( (int) $user_id );
    1779         if ( $user )
    1780                 $wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '', wp_unslash( $_SERVER['REMOTE_ADDR'] ) ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
     1908        if ( $user ) {
     1909                $wpdb->insert( $wpdb->registration_log, array( 'email'           => $user->user_email,
     1910                                                               'IP'              => preg_replace( '/[^0-9., ]/', '', wp_unslash( $_SERVER['REMOTE_ADDR'] ) ),
     1911                                                               'blog_id'         => $blog_id,
     1912                                                               'date_registered' => current_time( 'mysql' )
     1913                ) );
     1914        }
    17811915}
    17821916
    17831917/**
     
    17901924 * @global wpdb $wpdb WordPress database abstraction object.
    17911925 * @staticvar int $global_terms_recurse
    17921926 *
    1793  * @param int    $term_id    An ID for a term on the current blog.
     1927 * @param int $term_id An ID for a term on the current blog.
    17941928 * @param string $deprecated Not used.
     1929 *
    17951930 * @return int An ID from the global terms table mapped from $term_id.
    17961931 */
    17971932function global_terms( $term_id, $deprecated = '' ) {
     
    17981933        global $wpdb;
    17991934        static $global_terms_recurse = null;
    18001935
    1801         if ( !global_terms_enabled() )
     1936        if ( ! global_terms_enabled() ) {
    18021937                return $term_id;
     1938        }
    18031939
    18041940        // prevent a race condition
    18051941        $recurse_start = false;
    18061942        if ( $global_terms_recurse === null ) {
    1807                 $recurse_start = true;
     1943                $recurse_start        = true;
    18081944                $global_terms_recurse = 1;
    1809         } elseif ( 10 < $global_terms_recurse++ ) {
     1945        } elseif ( 10 < $global_terms_recurse ++ ) {
    18101946                return $term_id;
    18111947        }
    18121948
    18131949        $term_id = intval( $term_id );
    1814         $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
     1950        $c       = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
    18151951
    18161952        $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
    18171953        if ( $global_id == null ) {
    18181954                $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
    18191955                if ( null == $used_global_id ) {
    1820                         $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
     1956                        $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID'            => $term_id,
     1957                                                                     'cat_name'          => $c->name,
     1958                                                                     'category_nicename' => $c->slug
     1959                        ) );
    18211960                        $global_id = $wpdb->insert_id;
    1822                         if ( empty( $global_id ) )
     1961                        if ( empty( $global_id ) ) {
    18231962                                return $term_id;
     1963                        }
    18241964                } else {
    18251965                        $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
    1826                         $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
     1966                        $max_local_id  = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
    18271967                        $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
    1828                         $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
     1968                        $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID'            => $new_global_id,
     1969                                                                     'cat_name'          => $c->name,
     1970                                                                     'category_nicename' => $c->slug
     1971                        ) );
    18291972                        $global_id = $wpdb->insert_id;
    18301973                }
    18311974        } elseif ( $global_id != $term_id ) {
     
    18391982        }
    18401983
    18411984        if ( $global_id != $term_id ) {
    1842                 if ( get_option( 'default_category' ) == $term_id )
     1985                if ( get_option( 'default_category' ) == $term_id ) {
    18431986                        update_option( 'default_category', $global_id );
     1987                }
    18441988
    1845                 $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
    1846                 $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
    1847                 $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
     1989                $wpdb->update( $wpdb->terms, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
     1990                $wpdb->update( $wpdb->term_taxonomy, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
     1991                $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => $global_id ), array( 'parent' => $term_id ) );
    18481992
    1849                 clean_term_cache($term_id);
     1993                clean_term_cache( $term_id );
    18501994        }
    1851         if ( $recurse_start )
     1995        if ( $recurse_start ) {
    18521996                $global_terms_recurse = null;
     1997        }
    18531998
    18541999        return $global_id;
    18552000}
     
    18612006 * @since MU
    18622007 *
    18632008 * @param array|string $deprecated Not used.
     2009 *
    18642010 * @return array The current site's domain
    18652011 */
    18662012function redirect_this_site( $deprecated = '' ) {
     
    18752021 * @blessed
    18762022 *
    18772023 * @param array $upload
     2024 *
    18782025 * @return string|array If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
    18792026 */
    18802027function upload_is_file_too_big( $upload ) {
    1881         if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) )
     2028        if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) ) {
    18822029                return $upload;
     2030        }
    18832031
    1884         if ( strlen( $upload['bits'] )  > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
     2032        if ( strlen( $upload['bits'] ) > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
    18852033                return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ) );
    18862034        }
    18872035
     
    18962044function signup_nonce_fields() {
    18972045        $id = mt_rand();
    18982046        echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
    1899         wp_nonce_field('signup_form_' . $id, '_signup_form', false);
     2047        wp_nonce_field( 'signup_form_' . $id, '_signup_form', false );
    19002048}
    19012049
    19022050/**
     
    19052053 * @since MU
    19062054 *
    19072055 * @param array $result
     2056 *
    19082057 * @return array
    19092058 */
    19102059function signup_nonce_check( $result ) {
    1911         if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
     2060        if ( ! strpos( $_SERVER['PHP_SELF'], 'wp-signup.php' ) ) {
    19122061                return $result;
     2062        }
    19132063
    1914         if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
     2064        if ( wp_create_nonce( 'signup_form_' . $_POST['signup_form_id'] ) != $_POST['_signup_form'] ) {
    19152065                wp_die( __( 'Please try again.' ) );
     2066        }
    19162067
    19172068        return $result;
    19182069}
     
    19332084         * @param string $no_blog_redirect The redirect URL defined in NOBLOGREDIRECT.
    19342085         */
    19352086        if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
    1936                 if ( $destination == '%siteurl%' )
     2087                if ( $destination == '%siteurl%' ) {
    19372088                        $destination = network_home_url();
     2089                }
    19382090                wp_redirect( $destination );
    19392091                exit();
    19402092        }
     
    19502102 * @since MU
    19512103 */
    19522104function maybe_add_existing_user_to_blog() {
    1953         if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
     2105        if ( false === strpos( $_SERVER['REQUEST_URI'], '/newbloguser/' ) ) {
    19542106                return;
     2107        }
    19552108
    1956         $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
    1957         $key = array_pop( $parts );
     2109        $parts = explode( '/', $_SERVER['REQUEST_URI'] );
     2110        $key   = array_pop( $parts );
    19582111
    1959         if ( $key == '' )
     2112        if ( $key == '' ) {
    19602113                $key = array_pop( $parts );
     2114        }
    19612115
    19622116        $details = get_option( 'new_user_' . $key );
    1963         if ( !empty( $details ) )
     2117        if ( ! empty( $details ) ) {
    19642118                delete_option( 'new_user_' . $key );
     2119        }
    19652120
    1966         if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
    1967                 wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), home_url() ) );
     2121        if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) ) {
     2122                wp_die( sprintf( __( 'An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.' ), home_url() ) );
     2123        }
    19682124
    19692125        wp_die( sprintf( __( 'You have been added to this site. Please visit the <a href="%s">homepage</a> or <a href="%s">log in</a> using your username and password.' ), home_url(), admin_url() ), __( 'WordPress &rsaquo; Success' ), array( 'response' => 200 ) );
    19702126}
     
    19772133 * @global int $blog_id
    19782134 *
    19792135 * @param array $details
     2136 *
    19802137 * @return true|WP_Error|void
    19812138 */
    19822139function add_existing_user_to_blog( $details = false ) {
     
    19832140        global $blog_id;
    19842141
    19852142        if ( is_array( $details ) ) {
    1986                 $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
     2143                $result = add_user_to_blog( $blog_id, $details['user_id'], $details['role'] );
    19872144                /**
    19882145                 * Fires immediately after an existing user is added to a site.
    19892146                 *
    19902147                 * @since MU
    19912148                 *
    1992                  * @param int   $user_id User ID.
    1993                  * @param mixed $result  True on success or a WP_Error object if the user doesn't exist.
     2149                 * @param int $user_id User ID.
     2150                 * @param mixed $result True on success or a WP_Error object if the user doesn't exist.
    19942151                 */
    19952152                do_action( 'added_existing_user', $details['user_id'], $result );
     2153
    19962154                return $result;
    19972155        }
    19982156}
     
    20062164 * @since MU
    20072165 * @see add_user_to_blog()
    20082166 *
    2009  * @param int   $user_id
     2167 * @param int $user_id
    20102168 * @param mixed $password Ignored.
    20112169 * @param array $meta
    20122170 */
    20132171function add_new_user_to_blog( $user_id, $password, $meta ) {
    2014         if ( !empty( $meta[ 'add_to_blog' ] ) ) {
    2015                 $blog_id = $meta[ 'add_to_blog' ];
    2016                 $role = $meta[ 'new_role' ];
    2017                 remove_user_from_blog($user_id, get_current_site()->blog_id); // remove user from main blog.
     2172        if ( ! empty( $meta['add_to_blog'] ) ) {
     2173                $blog_id = $meta['add_to_blog'];
     2174                $role    = $meta['new_role'];
     2175                remove_user_from_blog( $user_id, get_current_site()->blog_id ); // remove user from main blog.
    20182176                add_user_to_blog( $blog_id, $user_id, $role );
    20192177                update_user_meta( $user_id, 'primary_blog', $blog_id );
    20202178        }
     
    20372195 * @since MU
    20382196 *
    20392197 * @param string|WP_User $user Optional. Defaults to current user. WP_User object,
    2040  *                                 or user login name as a string.
     2198 *                               or user login name as a string.
     2199 *
    20412200 * @return bool
    20422201 */
    20432202function is_user_spammy( $user = null ) {
    2044     if ( ! ( $user instanceof WP_User ) ) {
     2203        if ( ! ( $user instanceof WP_User ) ) {
    20452204                if ( $user ) {
    20462205                        $user = get_user_by( 'login', $user );
    20472206                } else {
     
    20602219 * @since MU
    20612220 *
    20622221 * @param int $old_value
    2063  * @param int $value     The new public value
     2222 * @param int $value The new public value
    20642223 */
    20652224function update_blog_public( $old_value, $value ) {
    20662225        update_blog_status( get_current_blog_id(), 'public', (int) $value );
     
    20742233 * @global wpdb $wpdb WordPress database abstraction object.
    20752234 *
    20762235 * @param string $key
    2077  * @param int    $user_id Optional. Defaults to current user.
    2078  * @param int    $blog_id Optional. Defaults to current blog.
     2236 * @param int $user_id Optional. Defaults to current user.
     2237 * @param int $blog_id Optional. Defaults to current blog.
     2238 *
    20792239 * @return bool
    20802240 */
    20812241function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
     
    20982258 * @return bool
    20992259 */
    21002260function users_can_register_signup_filter() {
    2101         $registration = get_site_option('registration');
     2261        $registration = get_site_option( 'registration' );
     2262
    21022263        return ( $registration == 'all' || $registration == 'user' );
    21032264}
    21042265
     
    21082269 * @since MU
    21092270 *
    21102271 * @param string $text
     2272 *
    21112273 * @return string
    21122274 */
    21132275function welcome_user_msg_filter( $text ) {
    2114         if ( !$text ) {
     2276        if ( ! $text ) {
    21152277                remove_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
    21162278
    21172279                /* translators: Do not translate USERNAME, PASSWORD, LOGINLINK, SITE_NAME: those are placeholders. */
     
    21292291--The Team @ SITE_NAME' );
    21302292                update_site_option( 'welcome_user_email', $text );
    21312293        }
     2294
    21322295        return $text;
    21332296}
    21342297
     
    21402303 * @staticvar bool $forced_content
    21412304 *
    21422305 * @param bool $force
     2306 *
    21432307 * @return bool True if forced, false if not forced.
    21442308 */
    21452309function force_ssl_content( $force = '' ) {
     
    21462310        static $forced_content = false;
    21472311
    21482312        if ( '' != $force ) {
    2149                 $old_forced = $forced_content;
     2313                $old_forced     = $forced_content;
    21502314                $forced_content = $force;
     2315
    21512316                return $old_forced;
    21522317        }
    21532318
     
    21622327 * @since 2.8.5
    21632328 *
    21642329 * @param string $url URL
     2330 *
    21652331 * @return string URL with https as the scheme
    21662332 */
    21672333function filter_SSL( $url ) {
    2168         if ( ! is_string( $url ) )
    2169                 return get_bloginfo( 'url' ); // Return home blog url with proper scheme
     2334        if ( ! is_string( $url ) ) {
     2335                return get_bloginfo( 'url' );
     2336        } // Return home blog url with proper scheme
    21702337
    2171         if ( force_ssl_content() && is_ssl() )
     2338        if ( force_ssl_content() && is_ssl() ) {
    21722339                $url = set_url_scheme( $url, 'https' );
     2340        }
    21732341
    21742342        return $url;
    21752343}
     
    21802348 * @since 3.1.0
    21812349 */
    21822350function wp_schedule_update_network_counts() {
    2183         if ( !is_main_site() )
     2351        if ( ! is_main_site() ) {
    21842352                return;
     2353        }
    21852354
    2186         if ( ! wp_next_scheduled('update_network_counts') && ! wp_installing() )
    2187                 wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
     2355        if ( ! wp_next_scheduled( 'update_network_counts' ) && ! wp_installing() ) {
     2356                wp_schedule_event( time(), 'twicedaily', 'update_network_counts' );
     2357        }
    21882358}
    21892359
    21902360/**
    21912361 *  Update the network-wide counts for the current network.
    21922362 *
    2193  *  @since 3.1.0
     2363 * @since 3.1.0
    21942364 */
    21952365function wp_update_network_counts() {
    21962366        wp_update_network_user_counts();
     
    22152385         *
    22162386         * @see wp_is_large_network()
    22172387         *
    2218          * @param bool   $small_network Whether the network is considered small.
    2219          * @param string $context       Context. Either 'users' or 'sites'.
     2388         * @param bool $small_network Whether the network is considered small.
     2389         * @param string $context Context. Either 'users' or 'sites'.
    22202390         */
    2221         if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) )
     2391        if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) ) {
    22222392                return;
     2393        }
    22232394
    22242395        wp_update_network_site_counts();
    22252396}
     
    22362407        $is_small_network = ! wp_is_large_network( 'users' );
    22372408
    22382409        /** This filter is documented in wp-includes/ms-functions.php */
    2239         if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) )
     2410        if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) {
    22402411                return;
     2412        }
    22412413
    22422414        wp_update_network_user_counts();
    22432415}
     
    22522424function wp_update_network_site_counts() {
    22532425        global $wpdb;
    22542426
    2255         $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'", $wpdb->siteid) );
     2427
     2428        $query = new WP_Site_Query();
     2429        $args  = array( 'network_id' => $wpdb->siteid, 'spam' => 0, 'deleted' => 0, 'archived' => 0, 'count' => true );
     2430        $count = $query->query( $args );
     2431
    22562432        update_site_option( 'blog_count', $count );
    22572433}
    22582434
     
    23042480function get_space_allowed() {
    23052481        $space_allowed = get_option( 'blog_upload_space' );
    23062482
    2307         if ( ! is_numeric( $space_allowed ) )
     2483        if ( ! is_numeric( $space_allowed ) ) {
    23082484                $space_allowed = get_site_option( 'blog_upload_space' );
     2485        }
    23092486
    2310         if ( ! is_numeric( $space_allowed ) )
     2487        if ( ! is_numeric( $space_allowed ) ) {
    23112488                $space_allowed = 100;
     2489        }
    23122490
    23132491        /**
    23142492         * Filter the upload quota for the current site.
     
    23332511                $allowed = 0;
    23342512        }
    23352513        $space_allowed = $allowed * MB_IN_BYTES;
    2336         if ( get_site_option( 'upload_space_check_disabled' ) )
     2514        if ( get_site_option( 'upload_space_check_disabled' ) ) {
    23372515                return $space_allowed;
     2516        }
    23382517
    23392518        $space_used = get_space_used() * MB_IN_BYTES;
    23402519
    2341         if ( ( $space_allowed - $space_used ) <= 0 )
     2520        if ( ( $space_allowed - $space_used ) <= 0 ) {
    23422521                return 0;
     2522        }
    23432523
    23442524        return $space_allowed - $space_used;
    23452525}
     
    23512531 * @return bool True if space is available, false otherwise.
    23522532 */
    23532533function is_upload_space_available() {
    2354         if ( get_site_option( 'upload_space_check_disabled' ) )
     2534        if ( get_site_option( 'upload_space_check_disabled' ) ) {
    23552535                return true;
     2536        }
    23562537
    23572538        return (bool) get_upload_space_available();
    23582539}
     
    23632544 * @since 3.0.0
    23642545 *
    23652546 * @param  int $size Upload size limit in bytes.
     2547 *
    23662548 * @return int       Upload size limit in bytes.
    23672549 */
    23682550function upload_size_limit_filter( $size ) {
    23692551        $fileupload_maxk = KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 );
    2370         if ( get_site_option( 'upload_space_check_disabled' ) )
     2552        if ( get_site_option( 'upload_space_check_disabled' ) ) {
    23712553                return min( $size, $fileupload_maxk );
     2554        }
    23722555
    23732556        return min( $size, $fileupload_maxk, get_upload_space_available() );
    23742557}
     
    23802563 * Plugins can alter this criteria using the 'wp_is_large_network' filter.
    23812564 *
    23822565 * @since 3.3.0
     2566 *
    23832567 * @param string $using 'sites or 'users'. Default is 'sites'.
     2568 *
    23842569 * @return bool True if the network meets the criteria for large. False otherwise.
    23852570 */
    23862571function wp_is_large_network( $using = 'sites' ) {
    23872572        if ( 'users' == $using ) {
    23882573                $count = get_user_count();
     2574
    23892575                /**
    23902576                 * Filter whether the network is considered large.
    23912577                 *
    23922578                 * @since 3.3.0
    23932579                 *
    2394                  * @param bool   $is_large_network Whether the network has more than 10000 users or sites.
    2395                  * @param string $component        The component to count. Accepts 'users', or 'sites'.
    2396                  * @param int    $count            The count of items for the component.
     2580                 * @param bool $is_large_network Whether the network has more than 10000 users or sites.
     2581                 * @param string $component The component to count. Accepts 'users', or 'sites'.
     2582                 * @param int $count The count of items for the component.
    23972583                 */
    23982584                return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count );
    23992585        }
    24002586
    24012587        $count = get_blog_count();
     2588
    24022589        /** This filter is documented in wp-includes/ms-functions.php */
    24032590        return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count );
    24042591}
     
    24142601 * @param array $args {
    24152602 *     Array of default arguments. Optional.
    24162603 *
    2417  *     @type int|array $network_id A network ID or array of network IDs. Set to null to retrieve sites
     2604 * @type int|array $network_id A network ID or array of network IDs. Set to null to retrieve sites
    24182605 *                                 from all networks. Defaults to current network ID.
    2419  *     @type int       $public    Retrieve public or non-public sites. Default null, for any.
    2420  *     @type int       $archived  Retrieve archived or non-archived sites. Default null, for any.
    2421  *     @type int       $mature    Retrieve mature or non-mature sites. Default null, for any.
    2422  *     @type int       $spam      Retrieve spam or non-spam sites. Default null, for any.
    2423  *     @type int       $deleted    Retrieve deleted or non-deleted sites. Default null, for any.
    2424  *     @type int       $limit      Number of sites to limit the query to. Default 100.
    2425  *     @type int       $offset    Exclude the first x sites. Used in combination with the $limit parameter. Default 0.
     2606 * @type int $public Retrieve public or non-public sites. Default null, for any.
     2607 * @type int $archived Retrieve archived or non-archived sites. Default null, for any.
     2608 * @type int $mature Retrieve mature or non-mature sites. Default null, for any.
     2609 * @type int $spam Retrieve spam or non-spam sites. Default null, for any.
     2610 * @type int $deleted Retrieve deleted or non-deleted sites. Default null, for any.
     2611 * @type int $limit Number of sites to limit the query to. Default 100.
     2612 * @type int $offset Exclude the first x sites. Used in combination with the $limit parameter. Default 0.
    24262613 * }
    24272614 * @return array An empty array if the install is considered "large" via wp_is_large_network(). Otherwise,
    24282615 *               an associative array of site data arrays, each containing the site (network) ID, blog ID,
     
    24322619function wp_get_sites( $args = array() ) {
    24332620        global $wpdb;
    24342621
    2435         if ( wp_is_large_network() )
    2436                 return array();
    2437 
    24382622        $defaults = array(
    24392623                'network_id' => $wpdb->siteid,
    2440                 'public'     => null,
    2441                 'archived'   => null,
    2442                 'mature'     => null,
    2443                 'spam'       => null,
    2444                 'deleted'    => null,
    24452624                'limit'      => 100,
    24462625                'offset'     => 0,
    24472626        );
     
    24482627
    24492628        $args = wp_parse_args( $args, $defaults );
    24502629
    2451         $query = "SELECT * FROM $wpdb->blogs WHERE 1=1 ";
     2630        $query = new WP_Site_Query();
    24522631
    2453         if ( isset( $args['network_id'] ) && ( is_array( $args['network_id'] ) || is_numeric( $args['network_id'] ) ) ) {
    2454                 $network_ids = implode( ',', wp_parse_id_list( $args['network_id'] ) );
    2455                 $query .= "AND site_id IN ($network_ids) ";
    2456         }
    2457 
    2458         if ( isset( $args['public'] ) )
    2459                 $query .= $wpdb->prepare( "AND public = %d ", $args['public'] );
    2460 
    2461         if ( isset( $args['archived'] ) )
    2462                 $query .= $wpdb->prepare( "AND archived = %d ", $args['archived'] );
    2463 
    2464         if ( isset( $args['mature'] ) )
    2465                 $query .= $wpdb->prepare( "AND mature = %d ", $args['mature'] );
    2466 
    2467         if ( isset( $args['spam'] ) )
    2468                 $query .= $wpdb->prepare( "AND spam = %d ", $args['spam'] );
    2469 
    2470         if ( isset( $args['deleted'] ) )
    2471                 $query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] );
    2472 
    2473         if ( isset( $args['limit'] ) && $args['limit'] ) {
    2474                 if ( isset( $args['offset'] ) && $args['offset'] )
    2475                         $query .= $wpdb->prepare( "LIMIT %d , %d ", $args['offset'], $args['limit'] );
    2476                 else
    2477                         $query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
    2478         }
    2479 
    2480         $site_results = $wpdb->get_results( $query, ARRAY_A );
    2481 
    2482         return $site_results;
     2632        return $query->query( $args );
    24832633}
    24842634
    24852635/**
     
    24912641 */
    24922642function get_subdirectory_reserved_names() {
    24932643        $names = array(
    2494                 'page', 'comments', 'blog', 'files', 'feed', 'wp-admin',
    2495                 'wp-content', 'wp-includes', 'wp-json', 'embed'
     2644                'page',
     2645                'comments',
     2646                'blog',
     2647                'files',
     2648                'feed',
     2649                'wp-admin',
     2650                'wp-content',
     2651                'wp-includes',
     2652                'wp-json',
     2653                'embed'
    24962654        );
    24972655
    24982656        /**
  • wp-includes/ms-load.php

     
    231231        $domains = array( $domain );
    232232        if ( 'www.' === substr( $domain, 0, 4 ) ) {
    233233                $domains[] = substr( $domain, 4 );
    234                 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
    235234        }
    236235
    237         if ( count( $paths ) > 1 ) {
    238                 $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
    239         }
    240 
    241         if ( count( $domains ) > 1 && count( $paths ) > 1 ) {
    242                 $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" );
    243         } elseif ( count( $domains ) > 1 ) {
    244                 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE path = %s", $paths[0] );
    245                 $sql .= " AND domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1";
    246                 $site = $wpdb->get_row( $sql );
    247         } elseif ( count( $paths ) > 1 ) {
    248                 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domains[0] );
    249                 $sql .= " AND path IN ($search_paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1";
    250                 $site = $wpdb->get_row( $sql );
    251         } else {
    252                 $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domains[0], $paths[0] ) );
    253         }
    254 
     236        $query = new WP_Site_Query();
     237        $args = array( 'path__in' => $path, 'domain__in' => $domains, 'number' => 1 );
     238        $result = $query->query( $args );
     239        $site   = array_shift( $result );
    255240        if ( $site ) {
    256241                // @todo get_blog_details()
    257242                return $site;
  • wp-settings.php

     
    9999
    100100// Initialize multisite if enabled.
    101101if ( is_multisite() ) {
     102        require( ABSPATH . WPINC . '/class-wp-site-query.php' );
    102103        require( ABSPATH . WPINC . '/ms-blogs.php' );
    103104        require( ABSPATH . WPINC . '/ms-settings.php' );
    104105} elseif ( ! defined( 'MULTISITE' ) ) {