[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API: WP_REST_Attachments_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core controller used to access attachments via the REST API. 12 * 13 * @since 4.7.0 14 * 15 * @see WP_REST_Posts_Controller 16 */ 17 class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 18 19 /** 20 * Whether the controller supports batching. 21 * 22 * @since 5.9.0 23 * @var false 24 */ 25 protected $allow_batch = false; 26 27 /** 28 * Registers the routes for attachments. 29 * 30 * @since 5.3.0 31 * 32 * @see register_rest_route() 33 */ 34 public function register_routes() { 35 parent::register_routes(); 36 register_rest_route( 37 $this->namespace, 38 '/' . $this->rest_base . '/(?P<id>[\d]+)/post-process', 39 array( 40 'methods' => WP_REST_Server::CREATABLE, 41 'callback' => array( $this, 'post_process_item' ), 42 'permission_callback' => array( $this, 'post_process_item_permissions_check' ), 43 'args' => array( 44 'id' => array( 45 'description' => __( 'Unique identifier for the attachment.' ), 46 'type' => 'integer', 47 ), 48 'action' => array( 49 'type' => 'string', 50 'enum' => array( 'create-image-subsizes' ), 51 'required' => true, 52 ), 53 ), 54 ) 55 ); 56 register_rest_route( 57 $this->namespace, 58 '/' . $this->rest_base . '/(?P<id>[\d]+)/edit', 59 array( 60 'methods' => WP_REST_Server::CREATABLE, 61 'callback' => array( $this, 'edit_media_item' ), 62 'permission_callback' => array( $this, 'edit_media_item_permissions_check' ), 63 'args' => $this->get_edit_media_item_args(), 64 ) 65 ); 66 } 67 68 /** 69 * Determines the allowed query_vars for a get_items() response and 70 * prepares for WP_Query. 71 * 72 * @since 4.7.0 73 * 74 * @param array $prepared_args Optional. Array of prepared arguments. Default empty array. 75 * @param WP_REST_Request $request Optional. Request to prepare items for. 76 * @return array Array of query arguments. 77 */ 78 protected function prepare_items_query( $prepared_args = array(), $request = null ) { 79 $query_args = parent::prepare_items_query( $prepared_args, $request ); 80 81 if ( empty( $query_args['post_status'] ) ) { 82 $query_args['post_status'] = 'inherit'; 83 } 84 85 $media_types = $this->get_media_types(); 86 87 if ( ! empty( $request['media_type'] ) && isset( $media_types[ $request['media_type'] ] ) ) { 88 $query_args['post_mime_type'] = $media_types[ $request['media_type'] ]; 89 } 90 91 if ( ! empty( $request['mime_type'] ) ) { 92 $parts = explode( '/', $request['mime_type'] ); 93 if ( isset( $media_types[ $parts[0] ] ) && in_array( $request['mime_type'], $media_types[ $parts[0] ], true ) ) { 94 $query_args['post_mime_type'] = $request['mime_type']; 95 } 96 } 97 98 // Filter query clauses to include filenames. 99 if ( isset( $query_args['s'] ) ) { 100 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 101 } 102 103 return $query_args; 104 } 105 106 /** 107 * Checks if a given request has access to create an attachment. 108 * 109 * @since 4.7.0 110 * 111 * @param WP_REST_Request $request Full details about the request. 112 * @return true|WP_Error Boolean true if the attachment may be created, or a WP_Error if not. 113 */ 114 public function create_item_permissions_check( $request ) { 115 $ret = parent::create_item_permissions_check( $request ); 116 117 if ( ! $ret || is_wp_error( $ret ) ) { 118 return $ret; 119 } 120 121 if ( ! current_user_can( 'upload_files' ) ) { 122 return new WP_Error( 123 'rest_cannot_create', 124 __( 'Sorry, you are not allowed to upload media on this site.' ), 125 array( 'status' => 400 ) 126 ); 127 } 128 129 // Attaching media to a post requires ability to edit said post. 130 if ( ! empty( $request['post'] ) && ! current_user_can( 'edit_post', (int) $request['post'] ) ) { 131 return new WP_Error( 132 'rest_cannot_edit', 133 __( 'Sorry, you are not allowed to upload media to this post.' ), 134 array( 'status' => rest_authorization_required_code() ) 135 ); 136 } 137 138 return true; 139 } 140 141 /** 142 * Creates a single attachment. 143 * 144 * @since 4.7.0 145 * 146 * @param WP_REST_Request $request Full details about the request. 147 * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. 148 */ 149 public function create_item( $request ) { 150 if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) { 151 return new WP_Error( 152 'rest_invalid_param', 153 __( 'Invalid parent type.' ), 154 array( 'status' => 400 ) 155 ); 156 } 157 158 $insert = $this->insert_attachment( $request ); 159 160 if ( is_wp_error( $insert ) ) { 161 return $insert; 162 } 163 164 $schema = $this->get_item_schema(); 165 166 // Extract by name. 167 $attachment_id = $insert['attachment_id']; 168 $file = $insert['file']; 169 170 if ( isset( $request['alt_text'] ) ) { 171 update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) ); 172 } 173 174 if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) { 175 $thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment_id ); 176 177 if ( is_wp_error( $thumbnail_update ) ) { 178 return $thumbnail_update; 179 } 180 } 181 182 if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { 183 $meta_update = $this->meta->update_value( $request['meta'], $attachment_id ); 184 185 if ( is_wp_error( $meta_update ) ) { 186 return $meta_update; 187 } 188 } 189 190 $attachment = get_post( $attachment_id ); 191 $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); 192 193 if ( is_wp_error( $fields_update ) ) { 194 return $fields_update; 195 } 196 197 $terms_update = $this->handle_terms( $attachment_id, $request ); 198 199 if ( is_wp_error( $terms_update ) ) { 200 return $terms_update; 201 } 202 203 $request->set_param( 'context', 'edit' ); 204 205 /** 206 * Fires after a single attachment is completely created or updated via the REST API. 207 * 208 * @since 5.0.0 209 * 210 * @param WP_Post $attachment Inserted or updated attachment object. 211 * @param WP_REST_Request $request Request object. 212 * @param bool $creating True when creating an attachment, false when updating. 213 */ 214 do_action( 'rest_after_insert_attachment', $attachment, $request, true ); 215 216 wp_after_insert_post( $attachment, false, null ); 217 218 if ( wp_is_serving_rest_request() ) { 219 /* 220 * Set a custom header with the attachment_id. 221 * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. 222 */ 223 header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id ); 224 } 225 226 // Include media and image functions to get access to wp_generate_attachment_metadata(). 227 require_once ABSPATH . 'wp-admin/includes/media.php'; 228 require_once ABSPATH . 'wp-admin/includes/image.php'; 229 230 /* 231 * Post-process the upload (create image sub-sizes, make PDF thumbnails, etc.) and insert attachment meta. 232 * At this point the server may run out of resources and post-processing of uploaded images may fail. 233 */ 234 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); 235 236 $response = $this->prepare_item_for_response( $attachment, $request ); 237 $response = rest_ensure_response( $response ); 238 $response->set_status( 201 ); 239 $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $attachment_id ) ) ); 240 241 return $response; 242 } 243 244 /** 245 * Inserts the attachment post in the database. Does not update the attachment meta. 246 * 247 * @since 5.3.0 248 * 249 * @param WP_REST_Request $request 250 * @return array|WP_Error 251 */ 252 protected function insert_attachment( $request ) { 253 // Get the file via $_FILES or raw data. 254 $files = $request->get_file_params(); 255 $headers = $request->get_headers(); 256 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 268 if ( ! empty( $files ) ) { 269 $file = $this->upload_from_file( $files, $headers, $time ); 270 } else { 271 $file = $this->upload_from_data( $request->get_body(), $headers, $time ); 272 } 273 274 if ( is_wp_error( $file ) ) { 275 return $file; 276 } 277 278 $name = wp_basename( $file['file'] ); 279 $name_parts = pathinfo( $name ); 280 $name = trim( substr( $name, 0, -( 1 + strlen( $name_parts['extension'] ) ) ) ); 281 282 $url = $file['url']; 283 $type = $file['type']; 284 $file = $file['file']; 285 286 // Include image functions to get access to wp_read_image_metadata(). 287 require_once ABSPATH . 'wp-admin/includes/image.php'; 288 289 // Use image exif/iptc data for title and caption defaults if possible. 290 $image_meta = wp_read_image_metadata( $file ); 291 292 if ( ! empty( $image_meta ) ) { 293 if ( empty( $request['title'] ) && trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { 294 $request['title'] = $image_meta['title']; 295 } 296 297 if ( empty( $request['caption'] ) && trim( $image_meta['caption'] ) ) { 298 $request['caption'] = $image_meta['caption']; 299 } 300 } 301 302 $attachment = $this->prepare_item_for_database( $request ); 303 304 $attachment->post_mime_type = $type; 305 $attachment->guid = $url; 306 307 // If the title was not set, use the original filename. 308 if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) { 309 // Remove the file extension (after the last `.`) 310 $tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) ); 311 312 if ( ! empty( $tmp_title ) ) { 313 $attachment->post_title = $tmp_title; 314 } 315 } 316 317 // Fall back to the original approach. 318 if ( empty( $attachment->post_title ) ) { 319 $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); 320 } 321 322 // $post_parent is inherited from $attachment['post_parent']. 323 $id = wp_insert_attachment( wp_slash( (array) $attachment ), $file, 0, true, false ); 324 325 if ( is_wp_error( $id ) ) { 326 if ( 'db_update_error' === $id->get_error_code() ) { 327 $id->add_data( array( 'status' => 500 ) ); 328 } else { 329 $id->add_data( array( 'status' => 400 ) ); 330 } 331 332 return $id; 333 } 334 335 $attachment = get_post( $id ); 336 337 /** 338 * Fires after a single attachment is created or updated via the REST API. 339 * 340 * @since 4.7.0 341 * 342 * @param WP_Post $attachment Inserted or updated attachment object. 343 * @param WP_REST_Request $request The request sent to the API. 344 * @param bool $creating True when creating an attachment, false when updating. 345 */ 346 do_action( 'rest_insert_attachment', $attachment, $request, true ); 347 348 return array( 349 'attachment_id' => $id, 350 'file' => $file, 351 ); 352 } 353 354 /** 355 * Determines the featured media based on a request param. 356 * 357 * @since 6.5.0 358 * 359 * @param int $featured_media Featured Media ID. 360 * @param int $post_id Post ID. 361 * @return bool|WP_Error Whether the post thumbnail was successfully deleted, otherwise WP_Error. 362 */ 363 protected function handle_featured_media( $featured_media, $post_id ) { 364 $post_type = get_post_type( $post_id ); 365 $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ); 366 367 // Similar check as in wp_insert_post(). 368 if ( ! $thumbnail_support && get_post_mime_type( $post_id ) ) { 369 if ( wp_attachment_is( 'audio', $post_id ) ) { 370 $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); 371 } elseif ( wp_attachment_is( 'video', $post_id ) ) { 372 $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); 373 } 374 } 375 376 if ( $thumbnail_support ) { 377 return parent::handle_featured_media( $featured_media, $post_id ); 378 } 379 380 return new WP_Error( 381 'rest_no_featured_media', 382 sprintf( 383 /* translators: %s: attachment mime type */ 384 __( 'This site does not support post thumbnails on attachments with MIME type %s.' ), 385 get_post_mime_type( $post_id ) 386 ), 387 array( 'status' => 400 ) 388 ); 389 } 390 391 /** 392 * Updates a single attachment. 393 * 394 * @since 4.7.0 395 * 396 * @param WP_REST_Request $request Full details about the request. 397 * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. 398 */ 399 public function update_item( $request ) { 400 if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) { 401 return new WP_Error( 402 'rest_invalid_param', 403 __( 'Invalid parent type.' ), 404 array( 'status' => 400 ) 405 ); 406 } 407 408 $attachment_before = get_post( $request['id'] ); 409 $response = parent::update_item( $request ); 410 411 if ( is_wp_error( $response ) ) { 412 return $response; 413 } 414 415 $response = rest_ensure_response( $response ); 416 $data = $response->get_data(); 417 418 if ( isset( $request['alt_text'] ) ) { 419 update_post_meta( $data['id'], '_wp_attachment_image_alt', $request['alt_text'] ); 420 } 421 422 $attachment = get_post( $request['id'] ); 423 424 if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) { 425 $thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment->ID ); 426 427 if ( is_wp_error( $thumbnail_update ) ) { 428 return $thumbnail_update; 429 } 430 } 431 432 $fields_update = $this->update_additional_fields_for_object( $attachment, $request ); 433 434 if ( is_wp_error( $fields_update ) ) { 435 return $fields_update; 436 } 437 438 $request->set_param( 'context', 'edit' ); 439 440 /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */ 441 do_action( 'rest_after_insert_attachment', $attachment, $request, false ); 442 443 wp_after_insert_post( $attachment, true, $attachment_before ); 444 445 $response = $this->prepare_item_for_response( $attachment, $request ); 446 $response = rest_ensure_response( $response ); 447 448 return $response; 449 } 450 451 /** 452 * Performs post-processing on an attachment. 453 * 454 * @since 5.3.0 455 * 456 * @param WP_REST_Request $request Full details about the request. 457 * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. 458 */ 459 public function post_process_item( $request ) { 460 switch ( $request['action'] ) { 461 case 'create-image-subsizes': 462 require_once ABSPATH . 'wp-admin/includes/image.php'; 463 wp_update_image_subsizes( $request['id'] ); 464 break; 465 } 466 467 $request['context'] = 'edit'; 468 469 return $this->prepare_item_for_response( get_post( $request['id'] ), $request ); 470 } 471 472 /** 473 * Checks if a given request can perform post-processing on an attachment. 474 * 475 * @since 5.3.0 476 * 477 * @param WP_REST_Request $request Full details about the request. 478 * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. 479 */ 480 public function post_process_item_permissions_check( $request ) { 481 return $this->update_item_permissions_check( $request ); 482 } 483 484 /** 485 * Checks if a given request has access to editing media. 486 * 487 * @since 5.5.0 488 * 489 * @param WP_REST_Request $request Full details about the request. 490 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. 491 */ 492 public function edit_media_item_permissions_check( $request ) { 493 if ( ! current_user_can( 'upload_files' ) ) { 494 return new WP_Error( 495 'rest_cannot_edit_image', 496 __( 'Sorry, you are not allowed to upload media on this site.' ), 497 array( 'status' => rest_authorization_required_code() ) 498 ); 499 } 500 501 return $this->update_item_permissions_check( $request ); 502 } 503 504 /** 505 * Applies edits to a media item and creates a new attachment record. 506 * 507 * @since 5.5.0 508 * 509 * @param WP_REST_Request $request Full details about the request. 510 * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. 511 */ 512 public function edit_media_item( $request ) { 513 require_once ABSPATH . 'wp-admin/includes/image.php'; 514 515 $attachment_id = $request['id']; 516 517 // This also confirms the attachment is an image. 518 $image_file = wp_get_original_image_path( $attachment_id ); 519 $image_meta = wp_get_attachment_metadata( $attachment_id ); 520 521 if ( 522 ! $image_meta || 523 ! $image_file || 524 ! wp_image_file_matches_image_meta( $request['src'], $image_meta, $attachment_id ) 525 ) { 526 return new WP_Error( 527 'rest_unknown_attachment', 528 __( 'Unable to get meta information for file.' ), 529 array( 'status' => 404 ) 530 ); 531 } 532 533 $supported_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/heic' ); 534 $mime_type = get_post_mime_type( $attachment_id ); 535 if ( ! in_array( $mime_type, $supported_types, true ) ) { 536 return new WP_Error( 537 'rest_cannot_edit_file_type', 538 __( 'This type of file cannot be edited.' ), 539 array( 'status' => 400 ) 540 ); 541 } 542 543 // The `modifiers` param takes precedence over the older format. 544 if ( isset( $request['modifiers'] ) ) { 545 $modifiers = $request['modifiers']; 546 } else { 547 $modifiers = array(); 548 549 if ( ! empty( $request['rotation'] ) ) { 550 $modifiers[] = array( 551 'type' => 'rotate', 552 'args' => array( 553 'angle' => $request['rotation'], 554 ), 555 ); 556 } 557 558 if ( isset( $request['x'], $request['y'], $request['width'], $request['height'] ) ) { 559 $modifiers[] = array( 560 'type' => 'crop', 561 'args' => array( 562 'left' => $request['x'], 563 'top' => $request['y'], 564 'width' => $request['width'], 565 'height' => $request['height'], 566 ), 567 ); 568 } 569 570 if ( 0 === count( $modifiers ) ) { 571 return new WP_Error( 572 'rest_image_not_edited', 573 __( 'The image was not edited. Edit the image before applying the changes.' ), 574 array( 'status' => 400 ) 575 ); 576 } 577 } 578 579 /* 580 * If the file doesn't exist, attempt a URL fopen on the src link. 581 * This can occur with certain file replication plugins. 582 * Keep the original file path to get a modified name later. 583 */ 584 $image_file_to_edit = $image_file; 585 if ( ! file_exists( $image_file_to_edit ) ) { 586 $image_file_to_edit = _load_image_to_edit_path( $attachment_id ); 587 } 588 589 $image_editor = wp_get_image_editor( $image_file_to_edit ); 590 591 if ( is_wp_error( $image_editor ) ) { 592 return new WP_Error( 593 'rest_unknown_image_file_type', 594 __( 'Unable to edit this image.' ), 595 array( 'status' => 500 ) 596 ); 597 } 598 599 foreach ( $modifiers as $modifier ) { 600 $args = $modifier['args']; 601 switch ( $modifier['type'] ) { 602 case 'rotate': 603 // Rotation direction: clockwise vs. counterclockwise. 604 $rotate = 0 - $args['angle']; 605 606 if ( 0 !== $rotate ) { 607 $result = $image_editor->rotate( $rotate ); 608 609 if ( is_wp_error( $result ) ) { 610 return new WP_Error( 611 'rest_image_rotation_failed', 612 __( 'Unable to rotate this image.' ), 613 array( 'status' => 500 ) 614 ); 615 } 616 } 617 618 break; 619 620 case 'crop': 621 $size = $image_editor->get_size(); 622 623 $crop_x = (int) round( ( $size['width'] * $args['left'] ) / 100.0 ); 624 $crop_y = (int) round( ( $size['height'] * $args['top'] ) / 100.0 ); 625 $width = (int) round( ( $size['width'] * $args['width'] ) / 100.0 ); 626 $height = (int) round( ( $size['height'] * $args['height'] ) / 100.0 ); 627 628 if ( $size['width'] !== $width || $size['height'] !== $height ) { 629 $result = $image_editor->crop( $crop_x, $crop_y, $width, $height ); 630 631 if ( is_wp_error( $result ) ) { 632 return new WP_Error( 633 'rest_image_crop_failed', 634 __( 'Unable to crop this image.' ), 635 array( 'status' => 500 ) 636 ); 637 } 638 } 639 640 break; 641 642 } 643 } 644 645 // Calculate the file name. 646 $image_ext = pathinfo( $image_file, PATHINFO_EXTENSION ); 647 $image_name = wp_basename( $image_file, ".{$image_ext}" ); 648 649 /* 650 * Do not append multiple `-edited` to the file name. 651 * The user may be editing a previously edited image. 652 */ 653 if ( preg_match( '/-edited(-\d+)?$/', $image_name ) ) { 654 // Remove any `-1`, `-2`, etc. `wp_unique_filename()` will add the proper number. 655 $image_name = preg_replace( '/-edited(-\d+)?$/', '-edited', $image_name ); 656 } else { 657 // Append `-edited` before the extension. 658 $image_name .= '-edited'; 659 } 660 661 $filename = "{$image_name}.{$image_ext}"; 662 663 // Create the uploads subdirectory if needed. 664 $uploads = wp_upload_dir(); 665 666 // Make the file name unique in the (new) upload directory. 667 $filename = wp_unique_filename( $uploads['path'], $filename ); 668 669 // Save to disk. 670 $saved = $image_editor->save( $uploads['path'] . "/$filename" ); 671 672 if ( is_wp_error( $saved ) ) { 673 return $saved; 674 } 675 676 // Create new attachment post. 677 $new_attachment_post = array( 678 'post_mime_type' => $saved['mime-type'], 679 'guid' => $uploads['url'] . "/$filename", 680 'post_title' => $image_name, 681 'post_content' => '', 682 ); 683 684 // Copy post_content, post_excerpt, and post_title from the edited image's attachment post. 685 $attachment_post = get_post( $attachment_id ); 686 687 if ( $attachment_post ) { 688 $new_attachment_post['post_content'] = $attachment_post->post_content; 689 $new_attachment_post['post_excerpt'] = $attachment_post->post_excerpt; 690 $new_attachment_post['post_title'] = $attachment_post->post_title; 691 } 692 693 $new_attachment_id = wp_insert_attachment( wp_slash( $new_attachment_post ), $saved['path'], 0, true ); 694 695 if ( is_wp_error( $new_attachment_id ) ) { 696 if ( 'db_update_error' === $new_attachment_id->get_error_code() ) { 697 $new_attachment_id->add_data( array( 'status' => 500 ) ); 698 } else { 699 $new_attachment_id->add_data( array( 'status' => 400 ) ); 700 } 701 702 return $new_attachment_id; 703 } 704 705 // Copy the image alt text from the edited image. 706 $image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); 707 708 if ( ! empty( $image_alt ) ) { 709 // update_post_meta() expects slashed. 710 update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); 711 } 712 713 if ( wp_is_serving_rest_request() ) { 714 /* 715 * Set a custom header with the attachment_id. 716 * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. 717 */ 718 header( 'X-WP-Upload-Attachment-ID: ' . $new_attachment_id ); 719 } 720 721 // Generate image sub-sizes and meta. 722 $new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] ); 723 724 // Copy the EXIF metadata from the original attachment if not generated for the edited image. 725 if ( isset( $image_meta['image_meta'] ) && isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) { 726 // Merge but skip empty values. 727 foreach ( (array) $image_meta['image_meta'] as $key => $value ) { 728 if ( empty( $new_image_meta['image_meta'][ $key ] ) && ! empty( $value ) ) { 729 $new_image_meta['image_meta'][ $key ] = $value; 730 } 731 } 732 } 733 734 // Reset orientation. At this point the image is edited and orientation is correct. 735 if ( ! empty( $new_image_meta['image_meta']['orientation'] ) ) { 736 $new_image_meta['image_meta']['orientation'] = 1; 737 } 738 739 // The attachment_id may change if the site is exported and imported. 740 $new_image_meta['parent_image'] = array( 741 'attachment_id' => $attachment_id, 742 // Path to the originally uploaded image file relative to the uploads directory. 743 'file' => _wp_relative_upload_path( $image_file ), 744 ); 745 746 /** 747 * Filters the meta data for the new image created by editing an existing image. 748 * 749 * @since 5.5.0 750 * 751 * @param array $new_image_meta Meta data for the new image. 752 * @param int $new_attachment_id Attachment post ID for the new image. 753 * @param int $attachment_id Attachment post ID for the edited (parent) image. 754 */ 755 $new_image_meta = apply_filters( 'wp_edited_image_metadata', $new_image_meta, $new_attachment_id, $attachment_id ); 756 757 wp_update_attachment_metadata( $new_attachment_id, $new_image_meta ); 758 759 $response = $this->prepare_item_for_response( get_post( $new_attachment_id ), $request ); 760 $response->set_status( 201 ); 761 $response->header( 'Location', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $new_attachment_id ) ) ); 762 763 return $response; 764 } 765 766 /** 767 * Prepares a single attachment for create or update. 768 * 769 * @since 4.7.0 770 * 771 * @param WP_REST_Request $request Request object. 772 * @return stdClass|WP_Error Post object. 773 */ 774 protected function prepare_item_for_database( $request ) { 775 $prepared_attachment = parent::prepare_item_for_database( $request ); 776 777 // Attachment caption (post_excerpt internally). 778 if ( isset( $request['caption'] ) ) { 779 if ( is_string( $request['caption'] ) ) { 780 $prepared_attachment->post_excerpt = $request['caption']; 781 } elseif ( isset( $request['caption']['raw'] ) ) { 782 $prepared_attachment->post_excerpt = $request['caption']['raw']; 783 } 784 } 785 786 // Attachment description (post_content internally). 787 if ( isset( $request['description'] ) ) { 788 if ( is_string( $request['description'] ) ) { 789 $prepared_attachment->post_content = $request['description']; 790 } elseif ( isset( $request['description']['raw'] ) ) { 791 $prepared_attachment->post_content = $request['description']['raw']; 792 } 793 } 794 795 if ( isset( $request['post'] ) ) { 796 $prepared_attachment->post_parent = (int) $request['post']; 797 } 798 799 return $prepared_attachment; 800 } 801 802 /** 803 * Prepares a single attachment output for response. 804 * 805 * @since 4.7.0 806 * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support. 807 * 808 * @param WP_Post $item Attachment object. 809 * @param WP_REST_Request $request Request object. 810 * @return WP_REST_Response Response object. 811 */ 812 public function prepare_item_for_response( $item, $request ) { 813 // Restores the more descriptive, specific name for use within this method. 814 $post = $item; 815 816 $response = parent::prepare_item_for_response( $post, $request ); 817 $fields = $this->get_fields_for_response( $request ); 818 $data = $response->get_data(); 819 820 if ( in_array( 'description', $fields, true ) ) { 821 $data['description'] = array( 822 'raw' => $post->post_content, 823 /** This filter is documented in wp-includes/post-template.php */ 824 'rendered' => apply_filters( 'the_content', $post->post_content ), 825 ); 826 } 827 828 if ( in_array( 'caption', $fields, true ) ) { 829 /** This filter is documented in wp-includes/post-template.php */ 830 $caption = apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ); 831 832 /** This filter is documented in wp-includes/post-template.php */ 833 $caption = apply_filters( 'the_excerpt', $caption ); 834 835 $data['caption'] = array( 836 'raw' => $post->post_excerpt, 837 'rendered' => $caption, 838 ); 839 } 840 841 if ( in_array( 'alt_text', $fields, true ) ) { 842 $data['alt_text'] = get_post_meta( $post->ID, '_wp_attachment_image_alt', true ); 843 } 844 845 if ( in_array( 'media_type', $fields, true ) ) { 846 $data['media_type'] = wp_attachment_is_image( $post->ID ) ? 'image' : 'file'; 847 } 848 849 if ( in_array( 'mime_type', $fields, true ) ) { 850 $data['mime_type'] = $post->post_mime_type; 851 } 852 853 if ( in_array( 'media_details', $fields, true ) ) { 854 $data['media_details'] = wp_get_attachment_metadata( $post->ID ); 855 856 // Ensure empty details is an empty object. 857 if ( empty( $data['media_details'] ) ) { 858 $data['media_details'] = new stdClass(); 859 } elseif ( ! empty( $data['media_details']['sizes'] ) ) { 860 861 foreach ( $data['media_details']['sizes'] as $size => &$size_data ) { 862 863 if ( isset( $size_data['mime-type'] ) ) { 864 $size_data['mime_type'] = $size_data['mime-type']; 865 unset( $size_data['mime-type'] ); 866 } 867 868 // Use the same method image_downsize() does. 869 $image_src = wp_get_attachment_image_src( $post->ID, $size ); 870 if ( ! $image_src ) { 871 continue; 872 } 873 874 $size_data['source_url'] = $image_src[0]; 875 } 876 877 $full_src = wp_get_attachment_image_src( $post->ID, 'full' ); 878 879 if ( ! empty( $full_src ) ) { 880 $data['media_details']['sizes']['full'] = array( 881 'file' => wp_basename( $full_src[0] ), 882 'width' => $full_src[1], 883 'height' => $full_src[2], 884 'mime_type' => $post->post_mime_type, 885 'source_url' => $full_src[0], 886 ); 887 } 888 } else { 889 $data['media_details']['sizes'] = new stdClass(); 890 } 891 } 892 893 if ( in_array( 'post', $fields, true ) ) { 894 $data['post'] = ! empty( $post->post_parent ) ? (int) $post->post_parent : null; 895 } 896 897 if ( in_array( 'source_url', $fields, true ) ) { 898 $data['source_url'] = wp_get_attachment_url( $post->ID ); 899 } 900 901 if ( in_array( 'missing_image_sizes', $fields, true ) ) { 902 require_once ABSPATH . 'wp-admin/includes/image.php'; 903 $data['missing_image_sizes'] = array_keys( wp_get_missing_image_subsizes( $post->ID ) ); 904 } 905 906 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 907 908 $data = $this->filter_response_by_context( $data, $context ); 909 910 $links = $response->get_links(); 911 912 // Wrap the data in a response object. 913 $response = rest_ensure_response( $data ); 914 915 foreach ( $links as $rel => $rel_links ) { 916 foreach ( $rel_links as $link ) { 917 $response->add_link( $rel, $link['href'], $link['attributes'] ); 918 } 919 } 920 921 /** 922 * Filters an attachment returned from the REST API. 923 * 924 * Allows modification of the attachment right before it is returned. 925 * 926 * @since 4.7.0 927 * 928 * @param WP_REST_Response $response The response object. 929 * @param WP_Post $post The original attachment post. 930 * @param WP_REST_Request $request Request used to generate the response. 931 */ 932 return apply_filters( 'rest_prepare_attachment', $response, $post, $request ); 933 } 934 935 /** 936 * Retrieves the attachment's schema, conforming to JSON Schema. 937 * 938 * @since 4.7.0 939 * 940 * @return array Item schema as an array. 941 */ 942 public function get_item_schema() { 943 if ( $this->schema ) { 944 return $this->add_additional_fields_schema( $this->schema ); 945 } 946 947 $schema = parent::get_item_schema(); 948 949 $schema['properties']['alt_text'] = array( 950 'description' => __( 'Alternative text to display when attachment is not displayed.' ), 951 'type' => 'string', 952 'context' => array( 'view', 'edit', 'embed' ), 953 'arg_options' => array( 954 'sanitize_callback' => 'sanitize_text_field', 955 ), 956 ); 957 958 $schema['properties']['caption'] = array( 959 'description' => __( 'The attachment caption.' ), 960 'type' => 'object', 961 'context' => array( 'view', 'edit', 'embed' ), 962 'arg_options' => array( 963 'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database(). 964 'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database(). 965 ), 966 'properties' => array( 967 'raw' => array( 968 'description' => __( 'Caption for the attachment, as it exists in the database.' ), 969 'type' => 'string', 970 'context' => array( 'edit' ), 971 ), 972 'rendered' => array( 973 'description' => __( 'HTML caption for the attachment, transformed for display.' ), 974 'type' => 'string', 975 'context' => array( 'view', 'edit', 'embed' ), 976 'readonly' => true, 977 ), 978 ), 979 ); 980 981 $schema['properties']['description'] = array( 982 'description' => __( 'The attachment description.' ), 983 'type' => 'object', 984 'context' => array( 'view', 'edit' ), 985 'arg_options' => array( 986 'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database(). 987 'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database(). 988 ), 989 'properties' => array( 990 'raw' => array( 991 'description' => __( 'Description for the attachment, as it exists in the database.' ), 992 'type' => 'string', 993 'context' => array( 'edit' ), 994 ), 995 'rendered' => array( 996 'description' => __( 'HTML description for the attachment, transformed for display.' ), 997 'type' => 'string', 998 'context' => array( 'view', 'edit' ), 999 'readonly' => true, 1000 ), 1001 ), 1002 ); 1003 1004 $schema['properties']['media_type'] = array( 1005 'description' => __( 'Attachment type.' ), 1006 'type' => 'string', 1007 'enum' => array( 'image', 'file' ), 1008 'context' => array( 'view', 'edit', 'embed' ), 1009 'readonly' => true, 1010 ); 1011 1012 $schema['properties']['mime_type'] = array( 1013 'description' => __( 'The attachment MIME type.' ), 1014 'type' => 'string', 1015 'context' => array( 'view', 'edit', 'embed' ), 1016 'readonly' => true, 1017 ); 1018 1019 $schema['properties']['media_details'] = array( 1020 'description' => __( 'Details about the media file, specific to its type.' ), 1021 'type' => 'object', 1022 'context' => array( 'view', 'edit', 'embed' ), 1023 'readonly' => true, 1024 ); 1025 1026 $schema['properties']['post'] = array( 1027 'description' => __( 'The ID for the associated post of the attachment.' ), 1028 'type' => 'integer', 1029 'context' => array( 'view', 'edit' ), 1030 ); 1031 1032 $schema['properties']['source_url'] = array( 1033 'description' => __( 'URL to the original attachment file.' ), 1034 'type' => 'string', 1035 'format' => 'uri', 1036 'context' => array( 'view', 'edit', 'embed' ), 1037 'readonly' => true, 1038 ); 1039 1040 $schema['properties']['missing_image_sizes'] = array( 1041 'description' => __( 'List of the missing image sizes of the attachment.' ), 1042 'type' => 'array', 1043 'items' => array( 'type' => 'string' ), 1044 'context' => array( 'edit' ), 1045 'readonly' => true, 1046 ); 1047 1048 unset( $schema['properties']['password'] ); 1049 1050 $this->schema = $schema; 1051 1052 return $this->add_additional_fields_schema( $this->schema ); 1053 } 1054 1055 /** 1056 * Handles an upload via raw POST data. 1057 * 1058 * @since 4.7.0 1059 * @since 6.6.0 Added the `$time` parameter. 1060 * 1061 * @param string $data Supplied file data. 1062 * @param array $headers HTTP headers from the request. 1063 * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. 1064 * @return array|WP_Error Data from wp_handle_sideload(). 1065 */ 1066 protected function upload_from_data( $data, $headers, $time = null ) { 1067 if ( empty( $data ) ) { 1068 return new WP_Error( 1069 'rest_upload_no_data', 1070 __( 'No data supplied.' ), 1071 array( 'status' => 400 ) 1072 ); 1073 } 1074 1075 if ( empty( $headers['content_type'] ) ) { 1076 return new WP_Error( 1077 'rest_upload_no_content_type', 1078 __( 'No Content-Type supplied.' ), 1079 array( 'status' => 400 ) 1080 ); 1081 } 1082 1083 if ( empty( $headers['content_disposition'] ) ) { 1084 return new WP_Error( 1085 'rest_upload_no_content_disposition', 1086 __( 'No Content-Disposition supplied.' ), 1087 array( 'status' => 400 ) 1088 ); 1089 } 1090 1091 $filename = self::get_filename_from_disposition( $headers['content_disposition'] ); 1092 1093 if ( empty( $filename ) ) { 1094 return new WP_Error( 1095 'rest_upload_invalid_disposition', 1096 __( 'Invalid Content-Disposition supplied. Content-Disposition needs to be formatted as `attachment; filename="image.png"` or similar.' ), 1097 array( 'status' => 400 ) 1098 ); 1099 } 1100 1101 if ( ! empty( $headers['content_md5'] ) ) { 1102 $content_md5 = array_shift( $headers['content_md5'] ); 1103 $expected = trim( $content_md5 ); 1104 $actual = md5( $data ); 1105 1106 if ( $expected !== $actual ) { 1107 return new WP_Error( 1108 'rest_upload_hash_mismatch', 1109 __( 'Content hash did not match expected.' ), 1110 array( 'status' => 412 ) 1111 ); 1112 } 1113 } 1114 1115 // Get the content-type. 1116 $type = array_shift( $headers['content_type'] ); 1117 1118 // Include filesystem functions to get access to wp_tempnam() and wp_handle_sideload(). 1119 require_once ABSPATH . 'wp-admin/includes/file.php'; 1120 1121 // Save the file. 1122 $tmpfname = wp_tempnam( $filename ); 1123 1124 $fp = fopen( $tmpfname, 'w+' ); 1125 1126 if ( ! $fp ) { 1127 return new WP_Error( 1128 'rest_upload_file_error', 1129 __( 'Could not open file handle.' ), 1130 array( 'status' => 500 ) 1131 ); 1132 } 1133 1134 fwrite( $fp, $data ); 1135 fclose( $fp ); 1136 1137 // Now, sideload it in. 1138 $file_data = array( 1139 'error' => null, 1140 'tmp_name' => $tmpfname, 1141 'name' => $filename, 1142 'type' => $type, 1143 ); 1144 1145 $size_check = self::check_upload_size( $file_data ); 1146 if ( is_wp_error( $size_check ) ) { 1147 return $size_check; 1148 } 1149 1150 $overrides = array( 1151 'test_form' => false, 1152 ); 1153 1154 $sideloaded = wp_handle_sideload( $file_data, $overrides, $time ); 1155 1156 if ( isset( $sideloaded['error'] ) ) { 1157 @unlink( $tmpfname ); 1158 1159 return new WP_Error( 1160 'rest_upload_sideload_error', 1161 $sideloaded['error'], 1162 array( 'status' => 500 ) 1163 ); 1164 } 1165 1166 return $sideloaded; 1167 } 1168 1169 /** 1170 * Parses filename from a Content-Disposition header value. 1171 * 1172 * As per RFC6266: 1173 * 1174 * content-disposition = "Content-Disposition" ":" 1175 * disposition-type *( ";" disposition-parm ) 1176 * 1177 * disposition-type = "inline" | "attachment" | disp-ext-type 1178 * ; case-insensitive 1179 * disp-ext-type = token 1180 * 1181 * disposition-parm = filename-parm | disp-ext-parm 1182 * 1183 * filename-parm = "filename" "=" value 1184 * | "filename*" "=" ext-value 1185 * 1186 * disp-ext-parm = token "=" value 1187 * | ext-token "=" ext-value 1188 * ext-token = <the characters in token, followed by "*"> 1189 * 1190 * @since 4.7.0 1191 * 1192 * @link https://tools.ietf.org/html/rfc2388 1193 * @link https://tools.ietf.org/html/rfc6266 1194 * 1195 * @param string[] $disposition_header List of Content-Disposition header values. 1196 * @return string|null Filename if available, or null if not found. 1197 */ 1198 public static function get_filename_from_disposition( $disposition_header ) { 1199 // Get the filename. 1200 $filename = null; 1201 1202 foreach ( $disposition_header as $value ) { 1203 $value = trim( $value ); 1204 1205 if ( ! str_contains( $value, ';' ) ) { 1206 continue; 1207 } 1208 1209 list( , $attr_parts ) = explode( ';', $value, 2 ); 1210 1211 $attr_parts = explode( ';', $attr_parts ); 1212 $attributes = array(); 1213 1214 foreach ( $attr_parts as $part ) { 1215 if ( ! str_contains( $part, '=' ) ) { 1216 continue; 1217 } 1218 1219 list( $key, $value ) = explode( '=', $part, 2 ); 1220 1221 $attributes[ trim( $key ) ] = trim( $value ); 1222 } 1223 1224 if ( empty( $attributes['filename'] ) ) { 1225 continue; 1226 } 1227 1228 $filename = trim( $attributes['filename'] ); 1229 1230 // Unquote quoted filename, but after trimming. 1231 if ( str_starts_with( $filename, '"' ) && str_ends_with( $filename, '"' ) ) { 1232 $filename = substr( $filename, 1, -1 ); 1233 } 1234 } 1235 1236 return $filename; 1237 } 1238 1239 /** 1240 * Retrieves the query params for collections of attachments. 1241 * 1242 * @since 4.7.0 1243 * 1244 * @return array Query parameters for the attachment collection as an array. 1245 */ 1246 public function get_collection_params() { 1247 $params = parent::get_collection_params(); 1248 $params['status']['default'] = 'inherit'; 1249 $params['status']['items']['enum'] = array( 'inherit', 'private', 'trash' ); 1250 $media_types = $this->get_media_types(); 1251 1252 $params['media_type'] = array( 1253 'default' => null, 1254 'description' => __( 'Limit result set to attachments of a particular media type.' ), 1255 'type' => 'string', 1256 'enum' => array_keys( $media_types ), 1257 ); 1258 1259 $params['mime_type'] = array( 1260 'default' => null, 1261 'description' => __( 'Limit result set to attachments of a particular MIME type.' ), 1262 'type' => 'string', 1263 ); 1264 1265 return $params; 1266 } 1267 1268 /** 1269 * Handles an upload via multipart/form-data ($_FILES). 1270 * 1271 * @since 4.7.0 1272 * @since 6.6.0 Added the `$time` parameter. 1273 * 1274 * @param array $files Data from the `$_FILES` superglobal. 1275 * @param array $headers HTTP headers from the request. 1276 * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. 1277 * @return array|WP_Error Data from wp_handle_upload(). 1278 */ 1279 protected function upload_from_file( $files, $headers, $time = null ) { 1280 if ( empty( $files ) ) { 1281 return new WP_Error( 1282 'rest_upload_no_data', 1283 __( 'No data supplied.' ), 1284 array( 'status' => 400 ) 1285 ); 1286 } 1287 1288 // Verify hash, if given. 1289 if ( ! empty( $headers['content_md5'] ) ) { 1290 $content_md5 = array_shift( $headers['content_md5'] ); 1291 $expected = trim( $content_md5 ); 1292 $actual = md5_file( $files['file']['tmp_name'] ); 1293 1294 if ( $expected !== $actual ) { 1295 return new WP_Error( 1296 'rest_upload_hash_mismatch', 1297 __( 'Content hash did not match expected.' ), 1298 array( 'status' => 412 ) 1299 ); 1300 } 1301 } 1302 1303 // Pass off to WP to handle the actual upload. 1304 $overrides = array( 1305 'test_form' => false, 1306 ); 1307 1308 // Bypasses is_uploaded_file() when running unit tests. 1309 if ( defined( 'DIR_TESTDATA' ) && DIR_TESTDATA ) { 1310 $overrides['action'] = 'wp_handle_mock_upload'; 1311 } 1312 1313 $size_check = self::check_upload_size( $files['file'] ); 1314 if ( is_wp_error( $size_check ) ) { 1315 return $size_check; 1316 } 1317 1318 // Include filesystem functions to get access to wp_handle_upload(). 1319 require_once ABSPATH . 'wp-admin/includes/file.php'; 1320 1321 $file = wp_handle_upload( $files['file'], $overrides, $time ); 1322 1323 if ( isset( $file['error'] ) ) { 1324 return new WP_Error( 1325 'rest_upload_unknown_error', 1326 $file['error'], 1327 array( 'status' => 500 ) 1328 ); 1329 } 1330 1331 return $file; 1332 } 1333 1334 /** 1335 * Retrieves the supported media types. 1336 * 1337 * Media types are considered the MIME type category. 1338 * 1339 * @since 4.7.0 1340 * 1341 * @return array Array of supported media types. 1342 */ 1343 protected function get_media_types() { 1344 $media_types = array(); 1345 1346 foreach ( get_allowed_mime_types() as $mime_type ) { 1347 $parts = explode( '/', $mime_type ); 1348 1349 if ( ! isset( $media_types[ $parts[0] ] ) ) { 1350 $media_types[ $parts[0] ] = array(); 1351 } 1352 1353 $media_types[ $parts[0] ][] = $mime_type; 1354 } 1355 1356 return $media_types; 1357 } 1358 1359 /** 1360 * Determine if uploaded file exceeds space quota on multisite. 1361 * 1362 * Replicates check_upload_size(). 1363 * 1364 * @since 4.9.8 1365 * 1366 * @param array $file $_FILES array for a given file. 1367 * @return true|WP_Error True if can upload, error for errors. 1368 */ 1369 protected function check_upload_size( $file ) { 1370 if ( ! is_multisite() ) { 1371 return true; 1372 } 1373 1374 if ( get_site_option( 'upload_space_check_disabled' ) ) { 1375 return true; 1376 } 1377 1378 $space_left = get_upload_space_available(); 1379 1380 $file_size = filesize( $file['tmp_name'] ); 1381 1382 if ( $space_left < $file_size ) { 1383 return new WP_Error( 1384 'rest_upload_limited_space', 1385 /* translators: %s: Required disk space in kilobytes. */ 1386 sprintf( __( 'Not enough space to upload. %s KB needed.' ), number_format( ( $file_size - $space_left ) / KB_IN_BYTES ) ), 1387 array( 'status' => 400 ) 1388 ); 1389 } 1390 1391 if ( $file_size > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) { 1392 return new WP_Error( 1393 'rest_upload_file_too_big', 1394 /* translators: %s: Maximum allowed file size in kilobytes. */ 1395 sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ), get_site_option( 'fileupload_maxk', 1500 ) ), 1396 array( 'status' => 400 ) 1397 ); 1398 } 1399 1400 // Include multisite admin functions to get access to upload_is_user_over_quota(). 1401 require_once ABSPATH . 'wp-admin/includes/ms.php'; 1402 1403 if ( upload_is_user_over_quota( false ) ) { 1404 return new WP_Error( 1405 'rest_upload_user_quota_exceeded', 1406 __( 'You have used your space quota. Please delete files before uploading.' ), 1407 array( 'status' => 400 ) 1408 ); 1409 } 1410 1411 return true; 1412 } 1413 1414 /** 1415 * Gets the request args for the edit item route. 1416 * 1417 * @since 5.5.0 1418 * 1419 * @return array 1420 */ 1421 protected function get_edit_media_item_args() { 1422 return array( 1423 'src' => array( 1424 'description' => __( 'URL to the edited image file.' ), 1425 'type' => 'string', 1426 'format' => 'uri', 1427 'required' => true, 1428 ), 1429 'modifiers' => array( 1430 'description' => __( 'Array of image edits.' ), 1431 'type' => 'array', 1432 'minItems' => 1, 1433 'items' => array( 1434 'description' => __( 'Image edit.' ), 1435 'type' => 'object', 1436 'required' => array( 1437 'type', 1438 'args', 1439 ), 1440 'oneOf' => array( 1441 array( 1442 'title' => __( 'Rotation' ), 1443 'properties' => array( 1444 'type' => array( 1445 'description' => __( 'Rotation type.' ), 1446 'type' => 'string', 1447 'enum' => array( 'rotate' ), 1448 ), 1449 'args' => array( 1450 'description' => __( 'Rotation arguments.' ), 1451 'type' => 'object', 1452 'required' => array( 1453 'angle', 1454 ), 1455 'properties' => array( 1456 'angle' => array( 1457 'description' => __( 'Angle to rotate clockwise in degrees.' ), 1458 'type' => 'number', 1459 ), 1460 ), 1461 ), 1462 ), 1463 ), 1464 array( 1465 'title' => __( 'Crop' ), 1466 'properties' => array( 1467 'type' => array( 1468 'description' => __( 'Crop type.' ), 1469 'type' => 'string', 1470 'enum' => array( 'crop' ), 1471 ), 1472 'args' => array( 1473 'description' => __( 'Crop arguments.' ), 1474 'type' => 'object', 1475 'required' => array( 1476 'left', 1477 'top', 1478 'width', 1479 'height', 1480 ), 1481 'properties' => array( 1482 'left' => array( 1483 'description' => __( 'Horizontal position from the left to begin the crop as a percentage of the image width.' ), 1484 'type' => 'number', 1485 ), 1486 'top' => array( 1487 'description' => __( 'Vertical position from the top to begin the crop as a percentage of the image height.' ), 1488 'type' => 'number', 1489 ), 1490 'width' => array( 1491 'description' => __( 'Width of the crop as a percentage of the image width.' ), 1492 'type' => 'number', 1493 ), 1494 'height' => array( 1495 'description' => __( 'Height of the crop as a percentage of the image height.' ), 1496 'type' => 'number', 1497 ), 1498 ), 1499 ), 1500 ), 1501 ), 1502 ), 1503 ), 1504 ), 1505 'rotation' => array( 1506 'description' => __( 'The amount to rotate the image clockwise in degrees. DEPRECATED: Use `modifiers` instead.' ), 1507 'type' => 'integer', 1508 'minimum' => 0, 1509 'exclusiveMinimum' => true, 1510 'maximum' => 360, 1511 'exclusiveMaximum' => true, 1512 ), 1513 'x' => array( 1514 'description' => __( 'As a percentage of the image, the x position to start the crop from. DEPRECATED: Use `modifiers` instead.' ), 1515 'type' => 'number', 1516 'minimum' => 0, 1517 'maximum' => 100, 1518 ), 1519 'y' => array( 1520 'description' => __( 'As a percentage of the image, the y position to start the crop from. DEPRECATED: Use `modifiers` instead.' ), 1521 'type' => 'number', 1522 'minimum' => 0, 1523 'maximum' => 100, 1524 ), 1525 'width' => array( 1526 'description' => __( 'As a percentage of the image, the width to crop the image to. DEPRECATED: Use `modifiers` instead.' ), 1527 'type' => 'number', 1528 'minimum' => 0, 1529 'maximum' => 100, 1530 ), 1531 'height' => array( 1532 'description' => __( 'As a percentage of the image, the height to crop the image to. DEPRECATED: Use `modifiers` instead.' ), 1533 'type' => 'number', 1534 'minimum' => 0, 1535 'maximum' => 100, 1536 ), 1537 ); 1538 } 1539 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |