Make WordPress Core

Ticket #22917: 22917.5.diff

File 22917.5.diff, 5.1 KB (added by jeremyfelt, 11 years ago)
  • src/wp-admin/includes/ms.php

     
    146146                }
    147147
    148148                clean_blog_cache( $blog );
     149
     150                //duplicate hook
     151                if ( apply_filters( 'enable_live_network_counts', ! wp_is_large_network( 'sites' ), 'sites' ) )
     152                        wp_update_network_site_counts();
    149153        }
    150154
    151155        if ( $switch )
     
    202206
    203207        clean_user_cache( $user );
    204208
     209        //duplicate hook
     210        if ( apply_filters( 'enable_live_network_counts', ! wp_is_large_network( 'users' ), 'users' ) )
     211                wp_update_network_user_counts();
     212
    205213        /**
    206214         * Fires after the user is deleted from the network.
    207215         *
  • src/wp-includes/ms-functions.php

     
    908908
    909909        do_action( 'wpmu_new_user', $user_id );
    910910
     911        /**
     912         * Filter the decision to update network user and site counts in real time.
     913         *
     914         * @since 3.7.0
     915         *
     916         * @param bool   null Opposite of wp_is_large_network() result. Depending on context,
     917         *                    either 'users' or 'sites' will be passed to wp_is_large_network().
     918         * @param string null Context. Either 'users' or 'sites'.
     919         */
     920        if ( apply_filters( 'enable_live_network_counts', ! wp_is_large_network( 'users' ), 'users' ) )
     921                wp_update_network_user_counts();
     922
    911923        return $user_id;
    912924}
    913925
     
    11401152        populate_roles();
    11411153        $wp_roles->_init();
    11421154
     1155        //duplicate hook
     1156        if ( apply_filters( 'enable_live_network_counts', ! wp_is_large_network( 'sites' ), 'sites' ) )
     1157                wp_update_network_site_counts();
     1158
    11431159        $url = untrailingslashit( $url );
    11441160
    11451161        update_option( 'siteurl', $url );
     
    18741890 *  Update the network-wide counts for the current network.
    18751891 *
    18761892 *  @since 3.1.0
     1893 *
     1894 *  @uses wp_update_network_user_counts()
     1895 *  @uses wp_update_network_site_counts()
    18771896 */
    18781897function wp_update_network_counts() {
     1898        wp_update_network_user_counts();
     1899        wp_update_network_site_counts();
     1900}
     1901
     1902/**
     1903 * Update the network-wide user count.
     1904 *
     1905 * @since 3.7.0
     1906 */
     1907function wp_update_network_user_counts() {
    18791908        global $wpdb;
    18801909
    1881         $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) );
    1882         update_site_option( 'blog_count', $count );
    1883 
    18841910        $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    18851911        update_site_option( 'user_count', $count );
    18861912}
    18871913
    18881914/**
     1915 * Update the network-wide site count.
     1916 *
     1917 * @since 3.7.0
     1918 */
     1919function wp_update_network_site_counts() {
     1920        global $wpdb;
     1921
     1922        $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 ) );
     1923        update_site_option( 'blog_count', $count );
     1924}
     1925
     1926/**
    18891927 * Returns the space used by the current blog.
    18901928 *
    18911929 * @since 3.5.0
  • 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                add_filter( 'enable_live_network_counts', __return_false ); //false by default as of 3.7
     25                $this->factory->blog->create_many( 4 );
     26
     27                $this->assertEquals( $site_count_start, (int) get_blog_count() ); //count only updated when cron runs, so likely unchanged
     28               
     29                add_filter( 'enable_live_network_counts', __return_true ); //turns on live updating
     30                $site_ids = $this->factory->blog->create_many( 4 );
     31
     32                $this->assertEquals( $site_count_start + 9, (int) get_blog_count() ); //count should be updated
     33
     34                //clean up
     35                remove_filter( 'enable_live_network_counts', __return_false );
     36                remove_filter( 'enable_live_network_counts', __return_true );
     37                foreach ( $site_ids as $site_id ) {
     38                        wpmu_delete_blog( $site_id, true );
     39                }
     40        }
     41
     42        /**
     43         * @ticket 22917
     44         */
     45        function test_enable_live_network_user_counts_filter() {
     46                add_filter( 'enable_live_network_counts', __return_false ); //false by default as of 3.7
     47
     48                // Refresh the cache
     49                wp_update_network_counts();
     50                $start_count = get_user_count();
     51
     52                wpmu_create_user( 'user', 'pass', 'email' );
     53
     54                $count = get_user_count(); // No change, cache not refreshed
     55
     56                $this->assertEquals( $start_count, $count );
     57               
     58                //start over with filter on
     59                wp_update_network_counts();
     60                $start_count = get_user_count();
     61
     62                add_filter( 'enable_live_network_counts', __return_true ); //turns on live updating
     63               
     64                wpmu_create_user( 'user2', 'pass2', 'email2' );
     65
     66                $count = get_user_count();
     67                $this->assertEquals( $start_count + 1, $count );
     68
     69                //turn filter back off
     70                remove_filter( 'enable_live_network_counts', __return_false );
     71                remove_filter( 'enable_live_network_counts', __return_true );
     72        }
     73
    1974        function test_create_and_delete_blog() {
    2075                global $wpdb;
    2176