Skip to content

Networks and Sites: Allow filtering the directory size cache - #12750

Open
Sukhendu2002 wants to merge 2 commits into
WordPress:trunkfrom
Sukhendu2002:fix/56909-dirsize-cache-filter
Open

Networks and Sites: Allow filtering the directory size cache#12750
Sukhendu2002 wants to merge 2 commits into
WordPress:trunkfrom
Sukhendu2002:fix/56909-dirsize-cache-filter

Conversation

@Sukhendu2002

Copy link
Copy Markdown

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.

Copilot AI review requested due to automatic review settings July 29, 2026 06:13
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_cache to filter the directory size cache before calculation.
  • Updates pre_recurse_dirsize to receive a cached/default $space_used value 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_dirsize docs include an @since x.x.x placeholder. 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_cache transient 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.

Comment on lines +9017 to +9021
* @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 +8981 to +8987
* 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 thread tests/phpunit/tests/multisite/cleanDirsizeCache.php
Copilot AI review requested due to automatic review settings July 29, 2026 06:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_time is an int and $directory_cache is just “paths”, but in practice $max_execution_time may be a string (from ini_get()), and $directory_cache is 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_time is derived from ini_get( 'max_execution_time' ), which returns a string. The new hook’s phpdoc declares it as int, which can mislead callback implementations and static analysis. Consider documenting it as int|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 of wp_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' ] );

@Sukhendu2002
Sukhendu2002 marked this pull request as ready for review July 29, 2026 06:59
@github-actions

Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props sukhendu2002.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants