Make WordPress Core

Changeset 52932


Ignore:
Timestamp:
03/14/2022 04:30:35 PM (2 years ago)
Author:
hellofromTonya
Message:

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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/file.php

    r52837 r52932  
    25562556    return false;
    25572557}
    2558 
    2559 /**
    2560  * Wrapper for PHP filesize with filters and casting the result as an integer.
    2561  *
    2562  * @since 6.0.0
    2563  *
    2564  * @link https://www.php.net/manual/en/function.filesize.php
    2565  *
    2566  * @param string $path Path to the file.
    2567  * @return int The size of the file in bytes, or 0 in the event of an error.
    2568  */
    2569 function wp_filesize( $path ) {
    2570     /**
    2571      * Filters the result of wp_filesize before the PHP function is run.
    2572      *
    2573      * @since 6.0.0
    2574      *
    2575      * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call.
    2576      * @param string   $path Path to the file.
    2577      */
    2578     $size = apply_filters( 'pre_wp_filesize', null, $path );
    2579 
    2580     if ( is_int( $size ) ) {
    2581         return $size;
    2582     }
    2583 
    2584     $size = (int) @filesize( $path );
    2585 
    2586     /**
    2587      * Filters the size of the file.
    2588      *
    2589      * @since 6.0.0
    2590      *
    2591      * @param int    $size The result of PHP filesize on the file.
    2592      * @param string $path Path to the file.
    2593      */
    2594     return (int) apply_filters( 'wp_filesize', $size, $path );
    2595 }
  • trunk/src/wp-includes/functions.php

    r52929 r52932  
    34603460
    34613461/**
     3462 * Wrapper for PHP filesize with filters and casting the result as an integer.
     3463 *
     3464 * @since 6.0.0
     3465 *
     3466 * @link https://www.php.net/manual/en/function.filesize.php
     3467 *
     3468 * @param string $path Path to the file.
     3469 * @return int The size of the file in bytes, or 0 in the event of an error.
     3470 */
     3471function wp_filesize( $path ) {
     3472    /**
     3473     * Filters the result of wp_filesize before the PHP function is run.
     3474     *
     3475     * @since 6.0.0
     3476     *
     3477     * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call.
     3478     * @param string   $path Path to the file.
     3479     */
     3480    $size = apply_filters( 'pre_wp_filesize', null, $path );
     3481
     3482    if ( is_int( $size ) ) {
     3483        return $size;
     3484    }
     3485
     3486    $size = (int) @filesize( $path );
     3487
     3488    /**
     3489     * Filters the size of the file.
     3490     *
     3491     * @since 6.0.0
     3492     *
     3493     * @param int    $size The result of PHP filesize on the file.
     3494     * @param string $path Path to the file.
     3495     */
     3496    return (int) apply_filters( 'wp_filesize', $size, $path );
     3497}
     3498
     3499/**
    34623500 * Retrieve list of allowed mime types and file extensions.
    34633501 *
  • trunk/tests/phpunit/tests/file.php

    r52837 r52932  
    263263        return $keys;
    264264    }
    265 
    266     /**
    267      * @ticket 49412
    268      * @covers ::wp_filesize
    269      */
    270     function test_wp_filesize_with_nonexistent_file() {
    271         $file = 'nonexistent/file.jpg';
    272         $this->assertEquals( 0, wp_filesize( $file ) );
    273     }
    274 
    275     /**
    276      * @ticket 49412
    277      * @covers ::wp_filesize
    278      */
    279     function test_wp_filesize() {
    280         $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg';
    281 
    282         $this->assertEquals( filesize( $file ), wp_filesize( $file ) );
    283 
    284         $filter = function() {
    285             return 999;
    286         };
    287 
    288         add_filter( 'wp_filesize', $filter );
    289 
    290         $this->assertEquals( 999, wp_filesize( $file ) );
    291 
    292         $pre_filter = function() {
    293             return 111;
    294         };
    295 
    296         add_filter( 'pre_wp_filesize', $pre_filter );
    297 
    298         $this->assertEquals( 111, wp_filesize( $file ) );
    299     }
    300265}
  • trunk/tests/phpunit/tests/functions.php

    r52328 r52932  
    21072107        $this->assertFalse( wp_get_default_extension_for_mime_type( null ), 'false not returned when null as mime type supplied' );
    21082108    }
     2109
     2110    /**
     2111     * @ticket 49412
     2112     * @covers ::wp_filesize
     2113     */
     2114    function test_wp_filesize_with_nonexistent_file() {
     2115        $file = 'nonexistent/file.jpg';
     2116        $this->assertEquals( 0, wp_filesize( $file ) );
     2117    }
     2118
     2119    /**
     2120     * @ticket 49412
     2121     * @covers ::wp_filesize
     2122     */
     2123    function test_wp_filesize() {
     2124        $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg';
     2125
     2126        $this->assertEquals( filesize( $file ), wp_filesize( $file ) );
     2127
     2128        $filter = function() {
     2129            return 999;
     2130        };
     2131
     2132        add_filter( 'wp_filesize', $filter );
     2133
     2134        $this->assertEquals( 999, wp_filesize( $file ) );
     2135
     2136        $pre_filter = function() {
     2137            return 111;
     2138        };
     2139
     2140        add_filter( 'pre_wp_filesize', $pre_filter );
     2141
     2142        $this->assertEquals( 111, wp_filesize( $file ) );
     2143    }
    21092144}
Note: See TracChangeset for help on using the changeset viewer.