Make WordPress Core

Ticket #22917: 22917.6.diff

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

     
    146146                }
    147147
    148148                clean_blog_cache( $blog );
     149
     150                wp_update_network_counts_maybe_update( 'sites' );
    149151        }
    150152
    151153        if ( $switch )
     
    202204
    203205        clean_user_cache( $user );
    204206
     207        wp_update_network_counts_maybe_update( 'users' );
     208
    205209        /**
    206210         * Fires after the user is deleted from the network.
    207211         *
  • src/wp-includes/ms-functions.php

     
    908908
    909909        do_action( 'wpmu_new_user', $user_id );
    910910
     911        wp_update_network_counts_maybe_update( 'users' );
     912
    911913        return $user_id;
    912914}
    913915
     
    11021104
    11031105        $blog_id = $wpdb->insert_id;
    11041106        refresh_blog_details( $blog_id );
     1107        wp_update_network_counts_maybe_update( 'sites' );
     1108
    11051109        return $blog_id;
    11061110}
    11071111
     
    18741878 *  Update the network-wide counts for the current network.
    18751879 *
    18761880 *  @since 3.1.0
     1881 *
     1882 *  @uses wp_update_network_user_counts()
     1883 *  @uses wp_update_network_site_counts()
    18771884 */
    18781885function wp_update_network_counts() {
     1886        wp_update_network_user_counts();
     1887        wp_update_network_site_counts();
     1888}
     1889
     1890/**
     1891 * Update the network-wide counts for the current network.
     1892 *
     1893 * If enabled through the 'enable_live_network_counts' filter, update the sites or
     1894 * users count on a network as the user or site is created.
     1895 *
     1896 * @since 3.7.0
     1897 *
     1898 * @uses wp_update_network_user_counts()
     1899 * @uses wp_update_network_site_counts()
     1900 */
     1901function wp_update_network_counts_maybe_update( $context = 'sites' ) {
     1902        /**
     1903         * Filter the decision to update network user and site counts in real time.
     1904         *
     1905         * @since 3.7.0
     1906         *
     1907         * @param bool   null Opposite of wp_is_large_network() result. Depending on context,
     1908         *                    either 'users' or 'sites' will be passed to wp_is_large_network().
     1909         * @param string null Context. Either 'users' or 'sites'.
     1910         */
     1911        if ( ! apply_filters( 'enable_live_network_counts', ! wp_is_large_network( $context ), $context ) )
     1912                return;
     1913
     1914        if ( 'sites' === $context )
     1915                wp_update_network_site_counts();
     1916
     1917        if ( 'users' === $context )
     1918                wp_update_network_user_counts();
     1919
     1920        return;
     1921}
     1922
     1923/**
     1924 * Update the network-wide user count.
     1925 *
     1926 * @since 3.7.0
     1927 */
     1928function wp_update_network_user_counts() {
    18791929        global $wpdb;
    18801930
    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 
    18841931        $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
    18851932        update_site_option( 'user_count', $count );
    18861933}
    18871934
    18881935/**
     1936 * Update the network-wide site count.
     1937 *
     1938 * @since 3.7.0
     1939 */
     1940function wp_update_network_site_counts() {
     1941        global $wpdb;
     1942
     1943        $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 ) );
     1944        update_site_option( 'blog_count', $count );
     1945}
     1946
     1947/**
    18891948 * Returns the space used by the current blog.
    18901949 *
    18911950 * @since 3.5.0
  • src/wp-includes/user.php

     
    14341434        wp_cache_delete($user_id, 'users');
    14351435        wp_cache_delete($user_login, 'userlogins');
    14361436
     1437        if ( is_multisite() )
     1438                wp_update_network_counts_maybe_update( 'users' );
     1439
    14371440        if ( $update )
    14381441                do_action('profile_update', $user_id, $old_user_data);
    14391442        else
  • 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
     
    230285                wp_update_network_counts();
    231286                $start_count = get_user_count();
    232287
     288                add_filter( 'enable_live_network_counts', __return_false ); //false by default as of 3.7
    233289                $this->factory->user->create( array( 'role' => 'administrator' ) );
    234290
    235291                $count = get_user_count(); // No change, cache not refreshed
     
    239295
    240296                $count = get_user_count();
    241297                $this->assertEquals( $start_count + 1, $count );
     298                remove_filter( 'enable_live_network_counts', __return_false );
    242299        }
    243300
    244301        function test_wp_schedule_update_network_counts() {