improved the save product images methods in product API

This commit is contained in:
claudiosmweb 2014-06-24 15:22:49 -03:00
parent d7e49ea801
commit f583f94885
1 changed files with 4 additions and 7 deletions

View File

@ -533,23 +533,20 @@ class WC_API_Products extends WC_API_Resource {
* @return void|WP_Error * @return void|WP_Error
*/ */
protected function save_product_images( $images ) { protected function save_product_images( $images ) {
$gallery_ids = array();
foreach ( $images as $image ) { foreach ( $images as $image ) {
if ( isset( $image['position'] ) && isset( $image['src'] ) && $image['position'] == 0 ) { if ( isset( $image['position'] ) && isset( $image['src'] ) && $image['position'] == 0 ) {
$upload = $this->upload_product_image( wc_clean( $image['src'] ) ); $upload = $this->upload_product_image( wc_clean( $image['src'] ) );
if ( is_wp_error( $upload ) ) { if ( is_wp_error( $upload ) ) {
return new WP_Error( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), array( 'status' => 400 ) ); return new WP_Error( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), array( 'status' => 400 ) );
} }
$attachment_id = $this->get_product_image_attachment_id( $upload, $id ); $attachment_id = $this->set_product_image_as_attachment( $upload, $id );
set_post_thumbnail( $id, $attachment_id ); set_post_thumbnail( $id, $attachment_id );
} else if ( isset( $image['src'] ) ) { } else if ( isset( $image['src'] ) ) {
$upload = $this->upload_product_image( wc_clean( $image['src'] ) ); $upload = $this->upload_product_image( wc_clean( $image['src'] ) );
if ( is_wp_error( $upload ) ) { if ( is_wp_error( $upload ) ) {
return new WP_Error( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), array( 'status' => 400 ) ); return new WP_Error( 'woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), array( 'status' => 400 ) );
} }
$attachment_id = $this->get_product_image_attachment_id( $upload, $id ); $this->set_product_image_as_attachment( $upload, $id );
$gallery_ids[] = $attachment_id;
} }
} }
} }
@ -618,14 +615,14 @@ class WC_API_Products extends WC_API_Resource {
} }
/** /**
* Get product image attachment ID * Get product image as attachment
* *
* @since 2.2 * @since 2.2
* @param array $upload * @param array $upload
* @param int $post_id * @param int $post_id
* @return int * @return int
*/ */
protected function get_product_image_attachment_id( $upload, $post_id ) { protected function set_product_image_as_attachment( $upload, $post_id ) {
$info = wp_check_filetype( $upload['file'] ); $info = wp_check_filetype( $upload['file'] );
$attachment = array( $attachment = array(
'guid' => $upload['url'], 'guid' => $upload['url'],