Merge pull request #16380 from woocommerce/fix/16316

Fix filename image handling
This commit is contained in:
Claudio Sanches 2017-08-08 12:11:17 -03:00 committed by GitHub
commit d8af7cdeec
2 changed files with 20 additions and 8 deletions

View File

@ -504,8 +504,8 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
$base_url = $upload_dir['baseurl'] . '/'; $base_url = $upload_dir['baseurl'] . '/';
// Check first if attachment is on WordPress uploads directory. // Check first if attachment is on WordPress uploads directory.
if ( false !== strpos( $url, $base_url ) ) { if ( false === strpos( $url, $base_url ) ) {
// Search for yyyy/mm/slug.extension // Search for yyyy/mm/slug.extension.
$file = str_replace( $base_url, '', $url ); $file = str_replace( $base_url, '', $url );
$args = array( $args = array(
'post_type' => 'attachment', 'post_type' => 'attachment',
@ -515,11 +515,10 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
array( array(
'value' => $file, 'value' => $file,
'compare' => 'LIKE', 'compare' => 'LIKE',
'key' => '_wp_attachment_metadata', 'key' => '_wp_attached_file',
), ),
), ),
); );
if ( $ids = get_posts( $args ) ) { if ( $ids = get_posts( $args ) ) {
$id = current( $ids ); $id = current( $ids );
} }
@ -535,14 +534,13 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
), ),
), ),
); );
if ( $ids = get_posts( $args ) ) { if ( $ids = get_posts( $args ) ) {
$id = current( $ids ); $id = current( $ids );
} }
} }
// Upload if attachment does not exists. // Upload if attachment does not exists.
if ( ! $id ) { if ( ! $id && stristr( $url, '://' ) ) {
$upload = wc_rest_upload_image_from_url( $url ); $upload = wc_rest_upload_image_from_url( $url );
if ( is_wp_error( $upload ) ) { if ( is_wp_error( $upload ) ) {
@ -559,6 +557,10 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
update_post_meta( $id, '_wc_attachment_source', $url ); update_post_meta( $id, '_wc_attachment_source', $url );
} }
if ( ! $id ) {
throw new Exception( sprintf( __( 'Unable to use image "%s".', 'woocommerce' ), $url ), 400 );
}
return $id; return $id;
} }

View File

@ -377,7 +377,7 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
} }
/** /**
* Parse images list from a CSV. * Parse images list from a CSV. Images can be filenames or URLs.
* *
* @param string $field Field value. * @param string $field Field value.
* @return array * @return array
@ -387,7 +387,17 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
return array(); return array();
} }
return array_map( 'esc_url_raw', $this->explode_values( $field ) ); $images = array();
foreach ( $this->explode_values( $field ) as $image ) {
if ( stristr( $image, '://' ) ) {
$images[] = esc_url_raw( $image );
} else {
$images[] = sanitize_file_name( $image );
}
}
return $images;
} }
/** /**