PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.1
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.1
3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 2.8.17 2.8.18 All 259 releases
wpfunnels / includes / core / rest-api / Controllers / class-orderbump-controller.php

class-orderbump-controller.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.1, at includes/core/rest-api/Controllers/class-orderbump-controller.php

602 lines 20.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Orderbump controller
4 *
5 * @package WPFunnels\Rest\Controllers
6 */
7 namespace WPFunnels\Rest\Controllers;
8
9 use WP_Error;
10 use WP_REST_Request;
11 use WP_REST_Response;
12 use WP_REST_Server;
13 use WPFunnels\Wpfnl_functions;
14
15 use Wpfnl_Controller_Type_Factory;
16
17 class OrderBumpController extends Wpfnl_REST_Controller
18 {
19
20 /**
21 * Endpoint namespace.
22 *
23 * @var string
24 */
25 protected $namespace = 'wpfunnels/v1';
26
27 /**
28 * Route base.
29 *
30 * @var string
31 */
32 protected $rest_base = 'order-bump';
33
34 /**
35 * Check if user has valid permission
36 *
37 * @param $request
38 *
39 * @return bool|WP_Error
40 * @since 1.0.0
41 */
42 public function update_items_permissions_check($request)
43 {
44 if (!Wpfnl_functions::wpfnl_rest_check_manager_permissions('steps', 'edit')) {
45 return new WP_Error('wpfunnels_rest_cannot_edit', __('Sorry, you cannot edit this resource.', 'wpfnl'), array('status' => rest_authorization_required_code()));
46 }
47 return true;
48 }
49
50 /**
51 * Makes sure the current user has access to READ the settings APIs.
52 *
53 * @param WP_REST_Request $request Full data about the request.
54 *
55 * @return WP_Error|boolean
56 * @since 3.0.0
57 */
58 public function get_items_permissions_check($request)
59 {
60 if (!Wpfnl_functions::wpfnl_rest_check_manager_permissions('settings')) {
61 return new WP_Error('wpfunnels_rest_cannot_edit', __('Sorry, you cannot list resources.', 'wpfnl'), array('status' => rest_authorization_required_code()));
62 }
63 return true;
64 }
65
66
67 /**
68 * Register rest routes
69 *
70 * @since 1.0.0
71 */
72 public function register_routes()
73 {
74 register_rest_route(
75 $this->namespace, '/' . $this->rest_base, array(
76 'args' => array(),
77 array(
78 'methods' => WP_REST_Server::EDITABLE,
79 'callback' => array($this, 'update_settings'),
80 'permission_callback' => array($this, 'update_items_permissions_check'),
81 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE),
82 )
83 )
84 );
85 register_rest_route(
86 $this->namespace, '/' . $this->rest_base . '/(?P<step_id>\d+)', array(
87 'args' => array(
88 'step_id' => array(
89 'description' => __('Step ID.', 'wpfnl'),
90 'type' => 'string',
91 )
92 ),
93 array(
94 'methods' => WP_REST_Server::READABLE,
95 'callback' => array($this, 'get_setting'),
96 'permission_callback' => array($this, 'get_items_permissions_check'),
97 ),
98 )
99 );
100 register_rest_route(
101 $this->namespace, '/' . $this->rest_base . '/update-status/(?P<step_id>\d+)', array(
102 'args' => array(
103 'step_id' => array(
104 'description' => __('Step ID.', 'wpfnl'),
105 'type' => 'string',
106 )
107 ),
108 array(
109 'methods' => WP_REST_Server::EDITABLE,
110 'callback' => array($this, 'update_status'),
111 'permission_callback' => array($this, 'get_items_permissions_check'),
112 ),
113 )
114 );
115
116 register_rest_route($this->namespace, '/getProducts/', [
117 [
118 'methods' => \WP_REST_Server::READABLE,
119 'callback' => [
120 $this,
121 'get_wc_products'
122 ],
123 'permission_callback' => [
124 $this,
125 'get_items_permissions_check'
126 ],
127 ],
128 ]);
129
130 register_rest_route($this->namespace, '/getFluentForms', [
131 [
132 'methods' => \WP_REST_Server::READABLE,
133 'callback' => [
134 $this,
135 'get_fluent_form_list'
136 ],
137 'permission_callback' => [
138 $this,
139 'get_items_permissions_check'
140 ],
141 ],
142 ]);
143
144 register_rest_route($this->namespace, '/getOtherSteps', [
145 [
146 'methods' => \WP_REST_Server::READABLE,
147 'callback' => array( $this, 'get_other_steps' ),
148 'permission_callback' => array( $this, 'get_items_permissions_check'),
149 ],
150 ]);
151
152
153
154 register_rest_route($this->namespace, '/updateSelectedProduct/', [
155 [
156 'methods' => \WP_REST_Server::READABLE,
157 'callback' => [
158 $this,
159 'update_selected_product'
160 ],
161 'permission_callback' => [
162 $this,
163 'get_items_permissions_check'
164 ] ,
165 ],
166 ]);
167
168 }
169
170
171
172 /**
173 * Update selected product
174 *
175 * @param string $existing_product.
176 *
177 * @return array|WP_Error
178 */
179 public function update_selected_product($request)
180 {
181 $data = $request->get_params();
182 $product_data = [
183 'name' => '',
184 'price' => '',
185 'sale_price' => '',
186 'html_price' => '',
187 'title' => '',
188 'img' => '',
189 ];
190
191 if (isset($data['product'])) {
192 $product_id = $data['product'];
193 $product_object = wc_get_product($product_id);
194 if( $product_object ){
195 $formatted_name = $product_object->get_formatted_name();
196 if ($product_object->get_type() == 'variable-subscription') {
197 $sale_price = $product_object->get_sale_price();
198 if ($sale_price != "") {
199 $sale_price = wc_price($product_object->get_sale_price());
200 }
201 //=== Return updated product data===//
202 $product_data = [
203 'name' => rawurldecode($formatted_name),
204 'price' => $product_object->get_price_html(),
205 'sale_price' => $sale_price,
206 'html_price' => $product_object->get_price_html(),
207 'title' => $product_object->get_title(),
208 'img' => $product_object->get_image(),
209 ];
210
211 } elseif ($product_object->get_type() == 'variable') {
212 $sale_price = $product_object->get_sale_price();
213 if ($sale_price != "") {
214 $sale_price = wc_price($product_object->get_sale_price());
215 }
216
217 //=== Return updated product data===//
218 $product_data = [
219 'name' => rawurldecode($formatted_name),
220 'price' => wc_price($product_object->get_regular_price()),
221 'sale_price' => $sale_price,
222 'html_price' => $product_object->get_price_html(),
223 'title' => $product_object->get_title(),
224 'img' => $product_object->get_image(),
225 ];
226 } else {
227 $sale_price = $product_object->get_sale_price();
228 if ($sale_price != "") {
229 $sale_price = wc_price($product_object->get_sale_price());
230 }
231 //=== Return updated product data===//
232 $product_data = [
233 'name' => rawurldecode($formatted_name),
234 'price' => wc_price($product_object->get_regular_price()),
235 'sale_price' => $sale_price,
236 'html_price' => $product_object->get_price_html(),
237 'title' => $product_object->get_title(),
238 'img' => $product_object->get_image(),
239 ];
240 }
241 }
242 }
243 return $product_data;
244 }
245
246
247
248 /**
249 * Get Other Steps.
250 *
251 * @param string $request data.
252 *
253 * @return array|WP_Error
254 */
255 public function get_other_steps($request)
256 {
257 $step_id = $request['step_id'];
258 if (isset($request['from']) && $request['from'] == 'plugin') {
259 $funnel_id = get_post_meta($step_id, '_funnel_id', true);
260 if (!$funnel_id) {
261 return null;
262 }
263 $data = [];
264 $data[] = [
265 'value' => 'none',
266 'name' => 'Default',
267 ];
268 $steps = get_post_meta($funnel_id, '_steps_order', true);
269 foreach ($steps as $step_key => $step_value) {
270 if ($step_value['step_type'] != 'landing' && $step_value['step_type'] != 'checkout') {
271 $step_data = [
272 'value' => $step_value['id'],
273 'name' => $step_value['name'],
274 ];
275 $data[] = $step_data;
276 }
277 }
278 return $data;
279 }
280
281 $funnel_id = get_post_meta($step_id, '_funnel_id', true);
282 if (!$funnel_id) {
283 return null;
284 }
285 $data = [];
286 $data[] = [
287 'value' => 'none',
288 'label' => 'Default',
289 ];
290 $steps = get_post_meta($funnel_id, '_steps_order', true);
291 foreach ($steps as $step_key => $step_value) {
292 if ($step_value['step_type'] != 'landing' && $step_value['step_type'] != 'checkout') {
293 $step_data = [
294 'value' => $step_value['id'],
295 'label' => $step_value['name'],
296 ];
297 $data[] = $step_data;
298 }
299 }
300 return $data;
301
302 }
303
304
305
306 /**
307 * Get Fluent Forms Data
308 *
309 * @param string $request to get.
310 *
311 * @return array|WP_Error
312 */
313 public function get_fluent_form_list($request)
314 {
315 $data = [];
316 $default = [
317 'value' => 'null',
318 'label' => 'Select A Form',
319 ];
320 $data[] = $default;
321 if (in_array('fluentform/fluentform.php', WPFNL_ACTIVE_PLUGINS)) {
322 global $wpdb;
323 $sql = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}fluentform_forms");
324 $results = $wpdb->get_results($sql);
325 foreach ($results as $value) {
326 $sql = $wpdb->prepare("SELECT title FROM {$wpdb->prefix}fluentform_forms WHERE id=$value->id");
327 $title = $wpdb->get_results($sql);
328 $result = [
329 'value' => $value->id,
330 'label' => $title[0]->title,
331 ];
332 $data[] = $result;
333 }
334 }
335 return $data;
336 }
337
338
339
340 /**
341 * Get all Products.
342 *
343 * @param string $request Data.
344 *
345 * @return array|WP_Error
346 */
347 public function get_wc_products($request)
348 {
349 $data = [];
350 $default = [
351 'value' => null,
352 'label' => 'Select a Product'
353 ];
354 $data[] = $default;
355 if (in_array('woocommerce/woocommerce.php', WPFNL_ACTIVE_PLUGINS)) {
356 $all_ids = get_posts([
357 'post_type' => 'product',
358 'numberposts' => -1,
359 'post_status' => 'publish',
360 'fields' => 'ids',
361 ]);
362 foreach ($all_ids as $id) {
363 $product = wc_get_product($id);
364 if( $product ){
365 $type = $product->get_type();
366 if ($type == 'variable') {
367 $variations = $product->get_available_variations();
368 if( !empty($variations) ){
369 foreach ($variations as $variation) {
370 $product = wc_get_product($variation['variation_id']);
371 if( $product ){
372 $value = $variation['variation_id'];
373 $label = $product->get_name();
374 $result = [
375 'value' => $value,
376 'label' => $label,
377 ];
378 $data[] = $result;
379 }
380 }
381 }
382 } else {
383 $value = $id;
384 $label = $product->get_name();
385 $result = [
386 'value' => $value,
387 'label' => $label,
388 ];
389 $data[] = $result;
390 }
391 }
392 }
393 }
394 return $data;
395 }
396
397
398 /**
399 * Get order bump settings
400 *
401 * @param WP_REST_Request $request
402 *
403 * @return mixed
404 * @throws \Exception
405 */
406 public function get_setting(WP_REST_Request $request){
407 $step_id = $request['step_id'];
408 $all_settings = get_post_meta($step_id, 'order-bump-settings', true) ? get_post_meta($step_id, 'order-bump-settings', true) : array();
409 $is_multiple = Wpfnl_functions::check_array_is_multidimentional( $all_settings );
410
411 if( !$is_multiple && $all_settings ){
412 $all_settings['name'] = 'Order bump';
413 $all_settings = Wpfnl_functions::migrate_order_bump( $all_settings , $step_id );
414 }
415
416 if( is_array( $all_settings ) && count($all_settings) > 0 ) {
417 $funnel_id = get_post_meta($step_id,'_funnel_id',true);
418 $type = get_post_meta($funnel_id,'_wpfnl_funnel_type',true);
419 $type = !$type ? 'wc' : $type;
420 $class_object = Wpfnl_Controller_Type_Factory::build($type);
421 if( $class_object ){
422 $all_settings = $class_object->get_ob_settings( $all_settings );
423 }
424 }
425
426 foreach($all_settings as $key=>$settings){
427 if( !isset($settings['obTitleColor']) || !$settings['obTitleColor'] ){
428 $all_settings[$key]['obTitleColor'] = '#363B4E';
429 }
430 if( !isset($settings['obHighlightColor']) || !$settings['obHighlightColor'] ){
431 $all_settings[$key]['obHighlightColor'] = '#6E42D3';
432 }
433 if( !isset($settings['obCheckboxTitleColor']) || !$settings['obCheckboxTitleColor'] ){
434 $all_settings[$key]['obCheckboxTitleColor'] = '#d9d9d9';
435 }
436 if( !isset($settings['obDescriptionColor']) || !$settings['obDescriptionColor'] ){
437 $all_settings[$key]['obDescriptionColor'] = '#7A8B9A';
438 }
439 if(!isset($settings['obChooseVariantColor']) ||!$settings['obChooseVariantColor'] ){
440 $all_settings[$key]['obChooseVariantColor'] = '#F34D01';
441 }
442 if(!isset($settings['chooseVariantName']) ||!$settings['chooseVariantName'] ){
443 $all_settings[$key]['chooseVariantName'] = 'Choose an Option';
444 }
445
446 if( !isset($settings['replaceSettings']) || !$settings['replaceSettings'] ){
447 $all_settings[$key]['replaceSettings'] = [
448 'isAllReplace' => 'yes',
449 'selectedProducts' => [],
450 ];
451 }
452
453 if( !isset($settings['prePurchaseUpsell']) || !$settings['prePurchaseUpsell'] ){
454 $all_settings[$key]['prePurchaseUpsell'] = 'no';
455 }
456
457 if( !empty($settings['product']) ){
458 $product = wc_get_product($settings['product']);
459 if( $product ){
460 $sale_price = 0;
461 $regular_price = 0;
462
463 // For variable products, get prices from variation range
464 if( $product->is_type('variable') ){
465 $sale_price = $product->get_variation_sale_price('min');
466 $regular_price = $product->get_variation_regular_price('min');
467 } else {
468 $sale_price = $product->get_sale_price();
469 $regular_price = $product->get_regular_price();
470 }
471
472 $all_settings[$key]['numericSalePrice'] = wc_price(floatval($sale_price) * intval($settings['quantity']));
473 $all_settings[$key]['numericRegularPrice'] = wc_price(floatval($regular_price) * intval($settings['quantity']));
474 }
475 }
476 }
477 // Add global funnel status to response
478 $funnel_id = get_post_meta($step_id, '_funnel_id', true);
479 $is_global_funnel = get_post_meta($funnel_id, 'is_global_funnel', true);
480
481 $response['success'] = true;
482 $response['data'] = $all_settings;
483 $response['isEnableGbf'] = $is_global_funnel === 'yes' ? true : false;
484 $response['ob_position'] = $step_id ? Wpfnl_functions::supported_orderbump_position( $step_id ) : [];
485 return rest_ensure_response($response);
486
487 }
488
489
490
491 /**
492 * Update a single setting in a group.
493 *
494 * @param WP_REST_Request $request Request data.
495 *
496 * @return WP_Error|WP_REST_Response
497 * @since 3.0.0
498 */
499 public function update_settings( $request )
500 {
501
502
503 $all_settings_value = $request->get_params();
504 $response['success'] = false;
505 if (isset($all_settings_value['value'])) {
506 $all_settings = $all_settings_value['value'];
507
508 $funnel_id = get_post_meta($all_settings_value['stepID'],'_funnel_id',true);
509 $type = get_post_meta($funnel_id,'_wpfnl_funnel_type',true);
510 $type = !$type ? 'wc' : $type;
511 $class_object = Wpfnl_Controller_Type_Factory::build($type);
512 if( $class_object ){
513 $all_settings = $class_object->update_ob_settings( $all_settings );
514 }
515
516 // Validate quantity in all order bump settings
517 if (is_array($all_settings)) {
518 foreach ($all_settings as $key => $settings) {
519 if (isset($settings['quantity'])) {
520 $quantity = absint($settings['quantity']);
521 if ($quantity < 1) {
522 $quantity = 1;
523 }
524 $all_settings[$key]['quantity'] = $quantity;
525 }
526 }
527 }
528
529 $response = array();
530 $step_id = $all_settings_value['stepID'];
531 update_post_meta($step_id, 'order-bump-settings', $all_settings);
532 // update_post_meta($step_id, 'order-bump', $settings['isEnabled']);
533 do_action( 'wpfunnels/after_save_order_bump_data', $step_id, $all_settings );
534 $response['success'] = true;
535
536 do_action( 'wpfunnels_order_bump_added', absint( $funnel_id ), absint( $step_id ) );
537
538 }
539
540
541 return rest_ensure_response($response);
542 }
543
544
545 /**
546 * Calculate Discount Price.
547 *
548 * @param $discount_type
549 * @param $discount_value
550 * @param $product_price
551 * @param string $apply_to
552 *
553 * @return string
554 */
555 public function calculate_custom_price( $discount_type, $discount_value, $product_price, $apply_to = 'regular' )
556 {
557 $custom_price = $product_price;
558 if (!empty($discount_type)) {
559 if ('discount-percentage' === $discount_type) {
560 if ($discount_value > 0) {
561 $custom_price = $product_price - ( ( $product_price * $discount_value ) / 100);
562 }
563 } elseif ('discount-price' === $discount_type) {
564 if ($discount_value < $product_price) {
565 $custom_price = $product_price - $discount_value;
566 }else{
567 $custom_price = $product_price;
568 }
569 }
570 }
571
572 return number_format((float)$custom_price, 2);
573 }
574
575
576 /**
577 * Update orderbump status by index and step Id
578 *
579 * @param WP_REST_Request $request Request data.
580 *
581 * @return WP_Error|WP_REST_Response
582 */
583 public function update_status( $request ){
584 $payload = $request->get_params();
585 $response['success'] = false;
586 if( isset($payload['step_id'],$payload['index'],$payload['data']) ){
587 $step_id = $payload['step_id'];
588 $index = $payload['index'];
589 $data = $payload['data'];
590
591 if( $step_id && isset($settings[$index]['isEnabled']) ){
592 $settings = get_post_meta($step_id, 'order-bump-settings', true);
593 $settings[$index]['isEnabled'] = $data == 'true' ? true : false;
594 update_post_meta($step_id, 'order-bump-settings', $settings);
595 $response['success'] = true;
596 }
597
598 }
599 return rest_ensure_response($response);
600 }
601 }
602