WP_REST_Attachments_Controller::edit_media_item() – Applies edits to a media item and creates a new attachment record.

You appear to be a bot. Output may be restricted

Description

Applies edits to a media item and creates a new attachment record.

Usage

$WP_REST_Response|WP_Error = WP_REST_Attachments_Controller::edit_media_item( $request );

Parameters

$request
( WP_REST_Request ) required – Full details about the request.

Returns

WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.

Source

File name: wordpress/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
Lines:

101 to 200 of 253
                __( 'Unable to rotate this image.' ),
                array( 'status' => 500 )
              );
            }
          }

          break;

        case 'crop':
          $size = $image_editor->get_size();

          $crop_x = round( ( $size['width'] * $args['left'] ) / 100.0 );
          $crop_y = round( ( $size['height'] * $args['top'] ) / 100.0 );
          $width  = round( ( $size['width'] * $args['width'] ) / 100.0 );
          $height = round( ( $size['height'] * $args['height'] ) / 100.0 );

          if ( $size['width'] !== $width && $size['height'] !== $height ) {
            $result = $image_editor->crop( $crop_x, $crop_y, $width, $height );

            if ( is_wp_error( $result ) ) {
              return new WP_Error(
                'rest_image_crop_failed',
                __( 'Unable to crop this image.' ),
                array( 'status' => 500 )
              );
            }
          }

          break;

      }
    }

    // Calculate the file name.
    $image_ext  = pathinfo( $image_file, PATHINFO_EXTENSION );
    $image_name = wp_basename( $image_file, ".{$image_ext}" );

    /*
		 * Do not append multiple `-edited` to the file name.
		 * The user may be editing a previously edited image.
		 */
    if ( preg_match( '/-edited(-\d+)?$/', $image_name ) ) {
      // Remove any `-1`, `-2`, etc. `wp_unique_filename()` will add the proper number.
      $image_name = preg_replace( '/-edited(-\d+)?$/', '-edited', $image_name );
    } else {
      // Append `-edited` before the extension.
      $image_name .= '-edited';
    }

    $filename = "{$image_name}.{$image_ext}";

    // Create the uploads sub-directory if needed.
    $uploads = wp_upload_dir();

    // Make the file name unique in the (new) upload directory.
    $filename = wp_unique_filename( $uploads['path'], $filename );

    // Save to disk.
    $saved = $image_editor->save( $uploads['path'] . "/$filename" );

    if ( is_wp_error( $saved ) ) {
      return $saved;
    }

    // Create new attachment post.
    $new_attachment_post = array(
      'post_mime_type' => $saved['mime-type'],
      'guid'           => $uploads['url'] . "/$filename",
      'post_title'     => $image_name,
      'post_content'   => '',
    );

    // Copy post_content, post_excerpt, and post_title from the edited image's attachment post.
    $attachment_post = get_post( $attachment_id );

    if ( $attachment_post ) {
      $new_attachment_post['post_content'] = $attachment_post->post_content;
      $new_attachment_post['post_excerpt'] = $attachment_post->post_excerpt;
      $new_attachment_post['post_title']   = $attachment_post->post_title;
    }

    $new_attachment_id = wp_insert_attachment( wp_slash( $new_attachment_post ), $saved['path'], 0, true );

    if ( is_wp_error( $new_attachment_id ) ) {
      if ( 'db_update_error' === $new_attachment_id->get_error_code() ) {
        $new_attachment_id->add_data( array( 'status' => 500 ) );
      } else {
        $new_attachment_id->add_data( array( 'status' => 400 ) );
      }

      return $new_attachment_id;
    }

    // Copy the image alt text from the edited image.
    $image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );

    if ( ! empty( $image_alt ) ) {
      // update_post_meta() expects slashed.
      update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
    }
 

 View on GitHub View on Trac