Make WordPress Core


Ignore:
Timestamp:
05/10/2024 06:57:53 PM (2 years 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/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r58065 r58130  
    20762076        }
    20772077
     2078        /**
     2079         * @ticket 61189
     2080         * @requires function imagejpeg
     2081         */
     2082        public function test_create_item_year_month_based_folders() {
     2083                update_option( 'uploads_use_yearmonth_folders', 1 );
     2084
     2085                wp_set_current_user( self::$editor_id );
     2086
     2087                $published_post = self::factory()->post->create(
     2088                        array(
     2089                                'post_status'   => 'publish',
     2090                                'post_date'     => '2017-02-14 00:00:00',
     2091                                'post_date_gmt' => '2017-02-14 00:00:00',
     2092                        )
     2093                );
     2094
     2095                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     2096                $request->set_header( 'Content-Type', 'image/jpeg' );
     2097                $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     2098                $request->set_param( 'title', 'My title is very cool' );
     2099                $request->set_param( 'caption', 'This is a better caption.' );
     2100                $request->set_param( 'description', 'Without a description, my attachment is descriptionless.' );
     2101                $request->set_param( 'alt_text', 'Alt text is stored outside post schema.' );
     2102                $request->set_param( 'post', $published_post );
     2103
     2104                $request->set_body( file_get_contents( self::$test_file ) );
     2105                $response = rest_get_server()->dispatch( $request );
     2106                $data     = $response->get_data();
     2107
     2108                update_option( 'uploads_use_yearmonth_folders', 0 );
     2109
     2110                $this->assertSame( 201, $response->get_status() );
     2111
     2112                $attachment = get_post( $data['id'] );
     2113
     2114                $this->assertSame( $attachment->post_parent, $data['post'] );
     2115                $this->assertSame( $attachment->post_parent, $published_post );
     2116                $this->assertSame( wp_get_attachment_url( $attachment->ID ), $data['source_url'] );
     2117                $this->assertStringContainsString( '2017/02', $data['source_url'] );
     2118        }
     2119
     2120
     2121        /**
     2122         * @ticket 61189
     2123         * @requires function imagejpeg
     2124         */
     2125        public function test_create_item_year_month_based_folders_page_post_type() {
     2126                update_option( 'uploads_use_yearmonth_folders', 1 );
     2127
     2128                wp_set_current_user( self::$editor_id );
     2129
     2130                $published_post = self::factory()->post->create(
     2131                        array(
     2132                                'post_type'     => 'page',
     2133                                'post_status'   => 'publish',
     2134                                'post_date'     => '2017-02-14 00:00:00',
     2135                                'post_date_gmt' => '2017-02-14 00:00:00',
     2136                        )
     2137                );
     2138
     2139                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     2140                $request->set_header( 'Content-Type', 'image/jpeg' );
     2141                $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     2142                $request->set_param( 'title', 'My title is very cool' );
     2143                $request->set_param( 'caption', 'This is a better caption.' );
     2144                $request->set_param( 'description', 'Without a description, my attachment is descriptionless.' );
     2145                $request->set_param( 'alt_text', 'Alt text is stored outside post schema.' );
     2146                $request->set_param( 'post', $published_post );
     2147
     2148                $request->set_body( file_get_contents( self::$test_file ) );
     2149                $response = rest_get_server()->dispatch( $request );
     2150                $data     = $response->get_data();
     2151
     2152                update_option( 'uploads_use_yearmonth_folders', 0 );
     2153
     2154                $time   = current_time( 'mysql' );
     2155                $y      = substr( $time, 0, 4 );
     2156                $m      = substr( $time, 5, 2 );
     2157                $subdir = "/$y/$m";
     2158
     2159                $this->assertSame( 201, $response->get_status() );
     2160
     2161                $attachment = get_post( $data['id'] );
     2162
     2163                $this->assertSame( $attachment->post_parent, $data['post'] );
     2164                $this->assertSame( $attachment->post_parent, $published_post );
     2165                $this->assertSame( wp_get_attachment_url( $attachment->ID ), $data['source_url'] );
     2166                $this->assertStringNotContainsString( '2017/02', $data['source_url'] );
     2167                $this->assertStringContainsString( $subdir, $data['source_url'] );
     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.