| 1 |
<?php namespace TierPricingTable\Addons\RequestAQuote\API; |
| 2 |
|
| 3 |
use WP_REST_Controller; |
| 4 |
use WP_REST_Server; |
| 5 |
|
| 6 |
class SettingsEndpoint extends WP_REST_Controller { |
| 7 |
|
| 8 |
protected $namespace = 'tier-pricing-table/v1'; |
| 9 |
protected $restBase = 'quote-settings'; |
| 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 |
$settings = get_option( 'tier_pricing_table_quote_global_settings', array() ); |
| 32 |
return rest_ensure_response( $settings ); |
| 33 |
} |
| 34 |
|
| 35 |
public function create_item( $request ) { |
| 36 |
$settings = $request->get_param( 'settings' ); |
| 37 |
|
| 38 |
if ( ! is_array( $settings ) ) { |
| 39 |
return new \WP_Error( 'invalid_data', 'Settings must be an array', array( 'status' => 400 ) ); |
| 40 |
} |
| 41 |
|
| 42 |
// Ensure we always have an array |
| 43 |
$current_settings = get_option( 'tier_pricing_table_quote_global_settings', array() ); |
| 44 |
if ( ! is_array( $current_settings ) ) { |
| 45 |
$current_settings = array(); |
| 46 |
} |
| 47 |
|
| 48 |
$new_settings = array_merge( $current_settings, $settings ); |
| 49 |
update_option( 'tier_pricing_table_quote_global_settings', $new_settings ); |
| 50 |
|
| 51 |
return rest_ensure_response( $new_settings ); |
| 52 |
} |
| 53 |
} |
| 54 |
|