Make WordPress Core


Ignore:
Timestamp:
05/10/2024 06:57:53 PM (9 months ago)
Author:
swissspidy
Message:

REST API: Ensure attachments are uploaded to the post's year/month folder.

If organizing uploads into month- and year-based folders, uploading an attachment to an existing post should store the file in wp-content/uploads/<year>/<month> based on the post's publish date. This is in line with the behavior in classic editor / the media modal.

Props swissspidy, adamsilverstein, timothyblynjacobs, skithund, sergeybiryukov, patricia70.
Fixes #61189.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r57603 r58130  
    255255        $headers = $request->get_headers();
    256256
     257        $time = null;
     258
     259        // Matches logic in media_handle_upload().
     260        if ( ! empty( $request['post'] ) ) {
     261            $post = get_post( $request['post'] );
     262            // The post date doesn't usually matter for pages, so don't backdate this upload.
     263            if ( $post && 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
     264                $time = $post->post_date;
     265            }
     266        }
     267
    257268        if ( ! empty( $files ) ) {
    258             $file = $this->upload_from_file( $files, $headers );
     269            $file = $this->upload_from_file( $files, $headers, $time );
    259270        } else {
    260             $file = $this->upload_from_data( $request->get_body(), $headers );
     271            $file = $this->upload_from_data( $request->get_body(), $headers, $time );
    261272        }
    262273
     
    10361047     *
    10371048     * @since 4.7.0
    1038      *
    1039      * @param string $data    Supplied file data.
    1040      * @param array  $headers HTTP headers from the request.
     1049     * @since 6.6.0 Added the `$time` parameter.
     1050     *
     1051     * @param string      $data    Supplied file data.
     1052     * @param array       $headers HTTP headers from the request.
     1053     * @param string|null $time    Optional. Time formatted in 'yyyy/mm'. Default null.
    10411054     * @return array|WP_Error Data from wp_handle_sideload().
    10421055     */
    1043     protected function upload_from_data( $data, $headers ) {
     1056    protected function upload_from_data( $data, $headers, $time = null ) {
    10441057        if ( empty( $data ) ) {
    10451058            return new WP_Error(
     
    11291142        );
    11301143
    1131         $sideloaded = wp_handle_sideload( $file_data, $overrides );
     1144        $sideloaded = wp_handle_sideload( $file_data, $overrides, $time );
    11321145
    11331146        if ( isset( $sideloaded['error'] ) ) {
     
    12471260     *
    12481261     * @since 4.7.0
    1249      *
    1250      * @param array $files   Data from the `$_FILES` superglobal.
    1251      * @param array $headers HTTP headers from the request.
     1262     * @since 6.6.0 Added the `$time` parameter.
     1263     *
     1264     * @param array       $files   Data from the `$_FILES` superglobal.
     1265     * @param array       $headers HTTP headers from the request.
     1266     * @param string|null $time    Optional. Time formatted in 'yyyy/mm'. Default null.
    12521267     * @return array|WP_Error Data from wp_handle_upload().
    12531268     */
    1254     protected function upload_from_file( $files, $headers ) {
     1269    protected function upload_from_file( $files, $headers, $time = null ) {
    12551270        if ( empty( $files ) ) {
    12561271            return new WP_Error(
     
    12941309        require_once ABSPATH . 'wp-admin/includes/file.php';
    12951310
    1296         $file = wp_handle_upload( $files['file'], $overrides );
     1311        $file = wp_handle_upload( $files['file'], $overrides, $time );
    12971312
    12981313        if ( isset( $file['error'] ) ) {
Note: See TracChangeset for help on using the changeset viewer.