WP_REST_Attachments_Controller::prepare_item_for_response

Advertisement

Summery Summery

Prepares a single attachment output for response.

Syntax Syntax

WP_REST_Attachments_Controller::prepare_item_for_response( WP_Post $post, WP_REST_Request $request )

Parameters Parameters

$post

(WP_Post) (Required) Attachment object.

$request

(WP_REST_Request) (Required) Request object.

Return Return

(WP_REST_Response) Response object.

Source Source

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

			! wp_image_file_matches_image_meta( $request['src'], $image_meta, $attachment_id )
		) {
			return new WP_Error(
				'rest_unknown_attachment',
				__( 'Unable to get meta information for file.' ),
				array( 'status' => 404 )
			);
		}

		$supported_types = array( 'image/jpeg', 'image/png', 'image/gif' );
		$mime_type       = get_post_mime_type( $attachment_id );
		if ( ! in_array( $mime_type, $supported_types, true ) ) {
			return new WP_Error(
				'rest_cannot_edit_file_type',
				__( 'This type of file cannot be edited.' ),
				array( 'status' => 400 )
			);
		}

		// Check if we need to do anything.
		$rotate = 0;
		$crop   = false;

		if ( ! empty( $request['rotation'] ) ) {
			// Rotation direction: clockwise vs. counter clockwise.
			$rotate = 0 - (int) $request['rotation'];
		}

		if ( isset( $request['x'], $request['y'], $request['width'], $request['height'] ) ) {
			$crop = true;
		}

		if ( ! $rotate && ! $crop ) {
			return new WP_Error(
				'rest_image_not_edited',
				__( 'The image was not edited. Edit the image before applying the changes.' ),
				array( 'status' => 400 )
			);
		}

		/*
		 * If the file doesn't exist, attempt a URL fopen on the src link.
		 * This can occur with certain file replication plugins.
		 * Keep the original file path to get a modified name later.
		 */
		$image_file_to_edit = $image_file;
		if ( ! file_exists( $image_file_to_edit ) ) {
			$image_file_to_edit = _load_image_to_edit_path( $attachment_id );
		}

		$image_editor = wp_get_image_editor( $image_file_to_edit );

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

		if ( 0 !== $rotate ) {
			$result = $image_editor->rotate( $rotate );

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

		if ( $crop ) {
			$size = $image_editor->get_size();

			$crop_x = round( ( $size['width'] * floatval( $request['x'] ) ) / 100.0 );
			$crop_y = round( ( $size['height'] * floatval( $request['y'] ) ) / 100.0 );
			$width  = round( ( $size['width'] * floatval( $request['width'] ) ) / 100.0 );
			$height = round( ( $size['height'] * floatval( $request['height'] ) ) / 100.0 );

			$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 )
				);
			}
		}

		// 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;
		}

Advertisement

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.