132 'rest_cannot_edit', |
132 'rest_cannot_edit', |
133 __( 'Sorry, you are not allowed to upload media to this post.' ), |
133 __( 'Sorry, you are not allowed to upload media to this post.' ), |
134 array( 'status' => rest_authorization_required_code() ) |
134 array( 'status' => rest_authorization_required_code() ) |
135 ); |
135 ); |
136 } |
136 } |
|
137 $files = $request->get_file_params(); |
|
138 |
|
139 /** |
|
140 * Filter whether the server should prevent uploads for image types it doesn't support. Default true. |
|
141 * |
|
142 * Developers can use this filter to enable uploads of certain image types. By default image types that are not |
|
143 * supported by the server are prevented from being uploaded. |
|
144 * |
|
145 * @since 6.8.0 |
|
146 * |
|
147 * @param bool $check_mime Whether to prevent uploads of unsupported image types. |
|
148 * @param string|null $mime_type The mime type of the file being uploaded (if available). |
|
149 */ |
|
150 $prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, isset( $files['file']['type'] ) ? $files['file']['type'] : null ); |
|
151 |
|
152 // If the upload is an image, check if the server can handle the mime type. |
|
153 if ( |
|
154 $prevent_unsupported_uploads && |
|
155 isset( $files['file']['type'] ) && |
|
156 str_starts_with( $files['file']['type'], 'image/' ) |
|
157 ) { |
|
158 // List of non-resizable image formats. |
|
159 $editor_non_resizable_formats = array( |
|
160 'image/svg+xml', |
|
161 ); |
|
162 |
|
163 // Check if the image editor supports the type or ignore if it isn't a format resizable by an editor. |
|
164 if ( |
|
165 ! in_array( $files['file']['type'], $editor_non_resizable_formats, true ) && |
|
166 ! wp_image_editor_supports( array( 'mime_type' => $files['file']['type'] ) ) |
|
167 ) { |
|
168 return new WP_Error( |
|
169 'rest_upload_image_type_not_supported', |
|
170 __( 'The web server cannot generate responsive image sizes for this image. Convert it to JPEG or PNG before uploading.' ), |
|
171 array( 'status' => 400 ) |
|
172 ); |
|
173 } |
|
174 } |
137 |
175 |
138 return true; |
176 return true; |
139 } |
177 } |
140 |
178 |
141 /** |
179 /** |
337 /** |
375 /** |
338 * Fires after a single attachment is created or updated via the REST API. |
376 * Fires after a single attachment is created or updated via the REST API. |
339 * |
377 * |
340 * @since 4.7.0 |
378 * @since 4.7.0 |
341 * |
379 * |
342 * @param WP_Post $attachment Inserted or updated attachment |
380 * @param WP_Post $attachment Inserted or updated attachment object. |
343 * object. |
|
344 * @param WP_REST_Request $request The request sent to the API. |
381 * @param WP_REST_Request $request The request sent to the API. |
345 * @param bool $creating True when creating an attachment, false when updating. |
382 * @param bool $creating True when creating an attachment, false when updating. |
346 */ |
383 */ |
347 do_action( 'rest_insert_attachment', $attachment, $request, true ); |
384 do_action( 'rest_insert_attachment', $attachment, $request, true ); |
348 |
385 |
469 |
506 |
470 return $this->prepare_item_for_response( get_post( $request['id'] ), $request ); |
507 return $this->prepare_item_for_response( get_post( $request['id'] ), $request ); |
471 } |
508 } |
472 |
509 |
473 /** |
510 /** |
474 * Checks if a given request can perform post processing on an attachment. |
511 * Checks if a given request can perform post-processing on an attachment. |
475 * |
512 * |
476 * @since 5.3.0 |
513 * @since 5.3.0 |
477 * |
514 * |
478 * @param WP_REST_Request $request Full details about the request. |
515 * @param WP_REST_Request $request Full details about the request. |
479 * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
516 * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
529 __( 'Unable to get meta information for file.' ), |
566 __( 'Unable to get meta information for file.' ), |
530 array( 'status' => 404 ) |
567 array( 'status' => 404 ) |
531 ); |
568 ); |
532 } |
569 } |
533 |
570 |
534 $supported_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif' ); |
571 $supported_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/heic' ); |
535 $mime_type = get_post_mime_type( $attachment_id ); |
572 $mime_type = get_post_mime_type( $attachment_id ); |
536 if ( ! in_array( $mime_type, $supported_types, true ) ) { |
573 if ( ! in_array( $mime_type, $supported_types, true ) ) { |
537 return new WP_Error( |
574 return new WP_Error( |
538 'rest_cannot_edit_file_type', |
575 'rest_cannot_edit_file_type', |
539 __( 'This type of file cannot be edited.' ), |
576 __( 'This type of file cannot be edited.' ), |
599 |
636 |
600 foreach ( $modifiers as $modifier ) { |
637 foreach ( $modifiers as $modifier ) { |
601 $args = $modifier['args']; |
638 $args = $modifier['args']; |
602 switch ( $modifier['type'] ) { |
639 switch ( $modifier['type'] ) { |
603 case 'rotate': |
640 case 'rotate': |
604 // Rotation direction: clockwise vs. counter clockwise. |
641 // Rotation direction: clockwise vs. counterclockwise. |
605 $rotate = 0 - $args['angle']; |
642 $rotate = 0 - $args['angle']; |
606 |
643 |
607 if ( 0 !== $rotate ) { |
644 if ( 0 !== $rotate ) { |
608 $result = $image_editor->rotate( $rotate ); |
645 $result = $image_editor->rotate( $rotate ); |
609 |
646 |
659 $image_name .= '-edited'; |
696 $image_name .= '-edited'; |
660 } |
697 } |
661 |
698 |
662 $filename = "{$image_name}.{$image_ext}"; |
699 $filename = "{$image_name}.{$image_ext}"; |
663 |
700 |
664 // Create the uploads sub-directory if needed. |
701 // Create the uploads subdirectory if needed. |
665 $uploads = wp_upload_dir(); |
702 $uploads = wp_upload_dir(); |
666 |
703 |
667 // Make the file name unique in the (new) upload directory. |
704 // Make the file name unique in the (new) upload directory. |
668 $filename = wp_unique_filename( $uploads['path'], $filename ); |
705 $filename = wp_unique_filename( $uploads['path'], $filename ); |
669 |
706 |
1205 |
1242 |
1206 if ( ! str_contains( $value, ';' ) ) { |
1243 if ( ! str_contains( $value, ';' ) ) { |
1207 continue; |
1244 continue; |
1208 } |
1245 } |
1209 |
1246 |
1210 list( $type, $attr_parts ) = explode( ';', $value, 2 ); |
1247 list( , $attr_parts ) = explode( ';', $value, 2 ); |
1211 |
1248 |
1212 $attr_parts = explode( ';', $attr_parts ); |
1249 $attr_parts = explode( ';', $attr_parts ); |
1213 $attributes = array(); |
1250 $attributes = array(); |
1214 |
1251 |
1215 foreach ( $attr_parts as $part ) { |
1252 foreach ( $attr_parts as $part ) { |