Make WordPress Core

Changeset 25621


Ignore:
Timestamp:
09/25/2013 04:20:21 PM (11 years ago)
Author:
nacin
Message:

Live network counts of users and sites for small networks.

props adamsilverstein, jeremyfelt.
fixes #22917.

Location:
trunk
Files:
3 edited

Legend:

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

    r25618 r25621  
    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
  • trunk/src/wp-includes/ms-functions.php

    r25590 r25621  
    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}
     
    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}
     1951
     1952/**
     1953 * Update the network-wide user count.
     1954 *
     1955 * @since 3.7.0
     1956 */
     1957function wp_update_network_user_counts() {
     1958    global $wpdb;
    18831959
    18841960    $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
  • trunk/tests/phpunit/tests/ms.php

    r25590 r25621  
    1515
    1616        $_SERVER['REMOTE_ADDR'] = '';
     17    }
     18
     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' );
    1774    }
    1875
     
    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
     
    240299        $count = get_user_count();
    241300        $this->assertEquals( $start_count + 1, $count );
     301        remove_filter( 'enable_live_network_counts', '__return_false' );
    242302    }
    243303
Note: See TracChangeset for help on using the changeset viewer.