Media: Relocate wp_filesize() function for use in frontend and backend.

A new function `wp_filesize()` was added with [52837]. The function lived in the `wp-admin/includes/file.php` file. However, this admin specific function is not loaded into memory when hitting `media/edit` endpoint. The result was a `500` Internal Server Error. Why? The function is invoked with that endpoint, but the function does not exist in memory.

This commit relocates the new function to the `wp-includes/functions.php` file. In doing so, the function is available for both the frontend and backend.

Follow-up to [52837].

Props talldanwp, spacedmonkey, costdev, antonvlasenko.
Fixes #55367.

git-svn-id: https://develop.svn.wordpress.org/trunk@52932 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork
2022-03-14 16:30:35 +00:00
parent 3bbfc064e9
commit f6b39a9d66
4 changed files with 73 additions and 73 deletions

View File

@@ -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 );
}

View File

@@ -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.
*

View File

@@ -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 ) );
}
}

View File

@@ -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 ) );
}
}