| 1 |
<?php namespace TierPricingTable\Addons\RequestAQuote\API; |
| 2 |
|
| 3 |
use WP_REST_Controller; |
| 4 |
use WP_REST_Server; |
| 5 |
|
| 6 |
class FormsEndpoint extends WP_REST_Controller { |
| 7 |
|
| 8 |
protected $namespace = 'tier-pricing-table/v1'; |
| 9 |
protected $restBase = 'quote-forms'; |
| 10 |
|
| 11 |
public function register_routes() { |
| 12 |
register_rest_route( $this->namespace, '/' . $this->restBase, array( |
| 13 |
array( |
| 14 |
'methods' => WP_REST_Server::READABLE, |
| 15 |
'callback' => array( $this, 'get_items' ), |
| 16 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 17 |
), |
| 18 |
array( |
| 19 |
'methods' => WP_REST_Server::CREATABLE, |
| 20 |
'callback' => array( $this, 'create_item' ), |
| 21 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 22 |
), |
| 23 |
) ); |
| 24 |
} |
| 25 |
|
| 26 |
public function permissions_check( $request ) { |
| 27 |
return current_user_can( 'manage_woocommerce' ); |
| 28 |
} |
| 29 |
|
| 30 |
public function get_items( $request ) { |
| 31 |
$forms = get_option( 'tier_pricing_table_quote_forms', array() ); |
| 32 |
return rest_ensure_response( $forms ); |
| 33 |
} |
| 34 |
|
| 35 |
public function create_item( $request ) { |
| 36 |
$forms = $request->get_param( 'forms' ); |
| 37 |
|
| 38 |
if ( ! is_array( $forms ) ) { |
| 39 |
return new \WP_Error( 'invalid_data', 'Forms must be an array', array( 'status' => 400 ) ); |
| 40 |
} |
| 41 |
|
| 42 |
update_option( 'tier_pricing_table_quote_forms', $forms ); |
| 43 |
|
| 44 |
return rest_ensure_response( $forms ); |
| 45 |
} |
| 46 |
} |
| 47 |
|