Networks and Sites: Allow filtering the directory size cache - #12750
Networks and Sites: Allow filtering the directory size cache#12750Sukhendu2002 wants to merge 2 commits into
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Pull request overview
Adds a new hook to let multisite/network code alter or supply the dirsize_cache data used by recurse_dirsize(), enabling cache-driven short-circuiting of directory size calculation.
Changes:
- Introduces
pre_recurse_dirsize_cacheto filter the directory size cache before calculation. - Updates
pre_recurse_dirsizeto receive a cached/default$space_usedvalue when available. - Adds PHPUnit coverage for the new cache filter and for invalid filter return values.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/wp-includes/functions.php |
Adds a new cache-filtering hook in recurse_dirsize() and threads cached size into the existing short-circuit filter. |
tests/phpunit/tests/multisite/cleanDirsizeCache.php |
Adds tests covering the new cache filter behavior and invalid filter return handling. |
Comments suppressed due to low confidence (2)
src/wp-includes/functions.php:9019
- The updated
pre_recurse_dirsizedocs include an@since x.x.xplaceholder. This should be replaced with the actual version number before commit.
* @since 5.6.0
* @since x.x.x The default `$space_used` value may be provided by the
* `pre_recurse_dirsize_cache` filter.
*
tests/phpunit/tests/multisite/cleanDirsizeCache.php:258
- This test sets/updates the global
dirsize_cachetransient but doesn’t clean it up. Deleting it at the end helps avoid leaking state into other tests.
$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' );
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * @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. |
| * 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 | ||
| * |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (4)
src/wp-includes/functions.php:9026
- The phpdoc here says
$max_execution_timeis anintand$directory_cacheis just “paths”, but in practice$max_execution_timemay be a string (fromini_get()), and$directory_cacheis a path => size map. Updating these doc types/descriptions will keep the filter contract accurate.
* @param int $max_execution_time Maximum time to run before giving up. In seconds.
* @param array $directory_cache Array of cached directory paths.
tests/phpunit/tests/multisite/cleanDirsizeCache.php:247
- For consistency with the rest of this test file, store the result of
wp_upload_dir()in a variable before indexing into it.
$directory = untrailingslashit( wp_upload_dir()['path'] );
src/wp-includes/functions.php:8993
$max_execution_timeis derived fromini_get( 'max_execution_time' ), which returns a string. The new hook’s phpdoc declares it asint, which can mislead callback implementations and static analysis. Consider documenting it asint|string(or casting to int before applying the filter).
This issue also appears on line 9025 of the same file.
* @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.
*/
tests/phpunit/tests/multisite/cleanDirsizeCache.php:231
- This test indexes directly into
get_transient( 'dirsize_cache' ). If the transient is unexpectedly missing/non-array, this will raise a PHP error rather than producing a helpful assertion failure. Also, the rest of this file stores the result ofwp_upload_dir()before accessing array keys.
$directory = untrailingslashit( wp_upload_dir()['path'] );
$this->assertSame( 1042, recurse_dirsize( $directory ) );
$this->assertSame( 42, get_transient( 'dirsize_cache' )[ $directory . '/subdirectory' ] );
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Trac ticket: https://core.trac.wordpress.org/ticket/56909
Use of AI Tools
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.