2016-03-30 17:29:57 +00:00
< ? php
/**
2017-02-11 14:51:13 +00:00
* REST API Settings controller
*
* Handles requests to the / settings endpoints .
2016-03-30 17:29:57 +00:00
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-03-30 17:29:57 +00:00
*/
2017-02-11 14:51:13 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* REST API Settings controller class .
*
* @ package WooCommerce / API
* @ extends WC_REST_Controller
*/
2016-07-26 21:33:55 +00:00
class WC_REST_Settings_Controller extends WC_REST_Controller {
2016-03-30 17:29:57 +00:00
2016-04-04 17:34:14 +00:00
/**
* WP REST API namespace / version .
*/
2017-02-09 17:06:13 +00:00
protected $namespace = 'wc/v2' ;
2016-04-04 17:34:14 +00:00
2016-06-16 20:47:56 +00:00
/**
* Route base .
*
* @ var string
*/
protected $rest_base = 'settings' ;
2016-03-30 17:29:57 +00:00
/**
* Register routes .
2016-07-19 18:24:05 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-03-30 17:29:57 +00:00
*/
public function register_routes () {
2016-04-04 17:34:14 +00:00
register_rest_route ( $this -> namespace , '/' . $this -> rest_base , array (
2016-03-30 17:29:57 +00:00
array (
'methods' => WP_REST_Server :: READABLE ,
2016-04-04 17:34:14 +00:00
'callback' => array ( $this , 'get_items' ),
2016-06-16 20:47:56 +00:00
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
2016-03-30 17:29:57 +00:00
),
2016-06-06 17:59:54 +00:00
'schema' => array ( $this , 'get_public_item_schema' ),
2016-03-30 17:29:57 +00:00
) );
}
/**
2016-04-04 17:34:14 +00:00
* Get all settings groups items .
2016-06-06 17:59:54 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-03-30 17:29:57 +00:00
* @ param WP_REST_Request $request
* @ return WP_Error | WP_REST_Response
*/
2016-04-04 17:34:14 +00:00
public function get_items ( $request ) {
2016-03-30 17:29:57 +00:00
$groups = apply_filters ( 'woocommerce_settings_groups' , array () );
if ( empty ( $groups ) ) {
return new WP_Error ( 'rest_setting_groups_empty' , __ ( 'No setting groups have been registered.' , 'woocommerce' ), array ( 'status' => 500 ) );
}
$defaults = $this -> group_defaults ();
$filtered_groups = array ();
foreach ( $groups as $group ) {
$sub_groups = array ();
foreach ( $groups as $_group ) {
if ( ! empty ( $_group [ 'parent_id' ] ) && $group [ 'id' ] === $_group [ 'parent_id' ] ) {
$sub_groups [] = $_group [ 'id' ];
}
}
$group [ 'sub_groups' ] = $sub_groups ;
$group = wp_parse_args ( $group , $defaults );
if ( ! is_null ( $group [ 'id' ] ) && ! is_null ( $group [ 'label' ] ) ) {
2016-06-08 16:34:44 +00:00
$group_obj = $this -> filter_group ( $group );
$group_data = $this -> prepare_item_for_response ( $group_obj , $request );
$group_data = $this -> prepare_response_for_collection ( $group_data );
$filtered_groups [] = $group_data ;
2016-03-30 17:29:57 +00:00
}
}
$response = rest_ensure_response ( $filtered_groups );
return $response ;
}
/**
2016-06-08 16:34:44 +00:00
* Prepare links for the request .
2016-06-06 17:59:54 +00:00
*
2016-06-08 16:34:44 +00:00
* @ param string $group_id Group ID .
* @ return array Links for the given group .
2016-03-30 17:29:57 +00:00
*/
2016-06-08 16:34:44 +00:00
protected function prepare_links ( $group_id ) {
$base = '/' . $this -> namespace . '/' . $this -> rest_base ;
$links = array (
'item' => array (
'href' => rest_url ( trailingslashit ( $base ) . $group_id ),
'embeddable' => true ,
),
);
return $links ;
2016-03-30 17:29:57 +00:00
}
/**
2016-06-06 17:59:54 +00:00
* Prepare a report sales object for serialization .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-06-08 16:34:44 +00:00
* @ param array $item Group object .
2016-06-06 17:59:54 +00:00
* @ param WP_REST_Request $request Request object .
* @ return WP_REST_Response $response Response data .
2016-03-30 17:29:57 +00:00
*/
2016-06-08 16:34:44 +00:00
public function prepare_item_for_response ( $item , $request ) {
$context = empty ( $request [ 'context' ] ) ? 'view' : $request [ 'context' ];
$data = $this -> add_additional_fields_to_object ( $item , $request );
$data = $this -> filter_response_by_context ( $data , $context );
2016-03-30 17:29:57 +00:00
2016-06-08 16:34:44 +00:00
$response = rest_ensure_response ( $data );
2016-03-30 17:29:57 +00:00
2016-06-08 16:34:44 +00:00
$response -> add_links ( $this -> prepare_links ( $item [ 'id' ] ) );
2016-03-30 17:29:57 +00:00
2016-06-08 16:34:44 +00:00
return $response ;
2016-03-30 17:29:57 +00:00
}
2016-06-06 17:59:54 +00:00
/**
* Filters out bad values from the groups array / filter so we
* only return known values via the API .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-06-06 17:59:54 +00:00
* @ param array $group
* @ return array
*/
public function filter_group ( $group ) {
return array_intersect_key (
$group ,
array_flip ( array_filter ( array_keys ( $group ), array ( $this , 'allowed_group_keys' ) ) )
);
}
/**
* Callback for allowed keys for each group response .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-06-06 17:59:54 +00:00
* @ param string $key Key to check
* @ return boolean
*/
public function allowed_group_keys ( $key ) {
return in_array ( $key , array ( 'id' , 'label' , 'description' , 'parent_id' , 'sub_groups' ) );
}
/**
* Returns default settings for groups . null means the field is required .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-06-06 17:59:54 +00:00
* @ return array
*/
protected function group_defaults () {
return array (
'id' => null ,
'label' => null ,
'description' => '' ,
'parent_id' => '' ,
'sub_groups' => array (),
);
}
2016-06-16 20:47:56 +00:00
/**
* Makes sure the current user has access to READ the settings APIs .
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-06-16 20:47:56 +00:00
* @ param WP_REST_Request $request Full data about the request .
* @ return WP_Error | boolean
*/
public function get_items_permissions_check ( $request ) {
if ( ! wc_rest_check_manager_permissions ( 'settings' , 'read' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot list resources.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
2016-03-30 17:29:57 +00:00
/**
* Get the groups schema , conforming to JSON Schema .
2016-06-06 17:59:54 +00:00
*
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-03-30 17:29:57 +00:00
* @ return array
*/
2016-06-06 17:59:54 +00:00
public function get_item_schema () {
2016-03-30 17:29:57 +00:00
$schema = array (
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
2016-09-07 22:05:45 +00:00
'title' => 'setting_group' ,
2016-03-30 17:29:57 +00:00
'type' => 'object' ,
'properties' => array (
'id' => array (
'description' => __ ( 'A unique identifier that can be used to link settings together.' , 'woocommerce' ),
'type' => 'string' ,
'arg_options' => array (
'sanitize_callback' => 'sanitize_title' ,
),
),
'label' => array (
2017-03-22 13:51:52 +00:00
'description' => __ ( 'A human readable label for the setting used in interfaces.' , 'woocommerce' ),
2016-03-30 17:29:57 +00:00
'type' => 'string' ,
'arg_options' => array (
'sanitize_callback' => 'sanitize_text_field' ,
),
),
'description' => array (
2017-03-22 13:51:52 +00:00
'description' => __ ( 'A human readable description for the setting used in interfaces.' , 'woocommerce' ),
2016-03-30 17:29:57 +00:00
'type' => 'string' ,
'arg_options' => array (
'sanitize_callback' => 'sanitize_text_field' ,
),
),
'parent_id' => array (
'description' => __ ( 'ID of parent grouping.' , 'woocommerce' ),
'type' => 'string' ,
'arg_options' => array (
'sanitize_callback' => 'sanitize_text_field' ,
),
),
'sub_groups' => array (
'description' => __ ( 'IDs for settings sub groups.' , 'woocommerce' ),
'type' => 'string' ,
'arg_options' => array (
'sanitize_callback' => 'sanitize_text_field' ,
),
),
),
);
return $this -> add_additional_fields_schema ( $schema );
}
}