Make WordPress Core

Ticket #30294: 30294.patch

File 30294.patch, 2.1 KB (added by johnjamesjacoby, 12 years ago)

get_primary_network_id() example

  • wordpress/wp-includes/functions.php

    diff --git a/wordpress/wp-includes/functions.php b/wordpress/wp-includes/functions.php
    index 0a8e1a0..eeabec9 100644
    a b  
    36603660 * @return bool True if $network_id is the main network, or if not running Multisite.
    36613661 */
    36623662function is_main_network( $network_id = null ) {
    3663         global $wpdb;
    36643663
    3665         if ( ! is_multisite() )
     3664        // Always return true if not a multisite installation
     3665        if ( ! is_multisite() ) {
    36663666                return true;
     3667        }
    36673668
    36683669        $current_network_id = (int) get_current_site()->id;
    36693670
    3670         if ( ! $network_id )
     3671        if ( null === $network_id ) {
    36713672                $network_id = $current_network_id;
     3673        }
     3674
    36723675        $network_id = (int) $network_id;
    36733676
    3674         if ( defined( 'PRIMARY_NETWORK_ID' ) )
    3675                 return $network_id === (int) PRIMARY_NETWORK_ID;
     3677        if ( 1 === $current_network_id ) {
     3678                return ( $network_id === $current_network_id );
     3679        }
    36763680
    3677         if ( 1 === $current_network_id )
    3678                 return $network_id === $current_network_id;
     3681        return ( $network_id === get_primary_network_id() );
     3682}
    36793683
    3680         $primary_network_id = (int) wp_cache_get( 'primary_network_id', 'site-options' );
     3684/**
     3685 * Get the primary network ID
     3686 *
     3687 * @global wpdb $wpdb
     3688 * @return int 
     3689 */
     3690function get_primary_network_id() {
     3691        global $wpdb;
    36813692
    3682         if ( $primary_network_id )
    3683                 return $network_id === $primary_network_id;
     3693        // Use constant if explicitly set
     3694        if ( defined( 'PRIMARY_NETWORK_ID' ) ) {
     3695                $primary_network_id = PRIMARY_NETWORK_ID;
    36843696
    3685         $primary_network_id = (int) $wpdb->get_var( "SELECT id FROM $wpdb->site ORDER BY id LIMIT 1" );
    3686         wp_cache_add( 'primary_network_id', $primary_network_id, 'site-options' );
     3697        // Fallback on cached value, queried from sites table
     3698        } else {
     3699                $primary_network_id = wp_cache_get( 'primary_network_id', 'site-options' );
    36873700
    3688         return $network_id === $primary_network_id;
     3701                if ( false === $primary_network_id ) {
     3702                        $primary_network_id = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->site} ORDER BY id LIMIT 1" );
     3703                        wp_cache_add( 'primary_network_id', $primary_network_id, 'site-options' );
     3704                }
     3705        }
     3706
     3707        return (int) apply_filters( 'get_primary_network_id', $primary_network_id );
    36893708}
    36903709
    36913710/**