2016-03-07 16:46:58 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST API Settings Controller.
|
2016-03-30 17:29:57 +00:00
|
|
|
* Handles requests to the /settings/$group/$setting endpoints.
|
2016-03-07 16:46:58 +00:00
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category API
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @version 2.7.0
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
2016-03-30 17:29:57 +00:00
|
|
|
class WC_Rest_Settings_Controller extends WP_Rest_Settings_Base {
|
2016-03-07 16:46:58 +00:00
|
|
|
|
2016-04-04 17:34:14 +00:00
|
|
|
/**
|
|
|
|
* WP REST API namespace/version.
|
|
|
|
*/
|
|
|
|
protected $namespace = 'wc/v1';
|
|
|
|
|
2016-03-07 16:46:58 +00:00
|
|
|
/**
|
|
|
|
* Route base.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rest_base = 'settings';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register routes.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function register_routes() {
|
2016-04-04 17:34:14 +00:00
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<group>[\w-]+)/(?P<setting>[\w-]+)', array(
|
2016-03-23 19:36:59 +00:00
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
2016-04-04 17:34:14 +00:00
|
|
|
'callback' => array( $this, 'get_item' ),
|
2016-03-23 19:36:59 +00:00
|
|
|
'permission_callback' => array( $this, 'permissions_check' ),
|
|
|
|
),
|
2016-03-24 21:01:22 +00:00
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::EDITABLE,
|
2016-04-04 19:09:32 +00:00
|
|
|
'callback' => array( $this, 'update_item' ),
|
2016-03-24 21:01:22 +00:00
|
|
|
'permission_callback' => array( $this, 'permissions_check' ),
|
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
|
|
|
|
),
|
2016-03-23 19:36:59 +00:00
|
|
|
'schema' => array( $this, 'setting_schema' ),
|
2016-03-07 20:44:07 +00:00
|
|
|
) );
|
2016-03-07 16:46:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 21:01:22 +00:00
|
|
|
/**
|
|
|
|
* Return a single setting.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param WP_REST_Request $request
|
|
|
|
* @return WP_Error|WP_REST_Response
|
|
|
|
*/
|
2016-04-04 17:34:14 +00:00
|
|
|
public function get_item( $request ) {
|
2016-03-24 21:01:22 +00:00
|
|
|
$setting = $this->_get_setting_from_request( $request );
|
|
|
|
if ( is_wp_error( $setting ) ) {
|
|
|
|
return $setting;
|
2016-03-07 20:44:07 +00:00
|
|
|
}
|
2016-03-24 21:01:22 +00:00
|
|
|
return rest_ensure_response( $setting );
|
|
|
|
}
|
2016-03-07 20:44:07 +00:00
|
|
|
|
2016-03-24 21:01:22 +00:00
|
|
|
/**
|
|
|
|
* Update a single setting.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param WP_REST_Request $request
|
|
|
|
* @return WP_Error|WP_REST_Response
|
|
|
|
*/
|
2016-04-04 19:09:32 +00:00
|
|
|
public function update_item( $request ) {
|
2016-03-24 21:01:22 +00:00
|
|
|
$setting = $this->_get_setting_from_request( $request );
|
|
|
|
if ( is_wp_error( $setting ) ) {
|
|
|
|
return $setting;
|
2016-03-07 20:44:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 21:01:22 +00:00
|
|
|
$value = $setting['value'] = $this->sanitize_setting_value( $setting, $request['value'] );
|
|
|
|
update_option( $setting['id'], $value );
|
|
|
|
|
|
|
|
return rest_ensure_response( $setting );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a valid request and returns back the corresponding setting array.
|
|
|
|
* @since 2.7.0
|
2016-03-23 19:36:59 +00:00
|
|
|
* @param WP_REST_Request $request
|
2016-03-24 21:01:22 +00:00
|
|
|
* @return WP_Error|array
|
2016-03-07 18:24:03 +00:00
|
|
|
*/
|
2016-03-24 21:01:22 +00:00
|
|
|
private function _get_setting_from_request( $request ) {
|
2016-03-23 19:36:59 +00:00
|
|
|
if ( empty( $request['group'] ) || empty( $request['setting'] ) ) {
|
2016-03-24 21:01:22 +00:00
|
|
|
return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'woocommerce' ), array( 'status' => 404 ) );
|
2016-03-23 19:36:59 +00:00
|
|
|
}
|
2016-03-07 18:24:03 +00:00
|
|
|
|
2016-03-28 19:04:26 +00:00
|
|
|
$settings = apply_filters( 'woocommerce_settings-' . $request['group'], array() );
|
2016-03-23 19:36:59 +00:00
|
|
|
$array_key = array_keys( wp_list_pluck( $settings, 'id' ), $request['setting'] );
|
2016-03-21 21:45:08 +00:00
|
|
|
|
2016-03-23 19:36:59 +00:00
|
|
|
if ( empty( $array_key ) ) {
|
2016-03-24 21:01:22 +00:00
|
|
|
return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'woocommerce' ), array( 'status' => 404 ) );
|
2016-03-23 19:36:59 +00:00
|
|
|
}
|
2016-03-07 18:24:03 +00:00
|
|
|
|
2016-03-23 19:36:59 +00:00
|
|
|
$setting = $this->filter_setting( $settings[ $array_key[0] ] );
|
|
|
|
$setting['value'] = $this->get_value( $setting['id'] );
|
|
|
|
|
2016-03-30 17:29:57 +00:00
|
|
|
if ( ! $this->is_setting_type_valid( $setting['type'] ) ) {
|
2016-03-24 21:01:22 +00:00
|
|
|
return new WP_Error( 'rest_setting_setting_invalid', __( 'Invalid setting.', 'woocommerce' ), array( 'status' => 404 ) );
|
2016-03-23 19:36:59 +00:00
|
|
|
}
|
2016-03-07 18:24:03 +00:00
|
|
|
|
2016-03-24 21:01:22 +00:00
|
|
|
return $setting;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the settings schema, conforming to JSON Schema.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function setting_schema() {
|
|
|
|
$schema = array(
|
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
|
|
|
'title' => 'settings',
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
|
|
|
'description' => __( 'A unique identifier for the setting.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_title',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'label' => array(
|
|
|
|
'description' => __( 'A human readable label. This is a translated string that can be used in interfaces.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'description' => array(
|
|
|
|
'description' => __( 'A human readable description. This is a translated string that can be used in interfaces.', 'woocommerce' ),
|
2016-03-23 19:36:59 +00:00
|
|
|
'type' => 'string',
|
2016-03-24 21:01:22 +00:00
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'default' => array(
|
|
|
|
'description' => __( 'Default value for the setting.', 'woocommerce' ),
|
|
|
|
'type' => 'mixed',
|
|
|
|
),
|
|
|
|
'tip' => array(
|
|
|
|
'description' => __( 'Extra help text explaining the setting.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'placeholder' => array(
|
|
|
|
'description' => __( 'Placeholder text to be displayed in text inputs.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'arg_options' => array(
|
2016-03-23 19:36:59 +00:00
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
),
|
|
|
|
),
|
2016-03-24 21:01:22 +00:00
|
|
|
'type' => array(
|
|
|
|
'description' => __( 'Type of setting. Allowed values: text, email, number, color, password, textarea, select, multiselect, radio, image_width, checkbox.', 'woocommerce' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'options' => array(
|
|
|
|
'description' => __( 'Array of options (key value pairs) for inputs such as select, multiselect, and radio buttons.', 'woocommerce' ),
|
|
|
|
'type' => 'array',
|
|
|
|
),
|
2016-03-07 20:44:07 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->add_additional_fields_schema( $schema );
|
2016-03-07 18:24:03 +00:00
|
|
|
}
|
2016-03-07 16:46:58 +00:00
|
|
|
|
|
|
|
}
|