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-checkout-controller.php

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

142 lines 3.5 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 * @since 2.7.9
7 */
8
9 namespace WPFunnels\Rest\Controllers;
10
11 use WP_Error;
12 use WP_REST_Request;
13 use Wpfnl_Type_Factory;
14 use WPFunnels\Wpfnl_functions;
15
16
17 /**
18 * This class has the functions which are responsible
19 * to control the checkout step
20 *
21 * @package /includes/core/rest-api/Controllers
22 * @since 2.7.9
23 */
24 class CheckoutController extends Wpfnl_REST_Controller {
25 /**
26 * Endpoint namespace.
27 *
28 * @var string
29 */
30 protected $namespace = 'wpfunnels/v1';
31
32 /**
33 * Route base.
34 *
35 * @var string
36 */
37 protected $rest_base = 'checkout';
38
39 /**
40 * Makes sure the current user has access to READ the settings APIs.
41 *
42 *
43 * @return WP_Error|boolean
44 * @since 2.7.9
45 */
46 public function get_items_permissions_check( $request ) {
47 if ( !Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'settings' ) ) {
48 return new WP_Error( 'wpfunnels_rest_cannot_edit', __( 'Sorry, you cannot list resources.', 'wpfnl' ), array( 'status' => rest_authorization_required_code() ) );
49 }
50 return true;
51 }
52
53 /**
54 * Register rest routes
55 *
56 * @since 2.7.9
57 */
58 public function register_routes() {
59 register_rest_route(
60 $this->namespace,
61 '/' . $this->rest_base . '/wpfnl-add-product',
62 array(
63 array(
64 'methods' => \WP_REST_Server::EDITABLE,
65 'callback' => array(
66 $this,
67 'add_product',
68 ),
69 'args' => array(
70 'id' => array(
71 'required' => true,
72 'type' => array('integer', 'string'),
73 'sanitize_callback' => 'absint',
74 'validate_callback' => 'rest_validate_request_arg'
75 ),
76 'step_id' => array(
77 'default' => 0,
78 'type' => array('integer', 'string'),
79 'sanitize_callback' => 'absint',
80 'validate_callback' => 'rest_validate_request_arg'
81 ),
82 'isLms' => array(
83 'type' => 'string',
84 'sanitize_callback' => 'sanitize_text_field',
85 'default' => 'wc',
86 'validate_callback' => 'rest_validate_request_arg'
87 )
88 ),
89 'permission_callback' => array(
90 $this,
91 'get_items_permissions_check',
92 ),
93 ),
94 )
95 );
96 }
97
98 /**
99 * Add product row
100 *
101 * @param WP_REST_Request $payload Payload request.
102 *
103 * @return array
104 *
105 * @since 1.0.0
106 */
107 public function add_product( $payload ) {
108 if ( !$payload || !isset( $payload['id'] ) ) {
109 return $this->prepare_wp_error_response(
110 'product_add_error',
111 __( 'Product Can\'t be added', 'wpfnl'),
112 array(
113 'success' => false,
114 'status' => 400,
115 'products' => array()
116 )
117 );
118 }
119 $product_id = $payload['id'];
120 $step_id = isset ( $payload['step_id'] ) ? $payload['step_id'] : 0;
121 $saved_products = get_post_meta( $step_id, '_wpfnl_checkout_products', true );
122
123 $_class = ( isset( $payload['isLms'] ) && 'true' === $payload['isLms'] ) ? 'lms' : 'wc';
124
125 $class_object = Wpfnl_Type_Factory::build( $_class );
126 if ( $class_object ) {
127 $response = $class_object->save_items( $payload, $product_id, $saved_products );
128 if ( $response ) {
129 return $response;
130 }
131 }
132 return $this->prepare_wp_error_response(
133 'product_add_error',
134 __( 'Product Can\'t be added', 'wpfnl'),
135 array(
136 'success' => false,
137 'status' => 400,
138 'products' => array()
139 )
140 );
141 }
142 }