ImageAttachment utility

This commit is contained in:
Mike Jolley 2019-06-17 15:23:55 +01:00
parent b6dc7b745f
commit ab42dc41b3
3 changed files with 114 additions and 57 deletions

View File

@ -9,6 +9,8 @@ namespace WooCommerce\RestApi\Controllers\Version4\Schema;
defined( 'ABSPATH' ) || exit;
use \WooCommerce\RestApi\Utilities\ImageAttachment;
/**
* ProductRequest class.
*/
@ -300,45 +302,24 @@ class ProductRequest extends AbstractRequest {
foreach ( $images as $index => $image ) {
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;
$attachment = new ImageAttachment( $attachment_id, $object->get_id() );
if ( 0 === $attachment_id && isset( $image['src'] ) ) {
$upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) );
if ( is_wp_error( $upload ) ) {
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $object->get_id(), $images ) ) {
throw new \WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 );
} else {
continue;
}
}
$attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $object->get_id() );
if ( 0 === $attachment->id && ! empty( $image['src'] ) ) {
$attachment->upload_image_from_src( $image['src'] );
}
if ( ! wp_attachment_is_image( $attachment_id ) ) {
/* translators: %s: image ID */
throw new \WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 );
if ( ! empty( $image['alt'] ) ) {
$attachment->update_alt_text( $image['alt'] );
}
if ( ! empty( $image['name'] ) ) {
$attachment->update_name( $image['name'] );
}
if ( 0 === $index ) {
$response['image_id'] = $attachment_id;
$response['image_id'] = $attachment->id;
} else {
$response['gallery_image_ids'][] = $attachment_id;
}
// Set the image alt if present.
if ( ! empty( $image['alt'] ) ) {
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) );
}
// Set the image name if present.
if ( ! empty( $image['name'] ) ) {
wp_update_post(
array(
'ID' => $attachment_id,
'post_title' => $image['name'],
)
);
$response['gallery_image_ids'][] = $attachment->id;
}
}

View File

@ -9,6 +9,8 @@ namespace WooCommerce\RestApi\Controllers\Version4\Schema;
defined( 'ABSPATH' ) || exit;
use \WooCommerce\RestApi\Utilities\ImageAttachment;
/**
* ProductVariationRequest class.
*/
@ -167,39 +169,20 @@ class ProductVariationRequest extends ProductRequest {
}
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0;
$attachment = new ImageAttachment( $attachment_id, $object->get_id() );
if ( 0 === $attachment_id && isset( $image['src'] ) ) {
$upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) );
if ( is_wp_error( $upload ) ) {
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $object->get_id(), array( $image ) ) ) {
throw new \WC_REST_Exception( 'woocommerce_variation_image_upload_error', $upload->get_error_message(), 400 );
}
}
$attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $object->get_id() );
if ( 0 === $attachment->id && ! empty( $image['src'] ) ) {
$attachment->upload_image_from_src( $image['src'] );
}
if ( ! wp_attachment_is_image( $attachment_id ) ) {
/* translators: %s: attachment ID */
throw new \WC_REST_Exception( 'woocommerce_variation_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 );
}
// Set the image alt if present.
if ( ! empty( $image['alt'] ) ) {
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) );
$attachment->update_alt_text( $image['alt'] );
}
// Set the image name if present.
if ( ! empty( $image['name'] ) ) {
wp_update_post(
array(
'ID' => $attachment_id,
'post_title' => $image['name'],
)
);
$attachment->update_name( $image['name'] );
}
return $attachment_id;
return $attachment->id;
}
}

View File

@ -0,0 +1,93 @@
<?php
/**
* Helper to upload files via the REST API.
*
* @package WooCommerce/Utilities
*/
namespace WooCommerce\RestApi\Utilities;
/**
* ImageAttachment class.
*/
class ImageAttachment {
/**
* Attachment ID.
*
* @var integer
*/
public $id = 0;
/**
* Object attached to.
*
* @var integer
*/
public $object_id = 0;
/**
* Constructor.
*
* @param integer $id Attachment ID.
* @param integer $object_id Object ID.
*/
public function __construct( $id = 0, $object_id = 0 ) {
$this->id = (int) $id;
$this->object_id = (int) $object_id;
}
/**
* Upload an attachment file.
*
* @throws \WC_REST_Exception REST API exceptions.
* @param string $src URL to file.
*/
public function upload_image_from_src( $src ) {
$upload = wc_rest_upload_image_from_url( esc_url_raw( $src ) );
if ( is_wp_error( $upload ) ) {
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $object->get_id(), $images ) ) {
throw new \WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 );
} else {
return;
}
}
$this->id = wc_rest_set_uploaded_image_as_attachment( $upload, $this->object_id );
if ( ! wp_attachment_is_image( $this->id ) ) {
/* translators: %s: image ID */
throw new \WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $this->attachment_id ), 400 );
}
}
/**
* Update attachment alt text.
*
* @param string $text Text to set.
*/
public function update_alt_text( $text ) {
if ( ! $this->id ) {
return;
}
update_post_meta( $this->id, '_wp_attachment_image_alt', wc_clean( $text ) );
}
/**
* Update attachment name.
*
* @param string $text Text to set.
*/
public function update_name( $text ) {
if ( ! $this->id ) {
return;
}
wp_update_post(
array(
'ID' => $this->id,
'post_title' => $text,
)
);
}
}