diff --git a/src/Controllers/Version4/Schema/ProductRequest.php b/src/Controllers/Version4/Schema/ProductRequest.php index 95a0921ea9d..0be60ff2142 100644 --- a/src/Controllers/Version4/Schema/ProductRequest.php +++ b/src/Controllers/Version4/Schema/ProductRequest.php @@ -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; } } diff --git a/src/Controllers/Version4/Schema/ProductVariationRequest.php b/src/Controllers/Version4/Schema/ProductVariationRequest.php index 186a953b266..4f9d98fb8ab 100644 --- a/src/Controllers/Version4/Schema/ProductVariationRequest.php +++ b/src/Controllers/Version4/Schema/ProductVariationRequest.php @@ -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; } } diff --git a/src/Utilities/ImageAttachment.php b/src/Utilities/ImageAttachment.php new file mode 100644 index 00000000000..27367fa535a --- /dev/null +++ b/src/Utilities/ImageAttachment.php @@ -0,0 +1,93 @@ +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, + ) + ); + } +}