Using WordPress ‘pre_recurse_dirsize’ PHP filter

The ‘pre_recurse_dirsize’ WordPress PHP filter is used to modify the directory size calculation before it is performed.

This filter is called before the ‘recurse_dirsize‘ function is called, which is responsible for calculating the size of a directory when it contains other directories.

Usage

function my_custom_space_calculation( $space_used, $directory, $exclude, $max_execution_time, $directory_cache ) {
    // Custom space calculation logic goes here
    return $custom_space_used;
}
add_filter( 'pre_recurse_dirsize', 'my_custom_space_calculation', 10, 5 );

Parameters

  • $space_used (int|false): The amount of used space, in bytes. Default value is false. This parameter represents the initial value that will be modified by the custom filter function.
  • $directory (string): The full path of the directory for which the space calculation is being performed. This parameter is used to specify the target directory.
  • $exclude (string|string[]|null): The full path of a subdirectory to exclude from the total space calculation or an array of paths. Default value is null. This parameter allows you to define specific directories to be excluded from the calculation.
  • $max_execution_time (int): The maximum time, in seconds, for the space calculation process to run before giving up. This parameter can be used to set a limit on the time allowed for the custom filter function to complete.
  • $directory_cache (array): An array of cached directory paths. This parameter can be used to improve the performance of the space calculation process by reusing previously cached directory paths.

Examples

Calculate space using a CDN API

function calculate_space_with_cdn_api( $space_used, $directory, $exclude, $max_execution_time, $directory_cache ) {
    // Call CDN API to get space used
    $cdn_space_used = get_cdn_space_used( $directory, $exclude );
    return $cdn_space_used;
}
add_filter( 'pre_recurse_dirsize', 'calculate_space_with_cdn_api', 10, 5 );

This example replaces the default PHP file size calculation with a CDN API call to determine the space used by a directory.

Using operating system tools for space calculation

function calculate_space_with_os_tools( $space_used, $directory, $exclude, $max_execution_time, $directory_cache ) {
    // Use operating system tools to get space used
    $os_space_used = get_os_space_used( $directory, $exclude );
    return $os_space_used;
}
add_filter( 'pre_recurse_dirsize', 'calculate_space_with_os_tools', 10, 5 );

This example leverages operating system tools for a faster and more accurate space calculation.

Excluding specific subdirectories

function exclude_specific_subdirs( $space_used, $directory, $exclude, $max_execution_time, $directory_cache ) {
    // Custom logic to exclude specific subdirectories from the total space used
    $exclude_dirs = array( '/path/to/first/excluded/dir', '/path/to/second/excluded/dir' );
    $custom_space_used = get_space_used_with_exclusions( $directory, $exclude_dirs );
    return $custom_space_used;
}
add_filter( 'pre_recurse_dirsize', 'exclude_specific_subdirs', 10, 5 );

This example demonstrates how to exclude specific subdirectories from the total space calculation.

Adjusting the maximum execution time

function adjust_max_execution_time( $space_used, $directory, $exclude, $max_execution_time, $directory_cache ) {
    // Custom logic with a modified maximum execution time
    $custom_max_execution_time = 30; // 30 seconds
    $custom_space_used = get_space_used_with_time_limit( $directory, $exclude, $custom_max_execution_time );
    return $custom_space_used;
}
add_filter( 'pre_recurse_dirsize', 'adjust_max_execution_time', 10, 5 );

This example shows how to adjust the maximum execution time for the space calculation process.

Caching directory paths

function use_directory_cache( $space_used, $directory, $exclude, $max_execution_time, $directory_cache ) { 
    // Custom logic that uses a cached directory paths array 
    $custom_space_used = get_space_used_with_cache( $directory, $exclude, $directory_cache ); 
    return $custom_space_used; 
} 
add_filter( 'pre_recurse_dirsize', 'use_directory_cache', 10, 5 );

This example demonstrates how to utilize a directory cache to improve the performance of the space calculation process.