Make WordPress Core


Ignore:
Timestamp:
09/15/2017 11:14:49 AM (8 years ago)
Author:
flixos90
Message:

Multisite: Introduce get_main_site_id().

This function can be used to easily get the main site ID of a given network via the optional $network_id parameter, which defaults to the current network. The existing is_main_site() now uses the new function internally and now accepts an optional $network_id parameter as well.

The main purpose of the new function at this point is to ensure that the WP_Network::$blog_id property is always set. Magic getters in the class have been adjusted to auto-fill the property when it is accessed and empty. Furthermore the function encapsulates logic that was previously part of ms_load_current_site_and_network() and has been replaced with a call to the function now.

Props spacedmonkey, jeremyfelt, johnjamesjacoby, flixos90.
Fixes #29684.

File:
1 edited

Legend:

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

    r41289 r41380  
    149149                return (int) $this->id;
    150150            case 'blog_id':
    151                 return $this->blog_id;
     151                return $this->get_main_site_id();
    152152            case 'site_id':
    153                 return (int) $this->blog_id;
     153                return (int) $this->get_main_site_id();
    154154        }
    155155
     
    200200                $this->$key = $value;
    201201        }
     202    }
     203
     204    /**
     205     * Returns the main site ID for the network.
     206     *
     207     * Internal method used by the magic getter for the 'blog_id' and 'site_id'
     208     * properties.
     209     *
     210     * @since 4.9.0
     211     * @see get_main_site_id()
     212     *
     213     * @return string Main site ID as numeric string, for compatibility reasons.
     214     */
     215    private function get_main_site_id() {
     216        if ( empty( $this->blog_id ) ) {
     217            $this->blog_id = (string) get_main_site_id( $this->id );
     218        }
     219
     220        return $this->blog_id;
    202221    }
    203222
Note: See TracChangeset for help on using the changeset viewer.