|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * These functions are either introduced in WP Core in latest version or |
| 4 | + * available in different versions. For simplicity for future, we can remove them |
| 5 | + * if they are supported in WP Core versions which this plugin supports. |
| 6 | + * |
| 7 | + * @package performance-lab |
| 8 | + * @since n.e.x.t |
| 9 | + */ |
| 10 | + |
| 11 | +// WP Filesize function. |
| 12 | +if ( ! function_exists( 'wp_filesize' ) ) { |
| 13 | + /** |
| 14 | + * Wrapper for PHP filesize with filters and casting the result as an integer. |
| 15 | + * |
| 16 | + * This function was introduced in WP 6.0, for backward compatibility the |
| 17 | + * function is added as backup here. |
| 18 | + * |
| 19 | + * @param string $path Path to the file. |
| 20 | + * |
| 21 | + * @return int The size of the file in bytes, or 0 in the event of an error. |
| 22 | + * @since n.e.x.t |
| 23 | + * |
| 24 | + * @link https://www.php.net/manual/en/function.filesize.php |
| 25 | + */ |
| 26 | + function wp_filesize( $path ) { |
| 27 | + /** |
| 28 | + * Filters the result of wp_filesize before the PHP function is run. |
| 29 | + * |
| 30 | + * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call. |
| 31 | + * @param string $path Path to the file. |
| 32 | + * |
| 33 | + * @since n.e.x.t |
| 34 | + */ |
| 35 | + $size = apply_filters( 'pre_wp_filesize', null, $path ); |
| 36 | + |
| 37 | + if ( is_int( $size ) ) { |
| 38 | + return $size; |
| 39 | + } |
| 40 | + |
| 41 | + $size = file_exists( $path ) ? (int) filesize( $path ) : 0; |
| 42 | + |
| 43 | + /** |
| 44 | + * Filters the size of the file. |
| 45 | + * |
| 46 | + * @param int $size The result of PHP filesize on the file. |
| 47 | + * @param string $path Path to the file. |
| 48 | + * |
| 49 | + * @since n.e.x.t |
| 50 | + */ |
| 51 | + return (int) apply_filters( 'wp_filesize', $size, $path ); |
| 52 | + } |
| 53 | +} |
0 commit comments