Multisite: More consistency for clean_dirsize_cache().

Props SergeyBiryukov.
Fixes #19879.


git-svn-id: https://develop.svn.wordpress.org/trunk@49616 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Helen Hou-Sandi 2020-11-16 22:29:42 +00:00
parent 740d99daf4
commit 56ffe940b0
4 changed files with 14 additions and 29 deletions

View File

@ -928,7 +928,7 @@ function _wp_handle_upload( &$file, $overrides, $time, $action ) {
$url = $uploads['url'] . "/$filename";
if ( is_multisite() ) {
invalidate_dirsize_cache( $new_file );
clean_dirsize_cache( $new_file );
}
/**

View File

@ -2747,7 +2747,7 @@ function wp_upload_bits( $name, $deprecated, $bits, $time = null ) {
$url = $upload['url'] . "/$filename";
if ( is_multisite() ) {
invalidate_dirsize_cache( $new_file );
clean_dirsize_cache( $new_file );
}
/** This filter is documented in wp-admin/includes/file.php */
@ -7628,7 +7628,8 @@ function recurse_dirsize( $directory, $exclude = null, $max_execution_time = nul
$size = 0;
$directory = untrailingslashit( $directory );
$cache_path = normalize_dirsize_cache_path( $directory );
$cache_path = untrailingslashit( str_replace( ABSPATH, '', $directory ) );
$save_cache = false;
if ( ! isset( $directory_cache ) ) {
@ -7722,14 +7723,14 @@ function recurse_dirsize( $directory, $exclude = null, $max_execution_time = nul
*
* @param string $path Full path of a directory or file.
*/
function invalidate_dirsize_cache( $path ) {
function clean_dirsize_cache( $path ) {
$directory_cache = get_transient( 'dirsize_cache' );
if ( empty( $directory_cache ) ) {
return;
}
$cache_path = normalize_dirsize_cache_path( $path );
$cache_path = untrailingslashit( str_replace( ABSPATH, '', $path ) );
unset( $directory_cache[ $cache_path ] );
while ( DIRECTORY_SEPARATOR !== $cache_path && '.' !== $cache_path && '..' !== $cache_path ) {
@ -7740,22 +7741,6 @@ function invalidate_dirsize_cache( $path ) {
set_transient( 'dirsize_cache', $directory_cache );
}
/**
* Normalize dirsize cache path.
*
* Ensures array keys within the dirsize_cache transient follow the same format.
*
* @since 5.6.0
*
* @param string $path
* @return string
*/
function normalize_dirsize_cache_path( $path ) {
$path = str_replace( ABSPATH, '', $path );
return untrailingslashit( $path );
}
/**
* Checks compatibility with the current WordPress version.
*

View File

@ -5918,7 +5918,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) {
$file = get_attached_file( $post_id );
if ( is_multisite() ) {
invalidate_dirsize_cache( $file );
clean_dirsize_cache( $file );
}
/**

View File

@ -71,7 +71,7 @@ if ( is_multisite() ) :
* Test whether the invalidation of the dirsize_cache works
* Given a file path as input
*/
function test_invalidate_dirsize_cache_file_input_mock() {
function test_clean_dirsize_cache_file_input_mock() {
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );
@ -85,7 +85,7 @@ if ( is_multisite() ) :
}
$upload_dir = wp_upload_dir();
$cache_key_prefix = normalize_dirsize_cache_path( $upload_dir['basedir'] );
$cache_key_prefix = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['basedir'] ) );
// Clear the dirsize_cache
delete_transient( 'dirsize_cache' );
@ -99,7 +99,7 @@ if ( is_multisite() ) :
// Invalidation should also respect the directory tree up
// Should work fine with path to folder OR file
invalidate_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' );
clean_dirsize_cache( $upload_dir['basedir'] . '/2/1/file.dummy' );
$this->assertSame( false, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
$this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
@ -116,7 +116,7 @@ if ( is_multisite() ) :
* Test whether the invalidation of the dirsize_cache works
* Given a folder path as input
*/
function test_invalidate_dirsize_cache_folder_input_mock() {
function test_clean_dirsize_cache_folder_input_mock() {
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );
@ -130,7 +130,7 @@ if ( is_multisite() ) :
}
$upload_dir = wp_upload_dir();
$cache_key_prefix = normalize_dirsize_cache_path( $upload_dir['basedir'] );
$cache_key_prefix = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['basedir'] ) );
// Clear the dirsize_cache
delete_transient( 'dirsize_cache' );
@ -144,7 +144,7 @@ if ( is_multisite() ) :
// Invalidation should also respect the directory tree up
// Should work fine with path to folder OR file
invalidate_dirsize_cache( $upload_dir['basedir'] . '/2/1' );
clean_dirsize_cache( $upload_dir['basedir'] . '/2/1' );
$this->assertSame( false, array_key_exists( $cache_key_prefix . '/2/1', get_transient( 'dirsize_cache' ) ) );
$this->assertSame( false, array_key_exists( $cache_key_prefix . '/2', get_transient( 'dirsize_cache' ) ) );
@ -190,7 +190,7 @@ if ( is_multisite() ) :
$this->assertSame( $size, $calc_size );
// dirsize_cache should now be filled after upload and recurse_dirsize call
$cache_path = normalize_dirsize_cache_path( $upload_dir['path'] );
$cache_path = untrailingslashit( str_replace( ABSPATH, '', $upload_dir['path'] ) );
$this->assertSame( true, is_array( get_transient( 'dirsize_cache' ) ) );
$this->assertSame( $size, get_transient( 'dirsize_cache' )[ $cache_path ] );