PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.9.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.9.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 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 2.9.1, at includes/Core/Integration/WooCommerce/WooCommerceHandler.php

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