Make WordPress Core

Changeset 58130

Timestamp:
05/10/2024 06:57:53 PM (6 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.

Location:
trunk
Files:
4 edited

Legend:

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

    r57984 r58130  
    10901090 *                               to override default variables. Default false.
    10911091 *                               See _wp_handle_upload() for accepted values.
    1092  * @param string      $time      Optional. Time formatted in 'yyyy/mm'. Default null.
     1092 * @param string $time      Optional. Time formatted in 'yyyy/mm'. Default null.
    10931093 * @return array See _wp_handle_upload() for return value.
    10941094 */
     
    11211121 *                               to override default variables. Default false.
    11221122 *                               See _wp_handle_upload() for accepted values.
    1123  * @param string      $time      Optional. Time formatted in 'yyyy/mm'. Default null.
     1123 * @param string $time      Optional. Time formatted in 'yyyy/mm'. Default null.
    11241124 * @return array See _wp_handle_upload() for return value.
    11251125 */
  • trunk/src/wp-includes/functions.php

    r57987 r58130  
    23452345 * @uses _wp_upload_dir()
    23462346 *
    2347  * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
    2348  * @param bool   $create_dir Optional. Whether to check and create the uploads directory.
    2349  *                           Default true for backward compatibility.
    2350  * @param bool   $refresh_cache Optional. Whether to refresh the cache. Default false.
     2347 * @param string Optional. Time formatted in 'yyyy/mm'. Default null.
     2348 * @param bool    Optional. Whether to check and create the uploads directory.
     2349 *                           Default true for backward compatibility.
     2350 * @param bool   $refresh_cache Optional. Whether to refresh the cache. Default false.
    23512351 * @return array {
    23522352 *     Array of information about the upload directory.
     
    24202420 * @access private
    24212421 *
    2422  * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
     2422 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
    24232423 * @return array See wp_upload_dir()
    24242424 */
     
    28722872 * @param null|string $deprecated Never used. Set to null.
    28732873 * @param string      $bits       File content
    2874  * @param string      $time       Optional. Time formatted in 'yyyy/mm'. Default null.
     2874 * @param string $time       Optional. Time formatted in 'yyyy/mm'. Default null.
    28752875 * @return array {
    28762876 *     Information about the newly-uploaded file.
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r57603 r58130  
    255255        $headers = $request->get_headers();
    256256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
    257268        if ( ! empty( $files ) ) {
    258             $file = $this->upload_from_file( $files, $headers );
     269            $file = $this->upload_from_file( $files, $headers );
    259270        } else {
    260             $file = $this->upload_from_data( $request->get_body(), $headers );
     271            $file = $this->upload_from_data( $request->get_body(), $headers );
    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 ) {
    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 );
    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 ) {
    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 );
    12971312
    12981313        if ( isset( $file['error'] ) ) {
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r58065 r58130  
    20762076    }
    20772077
     2078
     2079
     2080
     2081
     2082
     2083
     2084
     2085
     2086
     2087
     2088
     2089
     2090
     2091
     2092
     2093
     2094
     2095
     2096
     2097
     2098
     2099
     2100
     2101
     2102
     2103
     2104
     2105
     2106
     2107
     2108
     2109
     2110
     2111
     2112
     2113
     2114
     2115
     2116
     2117
     2118
     2119
     2120
     2121
     2122
     2123
     2124
     2125
     2126
     2127
     2128
     2129
     2130
     2131
     2132
     2133
     2134
     2135
     2136
     2137
     2138
     2139
     2140
     2141
     2142
     2143
     2144
     2145
     2146
     2147
     2148
     2149
     2150
     2151
     2152
     2153
     2154
     2155
     2156
     2157
     2158
     2159
     2160
     2161
     2162
     2163
     2164
     2165
     2166
     2167
     2168
     2169
    20782170    public function filter_rest_insert_attachment( $attachment ) {
    20792171        ++self::$rest_insert_attachment_count;
Note: See TracChangeset for help on using the changeset viewer.