Make WordPress Core

Ticket #30202: 30202.4.diff

File 30202.4.diff, 5.1 KB (added by jeremyfelt, 8 years ago)
  • src/wp-includes/ms-functions.php

     
    16861686 *
    16871687 * @since MU
    16881688 *
    1689  * @param string $directory
    1690  * @return int
     1689 * @param string $directory Full path of a directory.
     1690 * @return int Size of the directory in MB.
    16911691 */
    16921692function get_dirsize( $directory ) {
    16931693        $dirsize = get_transient( 'dirsize_cache' );
     
    16971697        if ( ! is_array( $dirsize ) )
    16981698                $dirsize = array();
    16991699
    1700         $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
     1700        // If this is the main site, exclude individual site directories from the total.
     1701        if ( is_main_site() ) {
     1702                $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory, $directory . '/sites' );
     1703        } else {
     1704                $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
     1705        }
    17011706
    17021707        set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
    17031708        return $dirsize[ $directory ][ 'size' ];
     
    17101715 * other directories.
    17111716 *
    17121717 * @since MU
     1718 * @since 4.3.0 $exclude parameter added.
    17131719 *
    1714  * @param string $directory
    1715  * @return int|false
     1720 * @param string $directory Full path of a directory.
     1721 * @param string $exclude   Optional. Full path of a subdirectory to exclude from the total.
     1722 * @return int|false Size in MB if a valid directory. False if not.
    17161723 */
    1717 function recurse_dirsize( $directory ) {
     1724function recurse_dirsize( $directory, $exclude = null ) {
    17181725        $size = 0;
    17191726
    17201727        $directory = untrailingslashit( $directory );
    17211728
    1722         if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
     1729        if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
    17231730                return false;
     1731        }
    17241732
    17251733        if ($handle = opendir($directory)) {
    17261734                while(($file = readdir($handle)) !== false) {
     
    17291737                                if (is_file($path)) {
    17301738                                        $size += filesize($path);
    17311739                                } elseif (is_dir($path)) {
    1732                                         $handlesize = recurse_dirsize($path);
     1740                                        $handlesize = recurse_dirsize( $path, $exclude );
    17331741                                        if ($handlesize > 0)
    17341742                                                $size += $handlesize;
    17351743                                }
  • tests/phpunit/tests/multisite/getSpaceUsed.php

     
     1<?php
     2
     3if ( is_multisite() ) :
     4
     5/**
     6 * @group multisite
     7 * @covers ::get_space_used
     8 */
     9class Tests_Multisite_Get_Space_Used extends WP_UnitTestCase {
     10        protected $suppress = false;
     11
     12        function setUp() {
     13                global $wpdb;
     14                parent::setUp();
     15                $this->suppress = $wpdb->suppress_errors();
     16
     17                $_SERVER['REMOTE_ADDR'] = '';
     18        }
     19
     20        function tearDown() {
     21                global $wpdb;
     22                $wpdb->suppress_errors( $this->suppress );
     23                parent::tearDown();
     24        }
     25
     26        function test_get_space_used_switched_site() {
     27                $blog_id = $this->factory->blog->create();
     28                switch_to_blog( $blog_id );
     29
     30                // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
     31                // src directory already contains a content directory with site content, then the initial expectation
     32                // will be polluted. We create sites until an empty one is available.
     33                while ( 0 != get_space_used() ) {
     34                        restore_current_blog();
     35                        $blog_id = $this->factory->blog->create();
     36                        switch_to_blog( $blog_id );
     37                }
     38
     39                // Upload a file to the new site.
     40                $filename = rand_str().'.jpg';
     41                $contents = rand_str();
     42                $file = wp_upload_bits( $filename, null, $contents );
     43
     44                // get_space_used() is measures in MB, get the size of the new file in MB.
     45                $size = filesize( $file['file'] ) / 1024 / 1024;
     46
     47                delete_transient( 'dirsize_cache' );
     48
     49                $this->assertEquals( $size, get_space_used() );
     50                $upload_dir = wp_upload_dir();
     51                $this->remove_added_uploads();
     52                $this->delete_folders( $upload_dir['basedir'] );
     53                restore_current_blog();
     54        }
     55
     56        /**
     57         * Directories of sub sites on a network should not count against the same spaced used total for
     58         * the main site.
     59         */
     60        function test_get_space_used_main_site() {
     61                $space_used = get_space_used();
     62
     63                $blog_id = $this->factory->blog->create();
     64                switch_to_blog( $blog_id );
     65
     66                // We don't rely on an initial value of 0 for space used, but should have a clean space available
     67                // so that we can remove any uploaded files and directories without concern of a conflict with
     68                // existing content directories in src.
     69                while ( 0 != get_space_used() ) {
     70                        restore_current_blog();
     71                        $blog_id = $this->factory->blog->create();
     72                        switch_to_blog( $blog_id );
     73                }
     74
     75                // Upload a file to the new site.
     76                $filename = rand_str().'.jpg';
     77                $contents = rand_str();
     78                wp_upload_bits( $filename, null, $contents );
     79
     80                restore_current_blog();
     81
     82                delete_transient( 'dirsize_cache' );
     83
     84                $this->assertEquals( $space_used, get_space_used() );
     85
     86                // Switch back to the new site to remove the uploaded file.
     87                switch_to_blog( $blog_id );
     88                $upload_dir = wp_upload_dir();
     89                $this->remove_added_uploads();
     90                $this->delete_folders( $upload_dir['basedir'] );
     91                restore_current_blog();
     92        }
     93}
     94endif;
     95 No newline at end of file