| @@ -8,9 +8,11 @@ | ||
| 8 | 8 | |
| 9 | 9 | defined('ABSPATH') || die('No direct access.'); |
| 10 | 10 | |
| 11 | 11 | use Extendify\Config; |
| 12 | +use Extendify\Constants; | |
| 12 | 13 | use Extendify\PartnerData; |
| 14 | +use Extendify\Shared\Services\Import\ImageUploader; | |
| 13 | 15 | use Extendify\Shared\Services\Sanitizer; |
| 14 | 16 | |
| 15 | 17 | /** |
| 16 | 18 | * WooCommerceImporter class. |
| @@ -24,9 +26,9 @@ | ||
| 24 | 26 | * @return array|\WP_Error Array of imported product results or WP_Error on failure. |
| 25 | 27 | */ |
| 26 | 28 | public static function import() |
| 27 | 29 | { |
| 28 | - $response = wp_remote_post('https://ai.extendify.com/api/plugins/woo/content', [ | |
| 30 | + $response = wp_remote_post(Constants::AI_HOST . '/api/plugins/woo/content', [ | |
| 29 | 31 | 'headers' => [ |
| 30 | 32 | 'Content-Type' => 'application/json', |
| 31 | 33 | 'referer' => get_bloginfo('url'), |
| 32 | 34 | ], |
| @@ -129,8 +131,10 @@ | ||
| 129 | 131 | if (!count($images)) { |
| 130 | 132 | return; |
| 131 | 133 | } |
| 132 | 134 | |
| 135 | + $productGalleryImages = []; | |
| 136 | + | |
| 133 | 137 | foreach ($images as $index => $imageUrl) { |
| 134 | 138 | $imageId = $this->uploadImage($imageUrl); |
| 135 | 139 | if (!is_wp_error($imageId)) { |
| 136 | 140 | $productGalleryImages[] = $imageId; |
| @@ -148,17 +152,14 @@ | ||
| 148 | 152 | /** |
| 149 | 153 | * Uploads an image from URL to WordPress media library. |
| 150 | 154 | * |
| 151 | 155 | * @param string $url Image URL to upload. |
| 152 | - * @return int|false Attachment ID on success, false on failure. | |
| 156 | + * @return int|\WP_Error Attachment ID on success, WP_Error on failure. | |
| 153 | 157 | */ |
| 154 | 158 | public function uploadImage(string $url) |
| 155 | 159 | { |
| 156 | - if (! function_exists('\media_sideload_image')) { | |
| 157 | - require_once ABSPATH . 'wp-admin/includes/media.php'; | |
| 158 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 159 | - require_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 160 | - } | |
| 160 | + // media_sideload_image rejects our image urls, which carry no file extension. | |
| 161 | + $uploaded = (new ImageUploader())->uploadImage($url); | |
| 161 | 162 | |
| 162 | - return \media_sideload_image($url, 0, null, 'id'); | |
| 163 | + return is_wp_error($uploaded) ? $uploaded : $uploaded['attachment_id']; | |
| 163 | 164 | } |
| 164 | 165 | } |