| 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 |
'lms_provider' => array( |
| 89 |
'type' => 'string', |
| 90 |
'sanitize_callback' => 'sanitize_text_field', |
| 91 |
'default' => '', |
| 92 |
'validate_callback' => 'rest_validate_request_arg' |
| 93 |
) |
| 94 |
), |
| 95 |
'permission_callback' => array( |
| 96 |
$this, |
| 97 |
'get_items_permissions_check', |
| 98 |
), |
| 99 |
), |
| 100 |
) |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Add product row |
| 106 |
* |
| 107 |
* @param WP_REST_Request $payload Payload request. |
| 108 |
* |
| 109 |
* @return array |
| 110 |
* |
| 111 |
* @since 1.0.0 |
| 112 |
*/ |
| 113 |
public function add_product( $payload ) { |
| 114 |
if ( !$payload || !isset( $payload['id'] ) ) { |
| 115 |
return $this->prepare_wp_error_response( |
| 116 |
'product_add_error', |
| 117 |
__( 'Product Can\'t be added', 'wpfnl'), |
| 118 |
array( |
| 119 |
'success' => false, |
| 120 |
'status' => 400, |
| 121 |
'products' => array() |
| 122 |
) |
| 123 |
); |
| 124 |
} |
| 125 |
$product_id = $payload['id']; |
| 126 |
$step_id = isset ( $payload['step_id'] ) ? $payload['step_id'] : 0; |
| 127 |
$saved_products = get_post_meta( $step_id, '_wpfnl_checkout_products', true ); |
| 128 |
|
| 129 |
$_class = ( isset( $payload['isLms'] ) && 'true' === $payload['isLms'] ) ? 'lms' : 'wc'; |
| 130 |
|
| 131 |
$class_object = Wpfnl_Type_Factory::build( $_class ); |
| 132 |
if ( $class_object ) { |
| 133 |
$response = $class_object->save_items( $payload, $product_id, $saved_products ); |
| 134 |
if ( $response ) { |
| 135 |
return $response; |
| 136 |
} |
| 137 |
} |
| 138 |
return $this->prepare_wp_error_response( |
| 139 |
'product_add_error', |
| 140 |
__( 'Product Can\'t be added', 'wpfnl'), |
| 141 |
array( |
| 142 |
'success' => false, |
| 143 |
'status' => 400, |
| 144 |
'products' => array() |
| 145 |
) |
| 146 |
); |
| 147 |
} |
| 148 |
} |