Ticket #19879: 19879_WordPress_56-AddedFilterForDirSizeCalculation.2.patch
| File 19879_WordPress_56-AddedFilterForDirSizeCalculation.2.patch, 7.2 KB (added by , 5 years ago) |
|---|
-
wp-admin/includes/file.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
928 928 $url = $uploads['url'] . "/$filename"; 929 929 930 930 if ( is_multisite() ) { 931 delete_transient( 'dirsize_cache');931 clear_dirsize_cache( $new_file ); 932 932 } 933 933 934 934 /** -
wp-includes/post.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
5837 5837 $file = get_attached_file( $post_id ); 5838 5838 5839 5839 if ( is_multisite() ) { 5840 delete_transient( 'dirsize_cache');5840 clear_dirsize_cache( $file ); 5841 5841 } 5842 5842 5843 5843 /** -
wp-includes/functions.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
7525 7525 * @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout. 7526 7526 */ 7527 7527 function 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 }7537 7528 7538 7529 // Exclude individual site directories from the total when checking the main site of a network, 7539 7530 // as they are subdirectories and should not be counted. 7540 7531 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 ); 7542 7533 } else { 7543 $ dirsize[ $directory ]['size']= recurse_dirsize( $directory, null, $max_execution_time );7534 $size = recurse_dirsize( $directory, null, $max_execution_time ); 7544 7535 } 7545 7536 7546 set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS ); 7547 return $dirsize[ $directory ]['size']; 7537 return $size; 7548 7538 } 7549 7539 7550 7540 /** … … 7556 7546 * @since MU (3.0.0) 7557 7547 * @since 4.3.0 $exclude parameter added. 7558 7548 * @since 5.2.0 $max_execution_time parameter added. 7549 * @since 5.6.0 $directory_cache parameter added. 7559 7550 * 7560 7551 * @param string $directory Full path of a directory. 7561 7552 * @param string|array $exclude Optional. Full path of a subdirectory to exclude from the total, 7562 7553 * or array of paths. Expected without trailing slash(es). 7563 7554 * @param int $max_execution_time Maximum time to run before giving up. In seconds. The timeout is global 7564 7555 * and is measured from the moment WordPress started to load. 7556 * @param array $directory_cache Optional. Array of cached directory paths. 7557 * 7565 7558 * @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout. 7566 7559 */ 7567 function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null ) {7560 function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null, &$directory_cache = null ) { 7568 7561 $size = 0; 7569 7562 7570 7563 $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 } 7571 7575 7572 7576 if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) ) { 7573 7577 return false; … … 7595 7599 } 7596 7600 } 7597 7601 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 } 7611 7627 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 } 7620 7637 } 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 7621 7645 return $size; 7622 7646 } 7623 7647 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 */ 7658 function 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 */ 7687 function normalize_dirsize_cache_path( $path ) { 7688 $path = str_replace( ABSPATH, '', $path ); 7689 7690 return untrailingslashit( $path ); 7691 } 7692 7624 7693 /** 7625 7694 * Checks compatibility with the current WordPress version. 7626 7695 *