Make WordPress Core

Ticket #19879: 19879_WordPress_56-AddedFilterForDirSizeCalculation.2.patch

File 19879_WordPress_56-AddedFilterForDirSizeCalculation.2.patch, 7.2 KB (added by janthiel, 5 years ago)

New filter comment wrongly stated to expect the dir size in megabytes and not in bytes

  • wp-admin/includes/file.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    928928        $url = $uploads['url'] . "/$filename";
    929929
    930930        if ( is_multisite() ) {
    931                 delete_transient( 'dirsize_cache' );
     931                clear_dirsize_cache( $new_file );
    932932        }
    933933
    934934        /**
  • wp-includes/post.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    58375837        $file         = get_attached_file( $post_id );
    58385838
    58395839        if ( is_multisite() ) {
    5840                 delete_transient( 'dirsize_cache' );
     5840                clear_dirsize_cache( $file );
    58415841        }
    58425842
    58435843        /**
  • wp-includes/functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    75257525 * @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout.
    75267526 */
    75277527function get_dirsize( $directory, $max_execution_time = null ) {
    7528         $dirsize = get_transient( 'dirsize_cache' );
    7529 
    7530         if ( is_array( $dirsize ) && isset( $dirsize[ $directory ]['size'] ) ) {
    7531                 return $dirsize[ $directory ]['size'];
    7532         }
    7533 
    7534         if ( ! is_array( $dirsize ) ) {
    7535                 $dirsize = array();
    7536         }
    75377528
    75387529        // Exclude individual site directories from the total when checking the main site of a network,
    75397530        // as they are subdirectories and should not be counted.
    75407531        if ( is_multisite() && is_main_site() ) {
    7541                 $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
     7532                $size = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
    75427533        } else {
    7543                 $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, null, $max_execution_time );
     7534                $size = recurse_dirsize( $directory, null, $max_execution_time );
    75447535        }
    75457536
    7546         set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
    7547         return $dirsize[ $directory ]['size'];
     7537        return $size;
    75487538}
    75497539
    75507540/**
     
    75567546 * @since MU (3.0.0)
    75577547 * @since 4.3.0 $exclude parameter added.
    75587548 * @since 5.2.0 $max_execution_time parameter added.
     7549 * @since 5.6.0 $directory_cache parameter added.
    75597550 *
    75607551 * @param string       $directory          Full path of a directory.
    75617552 * @param string|array $exclude            Optional. Full path of a subdirectory to exclude from the total,
    75627553 *                                         or array of paths. Expected without trailing slash(es).
    75637554 * @param int          $max_execution_time Maximum time to run before giving up. In seconds. The timeout is global
    75647555 *                                         and is measured from the moment WordPress started to load.
     7556 * @param array        $directory_cache    Optional. Array of cached directory paths.
     7557 *
    75657558 * @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout.
    75667559 */
    7567 function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null ) {
     7560function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null, &$directory_cache = null ) {
    75687561        $size = 0;
    75697562
    75707563        $directory = untrailingslashit( $directory );
     7564        $cache_path = normalize_dirsize_cache_path( $directory );
     7565        $save_cache = false;
     7566
     7567        if ( ! isset( $directory_cache ) ) {
     7568                $directory_cache = get_transient( 'dirsize_cache' );
     7569                $save_cache      = true;
     7570        }
     7571
     7572        if ( isset( $directory_cache[ $cache_path ] ) ) {
     7573                return $directory_cache[ $cache_path ];
     7574        }
    75717575
    75727576        if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) ) {
    75737577                return false;
     
    75957599                }
    75967600        }
    75977601
    7598         $handle = opendir( $directory );
    7599         if ( $handle ) {
    7600                 while ( ( $file = readdir( $handle ) ) !== false ) {
    7601                         $path = $directory . '/' . $file;
    7602                         if ( '.' !== $file && '..' !== $file ) {
    7603                                 if ( is_file( $path ) ) {
    7604                                         $size += filesize( $path );
    7605                                 } elseif ( is_dir( $path ) ) {
    7606                                         $handlesize = recurse_dirsize( $path, $exclude, $max_execution_time );
    7607                                         if ( $handlesize > 0 ) {
    7608                                                 $size += $handlesize;
    7609                                         }
    7610                                 }
     7602        /**
     7603         * Filters the amount of storage space used by one directory and all it's children, in megabytes.
     7604         * Return the actual used space to shortcircuit the recursive PHP file size calculation and use something else
     7605     * like a CDN API or native operating system tools for better performance
     7606         *
     7607         * @since 5.6.0
     7608         *
     7609         * @param int|false $space_used The amount of used space, in bytes. Default 0.
     7610         */
     7611        $size = apply_filters( 'calculate_current_dirsize', $size, $directory, $exclude, $max_execution_time, $directory_cache );
     7612
     7613        if ( $size === 0 ) {
     7614                $handle = opendir( $directory );
     7615                if ( $handle ) {
     7616                        while ( ( $file = readdir( $handle ) ) !== false ) {
     7617                                $path = $directory . '/' . $file;
     7618                                if ( '.' !== $file && '..' !== $file ) {
     7619                                        if ( is_file( $path ) ) {
     7620                                                $size += filesize( $path );
     7621                                        } elseif ( is_dir( $path ) ) {
     7622                                                $handlesize = recurse_dirsize( $path, $exclude, $max_execution_time, $directory_cache );
     7623                                                if ( $handlesize > 0 ) {
     7624                                                        $size += $handlesize;
     7625                                                }
     7626                                        }
    76117627
    7612                                 if ( $max_execution_time > 0 && microtime( true ) - WP_START_TIMESTAMP > $max_execution_time ) {
    7613                                         // Time exceeded. Give up instead of risking a fatal timeout.
    7614                                         $size = null;
    7615                                         break;
    7616                                 }
    7617                         }
    7618                 }
    7619                 closedir( $handle );
     7628                                        if ( $max_execution_time > 0 && microtime( true ) - WP_START_TIMESTAMP > $max_execution_time ) {
     7629                                                // Time exceeded. Give up instead of risking a fatal timeout.
     7630                                                $size = null;
     7631                                                break;
     7632                                        }
     7633                                }
     7634                        }
     7635                        closedir( $handle );
     7636                }
    76207637        }
     7638        $directory_cache[ $cache_path ] = $size;
     7639
     7640        // Only write the transient on the top level call and not on recursive calls
     7641        if ( $save_cache ) {
     7642                set_transient( 'dirsize_cache', $directory_cache );
     7643        }
     7644
    76217645        return $size;
    76227646}
    76237647
     7648/**
     7649 * Clear dirsize_cache
     7650 *
     7651 * Remove the current directory and all parent directories
     7652 * from the dirsize_cache transient.
     7653 *
     7654 * @since 5.6.0
     7655 *
     7656 * @param string $path Full path of a directory.
     7657 */
     7658function clear_dirsize_cache( $path ) {
     7659        $directory_cache = get_transient( 'dirsize_cache' );
     7660
     7661        if ( empty( $directory_cache ) ) {
     7662                return;
     7663        }
     7664
     7665        $cache_path = normalize_dirsize_cache_path( $path );
     7666        unset( $directory_cache[ $cache_path ] );
     7667
     7668        while ( DIRECTORY_SEPARATOR !== $cache_path && '.' !== $cache_path && '..' !== $cache_path ) {
     7669                $cache_path = dirname( $cache_path );
     7670                unset( $directory_cache[ $cache_path ] );
     7671        }
     7672
     7673        set_transient( 'dirsize_cache', $directory_cache );
     7674}
     7675
     7676/**
     7677 * Normalize dirsize cache path.
     7678 *
     7679 * Ensures array keys follow the same format.
     7680 *
     7681 * @param string $path
     7682 *
     7683 * @since 5.6.0
     7684 *
     7685 * @return string
     7686 */
     7687function normalize_dirsize_cache_path( $path ) {
     7688        $path = str_replace( ABSPATH, '', $path );
     7689
     7690        return untrailingslashit( $path );
     7691}
     7692
    76247693/**
    76257694 * Checks compatibility with the current WordPress version.
    76267695 *