diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index 759b0974bb..ddd2be849a 100644 --- a/src/wp-admin/includes/file.php +++ b/src/wp-admin/includes/file.php @@ -2555,41 +2555,3 @@ function wp_opcache_invalidate( $filepath, $force = false ) { return false; } - -/** - * Wrapper for PHP filesize with filters and casting the result as an integer. - * - * @since 6.0.0 - * - * @link https://www.php.net/manual/en/function.filesize.php - * - * @param string $path Path to the file. - * @return int The size of the file in bytes, or 0 in the event of an error. - */ -function wp_filesize( $path ) { - /** - * Filters the result of wp_filesize before the PHP function is run. - * - * @since 6.0.0 - * - * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call. - * @param string $path Path to the file. - */ - $size = apply_filters( 'pre_wp_filesize', null, $path ); - - if ( is_int( $size ) ) { - return $size; - } - - $size = (int) @filesize( $path ); - - /** - * Filters the size of the file. - * - * @since 6.0.0 - * - * @param int $size The result of PHP filesize on the file. - * @param string $path Path to the file. - */ - return (int) apply_filters( 'wp_filesize', $size, $path ); -} diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index a2c1650c49..a93285c5fc 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3458,6 +3458,44 @@ function wp_get_ext_types() { ); } +/** + * Wrapper for PHP filesize with filters and casting the result as an integer. + * + * @since 6.0.0 + * + * @link https://www.php.net/manual/en/function.filesize.php + * + * @param string $path Path to the file. + * @return int The size of the file in bytes, or 0 in the event of an error. + */ +function wp_filesize( $path ) { + /** + * Filters the result of wp_filesize before the PHP function is run. + * + * @since 6.0.0 + * + * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call. + * @param string $path Path to the file. + */ + $size = apply_filters( 'pre_wp_filesize', null, $path ); + + if ( is_int( $size ) ) { + return $size; + } + + $size = (int) @filesize( $path ); + + /** + * Filters the size of the file. + * + * @since 6.0.0 + * + * @param int $size The result of PHP filesize on the file. + * @param string $path Path to the file. + */ + return (int) apply_filters( 'wp_filesize', $size, $path ); +} + /** * Retrieve list of allowed mime types and file extensions. * diff --git a/tests/phpunit/tests/file.php b/tests/phpunit/tests/file.php index cf3ec2d356..311ba30339 100644 --- a/tests/phpunit/tests/file.php +++ b/tests/phpunit/tests/file.php @@ -262,39 +262,4 @@ class Tests_File extends WP_UnitTestCase { return $keys; } - - /** - * @ticket 49412 - * @covers ::wp_filesize - */ - function test_wp_filesize_with_nonexistent_file() { - $file = 'nonexistent/file.jpg'; - $this->assertEquals( 0, wp_filesize( $file ) ); - } - - /** - * @ticket 49412 - * @covers ::wp_filesize - */ - function test_wp_filesize() { - $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg'; - - $this->assertEquals( filesize( $file ), wp_filesize( $file ) ); - - $filter = function() { - return 999; - }; - - add_filter( 'wp_filesize', $filter ); - - $this->assertEquals( 999, wp_filesize( $file ) ); - - $pre_filter = function() { - return 111; - }; - - add_filter( 'pre_wp_filesize', $pre_filter ); - - $this->assertEquals( 111, wp_filesize( $file ) ); - } } diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 96dee380ec..294a642f86 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -2106,4 +2106,39 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertFalse( wp_get_default_extension_for_mime_type( 123 ), 'false not returned when int as mime type supplied' ); $this->assertFalse( wp_get_default_extension_for_mime_type( null ), 'false not returned when null as mime type supplied' ); } + + /** + * @ticket 49412 + * @covers ::wp_filesize + */ + function test_wp_filesize_with_nonexistent_file() { + $file = 'nonexistent/file.jpg'; + $this->assertEquals( 0, wp_filesize( $file ) ); + } + + /** + * @ticket 49412 + * @covers ::wp_filesize + */ + function test_wp_filesize() { + $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg'; + + $this->assertEquals( filesize( $file ), wp_filesize( $file ) ); + + $filter = function() { + return 999; + }; + + add_filter( 'wp_filesize', $filter ); + + $this->assertEquals( 999, wp_filesize( $file ) ); + + $pre_filter = function() { + return 111; + }; + + add_filter( 'pre_wp_filesize', $pre_filter ); + + $this->assertEquals( 111, wp_filesize( $file ) ); + } }