From 1515787d31dd8dd47f647797d3478e6dd60bf31c Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 9 May 2017 15:50:04 +0000 Subject: [PATCH] Multisite: Add `$network_id` parameter to `wp_is_large_network()`. Now that `get_blog_count()` and `get_user_count()` both support passing a `$network_id` parameter (see [40370] and [40371]), similar functionality is now available for `wp_is_large_network()`. In addition, the filter `wp_is_large_network` now accepts the network ID as its fourth parameter. This changeset furthermore introduces unit tests for the `wp_is_large_network()` function and its filter. Fixes #40489. git-svn-id: https://develop.svn.wordpress.org/trunk@40590 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-functions.php | 22 +++-- tests/phpunit/tests/multisite/network.php | 115 ++++++++++++++++++++++ 2 files changed, 131 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/ms-functions.php b/src/wp-includes/ms-functions.php index ff639e30e1..edc85617ef 100644 --- a/src/wp-includes/ms-functions.php +++ b/src/wp-includes/ms-functions.php @@ -2490,27 +2490,37 @@ function upload_size_limit_filter( $size ) { * Plugins can alter this criteria using the {@see 'wp_is_large_network'} filter. * * @since 3.3.0 - * @param string $using 'sites or 'users'. Default is 'sites'. + * @since 4.8.0 The $network_id parameter has been added. + * + * @param string $using 'sites or 'users'. Default is 'sites'. + * @param int|null $network_id ID of the network. Default is the current network. * @return bool True if the network meets the criteria for large. False otherwise. */ -function wp_is_large_network( $using = 'sites' ) { +function wp_is_large_network( $using = 'sites', $network_id = null ) { + $network_id = (int) $network_id; + if ( ! $network_id ) { + $network_id = get_current_network_id(); + } + if ( 'users' == $using ) { - $count = get_user_count(); + $count = get_user_count( $network_id ); /** * Filters whether the network is considered large. * * @since 3.3.0 + * @since 4.8.0 The $network_id parameter has been added. * * @param bool $is_large_network Whether the network has more than 10000 users or sites. * @param string $component The component to count. Accepts 'users', or 'sites'. * @param int $count The count of items for the component. + * @param int $network_id The ID of the network being checked. */ - return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count ); + return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count, $network_id ); } - $count = get_blog_count(); + $count = get_blog_count( $network_id ); /** This filter is documented in wp-includes/ms-functions.php */ - return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count ); + return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count, $network_id ); } /** diff --git a/tests/phpunit/tests/multisite/network.php b/tests/phpunit/tests/multisite/network.php index 71e3ed8ff7..e5106578b3 100644 --- a/tests/phpunit/tests/multisite/network.php +++ b/tests/phpunit/tests/multisite/network.php @@ -470,6 +470,121 @@ class Tests_Multisite_Network extends WP_UnitTestCase { $this->assertTrue( $site_count > 0 && $user_count > 0 ); } + + /** + * @ticket 40489 + * @dataProvider data_wp_is_large_network + */ + public function test_wp_is_large_network( $using, $count, $expected, $different_network ) { + $network_id = $different_network ? self::$different_network_id : null; + $network_option = 'users' === $using ? 'user_count' : 'blog_count'; + + update_network_option( $network_id, $network_option, $count ); + + $result = wp_is_large_network( $using, $network_id ); + if ( $expected ) { + $this->assertTrue( $result ); + } else { + $this->assertFalse( $result ); + } + } + + public function data_wp_is_large_network() { + return array( + array( 'sites', 10000, false, false ), + array( 'sites', 10001, true, false ), + array( 'users', 10000, false, false ), + array( 'users', 10001, true, false ), + array( 'sites', 10000, false, true ), + array( 'sites', 10001, true, true ), + array( 'users', 10000, false, true ), + array( 'users', 10001, true, true ), + ); + } + + /** + * @ticket 40489 + * @dataProvider data_wp_is_large_network_filtered_by_component + */ + public function test_wp_is_large_network_filtered_by_component( $using, $count, $expected, $different_network ) { + $network_id = $different_network ? self::$different_network_id : null; + $network_option = 'users' === $using ? 'user_count' : 'blog_count'; + + update_network_option( $network_id, $network_option, $count ); + + add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10, 3 ); + $result = wp_is_large_network( $using, $network_id ); + remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_for_users' ), 10 ); + + if ( $expected ) { + $this->assertTrue( $result ); + } else { + $this->assertFalse( $result ); + } + } + + public function data_wp_is_large_network_filtered_by_component() { + return array( + array( 'sites', 10000, false, false ), + array( 'sites', 10001, true, false ), + array( 'users', 1000, false, false ), + array( 'users', 1001, true, false ), + array( 'sites', 10000, false, true ), + array( 'sites', 10001, true, true ), + array( 'users', 1000, false, true ), + array( 'users', 1001, true, true ), + ); + } + + public function filter_wp_is_large_network_for_users( $is_large_network, $using, $count ) { + if ( 'users' === $using ) { + return $count > 1000; + } + + return $is_large_network; + } + + /** + * @ticket 40489 + * @dataProvider data_wp_is_large_network_filtered_by_network + */ + public function test_wp_is_large_network_filtered_by_network( $using, $count, $expected, $different_network ) { + $network_id = $different_network ? self::$different_network_id : null; + $network_option = 'users' === $using ? 'user_count' : 'blog_count'; + + update_network_option( $network_id, $network_option, $count ); + + add_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10, 4 ); + $result = wp_is_large_network( $using, $network_id ); + remove_filter( 'wp_is_large_network', array( $this, 'filter_wp_is_large_network_on_different_network' ), 10 ); + + if ( $expected ) { + $this->assertTrue( $result ); + } else { + $this->assertFalse( $result ); + } + } + + public function data_wp_is_large_network_filtered_by_network() { + return array( + array( 'sites', 10000, false, false ), + array( 'sites', 10001, true, false ), + array( 'users', 10000, false, false ), + array( 'users', 10001, true, false ), + array( 'sites', 1000, false, true ), + array( 'sites', 1001, true, true ), + array( 'users', 1000, false, true ), + array( 'users', 1001, true, true ), + ); + } + + public function filter_wp_is_large_network_on_different_network( $is_large_network, $using, $count, $network_id ) { + if ( $network_id === (int) self::$different_network_id ) { + return $count > 1000; + } + + return $is_large_network; + } } endif;