Make WordPress Core

Ticket #22917: 22917.7.diff

File 22917.7.diff, 6.5 KB (added by nacin, 12 years ago)
  • src/wp-includes/ms-default-filters.php

     
    3737add_action( 'publish_post', 'update_posts_count' );
    3838add_action( 'delete_post', '_update_blog_date_on_post_delete' );
    3939add_action( 'transition_post_status', '_update_blog_date_on_post_publish', 10, 3 );
     40
     41// Counts
    4042add_action( 'admin_init', 'wp_schedule_update_network_counts');
    4143add_action( 'update_network_counts', 'wp_update_network_counts');
     44foreach ( array( 'user_register', 'deleted_user', 'wpmu_new_user', 'make_spam_user', 'make_ham_user' ) as $action )
     45        add_action( $action, 'wp_maybe_update_network_user_counts' );
     46foreach ( array( 'make_spam_blog', 'make_ham_blog', 'archive_blog', 'unarchive_blog', 'make_delete_blog', 'make_undelete_blog' ) as $action )
     47        add_action( $action, 'wp_maybe_update_network_site_counts' );
     48unset( $action );
    4249
    4350// Files
    4451add_filter( 'wp_upload_bits', 'upload_is_file_too_big' );
     
    6774
    6875// Whitelist multisite domains for HTTP requests
    6976add_filter( 'http_request_host_is_external', 'ms_allowed_http_request_hosts', 20, 2 );
     77
  • src/wp-includes/ms-functions.php

     
    11021102
    11031103        $blog_id = $wpdb->insert_id;
    11041104        refresh_blog_details( $blog_id );
     1105
     1106        wp_maybe_update_network_site_counts();
     1107
    11051108        return $blog_id;
    11061109}
    11071110
     
    18761879 *  @since 3.1.0
    18771880 */
    18781881function wp_update_network_counts() {
     1882        wp_update_network_user_counts();
     1883        wp_update_network_site_counts();
     1884}
     1885
     1886/**
     1887 * Update the count of sites for the current network.
     1888 *
     1889 * If enabled through the 'enable_live_network_counts' filter, update the sites count
     1890 * on a network when a site is created or its status is updated.
     1891 *
     1892 * @since 3.7.0
     1893 *
     1894 * @uses wp_update_network_site_counts()
     1895 */
     1896function wp_maybe_update_network_site_counts() {
     1897        $is_small_network = ! wp_is_large_network( 'sites' );
     1898
     1899        /**
     1900         * Filter the decision to update network user and site counts in real time.
     1901         *
     1902         * @since 3.7.0
     1903         *
     1904         * @param bool   $small_network Based on wp_is_large_network( $context ).
     1905         * @param string $context       Context. Either 'users' or 'sites'.
     1906         */
     1907        if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) )
     1908                return;
     1909
     1910        wp_update_network_site_counts();
     1911}
     1912
     1913/**
     1914 * Update the network-wide users count.
     1915 *
     1916 * If enabled through the 'enable_live_network_counts' filter, update the users count
     1917 * on a network when a user is created or its status is updated.
     1918 *
     1919 * @since 3.7.0
     1920 *
     1921 * @uses wp_update_network_user_counts()
     1922 */
     1923function wp_maybe_update_network_user_counts() {
     1924        $is_small_network = ! wp_is_large_network( 'users' );
     1925
     1926        /**
     1927         * Filter the decision to update network user and site counts in real time.
     1928         *
     1929         * @since 3.7.0
     1930         *
     1931         * @param bool   $small_network Based on wp_is_large_network( $context ).
     1932         * @param string $context       Context. Either 'users' or 'sites'.
     1933         */
     1934        if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) )
     1935                return;
     1936
     1937        wp_update_network_user_counts();
     1938}
     1939
     1940/**
     1941 * Update the network-wide site count.
     1942 *
     1943 * @since 3.7.0
     1944 */
     1945function wp_update_network_site_counts() {
    18791946        global $wpdb;
    18801947
    18811948        $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) );
    18821949        update_site_option( 'blog_count', $count );
     1950}
    18831951
     1952/**
     1953 * Update the network-wide user count.
     1954 *
     1955 * @since 3.7.0
     1956 */
     1957function wp_update_network_user_counts() {
     1958        global $wpdb;
     1959
    18841960        $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    18851961        update_site_option( 'user_count', $count );
    18861962}
  • tests/phpunit/tests/ms.php

     
    1616                $_SERVER['REMOTE_ADDR'] = '';
    1717        }
    1818
     19        /**
     20         * @ticket 22917
     21         */
     22        function test_enable_live_network_site_counts_filter() {
     23                $site_count_start = get_blog_count();
     24                // false for large networks by default
     25                add_filter( 'enable_live_network_counts', '__return_false' );
     26                $this->factory->blog->create_many( 4 );
     27
     28                // count only updated when cron runs, so unchanged
     29                $this->assertEquals( $site_count_start, (int) get_blog_count() );
     30
     31                add_filter( 'enable_live_network_counts', '__return_true' );
     32                $site_ids = $this->factory->blog->create_many( 4 );
     33
     34                $this->assertEquals( $site_count_start + 9, (int) get_blog_count() );
     35
     36                //clean up
     37                remove_filter( 'enable_live_network_counts', '__return_false' );
     38                remove_filter( 'enable_live_network_counts', '__return_true' );
     39                foreach ( $site_ids as $site_id ) {
     40                        wpmu_delete_blog( $site_id, true );
     41                }
     42        }
     43
     44        /**
     45         * @ticket 22917
     46         */
     47        function test_enable_live_network_user_counts_filter() {
     48                // false for large networks by default
     49                add_filter( 'enable_live_network_counts', '__return_false' );
     50
     51                // Refresh the cache
     52                wp_update_network_counts();
     53                $start_count = get_user_count();
     54
     55                wpmu_create_user( 'user', 'pass', 'email' );
     56
     57                // No change, cache not refreshed
     58                $count = get_user_count();
     59
     60                $this->assertEquals( $start_count, $count );
     61
     62                wp_update_network_counts();
     63                $start_count = get_user_count();
     64
     65                add_filter( 'enable_live_network_counts', '__return_true' );
     66
     67                wpmu_create_user( 'user2', 'pass2', 'email2' );
     68
     69                $count = get_user_count();
     70                $this->assertEquals( $start_count + 1, $count );
     71
     72                remove_filter( 'enable_live_network_counts', '__return_false' );
     73                remove_filter( 'enable_live_network_counts', '__return_true' );
     74        }
     75
    1976        function test_create_and_delete_blog() {
    2077                global $wpdb;
    2178
     
    230287                wp_update_network_counts();
    231288                $start_count = get_user_count();
    232289
     290                // Only false for large networks as of 3.7
     291                add_filter( 'enable_live_network_counts', '__return_false' );
    233292                $this->factory->user->create( array( 'role' => 'administrator' ) );
    234293
    235294                $count = get_user_count(); // No change, cache not refreshed
     
    239298
    240299                $count = get_user_count();
    241300                $this->assertEquals( $start_count + 1, $count );
     301                remove_filter( 'enable_live_network_counts', '__return_false' );
    242302        }
    243303
    244304        function test_wp_schedule_update_network_counts() {