diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index 04dea32a20..993635e460 100644 --- a/src/wp-admin/includes/file.php +++ b/src/wp-admin/includes/file.php @@ -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 ); } /** diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 58a098f916..ccf89a449d 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -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. * diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 2e1afdbf4a..7db2c44899 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -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 ); } /** diff --git a/tests/phpunit/tests/multisite/dirsizeCache.php b/tests/phpunit/tests/multisite/cleanDirsizeCache.php similarity index 94% rename from tests/phpunit/tests/multisite/dirsizeCache.php rename to tests/phpunit/tests/multisite/cleanDirsizeCache.php index c9bdb83c00..816eba4393 100644 --- a/tests/phpunit/tests/multisite/dirsizeCache.php +++ b/tests/phpunit/tests/multisite/cleanDirsizeCache.php @@ -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 ] );