diff options
Diffstat (limited to 'wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php')
-rw-r--r-- | wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 804d22a..cbc62b4 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -254,10 +254,21 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { $files = $request->get_file_params(); $headers = $request->get_headers(); + $time = null; + + // Matches logic in media_handle_upload(). + if ( ! empty( $request['post'] ) ) { + $post = get_post( $request['post'] ); + // The post date doesn't usually matter for pages, so don't backdate this upload. + if ( $post && 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) { + $time = $post->post_date; + } + } + if ( ! empty( $files ) ) { - $file = $this->upload_from_file( $files, $headers ); + $file = $this->upload_from_file( $files, $headers, $time ); } else { - $file = $this->upload_from_data( $request->get_body(), $headers ); + $file = $this->upload_from_data( $request->get_body(), $headers, $time ); } if ( is_wp_error( $file ) ) { @@ -293,6 +304,17 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { $attachment->post_mime_type = $type; $attachment->guid = $url; + // If the title was not set, use the original filename. + if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) { + // Remove the file extension (after the last `.`) + $tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) ); + + if ( ! empty( $tmp_title ) ) { + $attachment->post_title = $tmp_title; + } + } + + // Fall back to the original approach. if ( empty( $attachment->post_title ) ) { $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); } @@ -599,12 +621,12 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { case 'crop': $size = $image_editor->get_size(); - $crop_x = round( ( $size['width'] * $args['left'] ) / 100.0 ); - $crop_y = round( ( $size['height'] * $args['top'] ) / 100.0 ); - $width = round( ( $size['width'] * $args['width'] ) / 100.0 ); - $height = round( ( $size['height'] * $args['height'] ) / 100.0 ); + $crop_x = (int) round( ( $size['width'] * $args['left'] ) / 100.0 ); + $crop_y = (int) round( ( $size['height'] * $args['top'] ) / 100.0 ); + $width = (int) round( ( $size['width'] * $args['width'] ) / 100.0 ); + $height = (int) round( ( $size['height'] * $args['height'] ) / 100.0 ); - if ( $size['width'] !== $width && $size['height'] !== $height ) { + if ( $size['width'] !== $width || $size['height'] !== $height ) { $result = $image_editor->crop( $crop_x, $crop_y, $width, $height ); if ( is_wp_error( $result ) ) { @@ -1035,12 +1057,14 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { * Handles an upload via raw POST data. * * @since 4.7.0 + * @since 6.6.0 Added the `$time` parameter. * - * @param string $data Supplied file data. - * @param array $headers HTTP headers from the request. + * @param string $data Supplied file data. + * @param array $headers HTTP headers from the request. + * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. * @return array|WP_Error Data from wp_handle_sideload(). */ - protected function upload_from_data( $data, $headers ) { + protected function upload_from_data( $data, $headers, $time = null ) { if ( empty( $data ) ) { return new WP_Error( 'rest_upload_no_data', @@ -1128,7 +1152,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 'test_form' => false, ); - $sideloaded = wp_handle_sideload( $file_data, $overrides ); + $sideloaded = wp_handle_sideload( $file_data, $overrides, $time ); if ( isset( $sideloaded['error'] ) ) { @unlink( $tmpfname ); @@ -1246,12 +1270,14 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { * Handles an upload via multipart/form-data ($_FILES). * * @since 4.7.0 + * @since 6.6.0 Added the `$time` parameter. * - * @param array $files Data from the `$_FILES` superglobal. - * @param array $headers HTTP headers from the request. + * @param array $files Data from the `$_FILES` superglobal. + * @param array $headers HTTP headers from the request. + * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. * @return array|WP_Error Data from wp_handle_upload(). */ - protected function upload_from_file( $files, $headers ) { + protected function upload_from_file( $files, $headers, $time = null ) { if ( empty( $files ) ) { return new WP_Error( 'rest_upload_no_data', @@ -1293,7 +1319,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { // Include filesystem functions to get access to wp_handle_upload(). require_once ABSPATH . 'wp-admin/includes/file.php'; - $file = wp_handle_upload( $files['file'], $overrides ); + $file = wp_handle_upload( $files['file'], $overrides, $time ); if ( isset( $file['error'] ) ) { return new WP_Error( |