144 lines
3.3 KiB
PHP
144 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* REST API Options Controller
|
|
*
|
|
* Handles requests to get and update options in the wp_options table.
|
|
*
|
|
* @package WooCommerce Admin/API
|
|
*/
|
|
|
|
namespace Automattic\WooCommerce\Admin\API;
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
/**
|
|
* Options Controller.
|
|
*
|
|
* @package WooCommerce Admin/API
|
|
* @extends WC_REST_Data_Controller
|
|
*/
|
|
class Options extends \WC_REST_Data_Controller {
|
|
/**
|
|
* Endpoint namespace.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $namespace = 'wc-admin/v1';
|
|
|
|
/**
|
|
* Route base.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $rest_base = 'options';
|
|
|
|
/**
|
|
* Register routes.
|
|
*/
|
|
public function register_routes() {
|
|
register_rest_route(
|
|
$this->namespace,
|
|
'/' . $this->rest_base,
|
|
array(
|
|
array(
|
|
'methods' => \WP_REST_Server::READABLE,
|
|
'callback' => array( $this, 'get_options' ),
|
|
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
|
),
|
|
'schema' => array( $this, 'get_item_schema' ),
|
|
)
|
|
);
|
|
|
|
register_rest_route(
|
|
$this->namespace,
|
|
'/' . $this->rest_base,
|
|
array(
|
|
array(
|
|
'methods' => \WP_REST_Server::EDITABLE,
|
|
'callback' => array( $this, 'update_options' ),
|
|
'permission_callback' => array( $this, 'update_item_permissions_check' ),
|
|
),
|
|
'schema' => array( $this, 'get_item_schema' ),
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Check if a given request has access to manage options.
|
|
*
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
* @return WP_Error|boolean
|
|
*/
|
|
public function update_item_permissions_check( $request ) {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
return new \WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot manage options.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Gets an array of options and respective values.
|
|
*
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
* @return array Options object with option values.
|
|
*/
|
|
public function get_options( $request ) {
|
|
$params = explode( ',', $request[ 'options' ] );
|
|
$options = array();
|
|
|
|
if ( ! is_array( $params ) ) {
|
|
return array();
|
|
}
|
|
|
|
foreach ( $params as $option ) {
|
|
$options[ $option ] = get_option( $option );
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* Updates an array of objects.
|
|
*
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
* @return array Options object with a boolean if the option was updated.
|
|
*/
|
|
public function update_options( $request ) {
|
|
$params = $request->get_json_params();
|
|
$updated = array();
|
|
|
|
if ( ! is_array( $params ) ) {
|
|
return array();
|
|
}
|
|
|
|
foreach ( $params as $key => $value ) {
|
|
$updated[ $key ] = update_option( $key, $value );
|
|
}
|
|
|
|
return $updated;
|
|
}
|
|
|
|
/**
|
|
* Get the schema, conforming to JSON Schema.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get_item_schema() {
|
|
$schema = array(
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
|
'title' => 'options',
|
|
'type' => 'object',
|
|
'properties' => array(
|
|
'options' => array(
|
|
'type' => 'array',
|
|
'description' => __( 'Array of options with associated values.', 'woocommerce-admin' ),
|
|
'context' => array( 'view' ),
|
|
'readonly' => true,
|
|
),
|
|
),
|
|
);
|
|
|
|
return $this->add_additional_fields_schema( $schema );
|
|
}
|
|
}
|