PluginProbe
Extendify / 3.1.4
Extendify v3.1.4
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.1.4, at app/AutoLaunch/Services/WooCommerceImporter.php

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