PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 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 All 261 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 2.8.16, at includes/core/rest-api/Controllers/class-orderbump-controller.php

548 lines 18.0 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 {
408
409 $step_id = $request['step_id'];
410 $all_settings = get_post_meta($step_id, 'order-bump-settings', true) ? get_post_meta($step_id, 'order-bump-settings', true) : array();
411 $is_multiple = Wpfnl_functions::check_array_is_multidimentional( $all_settings );
412
413 if( !$is_multiple && $all_settings ){
414 $all_settings['name'] = 'Order bump';
415 $all_settings = Wpfnl_functions::migrate_order_bump( $all_settings , $step_id );
416 }
417
418 if( is_array( $all_settings ) && count($all_settings) > 0 ) {
419 $funnel_id = get_post_meta($step_id,'_funnel_id',true);
420 $type = get_post_meta($funnel_id,'_wpfnl_funnel_type',true);
421 $type = !$type ? 'wc' : $type;
422 $class_object = Wpfnl_Controller_Type_Factory::build($type);
423 if( $class_object ){
424 $all_settings = $class_object->get_ob_settings( $all_settings );
425 }
426 }
427
428 foreach($all_settings as $key=>$settings){
429 if( !isset($settings['obTitleColor']) || !$settings['obTitleColor'] ){
430 $all_settings[$key]['obTitleColor'] = '#363B4E';
431 }
432 if( !isset($settings['obHighlightColor']) || !$settings['obHighlightColor'] ){
433 $all_settings[$key]['obHighlightColor'] = '#6E42D3';
434 }
435 if( !isset($settings['obCheckboxTitleColor']) || !$settings['obCheckboxTitleColor'] ){
436 $all_settings[$key]['obCheckboxTitleColor'] = '#d9d9d9';
437 }
438 if( !isset($settings['obDescriptionColor']) || !$settings['obDescriptionColor'] ){
439 $all_settings[$key]['obDescriptionColor'] = '#7A8B9A';
440 }
441 }
442
443 $response['success'] = true;
444 $response['data'] = $all_settings;
445 $response['ob_position'] = $step_id ? Wpfnl_functions::supported_orderbump_position( $step_id ) : [];
446
447 return rest_ensure_response($response);
448
449 }
450
451
452
453 /**
454 * Update a single setting in a group.
455 *
456 * @param WP_REST_Request $request Request data.
457 *
458 * @return WP_Error|WP_REST_Response
459 * @since 3.0.0
460 */
461 public function update_settings( $request )
462 {
463
464
465 $all_settings_value = $request->get_params();
466 $response['success'] = false;
467 if (isset($all_settings_value['value'])) {
468 $all_settings = $all_settings_value['value'];
469
470 $funnel_id = get_post_meta($all_settings_value['stepID'],'_funnel_id',true);
471 $type = get_post_meta($funnel_id,'_wpfnl_funnel_type',true);
472 $type = !$type ? 'wc' : $type;
473 $class_object = Wpfnl_Controller_Type_Factory::build($type);
474 if( $class_object ){
475 $all_settings = $class_object->update_ob_settings( $all_settings );
476 }
477 $response = array();
478 $step_id = $all_settings_value['stepID'];
479 update_post_meta($step_id, 'order-bump-settings', $all_settings);
480 // update_post_meta($step_id, 'order-bump', $settings['isEnabled']);
481 do_action( 'wpfunnels/after_save_order_bump_data', $step_id, $all_settings );
482 $response['success'] = true;
483
484 }
485
486
487 return rest_ensure_response($response);
488 }
489
490
491 /**
492 * Calculate Discount Price.
493 *
494 * @param $discount_type
495 * @param $discount_value
496 * @param $product_price
497 * @param string $apply_to
498 *
499 * @return string
500 */
501 public function calculate_custom_price( $discount_type, $discount_value, $product_price, $apply_to = 'regular' )
502 {
503 $custom_price = $product_price;
504 if (!empty($discount_type)) {
505 if ('discount-percentage' === $discount_type) {
506 if ($discount_value > 0) {
507 $custom_price = $product_price - ( ( $product_price * $discount_value ) / 100);
508 }
509 } elseif ('discount-price' === $discount_type) {
510 if ($discount_value < $product_price) {
511 $custom_price = $product_price - $discount_value;
512 }else{
513 $custom_price = $product_price;
514 }
515 }
516 }
517
518 return number_format((float)$custom_price, 2);
519 }
520
521
522 /**
523 * Update orderbump status by index and step Id
524 *
525 * @param WP_REST_Request $request Request data.
526 *
527 * @return WP_Error|WP_REST_Response
528 */
529 public function update_status( $request ){
530 $payload = $request->get_params();
531 $response['success'] = false;
532 if( isset($payload['step_id'],$payload['index'],$payload['data']) ){
533 $step_id = $payload['step_id'];
534 $index = $payload['index'];
535 $data = $payload['data'];
536
537 if( $step_id && isset($settings[$index]['isEnabled']) ){
538 $settings = get_post_meta($step_id, 'order-bump-settings', true);
539 $settings[$index]['isEnabled'] = $data == 'true' ? true : false;
540 update_post_meta($step_id, 'order-bump-settings', $settings);
541 $response['success'] = true;
542 }
543
544 }
545 return rest_ensure_response($response);
546 }
547 }
548