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