Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8973,22 +8973,59 @@ function recurse_dirsize( $directory, $exclude = null, $max_execution_time = nul
}
}

if ( ! is_array( $directory_cache ) ) {
$directory_cache = array();
}

/**
* Filters the directory size cache before a directory's size is calculated.
*
* Returning an array containing the current directory's path with an integer size
* can short-circuit the recursive PHP file size calculation.
*
* @since x.x.x
*
Comment on lines +8981 to +8987
* @param array $directory_cache Array of cached directory paths and sizes.
* @param string $directory Full path of a directory.
* @param string|string[]|null $exclude Full path of a subdirectory to exclude from the total,
* or array of paths.
* @param int $max_execution_time Maximum time to run before giving up. In seconds.
*/
$filtered_directory_cache = apply_filters(
'pre_recurse_dirsize_cache',
$directory_cache,
$directory,
$exclude,
$max_execution_time
);

if ( is_array( $filtered_directory_cache ) ) {
$directory_cache = $filtered_directory_cache;
}

$size = isset( $directory_cache[ $directory ] ) && is_int( $directory_cache[ $directory ] )
? $directory_cache[ $directory ]
: false;

/**
* Filters the amount of storage space used by one directory and all its children, in megabytes.
* Filters the amount of storage space used by one directory and all its children, in bytes.
*
* Return the actual used space to short-circuit the recursive PHP file size calculation
* and use something else, like a CDN API or native operating system tools for better performance.
*
* @since 5.6.0
* @since x.x.x The default `$space_used` value may be provided by the
* `pre_recurse_dirsize_cache` filter.
*
* @param int|false $space_used The amount of used space, in bytes. Default false.
* @param int|false $space_used The amount of used space, in bytes. Default cached size
* for the current directory, or false.
Comment on lines +9017 to +9021
* @param string $directory Full path of a directory.
* @param string|string[]|null $exclude Full path of a subdirectory to exclude from the total,
* or array of paths.
* @param int $max_execution_time Maximum time to run before giving up. In seconds.
* @param array $directory_cache Array of cached directory paths.
*/
$size = apply_filters( 'pre_recurse_dirsize', false, $directory, $exclude, $max_execution_time, $directory_cache );
$size = apply_filters( 'pre_recurse_dirsize', $size, $directory, $exclude, $max_execution_time, $directory_cache );

if ( false === $size ) {
$size = 0;
Expand Down Expand Up @@ -9020,10 +9057,6 @@ function recurse_dirsize( $directory, $exclude = null, $max_execution_time = nul
}
}

if ( ! is_array( $directory_cache ) ) {
$directory_cache = array();
}

$directory_cache[ $directory ] = $size;

// Only write the transient on the top level call and not on recursive calls.
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/tests/multisite/cleanDirsizeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,47 @@ public function filter_pre_recurse_dirsize() {
return 1042;
}

/**
* @ticket 56909
*/
public function test_pre_recurse_dirsize_cache_filter() {
delete_transient( 'dirsize_cache' );
add_filter( 'pre_recurse_dirsize_cache', array( $this, 'filter_pre_recurse_dirsize_cache' ), 10, 2 );

$directory = untrailingslashit( wp_upload_dir()['path'] );
$this->assertSame( 1042, recurse_dirsize( $directory ) );
$this->assertSame( 42, get_transient( 'dirsize_cache' )[ $directory . '/subdirectory' ] );

remove_filter( 'pre_recurse_dirsize_cache', array( $this, 'filter_pre_recurse_dirsize_cache' ) );
delete_transient( 'dirsize_cache' );
}

public function filter_pre_recurse_dirsize_cache( $directory_cache, $directory ) {
$directory_cache[ $directory ] = 1042;
$directory_cache[ $directory . '/subdirectory' ] = 42;

return $directory_cache;
}

/**
* @ticket 56909
*/
public function test_pre_recurse_dirsize_cache_filter_with_invalid_value() {
$directory = untrailingslashit( wp_upload_dir()['path'] );
$cached_directory = $directory . '/cached';
set_transient( 'dirsize_cache', array( $cached_directory => 42 ) );
add_filter( 'pre_recurse_dirsize_cache', '__return_false' );

$this->assertIsInt( recurse_dirsize( $directory ) );

$directory_cache = get_transient( 'dirsize_cache' );
$this->assertIsArray( $directory_cache );
$this->assertSame( 42, $directory_cache[ $cached_directory ] );

remove_filter( 'pre_recurse_dirsize_cache', '__return_false' );
delete_transient( 'dirsize_cache' );
}

private function get_mock_dirsize_cache_for_site( $site_id ) {
$prefix = wp_upload_dir()['basedir'];

Expand Down
Loading