2016-03-07 16:46:58 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST API Settings Controller.
|
|
|
|
* Handles requests to the /settings endpoints.
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category API
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @version 2.7.0
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
class WC_Rest_Settings_Controller extends WP_Rest_Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Route base.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rest_base = 'settings';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register routes.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function register_routes() {
|
|
|
|
register_rest_route( WC_API::REST_API_NAMESPACE, '/' . $this->rest_base . '/locations', array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( $this, 'get_locations' ),
|
|
|
|
'permission_callback' => array( $this, 'permissions_check' ),
|
|
|
|
'args' => $this->get_locations_params(),
|
|
|
|
),
|
2016-03-07 20:44:07 +00:00
|
|
|
'schema' => array( $this, 'get_location_schema' ),
|
2016-03-07 16:46:58 +00:00
|
|
|
) );
|
|
|
|
|
2016-03-07 20:44:07 +00:00
|
|
|
register_rest_route( WC_API::REST_API_NAMESPACE, '/' . $this->rest_base . '/locations/(?P<location>[\w-]+)', array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( $this, 'get_location' ),
|
|
|
|
'permission_callback' => array( $this, 'permissions_check' ),
|
|
|
|
),
|
|
|
|
'schema' => array( $this, 'get_location_schema' ),
|
|
|
|
) );
|
2016-03-07 16:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sure the current user has access to the settings APIs.
|
2016-03-07 20:44:07 +00:00
|
|
|
* @since 2.7.0
|
2016-03-07 16:46:58 +00:00
|
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
|
|
* @return WP_Error|boolean
|
|
|
|
*/
|
|
|
|
public function permissions_check( $request ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| /settings/locations
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Returns a list of "settings" locations so all settings for a particular page
|
|
|
|
| or location can be properly loaded.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all settings locations.
|
2016-03-07 20:44:07 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
* @param WP_REST_Request $request Full details about the request.
|
2016-03-07 16:46:58 +00:00
|
|
|
* @return WP_Error|WP_REST_Response
|
|
|
|
*/
|
|
|
|
public function get_locations( $request ) {
|
2016-03-07 18:24:03 +00:00
|
|
|
$locations = apply_filters( 'woocommerce_settings_locations', array() );
|
|
|
|
$defaults = array(
|
|
|
|
'id' => null,
|
|
|
|
'type' => 'page',
|
|
|
|
'label' => null,
|
|
|
|
'description' => '',
|
|
|
|
);
|
|
|
|
$filtered_locations = array();
|
|
|
|
foreach ( $locations as $location ) {
|
|
|
|
$location = wp_parse_args( $location, $defaults );
|
|
|
|
$location_valid = true;
|
|
|
|
if ( is_null( $location['id'] ) || is_null( $location['label'] ) || is_null( $location['type'] ) ) { // id, label, and type are required fields
|
|
|
|
$location_valid = false;
|
|
|
|
} else if ( ! empty( $request['type'] ) ) {
|
|
|
|
if ( in_array( $request['type'], $this->get_location_types() ) && $request['type'] !== $location['type'] ) {
|
|
|
|
$location_valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $location_valid ) {
|
|
|
|
$filtered_locations[] = array_intersect_key(
|
|
|
|
$location,
|
|
|
|
array_flip( array_filter( array_keys( $location ), array( $this, 'filter_location_keys' ) ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$response = rest_ensure_response( $filtered_locations );
|
2016-03-07 16:46:58 +00:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2016-03-07 20:44:07 +00:00
|
|
|
/**
|
|
|
|
* Return a single setting location.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
|
|
* @return WP_Error|WP_REST_Response
|
|
|
|
*/
|
|
|
|
public function get_location( $request ) {
|
|
|
|
$locations = apply_filters( 'woocommerce_settings_locations', array() );
|
|
|
|
if ( empty( $locations ) ) {
|
|
|
|
return new WP_Error( 'rest_setting_location_invalid_id', __( 'Invalid location id.' ), array( 'status' => 404 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$index_key = $this->get_array_key_from_location_id( $locations, $request['location'] );
|
|
|
|
if ( is_null( $index_key ) || empty( $locations[ $index_key ] ) ) {
|
|
|
|
return new WP_Error( 'rest_setting_location_invalid_id', __( 'Invalid location id.' ), array( 'status' => 404 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$location = $locations[ $index_key ];
|
|
|
|
if ( is_null( $location['id'] ) || is_null( $location['label'] ) || is_null( $location['type'] ) ) {
|
|
|
|
return new WP_Error( 'rest_setting_location_invalid_id', __( 'Invalid location id.' ), array( 'status' => 404 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$filtered_location = array_intersect_key(
|
|
|
|
$location,
|
|
|
|
array_flip( array_filter( array_keys( $location ), array( $this, 'filter_location_keys' ) ) )
|
|
|
|
);
|
|
|
|
|
|
|
|
return $filtered_location;
|
|
|
|
}
|
|
|
|
|
2016-03-07 18:24:03 +00:00
|
|
|
/**
|
|
|
|
* Callback for Allowed keys for each location response.
|
2016-03-07 20:44:07 +00:00
|
|
|
* @since 2.7.0
|
2016-03-07 18:24:03 +00:00
|
|
|
* @param string $key Key to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function filter_location_keys( $key ) {
|
|
|
|
return in_array( $key, array( 'id', 'type', 'label', 'description' ) );
|
|
|
|
}
|
|
|
|
|
2016-03-07 16:46:58 +00:00
|
|
|
/**
|
|
|
|
* Get supported query parameters for locations.
|
2016-03-07 20:44:07 +00:00
|
|
|
* @since 2.7.0
|
2016-03-07 16:46:58 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_locations_params() {
|
2016-03-07 18:24:03 +00:00
|
|
|
$query_params = array();
|
|
|
|
|
|
|
|
$query_params['type'] = array(
|
|
|
|
'description' => __( 'Limit result set to setting locations of a specific type.', 'woocommerce' ),
|
|
|
|
'type' => 'string'
|
|
|
|
);
|
|
|
|
|
2016-03-07 16:46:58 +00:00
|
|
|
return $query_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-07 18:24:03 +00:00
|
|
|
* Get the locations chema, conforming to JSON Schema.
|
2016-03-07 20:44:07 +00:00
|
|
|
* @since 2.7.0
|
2016-03-07 16:46:58 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2016-03-07 20:44:07 +00:00
|
|
|
public function get_location_schema() {
|
|
|
|
$schema = array(
|
2016-03-07 18:24:03 +00:00
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
2016-03-07 20:44:07 +00:00
|
|
|
'title' => 'settings-locations',
|
2016-03-07 18:24:03 +00:00
|
|
|
'type' => 'object',
|
2016-03-07 20:44:07 +00:00
|
|
|
'properties' => array(
|
|
|
|
'id' => array(
|
|
|
|
'description' => __( 'A unique identifier that can be used to link settings together.' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_title',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'type' => array(
|
|
|
|
'description' => __( 'Context for where the settings in this location are going to be displayed.' ),
|
|
|
|
'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.' ),
|
|
|
|
'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.' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'arg_options' => array(
|
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->add_additional_fields_schema( $schema );
|
2016-03-07 18:24:03 +00:00
|
|
|
}
|
2016-03-07 16:46:58 +00:00
|
|
|
|
2016-03-07 18:24:03 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of allowed setting location types.
|
2016-03-07 20:44:07 +00:00
|
|
|
* @todo move this?
|
|
|
|
* @since 2.7.0
|
2016-03-07 18:24:03 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_location_types() {
|
|
|
|
return apply_filters( 'woocommerce_settings_location_types', array( 'page', 'metabox', 'shipping-zone' ) );
|
2016-03-07 16:46:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 20:44:07 +00:00
|
|
|
/**
|
|
|
|
* Returns the array key for a specific location ID so it can be pulled out of the 'locations' array.
|
|
|
|
* @todo move this?
|
|
|
|
* @param array $locations woocommerce_settings_locations
|
|
|
|
* @param string $id Location ID to get an array key index for
|
|
|
|
* @return integer|null
|
|
|
|
*/
|
|
|
|
protected function get_array_key_from_location_id( $locations, $id ) {
|
|
|
|
foreach ( $locations as $key => $location ) {
|
|
|
|
if ( $id === $location['id'] ) {
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-07 16:46:58 +00:00
|
|
|
}
|