| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | if ( is_multisite() ) : |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * A set of unit tests for WordPress Multisite |
|---|
| 7 | * |
|---|
| 8 | * @group multisite |
|---|
| 9 | */ |
|---|
| 10 | class Tests_MS_Switch_Speed extends WP_UnitTestCase { |
|---|
| 11 | |
|---|
| 12 | protected $num_of_sites = 100; |
|---|
| 13 | |
|---|
| 14 | function test_speed_new() { |
|---|
| 15 | global $wpdb, $current_site; |
|---|
| 16 | |
|---|
| 17 | $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); |
|---|
| 18 | $blog_ids = array_merge( |
|---|
| 19 | $this->factory->blog->create_many( $this->num_of_sites, array( 'user_id' => $user1_id ) ), |
|---|
| 20 | $this->factory->blog->create_many( $this->num_of_sites ) |
|---|
| 21 | ); |
|---|
| 22 | wp_set_current_user( $user1_id ); |
|---|
| 23 | |
|---|
| 24 | $start = microtime(true); |
|---|
| 25 | foreach ( $blog_ids as $blog_id ) { |
|---|
| 26 | switch_to_blog( $blog_id ); |
|---|
| 27 | if( current_user_can( 'upload_files' ) ) $sites[] = get_option( 'blogname' ); |
|---|
| 28 | restore_current_blog(); |
|---|
| 29 | } |
|---|
| 30 | echo "\n\n" . __FUNCTION__ . " duration: " . ( microtime(true) - $start ) . " (site-count: " . count( $sites ) . " of " . count( $blog_ids ) . ")\n\n"; |
|---|
| 31 | |
|---|
| 32 | // cleanup |
|---|
| 33 | foreach ( $blog_ids as $blog_id ) { |
|---|
| 34 | wpmu_delete_blog( $blog_id, true ); |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | function test_speed_old() { |
|---|
| 39 | global $wpdb, $current_site; |
|---|
| 40 | |
|---|
| 41 | $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); |
|---|
| 42 | $blog_ids = array_merge( |
|---|
| 43 | $this->factory->blog->create_many( $this->num_of_sites, array( 'user_id' => $user1_id ) ), |
|---|
| 44 | $this->factory->blog->create_many( $this->num_of_sites ) |
|---|
| 45 | ); |
|---|
| 46 | wp_set_current_user( $user1_id ); |
|---|
| 47 | |
|---|
| 48 | $start = microtime(true); |
|---|
| 49 | $sites = array(); |
|---|
| 50 | foreach ( $blog_ids as $blog_id ) { |
|---|
| 51 | if( old_current_user_can_for_blog( $blog_id , 'upload_files') ) $sites[] = get_blog_option( $blog_id, 'blogname' ); |
|---|
| 52 | } |
|---|
| 53 | echo "\n\n" . __FUNCTION__ . " duration: " . ( microtime(true) - $start ) . " (site-count: " . count( $sites ) . " of " . count( $blog_ids ) . ")\n\n"; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | // cleanup |
|---|
| 57 | foreach ( $blog_ids as $blog_id ) { |
|---|
| 58 | wpmu_delete_blog( $blog_id, true ); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | /** |
|---|
| 63 | * Original 3.4 version |
|---|
| 64 | * |
|---|
| 65 | * Whether current user has a capability or role for a given blog. |
|---|
| 66 | * |
|---|
| 67 | * @since 3.0.0 |
|---|
| 68 | * |
|---|
| 69 | * @param int $blog_id Blog ID |
|---|
| 70 | * @param string $capability Capability or role name. |
|---|
| 71 | * @return bool |
|---|
| 72 | */ |
|---|
| 73 | function old_current_user_can_for_blog( $blog_id, $capability ) { |
|---|
| 74 | $current_user = wp_get_current_user(); |
|---|
| 75 | |
|---|
| 76 | if ( empty( $current_user ) ) |
|---|
| 77 | return false; |
|---|
| 78 | |
|---|
| 79 | // Create new object to avoid stomping the global current_user. |
|---|
| 80 | $user = new WP_User( $current_user->ID) ; |
|---|
| 81 | |
|---|
| 82 | // Set the blog id. @todo add blog id arg to WP_User constructor? |
|---|
| 83 | $user->for_blog( $blog_id ); |
|---|
| 84 | |
|---|
| 85 | $args = array_slice( func_get_args(), 2 ); |
|---|
| 86 | $args = array_merge( array( $capability ), $args ); |
|---|
| 87 | |
|---|
| 88 | return call_user_func_array( array( &$user, 'has_cap' ), $args ); |
|---|
| 89 | } |
|---|
| 90 | endif; |
|---|