Make WordPress Core


Ignore:
Timestamp:
03/10/2022 01:08:19 PM (2 years ago)
Author:
spacedmonkey
Message:

Media: Store attachment’s file size in metadata.

Store the file size of all newly uploaded attachments, as part of the metadata stored in post meta. Storing file size means, developers will not have to resort to doing filesize function calls, that can be time consuming on assets on offloaded to services like Amazon’s S3.

This change also introduces a new helper function called, wp_filesize. This is a wrapper around the filesize php function, that adds some helpful filters and ensures the return value is an integer.

Props Cybr, Spacedmonkey, SergeyBiryukov, johnwatkins0, swissspidy, desrosj, joemcgill, azaozz, antpb, adamsilverstein, uday17035.
Fixes #49412.

File:
1 edited

Legend:

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

    r52734 r52837  
    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 */
     2569function 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}
Note: See TracChangeset for help on using the changeset viewer.