| @@ -10,8 +10,9 @@ | ||
| 10 | 10 | |
| 11 | 11 | use Extendify\Config; |
| 12 | 12 | use Extendify\Constants; |
| 13 | 13 | use Extendify\PartnerData; |
| 14 | +use Extendify\Shared\Services\Import\ImageUploader; | |
| 14 | 15 | use Extendify\Shared\Services\Sanitizer; |
| 15 | 16 | |
| 16 | 17 | /** |
| 17 | 18 | * WooCommerceImporter class. |
| @@ -130,8 +131,10 @@ | ||
| 130 | 131 | if (!count($images)) { |
| 131 | 132 | return; |
| 132 | 133 | } |
| 133 | 134 | |
| 135 | + $productGalleryImages = []; | |
| 136 | + | |
| 134 | 137 | foreach ($images as $index => $imageUrl) { |
| 135 | 138 | $imageId = $this->uploadImage($imageUrl); |
| 136 | 139 | if (!is_wp_error($imageId)) { |
| 137 | 140 | $productGalleryImages[] = $imageId; |
| @@ -149,17 +152,14 @@ | ||
| 149 | 152 | /** |
| 150 | 153 | * Uploads an image from URL to WordPress media library. |
| 151 | 154 | * |
| 152 | 155 | * @param string $url Image URL to upload. |
| 153 | - * @return int|false Attachment ID on success, false on failure. | |
| 156 | + * @return int|\WP_Error Attachment ID on success, WP_Error on failure. | |
| 154 | 157 | */ |
| 155 | 158 | public function uploadImage(string $url) |
| 156 | 159 | { |
| 157 | - if (! function_exists('\media_sideload_image')) { | |
| 158 | - require_once ABSPATH . 'wp-admin/includes/media.php'; | |
| 159 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 160 | - require_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 161 | - } | |
| 160 | + // media_sideload_image rejects our image urls, which carry no file extension. | |
| 161 | + $uploaded = (new ImageUploader())->uploadImage($url); | |
| 162 | 162 | |
| 163 | - return \media_sideload_image($url, 0, null, 'id'); | |
| 163 | + return is_wp_error($uploaded) ? $uploaded : $uploaded['attachment_id']; | |
| 164 | 164 | } |
| 165 | 165 | } |