public function prepare_item_for_response( $post, $request ) {
		$response = parent::prepare_item_for_response( $post, $request );
		$data = $response->get_data();

		$data['alt_text']      = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
		$data['caption']       = $post->post_excerpt;
		$data['description']   = $post->post_content;
		$data['media_type']    = wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
		$data['mime_type']     = $post->post_mime_type;
		$data['media_details'] = wp_get_attachment_metadata( $post->ID );
		$data['post']          = ! empty( $post->post_parent ) ? (int) $post->post_parent : null;
		$data['source_url']    = wp_get_attachment_url( $post->ID );

		// Ensure empty details is an empty object.
		if ( empty( $data['media_details'] ) ) {
			$data['media_details'] = new stdClass;
		} elseif ( ! empty( $data['media_details']['sizes'] ) ) {

			foreach ( $data['media_details']['sizes'] as $size => &$size_data ) {

				if ( isset( $size_data['mime-type'] ) ) {
					$size_data['mime_type'] = $size_data['mime-type'];
					unset( $size_data['mime-type'] );
				}

				// Use the same method image_downsize() does.
				$image_src = wp_get_attachment_image_src( $post->ID, $size );
				if ( ! $image_src ) {
					continue;
				}

				$size_data['source_url'] = $image_src[0];
			}

			$full_src = wp_get_attachment_image_src( $post->ID, 'full' );
			if ( ! empty( $full_src ) ) {
				$data['media_details']['sizes']['full'] = array(
					'file'          => wp_basename( $full_src[0] ),
					'width'         => $full_src[1],
					'height'        => $full_src[2],
					'mime_type'     => $post->post_mime_type,
					'source_url'    => $full_src[0],
					);
			}
		} else {
			$data['media_details']['sizes'] = new stdClass;
		}

		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';

		$data = $this->filter_response_by_context( $data, $context );

		// Wrap the data in a response object.
		$response = rest_ensure_response( $data );

		$response->add_links( $this->prepare_links( $post ) );

		/**
		 * Filter an attachment returned from the API.
		 *
		 * Allows modification of the attachment right before it is returned.
		 *
		 * @param WP_REST_Response  $response   The response object.
		 * @param WP_Post           $post       The original attachment post.
		 * @param WP_REST_Request   $request    Request used to generate the response.
		 */
		return apply_filters( 'rest_prepare_attachment', $response, $post, $request );
	}
public function prepare_item_for_response( $post, $request ) {
		$response = parent::prepare_item_for_response( $post, $request );
		$data = $response->get_data();

		$data['alt_text']      = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
		$data['caption']       = $post->post_excerpt;
		$data['description']   = $post->post_content;
		$data['media_type']    = wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
		$data['mime_type']     = $post->post_mime_type;
		$data['media_details'] = wp_get_attachment_metadata( $post->ID );
		$data['post']          = ! empty( $post->post_parent ) ? (int) $post->post_parent : null;
		$data['source_url']    = wp_get_attachment_url( $post->ID );

		// Ensure empty details is an empty object.
		if ( empty( $data['media_details'] ) ) {
			$data['media_details'] = new stdClass;
		} elseif ( ! empty( $data['media_details']['sizes'] ) ) {

			foreach ( $data['media_details']['sizes'] as $size => &$size_data ) {

				if ( isset( $size_data['mime-type'] ) ) {
					$size_data['mime_type'] = $size_data['mime-type'];
					unset( $size_data['mime-type'] );
				}

				// Use the same method image_downsize() does.
				$image_src = wp_get_attachment_image_src( $post->ID, $size );
				if ( ! $image_src ) {
					continue;
				}

				$size_data['source_url'] = $image_src[0];
			}

			$full_src = wp_get_attachment_image_src( $post->ID, 'full' );
			if ( ! empty( $full_src ) ) {
				$data['media_details']['sizes']['full'] = array(
					'file'          => wp_basename( $full_src[0] ),
					'width'         => $full_src[1],
					'height'        => $full_src[2],
					'mime_type'     => $post->post_mime_type,
					'source_url'    => $full_src[0],
					);
			}
		} else {
			$data['media_details']['sizes'] = new stdClass;
		}

		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';

		$data = $this->filter_response_by_context( $data, $context );

		// Wrap the data in a response object.
		$response = rest_ensure_response( $data );

		$response->add_links( $this->prepare_links( $post ) );

		/**
		 * Filter an attachment returned from the API.
		 *
		 * Allows modification of the attachment right before it is returned.
		 *
		 * @param WP_REST_Response  $response   The response object.
		 * @param WP_Post           $post       The original attachment post.
		 * @param WP_REST_Request   $request    Request used to generate the response.
		 */
		return apply_filters( 'rest_prepare_attachment', $response, $post, $request );
	}
public function prepare_item_for_response ( $ post , $ request ) { $ response = parent :: prepare_item_for_response ( $ post , $ request ) ; $ data = $ response -> get_data ( ) ; $ data [ 'alt_text' ] = get_post_meta ( $ post -> ID , '_wp_attachment_image_alt' , true ) ; $ data [ 'caption' ] = $ post -> post_excerpt ; $ data [ 'description' ] = $ post -> post_content ; $ data [ 'media_type' ] = wp_attachment_is_image ( $ post -> ID ) ? 'image' : 'file' ; $ data [ 'mime_type' ] = $ post -> post_mime_type ; $ data [ 'media_details' ] = wp_get_attachment_metadata ( $ post -> ID ) ; $ data [ 'post' ] = ! empty ( $ post -> post_parent ) ? ( int ) $ post -> post_parent : null ; $ data [ 'source_url' ] = wp_get_attachment_url ( $ post -> ID ) ; // Ensure empty details is an empty object. if ( empty ( $ data [ 'media_details' ] ) ) { $ data [ 'media_details' ] = new stdClass ; } elseif ( ! empty ( $ data [ 'media_details' ] [ 'sizes' ] ) ) { foreach ( $ data [ 'media_details' ] [ 'sizes' ] as $ size => & $ size_data ) { if ( isset ( $ size_data [ 'mime-type' ] ) ) { $ size_data [ 'mime_type' ] = $ size_data [ 'mime-type' ] ; unset ( $ size_data [ 'mime-type' ] ) ; } // Use the same method image_downsize() does. $ image_src = wp_get_attachment_image_src ( $ post -> ID , $ size ) ; if ( ! $ image_src ) { continue ; } $ size_data [ 'source_url' ] = $ image_src [ 0 ] ; } $ full_src = wp_get_attachment_image_src ( $ post -> ID , 'full' ) ; if ( ! empty ( $ full_src ) ) { $ data [ 'media_details' ] [ 'sizes' ] [ 'full' ] = array ( 'file' => wp_basename ( $ full_src [ 0 ] ) , 'width' => $ full_src [ 1 ] , 'height' => $ full_src [ 2 ] , 'mime_type' => $ post -> post_mime_type , 'source_url' => $ full_src [ 0 ] , ) ; } } else { $ data [ 'media_details' ] [ 'sizes' ] = new stdClass ; } $ context = ! empty ( $ request [ 'context' ] ) ? $ request [ 'context' ] : 'view' ; $ data = $ this -> filter_response_by_context ( $ data , $ context ) ; // Wrap the data in a response object. $ response = rest_ensure_response ( $ data ) ; $ response -> add_links ( $ this -> prepare_links ( $ post ) ) ; /**
		 * Filter an attachment returned from the API.
		 *
		 * Allows modification of the attachment right before it is returned.
		 *
		 * @param WP_REST_Response  $response   The response object.
		 * @param WP_Post           $post       The original attachment post.
		 * @param WP_REST_Request   $request    Request used to generate the response.
		 */ return apply_filters ( 'rest_prepare_attachment' , $ response , $ post , $ request ) ; }
Prepare a single attachment output for response.

@param WP_Post         $post    Post object.
@param WP_REST_Request $request Request object.
@return WP_REST_Response Response object.
Prepare a single attachment output for response .