PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
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 / WooCommerceHandler.php

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

557 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
128
129 GlobalHelper::requirePostMethod();
130
131 try {
132 $queryParams = GlobalHelper::formatRequestData();
133 } catch (\InvalidArgumentException $e) {
134 wp_send_json_error($e->getMessage(), 400);
135 }
136
137 $uploadFields = [];
138
139 if (
140 empty($queryParams->module)
141 ) {
142 wp_send_json_error(
143 __(
144 'Requested parameter is empty',
145 'bit-form'
146 ),
147 400
148 );
149 }
150
151 $metabox = self::metaboxFields($queryParams->module);
152
153 if ('product' === $queryParams->module) {
154 // $this->metaboxFields($queryParams->module);
155
156 $fields = [
157 'Product Name' => (object) [
158 'fieldKey' => 'post_title',
159 'fieldName' => 'Product Name',
160 'required' => true
161 ],
162 'Product Description' => (object) [
163 'fieldKey' => 'post_content',
164 'fieldName' => 'Product Description'
165 ],
166 'Product Short Description' => (object) [
167 'fieldKey' => 'post_excerpt',
168 'fieldName' => 'Product Short Description'
169 ],
170 'Post Date' => (object) [
171 'fieldKey' => 'post_date',
172 'fieldName' => 'Post Date'
173 ],
174 'Post Date GMT' => (object) [
175 'fieldKey' => 'post_date_gmt',
176 'fieldName' => 'Post Date GMT'
177 ],
178 'Product Status' => (object) [
179 'fieldKey' => 'post_status',
180 'fieldName' => 'Product Status'
181 ],
182 'Product Tag' => (object) [
183 'fieldKey' => 'tags_input',
184 'fieldName' => 'Product Tag'
185 ],
186 'Product Category' => (object) [
187 'fieldKey' => 'post_category',
188 'fieldName' => 'Product Category'
189 ],
190 'Catalog Visibility' => (object) [
191 'fieldKey' => '_visibility',
192 'fieldName' => 'Catalog Visibility'
193 ],
194 'Featured Product' => (object) [
195 'fieldKey' => '_featured',
196 'fieldName' => 'Featured Product'
197 ],
198 'Post Password' => (object) [
199 'fieldKey' => 'post_password',
200 'fieldName' => 'Post Password'
201 ],
202 'Regular Price' => (object) [
203 'fieldKey' => '_regular_price',
204 'fieldName' => 'Regular Price'
205 ],
206 'Sale Price' => (object) [
207 'fieldKey' => '_sale_price',
208 'fieldName' => 'Sale Price'
209 ],
210 'Sale Price From Date' => (object) [
211 'fieldKey' => '_sale_price_dates_from',
212 'fieldName' => 'Sale Price From Date'
213 ],
214 'Sale Price To Date' => (object) [
215 'fieldKey' => '_sale_price_dates_to',
216 'fieldName' => 'Sale Price To Date'
217 ],
218 'SKU' => (object) [
219 'fieldKey' => '_sku',
220 'fieldName' => 'SKU'
221 ],
222 'Manage Stock' => (object) [
223 'fieldKey' => '_manage_stock',
224 'fieldName' => 'Manage Stock'
225 ],
226 'Stock Quantity' => (object) [
227 'fieldKey' => '_stock',
228 'fieldName' => 'Stock Quantity'
229 ],
230 'Allow Backorders' => (object) [
231 'fieldKey' => '_backorders',
232 'fieldName' => 'Allow Backorders'
233 ],
234 'Low Stock Threshold' => (object) [
235 'fieldKey' => '_low_stock_amount',
236 'fieldName' => 'Low Stock Threshold'
237 ],
238 'Stock Status' => (object) [
239 'fieldKey' => '_stock_status',
240 'fieldName' => 'Stock Status'
241 ],
242 'Sold Individually' => (object) [
243 'fieldKey' => '_sold_individually',
244 'fieldName' => 'Sold Individually'
245 ],
246 'Weight' => (object) [
247 'fieldKey' => '_weight',
248 'fieldName' => 'Weight'
249 ],
250 'Length' => (object) [
251 'fieldKey' => '_length',
252 'fieldName' => 'Length'
253 ],
254 'Width' => (object) [
255 'fieldKey' => '_width',
256 'fieldName' => 'Width'
257 ],
258 'Height' => (object) [
259 'fieldKey' => '_height',
260 'fieldName' => 'Height'
261 ],
262 'Purchase Note' => (object) [
263 'fieldKey' => '_purchase_note',
264 'fieldName' => 'Purchase Note'
265 ],
266 'Menu Order' => (object) [
267 'fieldKey' => 'menu_order',
268 'fieldName' => 'Menu Order'
269 ],
270 'Enable Reviews' => (object) [
271 'fieldKey' => 'comment_status',
272 'fieldName' => 'Enable Reviews'
273 ],
274 'Virtual' => (object) [
275 'fieldKey' => '_virtual',
276 'fieldName' => 'Virtual'
277 ],
278 'Downloadable' => (object) [
279 'fieldKey' => '_downloadable',
280 'fieldName' => 'Downloadable'
281 ],
282 'Download Limit' => (object) [
283 'fieldKey' => '_download_limit',
284 'fieldName' => 'Download Limit'
285 ],
286 'Download Expiry' => (object) [
287 'fieldKey' => '_download_expiry',
288 'fieldName' => 'Download Expiry'
289 ],
290 'Product Type' => (object) [
291 'fieldKey' => 'product_type',
292 'fieldName' => 'Product Type'
293 ],
294 'Product URL' => (object) [
295 'fieldKey' => '_product_url',
296 'fieldName' => 'Product URL'
297 ],
298 'Button Text' => (object) [
299 'fieldKey' => '_button_text',
300 'fieldName' => 'Button Text'
301 ],
302 ];
303 $fields = array_merge($fields, $metabox['meta_fields']);
304
305 $uploadFields = [
306 'Product Image' => (object) [
307 'fieldKey' => 'product_image',
308 'fieldName' => 'Product Image'
309 ],
310 'Product Gallery' => (object) [
311 'fieldKey' => 'product_gallery',
312 'fieldName' => 'Product Gallery'
313 ],
314 'Downloadable Files' => (object) [
315 'fieldKey' => 'downloadable_files',
316 'fieldName' => 'Downloadable Files'
317 ],
318 ];
319 $uploadFields = array_merge($uploadFields, $metabox['upload_fields']);
320
321 $required = ['post_title'];
322 }
323
324 if ('customer' === $queryParams->module) {
325 //$customerFields = self::metaboxFields($queryParams->module);
326 $fields = [
327 'First Name' => (object) [
328 'fieldKey' => 'first_name',
329 'fieldName' => 'First Name'
330 ],
331 'Last Name' => (object) [
332 'fieldKey' => 'last_name',
333 'fieldName' => 'Last Name'
334 ],
335 'Email' => (object) [
336 'fieldKey' => 'user_email',
337 'fieldName' => 'Email',
338 'required' => true
339 ],
340 'Username' => (object) [
341 'fieldKey' => 'user_login',
342 'fieldName' => 'Username',
343 'required' => true
344 ],
345 'Password' => (object) [
346 'fieldKey' => 'user_pass',
347 'fieldName' => 'Password'
348 ],
349 'Display Name' => (object) [
350 'fieldKey' => 'display_name',
351 'fieldName' => 'Display Name'
352 ],
353 'Nickname' => (object) [
354 'fieldKey' => 'nickname',
355 'fieldName' => 'Nickname'
356 ],
357 'Description' => (object) [
358 'fieldKey' => 'description',
359 'fieldName' => 'Description'
360 ],
361 'Locale' => (object) [
362 'fieldKey' => 'locale',
363 'fieldName' => 'Locale'
364 ],
365 'Website' => (object) [
366 'fieldKey' => 'user_url',
367 'fieldName' => 'Website'
368 ],
369 'Billing First Name' => (object) [
370 'fieldKey' => 'billing_first_name',
371 'fieldName' => 'Billing First Name'
372 ],
373 'Billing Last Name' => (object) [
374 'fieldKey' => 'billing_last_name',
375 'fieldName' => 'Billing Last Name'
376 ],
377 'Billing Company' => (object) [
378 'fieldKey' => 'billing_company',
379 'fieldName' => 'Billing Company'
380 ],
381 'Billing Address 1' => (object) [
382 'fieldKey' => 'billing_address_1',
383 'fieldName' => 'Billing Address 1'
384 ],
385 'Billing Address 2' => (object) [
386 'fieldKey' => 'billing_address_2',
387 'fieldName' => 'Billing Address 2'
388 ],
389 'Billing City' => (object) [
390 'fieldKey' => 'billing_city',
391 'fieldName' => 'Billing City'
392 ],
393 'Billing Post Code' => (object) [
394 'fieldKey' => 'billing_postcode',
395 'fieldName' => 'Billing Post Code'
396 ],
397 'Billing Country' => (object) [
398 'fieldKey' => 'billing_country',
399 'fieldName' => 'Billing Country'
400 ],
401 'Billing State' => (object) [
402 'fieldKey' => 'billing_state',
403 'fieldName' => 'Billing State'
404 ],
405 'Billing Email' => (object) [
406 'fieldKey' => 'billing_email',
407 'fieldName' => 'Billing Email'
408 ],
409 'Billing Phone' => (object) [
410 'fieldKey' => 'billing_phone',
411 'fieldName' => 'Billing Phone'
412 ],
413 'Shipping First Name' => (object) [
414 'fieldKey' => 'shipping_first_name',
415 'fieldName' => 'Shipping First Name'
416 ],
417 'Shipping Last Name' => (object) [
418 'fieldKey' => 'shipping_last_name',
419 'fieldName' => 'Shipping Last Name'
420 ],
421 'Shipping Company' => (object) [
422 'fieldKey' => 'shipping_company',
423 'fieldName' => 'Shipping Company'
424 ],
425 'Shipping Address 1' => (object) [
426 'fieldKey' => 'shipping_address_1',
427 'fieldName' => 'Shipping Address 1'
428 ],
429 'Shipping Address 2' => (object) [
430 'fieldKey' => 'shipping_address_2',
431 'fieldName' => 'Shipping Address 2'
432 ],
433 'Shipping City' => (object) [
434 'fieldKey' => 'shipping_city',
435 'fieldName' => 'Shipping City'
436 ],
437 'Shipping Post Code' => (object) [
438 'fieldKey' => 'shipping_postcode',
439 'fieldName' => 'Shipping Post Code'
440 ],
441 'Shipping Country' => (object) [
442 'fieldKey' => 'shipping_country',
443 'fieldName' => 'Shipping Country'
444 ],
445 'Shipping State' => (object) [
446 'fieldKey' => 'shipping_state',
447 'fieldName' => 'Shipping State'
448 ],
449 ];
450
451 $required = ['user_login', 'user_email'];
452 }
453
454 uksort($fields, 'strnatcasecmp');
455 // $uploadFields = !empty($uploadFields) && is_array($uploadFields) ? uksort($uploadFields, 'strnatcasecmp') : $uploadFields;
456 uksort($uploadFields, 'strnatcasecmp');
457
458 $fields = array_merge($fields, $metabox['meta_fields']);
459
460 $response = [
461 'fields' => $fields,
462 'uploadFields' => $uploadFields,
463 'required' => $required
464 ];
465
466 wp_send_json_success($response, 200);
467 } else {
468 wp_send_json_error(
469 __(
470 'Token expired',
471 'bit-form'
472 ),
473 401
474 );
475 }
476 }
477
478 public function searchProjectsAjaxHelper()
479 {
480 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
481
482 GlobalHelper::requirePostMethod();
483
484 try {
485 $queryParams = GlobalHelper::formatRequestData();
486 } catch (\InvalidArgumentException $e) {
487 wp_send_json_error($e->getMessage(), 400);
488 }
489
490 include_once dirname(WC_PLUGIN_FILE) . '/includes/class-wc-product-functions.php';
491 $data_store = WC_Data_Store::load('product');
492 $search_results = $data_store->search_products($queryParams->searchTxt);
493 $products = [];
494 foreach ($search_results as $res) {
495 if ($res) {
496 $product = wc_get_product($res);
497 $products[] = [
498 'id' => $res,
499 'name' => $product->get_name(),
500 ];
501 }
502 }
503
504 wp_send_json_success($products, 200);
505 } else {
506 wp_send_json_error(
507 __(
508 'Token expired',
509 'bit-form'
510 ),
511 401
512 );
513 }
514 }
515
516 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
517 {
518 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
519
520 $module = $integrationDetails->module;
521 $fieldMap = $integrationDetails->field_map;
522 $uploadFieldMap = $integrationDetails->upload_field_map;
523 $required = $integrationDetails->default->fields->{$module}->required;
524
525 $entryDetails = [
526 'formId' => $this->_formID,
527 'entryId' => $entryID,
528 'fieldValues' => $fieldValues
529 ];
530 if (
531 empty($module)
532 || empty($fieldMap)
533 ) {
534 $error = new WP_Error('REQ_FIELD_EMPTY', __('module and field map are required for woocommerce', 'bit-form'));
535 $this->_logResponse->apiResponse($logID, $this->_integrationID, 'record', 'validation', $error, $entryDetails);
536 return $error;
537 }
538
539 $recordApiHelper = new RecordApiHelper($this->_integrationID, $logID);
540
541 $wcApiResponse = $recordApiHelper->executeRecordApi(
542 $this->_formID,
543 $entryID,
544 $module,
545 $fieldValues,
546 $fieldMap,
547 $uploadFieldMap,
548 $required
549 );
550
551 if (is_wp_error($wcApiResponse)) {
552 return $wcApiResponse;
553 }
554 return $wcApiResponse;
555 }
556 }
557