WordPress.org

Make WordPress Core

Changeset 40370


Ignore:
Timestamp:
04/03/17 23:13:40 (5 months ago)
Author:
flixos90
Message:

Multisite: Support the $network_id parameter of get_blog_count().

The get_blog_count() function used to support an $id parameter for the network ID prior to WordPress 3.1. This parameter has not been used since the introduction of get_site_option() and was later deprecated in [25113]. With get_network_option() however it is possible to support the parameter again, now properly renamed as $network_id.

A unit test has for the parameter has been added as well. Another unit test in the same class was adjusted to work properly with multiple networks existing.

Fixes #37865.

Location:
trunk
Files:
2 edited

Legend:

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

    r40295 r40370  
    110110 * 
    111111 * @since MU 1.0 
    112  * 
    113  * @param int $network_id Deprecated, not supported. 
    114  * @return int 
    115  */ 
    116 function get_blog_count( $network_id = 0 ) { 
    117     if ( func_num_args() ) 
    118         _deprecated_argument( __FUNCTION__, '3.1.0' ); 
    119  
    120     return get_site_option( 'blog_count' ); 
     112 * @since 3.7.0 The $network_id parameter has been deprecated. 
     113 * @since 4.8.0 The $network_id parameter is now being used. 
     114 * 
     115 * @param int|null $network_id ID of the network. Default is the current network. 
     116 * @return int Number of active sites on the network. 
     117 */ 
     118function get_blog_count( $network_id = null ) { 
     119    return get_network_option( $network_id, 'blog_count' ); 
    121120} 
    122121 
  • trunk/tests/phpunit/tests/multisite/network.php

    r37870 r40370  
    1313    protected $suppress = false; 
    1414 
     15    protected static $different_network_id; 
     16    protected static $different_site_ids = array(); 
     17 
    1518    function setUp() { 
    1619        global $wpdb; 
     
    2629    } 
    2730 
     31    public static function wpSetUpBeforeClass( $factory ) { 
     32        self::$different_network_id = $factory->network->create( array( 'domain' => 'wordpress.org', 'path' => '/' ) ); 
     33 
     34        $sites = array( 
     35            array( 'domain' => 'wordpress.org', 'path' => '/',     'site_id' => self::$different_network_id ), 
     36            array( 'domain' => 'wordpress.org', 'path' => '/foo/', 'site_id' => self::$different_network_id ), 
     37            array( 'domain' => 'wordpress.org', 'path' => '/bar/', 'site_id' => self::$different_network_id ), 
     38        ); 
     39 
     40        foreach ( $sites as $site ) { 
     41            self::$different_site_ids[] = $factory->blog->create( $site ); 
     42        } 
     43    } 
     44 
     45    public static function wpTearDownAfterClass() { 
     46        global $wpdb; 
     47 
     48        foreach( self::$different_site_ids as $id ) { 
     49            wpmu_delete_blog( $id, true ); 
     50        } 
     51 
     52        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$different_network_id ) ); 
     53        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$different_network_id ) ); 
     54 
     55        wp_update_network_site_counts(); 
     56    } 
     57 
    2858    /** 
    2959     * By default, only one network exists and has a network ID of 1. 
     
    6797        global $wpdb, $current_site; 
    6898 
    69         $id = self::factory()->network->create(); 
    70         $temp_id = $id + 1; 
    71  
    72         $current_site->id = (int) $id; 
     99        $temp_id = self::$different_network_id + 1; 
     100 
     101        $current_site->id = (int) self::$different_network_id; 
    73102        $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=%d WHERE id=1", $temp_id ) ); 
    74103        $main_network_id = get_main_network_id(); 
    75104        $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->site} SET id=1 WHERE id=%d", $temp_id ) ); 
    76105 
    77         $this->assertEquals( $id, $main_network_id ); 
     106        $this->assertEquals( self::$different_network_id, $main_network_id ); 
    78107    } 
    79108 
     
    158187 
    159188    /** 
     189     * @ticket 37865 
     190     */ 
     191    public function test_get_blog_count_on_different_network() { 
     192        global $current_site, $wpdb; 
     193 
     194        // switch_to_network()... 
     195        $orig_network_id = $current_site->id; 
     196        $orig_wpdb_network_id = $wpdb->siteid; 
     197        $current_site->id = self::$different_network_id; 
     198        $wpdb->siteid = self::$different_network_id; 
     199        wp_update_network_site_counts(); 
     200        $current_site->id = $orig_network_id; 
     201        $wpdb->siteid = $orig_wpdb_network_id; 
     202 
     203        $site_count = get_blog_count( self::$different_network_id ); 
     204 
     205        $this->assertSame( count( self::$different_site_ids ), $site_count ); 
     206    } 
     207 
     208    /** 
    160209     * @ticket 22917 
    161210     */ 
Note: See TracChangeset for help on using the changeset viewer.