PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
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 2.10.2 All 137 releases
bit-form / includes / Core / Integration / WooCommerce / WooCommerceHandler.php

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

554 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WooCommerce Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\WooCommerce;
9
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 use BitCode\BitForm\Core\Integration\IntegrationHandler;
15 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
16 use BitCode\BitForm\GlobalHelper;
17 use WC_Data_Store;
18 use WP_Error;
19
20 /**
21 * Provide functionality for ZohoCrm integration
22 */
23 class WooCommerceHandler
24 {
25 private $_formID;
26 private $_integrationID;
27
28 private $_logResponse;
29
30 public function __construct($integrationID, $formID)
31 {
32 $this->_formID = $formID;
33 $this->_integrationID = $integrationID;
34 $this->_logResponse = new UtilApiResponse();
35 }
36
37 /**
38 * Helps to register ajax function's with wp
39 *
40 * @return null
41 */
42 public static function registerAjax()
43 {
44 add_action('wp_ajax_bitforms_wc_authorize', [__CLASS__, 'authorizeWC']);
45 add_action('wp_ajax_bitforms_wc_refresh_fields', [__CLASS__, 'refreshFieldsAjaxHelper']);
46 add_action('wp_ajax_bitforms_wc_search_products', [__CLASS__, 'searchProjectsAjaxHelper']);
47 }
48
49 /**
50 * Process ajax request for generate_token
51 *
52 * @return JSON zoho crm api response and status
53 */
54 public static function authorizeWC()
55 {
56 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
57 include_once ABSPATH . 'wp-admin/includes/plugin.php';
58 if (is_plugin_active('woocommerce/woocommerce.php')) {
59 wp_send_json_success(true, 200);
60 }
61
62 wp_send_json_error(__('WooCommerce must be activated!', 'bit-form'));
63 } else {
64 wp_send_json_error(
65 __(
66 'Token expired',
67 'bit-form'
68 ),
69 401
70 );
71 }
72 }
73
74 public static function metaboxFields($module)
75 {
76 $fileTypes = [
77 'image',
78 'image_upload',
79 'file_advanced',
80 'file_upload',
81 'single_image',
82 'file',
83 'image_advanced',
84 'video'
85 ];
86
87 $metaboxFields = [];
88 $metaboxUploadFields = [];
89
90 if (function_exists('rwmb_meta')) {
91 if ('customer' === $module) {
92 $field_registry = rwmb_get_registry('field');
93 $meta_boxes = $field_registry->get_by_object_type($object_type = 'user');
94 $metaFields = array_values($meta_boxes['user']);
95 } else {
96 $metaFields = array_values(rwmb_get_object_fields($module));
97 }
98 foreach ($metaFields as $index => $field) {
99 if (!in_array($field['type'], $fileTypes)) {
100 $metaboxFields[$index] = (object) [
101 'fieldKey' => $field['id'],
102 'fieldName' => 'Metabox Field - ' . $field['name'],
103 'required' => $field['required'],
104 ];
105 } else {
106 $metaboxUploadFields[$index] = (object) [
107 'fieldKey' => $field['id'],
108 'fieldName' => 'Metabox Field - ' . $field['name'],
109 'required' => $field['required'],
110 ];
111 }
112 }
113 }
114
115 return ['meta_fields' => $metaboxFields, 'upload_fields' => $metaboxUploadFields];
116 }
117
118 /**
119 * Process ajax request for refresh crm modules
120 *
121 * @return JSON crm module data
122 */
123 public static function refreshFieldsAjaxHelper()
124 {
125 $required = null;
126 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
127 GlobalHelper::requirePostMethod();
128
129 try {
130 $queryParams = GlobalHelper::formatRequestData();
131 } catch (\InvalidArgumentException $e) {
132 wp_send_json_error($e->getMessage(), 400);
133 }
134
135 $uploadFields = [];
136
137 if (
138 empty($queryParams->module)
139 ) {
140 wp_send_json_error(
141 __(
142 'Requested parameter is empty',
143 'bit-form'
144 ),
145 400
146 );
147 }
148
149 $metabox = self::metaboxFields($queryParams->module);
150
151 if ('product' === $queryParams->module) {
152 // $this->metaboxFields($queryParams->module);
153
154 $fields = [
155 'Product Name' => (object) [
156 'fieldKey' => 'post_title',
157 'fieldName' => 'Product Name',
158 'required' => true
159 ],
160 'Product Description' => (object) [
161 'fieldKey' => 'post_content',
162 'fieldName' => 'Product Description'
163 ],
164 'Product Short Description' => (object) [
165 'fieldKey' => 'post_excerpt',
166 'fieldName' => 'Product Short Description'
167 ],
168 'Post Date' => (object) [
169 'fieldKey' => 'post_date',
170 'fieldName' => 'Post Date'
171 ],
172 'Post Date GMT' => (object) [
173 'fieldKey' => 'post_date_gmt',
174 'fieldName' => 'Post Date GMT'
175 ],
176 'Product Status' => (object) [
177 'fieldKey' => 'post_status',
178 'fieldName' => 'Product Status'
179 ],
180 'Product Tag' => (object) [
181 'fieldKey' => 'tags_input',
182 'fieldName' => 'Product Tag'
183 ],
184 'Product Category' => (object) [
185 'fieldKey' => 'post_category',
186 'fieldName' => 'Product Category'
187 ],
188 'Catalog Visibility' => (object) [
189 'fieldKey' => '_visibility',
190 'fieldName' => 'Catalog Visibility'
191 ],
192 'Featured Product' => (object) [
193 'fieldKey' => '_featured',
194 'fieldName' => 'Featured Product'
195 ],
196 'Post Password' => (object) [
197 'fieldKey' => 'post_password',
198 'fieldName' => 'Post Password'
199 ],
200 'Regular Price' => (object) [
201 'fieldKey' => '_regular_price',
202 'fieldName' => 'Regular Price'
203 ],
204 'Sale Price' => (object) [
205 'fieldKey' => '_sale_price',
206 'fieldName' => 'Sale Price'
207 ],
208 'Sale Price From Date' => (object) [
209 'fieldKey' => '_sale_price_dates_from',
210 'fieldName' => 'Sale Price From Date'
211 ],
212 'Sale Price To Date' => (object) [
213 'fieldKey' => '_sale_price_dates_to',
214 'fieldName' => 'Sale Price To Date'
215 ],
216 'SKU' => (object) [
217 'fieldKey' => '_sku',
218 'fieldName' => 'SKU'
219 ],
220 'Manage Stock' => (object) [
221 'fieldKey' => '_manage_stock',
222 'fieldName' => 'Manage Stock'
223 ],
224 'Stock Quantity' => (object) [
225 'fieldKey' => '_stock',
226 'fieldName' => 'Stock Quantity'
227 ],
228 'Allow Backorders' => (object) [
229 'fieldKey' => '_backorders',
230 'fieldName' => 'Allow Backorders'
231 ],
232 'Low Stock Threshold' => (object) [
233 'fieldKey' => '_low_stock_amount',
234 'fieldName' => 'Low Stock Threshold'
235 ],
236 'Stock Status' => (object) [
237 'fieldKey' => '_stock_status',
238 'fieldName' => 'Stock Status'
239 ],
240 'Sold Individually' => (object) [
241 'fieldKey' => '_sold_individually',
242 'fieldName' => 'Sold Individually'
243 ],
244 'Weight' => (object) [
245 'fieldKey' => '_weight',
246 'fieldName' => 'Weight'
247 ],
248 'Length' => (object) [
249 'fieldKey' => '_length',
250 'fieldName' => 'Length'
251 ],
252 'Width' => (object) [
253 'fieldKey' => '_width',
254 'fieldName' => 'Width'
255 ],
256 'Height' => (object) [
257 'fieldKey' => '_height',
258 'fieldName' => 'Height'
259 ],
260 'Purchase Note' => (object) [
261 'fieldKey' => '_purchase_note',
262 'fieldName' => 'Purchase Note'
263 ],
264 'Menu Order' => (object) [
265 'fieldKey' => 'menu_order',
266 'fieldName' => 'Menu Order'
267 ],
268 'Enable Reviews' => (object) [
269 'fieldKey' => 'comment_status',
270 'fieldName' => 'Enable Reviews'
271 ],
272 'Virtual' => (object) [
273 'fieldKey' => '_virtual',
274 'fieldName' => 'Virtual'
275 ],
276 'Downloadable' => (object) [
277 'fieldKey' => '_downloadable',
278 'fieldName' => 'Downloadable'
279 ],
280 'Download Limit' => (object) [
281 'fieldKey' => '_download_limit',
282 'fieldName' => 'Download Limit'
283 ],
284 'Download Expiry' => (object) [
285 'fieldKey' => '_download_expiry',
286 'fieldName' => 'Download Expiry'
287 ],
288 'Product Type' => (object) [
289 'fieldKey' => 'product_type',
290 'fieldName' => 'Product Type'
291 ],
292 'Product URL' => (object) [
293 'fieldKey' => '_product_url',
294 'fieldName' => 'Product URL'
295 ],
296 'Button Text' => (object) [
297 'fieldKey' => '_button_text',
298 'fieldName' => 'Button Text'
299 ],
300 ];
301 $fields = array_merge($fields, $metabox['meta_fields']);
302
303 $uploadFields = [
304 'Product Image' => (object) [
305 'fieldKey' => 'product_image',
306 'fieldName' => 'Product Image'
307 ],
308 'Product Gallery' => (object) [
309 'fieldKey' => 'product_gallery',
310 'fieldName' => 'Product Gallery'
311 ],
312 'Downloadable Files' => (object) [
313 'fieldKey' => 'downloadable_files',
314 'fieldName' => 'Downloadable Files'
315 ],
316 ];
317 $uploadFields = array_merge($uploadFields, $metabox['upload_fields']);
318
319 $required = ['post_title'];
320 }
321
322 if ('customer' === $queryParams->module) {
323 //$customerFields = self::metaboxFields($queryParams->module);
324 $fields = [
325 'First Name' => (object) [
326 'fieldKey' => 'first_name',
327 'fieldName' => 'First Name'
328 ],
329 'Last Name' => (object) [
330 'fieldKey' => 'last_name',
331 'fieldName' => 'Last Name'
332 ],
333 'Email' => (object) [
334 'fieldKey' => 'user_email',
335 'fieldName' => 'Email',
336 'required' => true
337 ],
338 'Username' => (object) [
339 'fieldKey' => 'user_login',
340 'fieldName' => 'Username',
341 'required' => true
342 ],
343 'Password' => (object) [
344 'fieldKey' => 'user_pass',
345 'fieldName' => 'Password'
346 ],
347 'Display Name' => (object) [
348 'fieldKey' => 'display_name',
349 'fieldName' => 'Display Name'
350 ],
351 'Nickname' => (object) [
352 'fieldKey' => 'nickname',
353 'fieldName' => 'Nickname'
354 ],
355 'Description' => (object) [
356 'fieldKey' => 'description',
357 'fieldName' => 'Description'
358 ],
359 'Locale' => (object) [
360 'fieldKey' => 'locale',
361 'fieldName' => 'Locale'
362 ],
363 'Website' => (object) [
364 'fieldKey' => 'user_url',
365 'fieldName' => 'Website'
366 ],
367 'Billing First Name' => (object) [
368 'fieldKey' => 'billing_first_name',
369 'fieldName' => 'Billing First Name'
370 ],
371 'Billing Last Name' => (object) [
372 'fieldKey' => 'billing_last_name',
373 'fieldName' => 'Billing Last Name'
374 ],
375 'Billing Company' => (object) [
376 'fieldKey' => 'billing_company',
377 'fieldName' => 'Billing Company'
378 ],
379 'Billing Address 1' => (object) [
380 'fieldKey' => 'billing_address_1',
381 'fieldName' => 'Billing Address 1'
382 ],
383 'Billing Address 2' => (object) [
384 'fieldKey' => 'billing_address_2',
385 'fieldName' => 'Billing Address 2'
386 ],
387 'Billing City' => (object) [
388 'fieldKey' => 'billing_city',
389 'fieldName' => 'Billing City'
390 ],
391 'Billing Post Code' => (object) [
392 'fieldKey' => 'billing_postcode',
393 'fieldName' => 'Billing Post Code'
394 ],
395 'Billing Country' => (object) [
396 'fieldKey' => 'billing_country',
397 'fieldName' => 'Billing Country'
398 ],
399 'Billing State' => (object) [
400 'fieldKey' => 'billing_state',
401 'fieldName' => 'Billing State'
402 ],
403 'Billing Email' => (object) [
404 'fieldKey' => 'billing_email',
405 'fieldName' => 'Billing Email'
406 ],
407 'Billing Phone' => (object) [
408 'fieldKey' => 'billing_phone',
409 'fieldName' => 'Billing Phone'
410 ],
411 'Shipping First Name' => (object) [
412 'fieldKey' => 'shipping_first_name',
413 'fieldName' => 'Shipping First Name'
414 ],
415 'Shipping Last Name' => (object) [
416 'fieldKey' => 'shipping_last_name',
417 'fieldName' => 'Shipping Last Name'
418 ],
419 'Shipping Company' => (object) [
420 'fieldKey' => 'shipping_company',
421 'fieldName' => 'Shipping Company'
422 ],
423 'Shipping Address 1' => (object) [
424 'fieldKey' => 'shipping_address_1',
425 'fieldName' => 'Shipping Address 1'
426 ],
427 'Shipping Address 2' => (object) [
428 'fieldKey' => 'shipping_address_2',
429 'fieldName' => 'Shipping Address 2'
430 ],
431 'Shipping City' => (object) [
432 'fieldKey' => 'shipping_city',
433 'fieldName' => 'Shipping City'
434 ],
435 'Shipping Post Code' => (object) [
436 'fieldKey' => 'shipping_postcode',
437 'fieldName' => 'Shipping Post Code'
438 ],
439 'Shipping Country' => (object) [
440 'fieldKey' => 'shipping_country',
441 'fieldName' => 'Shipping Country'
442 ],
443 'Shipping State' => (object) [
444 'fieldKey' => 'shipping_state',
445 'fieldName' => 'Shipping State'
446 ],
447 ];
448
449 $required = ['user_login', 'user_email'];
450 }
451
452 uksort($fields, 'strnatcasecmp');
453 // $uploadFields = !empty($uploadFields) && is_array($uploadFields) ? uksort($uploadFields, 'strnatcasecmp') : $uploadFields;
454 uksort($uploadFields, 'strnatcasecmp');
455
456 $fields = array_merge($fields, $metabox['meta_fields']);
457
458 $response = [
459 'fields' => $fields,
460 'uploadFields' => $uploadFields,
461 'required' => $required
462 ];
463
464 wp_send_json_success($response, 200);
465 } else {
466 wp_send_json_error(
467 __(
468 'Token expired',
469 'bit-form'
470 ),
471 401
472 );
473 }
474 }
475
476 public function searchProjectsAjaxHelper()
477 {
478 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
479 GlobalHelper::requirePostMethod();
480
481 try {
482 $queryParams = GlobalHelper::formatRequestData();
483 } catch (\InvalidArgumentException $e) {
484 wp_send_json_error($e->getMessage(), 400);
485 }
486
487 include_once dirname(WC_PLUGIN_FILE) . '/includes/class-wc-product-functions.php';
488 $data_store = WC_Data_Store::load('product');
489 $search_results = $data_store->search_products($queryParams->searchTxt);
490 $products = [];
491 foreach ($search_results as $res) {
492 if ($res) {
493 $product = wc_get_product($res);
494 $products[] = [
495 'id' => $res,
496 'name' => $product->get_name(),
497 ];
498 }
499 }
500
501 wp_send_json_success($products, 200);
502 } else {
503 wp_send_json_error(
504 __(
505 'Token expired',
506 'bit-form'
507 ),
508 401
509 );
510 }
511 }
512
513 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
514 {
515 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
516
517 $module = $integrationDetails->module;
518 $fieldMap = $integrationDetails->field_map;
519 $uploadFieldMap = $integrationDetails->upload_field_map;
520 $required = $integrationDetails->default->fields->{$module}->required;
521
522 $entryDetails = [
523 'formId' => $this->_formID,
524 'entryId' => $entryID,
525 'fieldValues' => $fieldValues
526 ];
527 if (
528 empty($module)
529 || empty($fieldMap)
530 ) {
531 $error = new WP_Error('REQ_FIELD_EMPTY', __('module and field map are required for woocommerce', 'bit-form'));
532 $this->_logResponse->apiResponse($logID, $this->_integrationID, 'record', 'validation', $error, $entryDetails);
533 return $error;
534 }
535
536 $recordApiHelper = new RecordApiHelper($this->_integrationID, $logID);
537
538 $wcApiResponse = $recordApiHelper->executeRecordApi(
539 $this->_formID,
540 $entryID,
541 $module,
542 $fieldValues,
543 $fieldMap,
544 $uploadFieldMap,
545 $required
546 );
547
548 if (is_wp_error($wcApiResponse)) {
549 return $wcApiResponse;
550 }
551 return $wcApiResponse;
552 }
553 }
554