PluginProbe
Extendify / 3.2.0
Extendify v3.2.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / app / Launch / Services / WooCommerceImporter.php

WooCommerceImporter.php in Extendify 3.2.0, at app/Launch/Services/WooCommerceImporter.php

165 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WooCommerce Importer class.
5 */
6
7 namespace Extendify\Launch\Services;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\Config;
12 use Extendify\Constants;
13 use Extendify\PartnerData;
14 use Extendify\Shared\Services\Import\ImageUploader;
15 use Extendify\Shared\Services\Sanitizer;
16
17 /**
18 * WooCommerceImporter class.
19 */
20
21 class WooCommerceImporter
22 {
23 /**
24 * Imports products from the fetched data.
25 *
26 * @return array|\WP_Error Array of imported product results or WP_Error on failure.
27 */
28 public static function import()
29 {
30 $response = wp_remote_post(Constants::AI_HOST . '/api/plugins/woo/content', [
31 'headers' => [
32 'Content-Type' => 'application/json',
33 'referer' => get_bloginfo('url'),
34 ],
35 'body' => wp_json_encode([
36 'wpLanguage' => get_locale(),
37 'siteProfile' => get_option('extendify_site_profile', []),
38 'siteId' => get_option('extendify_site_id'),
39 'version' => Config::$version,
40 'title' => get_bloginfo('name'),
41 'wpVersion' => get_bloginfo('version'),
42 'partnerId' => PartnerData::$id,
43 'devbuild' => constant('EXTENDIFY_DEVMODE'),
44 ]),
45 ]);
46 $data = [];
47
48 // Only update the data if the request was successful.
49 if (!is_wp_error($response)) {
50 $data = json_decode(trim(wp_remote_retrieve_body($response)), true);
51 if (json_last_error() !== JSON_ERROR_NONE) {
52 return new \WP_Error('invalid_data', 'Invalid JSON response');
53 }
54 }
55
56 // If the data is empty or invalid, return an error.
57 if (!isset($data['products']) || !is_array($data['products']) || empty($data)) {
58 return new \WP_Error('invalid_data', 'Invalid product data structure');
59 }
60
61 $results = [];
62 $instance = new self();
63
64 foreach ($data['products'] as $product) {
65 $productId = $instance->createProduct($product);
66 if ($productId) {
67 $results[] = [
68 'id' => $productId,
69 'sku' => $product['sku'],
70 'status' => 'success',
71 ];
72 }
73 }
74
75 return $results;
76 }
77
78 /**
79 * Creates a new WooCommerce product.
80 *
81 * @param array $productData Product data including name, description, status, etc.
82 * @return int|false Product ID on success, false on failure.
83 */
84 public function createProduct(array $productData)
85 {
86 $post = [
87 'post_title' => wp_strip_all_tags($productData['name']),
88 'post_content' => Sanitizer::sanitizePostContent($productData['description']),
89 'post_status' => $productData['status'] ? Sanitizer::sanitizeUnknown($productData['status']) : 'publish',
90 'post_type' => 'product',
91 'meta_input' => [
92 '_sku' => Sanitizer::sanitizeUnknown($productData['sku']),
93 '_regular_price' => Sanitizer::sanitizeUnknown($productData['price']),
94 '_price' => Sanitizer::sanitizeUnknown($productData['price']),
95 '_manage_stock' => 'yes',
96 '_stock' => Sanitizer::sanitizeUnknown($productData['stock']),
97 '_stock_status' => (int) Sanitizer::sanitizeUnknown($productData['stock']) > 0
98 ? 'instock'
99 : 'outofstock',
100 '_virtual' => 'no',
101 ],
102 ];
103 $productId = wp_insert_post($post);
104
105 if (is_wp_error($productId)) {
106 return false;
107 }
108
109 wp_set_object_terms($productId, 'simple', 'product_type');
110
111 if (!empty($productData['category'])) {
112 wp_set_object_terms($productId, $productData['category'], 'product_cat');
113 }
114
115 if (!empty($productData['images']) && is_array($productData['images'])) {
116 $this->setProductImages($productId, $productData['images']);
117 }
118
119 return $productId;
120 }
121
122 /**
123 * Sets product images from provided URLs.
124 *
125 * @param int $productId Product ID.
126 * @param array $images Array of image URLs.
127 * @return void
128 */
129 public function setProductImages(int $productId, array $images)
130 {
131 if (!count($images)) {
132 return;
133 }
134
135 $productGalleryImages = [];
136
137 foreach ($images as $index => $imageUrl) {
138 $imageId = $this->uploadImage($imageUrl);
139 if (!is_wp_error($imageId)) {
140 $productGalleryImages[] = $imageId;
141 }
142 }
143
144 if (!$productGalleryImages) {
145 return;
146 }
147
148 set_post_thumbnail($productId, $productGalleryImages[0]);
149 update_post_meta($productId, '_product_image_gallery', implode(',', array_slice($productGalleryImages, 1)));
150 }
151
152 /**
153 * Uploads an image from URL to WordPress media library.
154 *
155 * @param string $url Image URL to upload.
156 * @return int|\WP_Error Attachment ID on success, WP_Error on failure.
157 */
158 public function uploadImage(string $url)
159 {
160 $uploaded = (new ImageUploader())->uploadImage($url);
161
162 return is_wp_error($uploaded) ? $uploaded : $uploaded['attachment_id'];
163 }
164 }
165