PluginProbe
Extendify / 3.0.5
Extendify v3.0.5
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 / AutoLaunch / Services / WooCommerceImporter.php

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