Make WordPress Core

Ticket #19879: 19879.final.patch

File 19879.final.patch, 7.6 KB (added by janthiel, 5 years ago)

PHPCS Fixes

  • src/wp-includes/post.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    58665866        $file         = get_attached_file( $post_id );
    58675867
    58685868        if ( is_multisite() ) {
    5869                 delete_transient( 'dirsize_cache' );
     5869                invalidate_dirsize_cache( $file );
    58705870        }
    58715871
    58725872        /**
  • src/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                invalidate_dirsize_cache( $new_file );
    932932        }
    933933
    934934        /**
  • src/wp-includes/functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    27402740        // Compute the URL.
    27412741        $url = $upload['url'] . "/$filename";
    27422742
     2743        if ( is_multisite() ) {
     2744                invalidate_dirsize_cache( $new_file );
     2745        }
     2746
    27432747        /** This filter is documented in wp-admin/includes/file.php */
    27442748        return apply_filters(
    27452749                'wp_handle_upload',
     
    75267530 * @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout.
    75277531 */
    75287532function get_dirsize( $directory, $max_execution_time = null ) {
    7529         $dirsize = get_transient( 'dirsize_cache' );
    7530 
    7531         if ( is_array( $dirsize ) && isset( $dirsize[ $directory ]['size'] ) ) {
    7532                 return $dirsize[ $directory ]['size'];
    7533         }
    7534 
    7535         if ( ! is_array( $dirsize ) ) {
    7536                 $dirsize = array();
    7537         }
    75387533
    75397534        // Exclude individual site directories from the total when checking the main site of a network,
    75407535        // as they are subdirectories and should not be counted.
    75417536        if ( is_multisite() && is_main_site() ) {
    7542                 $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
     7537                $size = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
    75437538        } else {
    7544                 $dirsize[ $directory ]['size'] = recurse_dirsize( $directory, null, $max_execution_time );
     7539                $size = recurse_dirsize( $directory, null, $max_execution_time );
    75457540        }
    75467541
    7547         set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
    7548         return $dirsize[ $directory ]['size'];
     7542        return $size;
    75497543}
    75507544
    75517545/**
     
    75577551 * @since MU (3.0.0)
    75587552 * @since 4.3.0 $exclude parameter added.
    75597553 * @since 5.2.0 $max_execution_time parameter added.
     7554 * @since 5.6.0 $directory_cache parameter added.
    75607555 *
    75617556 * @param string       $directory          Full path of a directory.
    75627557 * @param string|array $exclude            Optional. Full path of a subdirectory to exclude from the total,
    75637558 *                                         or array of paths. Expected without trailing slash(es).
    75647559 * @param int          $max_execution_time Maximum time to run before giving up. In seconds. The timeout is global
    75657560 *                                         and is measured from the moment WordPress started to load.
     7561 * @param array        $directory_cache    Optional. Array of cached directory paths.
     7562 *
    75667563 * @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout.
    75677564 */
    7568 function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null ) {
     7565function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null, &$directory_cache = null ) {
    75697566        $size = 0;
    75707567
    75717568        $directory = untrailingslashit( $directory );
     7569        $cache_path = normalize_dirsize_cache_path( $directory );
     7570        $save_cache = false;
     7571
     7572        if ( ! isset( $directory_cache ) ) {
     7573                $directory_cache = get_transient( 'dirsize_cache' );
     7574                $save_cache      = true;
     7575        }
     7576
     7577        if ( isset( $directory_cache[ $cache_path ] ) ) {
     7578                return $directory_cache[ $cache_path ];
     7579        }
    75727580
    75737581        if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) ) {
    75747582                return false;
     
    75967604                }
    75977605        }
    75987606
    7599         $handle = opendir( $directory );
    7600         if ( $handle ) {
    7601                 while ( ( $file = readdir( $handle ) ) !== false ) {
    7602                         $path = $directory . '/' . $file;
    7603                         if ( '.' !== $file && '..' !== $file ) {
    7604                                 if ( is_file( $path ) ) {
    7605                                         $size += filesize( $path );
    7606                                 } elseif ( is_dir( $path ) ) {
    7607                                         $handlesize = recurse_dirsize( $path, $exclude, $max_execution_time );
    7608                                         if ( $handlesize > 0 ) {
    7609                                                 $size += $handlesize;
    7610                                         }
    7611                                 }
     7607        /**
     7608        * Filters the amount of storage space used by one directory and all it's children, in megabytes.
     7609        * Return the actual used space to shortcircuit the recursive PHP file size calculation and use something else
     7610        * like a CDN API or native operating system tools for better performance
     7611        *
     7612        * @since 5.6.0
     7613        *
     7614        * @param int|false $space_used The amount of used space, in bytes. Default 0.
     7615        */
     7616        $size = apply_filters( 'calculate_current_dirsize', $size, $directory, $exclude, $max_execution_time, $directory_cache );
     7617
     7618        if ( 0 === $size ) {
     7619                $handle = opendir( $directory );
     7620                if ( $handle ) {
     7621                        while ( ( $file = readdir( $handle ) ) !== false ) {
     7622                                $path = $directory . '/' . $file;
     7623                                if ( '.' !== $file && '..' !== $file ) {
     7624                                        if ( is_file( $path ) ) {
     7625                                                $size += filesize( $path );
     7626                                        } elseif ( is_dir( $path ) ) {
     7627                                                $handlesize = recurse_dirsize( $path, $exclude, $max_execution_time, $directory_cache );
     7628                                                if ( $handlesize > 0 ) {
     7629                                                        $size += $handlesize;
     7630                                                }
     7631                                        }
    76127632
    7613                                 if ( $max_execution_time > 0 && microtime( true ) - WP_START_TIMESTAMP > $max_execution_time ) {
    7614                                         // Time exceeded. Give up instead of risking a fatal timeout.
    7615                                         $size = null;
    7616                                         break;
    7617                                 }
    7618                         }
    7619                 }
    7620                 closedir( $handle );
     7633                                        if ( $max_execution_time > 0 && microtime( true ) - WP_START_TIMESTAMP > $max_execution_time ) {
     7634                                                // Time exceeded. Give up instead of risking a fatal timeout.
     7635                                                $size = null;
     7636                                                break;
     7637                                        }
     7638                                }
     7639                        }
     7640                        closedir( $handle );
     7641                }
    76217642        }
     7643        $directory_cache[ $cache_path ] = $size;
     7644
     7645        // Only write the transient on the top level call and not on recursive calls
     7646        if ( $save_cache ) {
     7647                set_transient( 'dirsize_cache', $directory_cache );
     7648        }
     7649
    76227650        return $size;
    76237651}
    76247652
     7653/**
     7654 * Invalidates entries within the dirsize_cache
     7655 *
     7656 * Remove the current directory and all parent directories
     7657 * from the dirsize_cache transient.
     7658 *
     7659 * @since 5.6.0
     7660 *
     7661 * @param string $path Full path of a directory or file.
     7662 */
     7663function invalidate_dirsize_cache( $path ) {
     7664        $directory_cache = get_transient( 'dirsize_cache' );
     7665
     7666        if ( empty( $directory_cache ) ) {
     7667                return;
     7668        }
     7669
     7670        $cache_path = normalize_dirsize_cache_path( $path );
     7671        unset( $directory_cache[ $cache_path ] );
     7672
     7673        while ( DIRECTORY_SEPARATOR !== $cache_path && '.' !== $cache_path && '..' !== $cache_path ) {
     7674                $cache_path = dirname( $cache_path );
     7675                unset( $directory_cache[ $cache_path ] );
     7676        }
     7677
     7678        set_transient( 'dirsize_cache', $directory_cache );
     7679}
     7680
     7681/**
     7682 * Normalize dirsize cache path.
     7683 *
     7684 * Ensures array keys within the dirsize_cache transient follow the same format.
     7685 *
     7686 * @since 5.6.0
     7687 *
     7688 * @param string $path
     7689 * @return string
     7690 */
     7691function normalize_dirsize_cache_path( $path ) {
     7692        $path = str_replace( ABSPATH, '', $path );
     7693
     7694        return untrailingslashit( $path );
     7695}
     7696
    76257697/**
    76267698 * Checks compatibility with the current WordPress version.
    76277699 *