PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.1.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.1.1
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Integration / WooCommerce / RecordApiHelper.php

RecordApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.1.1, at includes/Core/Integration/WooCommerce/RecordApiHelper.php

315 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WooCommerce Record Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\WooCommerce;
9
10 use BitCode\BitForm\Core\Database\FormEntryLogModel;
11 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
12 use BitCode\BitForm\Core\Util\FileHandler;
13 use WC_Product_Download;
14 use WP_Error;
15
16 /**
17 * Provide functionality for Record insert,upsert
18 */
19 class RecordApiHelper
20 {
21 private $_integrationID;
22
23 private $_logID;
24
25 private $_logResponse;
26
27 public function __construct($integId, $logID)
28 {
29 $this->_integrationID = $integId;
30 $this->_logID = $logID;
31 $this->_logResponse = new UtilApiResponse();
32 }
33
34 public function executeRecordApi($formID, $entryID, $module, $fieldValues, $fieldMap, $uploadFieldMap, $required)
35 {
36 $entryDetails = [
37 'formId' => $formID,
38 'entryId' => $entryID,
39 'fieldValues' => $fieldValues
40 ];
41 $fieldData = [];
42 foreach ($fieldMap as $fieldPair) {
43 if (!empty($fieldPair->wcField) && !empty($fieldPair->formField)) {
44 if ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
45 $fieldData[$fieldPair->wcField] = $fieldPair->customValue;
46 } else {
47 $fieldData[$fieldPair->wcField] = $fieldValues[$fieldPair->formField];
48 }
49
50 if (in_array($fieldPair->wcField, $required) && empty($fieldValues[$fieldPair->formField])) {
51 /* translators: %1$s: field name, %2$s: WooCommerce module name. */
52 $error = new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('%1$s is required for woocommerce %2$s', 'bit-form'), $fieldPair->wcField, $module));
53 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => $module, 'type_name' => 'create'], 'validation', $error, $entryDetails);
54 return $error;
55 }
56 }
57 }
58
59 $model = new FormEntryLogModel();
60 $result = $model->entryLogCheck($entryID, $this->_integrationID);
61
62 $entry_type = 'create';
63
64 if (count($result) && !isset($result->errors['result_empty'])) {
65 $entry_type = 'edit';
66 $api_type = json_decode($result[0]->api_type);
67
68 if ($api_type->type === $module) {
69 $id = $result[0]->response_obj;
70 }
71 }
72
73 if ('product' === $module) {
74 if (!empty($fieldData['tags_input'])) {
75 $tags = explode(',', $fieldData['tags_input']);
76 unset($fieldData['tags_input']);
77 }
78
79 if (!empty($fieldData['post_category'])) {
80 $categories = explode(',', $fieldData['post_category']);
81 unset($fieldData['post_category']);
82 }
83
84 if (!empty($fieldData['_regular_price'])) {
85 $price = $fieldData['_regular_price'];
86 }
87
88 if (!empty($fieldData['_sale_price'])) {
89 $price = $fieldData['_sale_price'];
90 }
91
92 if (!empty($fieldData['product_type'])) {
93 $product_type = $fieldData['product_type'];
94 if ('external' === $product_type && !empty($fieldData['_product_url'])) {
95 $product_type = 'external';
96 } else {
97 $product_type = 'simple';
98 unset($fieldData['_product_url'], $fieldData['_button_text']);
99 }
100 unset($fieldData['product_type']);
101 }
102
103 $post_fields = [
104 'post_content', 'post_title', 'post_status', 'post_type', 'comment_status', 'post_password', 'menu_order', 'post_excerpt', 'post_date', 'post_date_gmt'
105 ];
106
107 $post_inputs = array_intersect_key($fieldData, array_flip($post_fields));
108 $meta_inputs = array_diff_key($fieldData, array_flip($post_fields));
109
110 $fieldData = $post_inputs;
111 $fieldData['post_type'] = $module;
112 $fieldData['meta_input'] = $meta_inputs;
113
114 if (!empty($fieldData['post_date']) || !empty($fieldData['post_date_gmt'])) {
115 $fieldData['post_status'] = 'future';
116 }
117
118 if (isset($id)) {
119 $fieldData['ID'] = $id;
120 }
121
122 $product_id = wp_insert_post($fieldData);
123
124 if (isset($product_type)) {
125 wp_set_object_terms($product_id, $product_type, 'product_type');
126 }
127
128 if (isset($price)) {
129 update_post_meta($product_id, '_price', $price);
130 }
131
132 if (isset($categories)) {
133 wp_set_object_terms($product_id, $categories, 'product_cat');
134 }
135
136 if (isset($tags)) {
137 wp_set_object_terms($product_id, $tags, 'product_tag');
138 }
139
140 if (is_wp_error($product_id) || !$product_id) {
141 $response = is_wp_error($product_id) ? $product_id->get_error_message() : 'error';
142 return $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'product', 'type_name' => $entry_type], 'error', $response, $entryDetails);
143 } else {
144 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'product', 'type_name' => $entry_type], 'success', $product_id, $entryDetails);
145 }
146 }
147
148 if ('customer' === $module) {
149 $user_fields = ['user_pass', 'user_login', 'user_nicename', 'user_url', 'user_email', 'display_name', 'nickname', 'first_name', 'last_name', 'description', 'locale'];
150
151 $user_inputs = array_intersect_key($fieldData, array_flip($user_fields));
152 $meta_inputs = array_diff_key($fieldData, array_flip($user_fields));
153
154 $fieldData = $user_inputs;
155 $fieldData['role'] = $module;
156
157 if (isset($id)) {
158 $fieldData['ID'] = $id;
159 }
160
161 $user_id = wp_insert_user($fieldData);
162
163 if (is_wp_error($user_id) || !$user_id) {
164 $response = is_wp_error($user_id) ? $user_id->get_error_message() : 'error';
165 return $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'customer', 'type_name' => $entry_type], 'error', $response, $entryDetails);
166 } else {
167 $this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'customer', 'type_name' => $entry_type], 'success', $user_id, $entryDetails);
168 }
169
170 foreach ($meta_inputs as $metaKey => $metaValue) {
171 update_user_meta($user_id, $metaKey, $metaValue);
172 }
173
174 // Fires WooCommerce core hook so other WC extensions can react to the customer update.
175 do_action('woocommerce_update_customer', $user_id);
176 }
177
178 $flag = null;
179 if (isset($product_id)) {
180 $basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR;
181
182 foreach ($uploadFieldMap as $uploadField) {
183 if (!empty($uploadField->formField) && !empty($uploadField->wcField)) {
184 if ('product_image' === $uploadField->wcField) {
185 $flag = 0;
186 }
187 if ('product_gallery' === $uploadField->wcField) {
188 $flag = 1;
189 }
190 if ('downloadable_files' === $uploadField->wcField) {
191 $flag = 2;
192 }
193
194 $attach_ids = '';
195
196 if (!empty($fieldValues[$uploadField->formField])) {
197 $uplaodFiles = $fieldValues[$uploadField->formField];
198 if ('string' === gettype($fieldValues[$uploadField->formField])) {
199 $uplaodFiles = json_decode($fieldValues[$uploadField->formField]);
200 }
201 if (is_array($uplaodFiles)) {
202 foreach ($uplaodFiles as $singleFile) {
203 $url = $basepath . $singleFile;
204 $attach_id = $this->attach_product_attachments($product_id, $flag, $url, $singleFile);
205 if (1 === $flag && $attach_id) {
206 $attach_ids .= ',' . $attach_id;
207 }
208 }
209 } else {
210 $filename = $uplaodFiles;
211 $url = $basepath . $filename;
212 $this->attach_product_attachments($product_id, $flag, $url, $filename);
213 }
214 }
215
216 if (1 === $flag) {
217 update_post_meta($product_id, '_product_image_gallery', $attach_ids);
218 }
219 }
220 }
221 }
222 }
223
224 public function upload_attachment($product_id, $url)
225 {
226 require_once ABSPATH . 'wp-admin/includes/image.php';
227
228 $response = wp_remote_get($url);
229 if (is_wp_error($response)) {
230 return null;
231 }
232 $response_code = wp_remote_retrieve_response_code($response);
233 if (200 !== $response_code) {
234 return null;
235 }
236 $image_data = wp_remote_retrieve_body($response);
237 if (empty($image_data)) {
238 return null;
239 }
240
241 $url_array = explode('/', $url);
242 $image_name = $url_array[count($url_array) - 1];
243
244 $upload_dir = wp_upload_dir();
245 $unique_file_name = wp_unique_filename($upload_dir['path'], $image_name);
246 $filename = basename($unique_file_name);
247
248 if (wp_mkdir_p($upload_dir['path'])) {
249 $file = $upload_dir['path'] . '/' . $filename;
250 } else {
251 $file = $upload_dir['basedir'] . '/' . $filename;
252 }
253 file_put_contents($file, $image_data);
254
255 $wp_filetype = wp_check_filetype($filename, null);
256
257 $attachment = [
258 'post_mime_type' => $wp_filetype['type'],
259 'post_title' => sanitize_file_name($filename),
260 'post_content' => '',
261 'post_status' => 'inherit'
262 ];
263
264 $attach_id = wp_insert_attachment($attachment, $file, $product_id);
265
266 $attach_data = wp_generate_attachment_metadata($attach_id, $file);
267
268 wp_update_attachment_metadata($attach_id, $attach_data);
269
270 return $attach_id;
271 }
272
273 public function attach_product_attachments($product_id, $flag, $url, $filename)
274 {
275 $attach_id = $this->upload_attachment($product_id, $url);
276 if (0 === $flag) {
277 set_post_thumbnail($product_id, $attach_id);
278 }
279
280 if (1 === $flag) {
281 return $attach_id;
282 }
283
284 if (2 === $flag) {
285 $this->attach_downloadable_attachments($product_id, $url, $filename);
286 }
287 }
288
289 public function attach_downloadable_attachments($product_id, $url, $filename)
290 {
291 if ('yes' !== get_post_meta($product_id, '_downloadable', true)) {
292 return false;
293 }
294
295 require_once dirname(WC_PLUGIN_FILE) . '/includes/wc-product-functions.php';
296
297 $attach_id = $this->upload_attachment($product_id, $url);
298 $download_id = md5($url);
299 $file_url = wp_get_attachment_url($attach_id);
300
301 $pd_object = new WC_Product_Download();
302 $pd_object->set_id($download_id);
303 $pd_object->set_name($filename);
304 $pd_object->set_file($file_url);
305
306 $product = wc_get_product($product_id);
307
308 $downloads = $product->get_downloads();
309 $downloads[$download_id] = $pd_object;
310
311 $product->set_downloads($downloads);
312 $product->save();
313 }
314 }
315