[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-attachments-controller.php (source)

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


Generated: Tue Oct 26 01:00:02 2021 Cross-referenced by PHPXref 0.7.1