From fc076eb57e7b52993430405814b26a20d582797d Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Mon, 7 Mar 2016 08:46:58 -0800 Subject: [PATCH] Initial /settings API controller and /settings/locations route. Loads the WP-API and registers our namespace temporarily until WP-API is loaded by WC core. --- includes/api/wc-rest-settings-controller.php | 90 +++++++++ includes/class-wc-api.php | 3 + includes/vendor/wp-api-functions.php | 199 +++++++++++++++++++ tests/unit-tests/api/settings.php | 11 + 4 files changed, 303 insertions(+) create mode 100644 includes/api/wc-rest-settings-controller.php create mode 100644 includes/vendor/wp-api-functions.php create mode 100644 tests/unit-tests/api/settings.php diff --git a/includes/api/wc-rest-settings-controller.php b/includes/api/wc-rest-settings-controller.php new file mode 100644 index 00000000000..c9a70867875 --- /dev/null +++ b/includes/api/wc-rest-settings-controller.php @@ -0,0 +1,90 @@ +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(), + ), + 'schema' => array( $this, 'get_locations_schema' ), + ) ); + + } + + /** + * Makes sure the current user has access to the settings APIs. + * @since 2.7.0 + * @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. + * @since 2.7.0 + * @param WP_REST_Request $request Full details about the request. + * @return WP_Error|WP_REST_Response + */ + public function get_locations( $request ) { + $response = rest_ensure_response( array() ); + return $response; + } + + /** + * Get supported query parameters for locations. + * @since 2.7.0 + * @return array + */ + public function get_locations_params() { + $query_params = parent::get_collection_params(); + $query_params['context']['default'] = 'view'; + return $query_params; + } + + /** + * Get the locations chema, conforming to JSON Schema + * @since 2.7.0 + * @return array + */ + public function get_locations_schema() { + + } + +} diff --git a/includes/class-wc-api.php b/includes/class-wc-api.php index 59af72ee530..81d21796a86 100644 --- a/includes/class-wc-api.php +++ b/includes/class-wc-api.php @@ -144,6 +144,7 @@ class WC_API extends WC_Legacy_API { include_once( 'abstracts/abstract-wc-rest-controller.php' ); include_once( 'abstracts/abstract-wc-rest-posts-controller.php' ); include_once( 'abstracts/abstract-wc-rest-terms-controller.php' ); + include_once( 'abstracts/abstract-wc-settings-api.php' ); // REST API controllers. include_once( 'api/class-wc-rest-coupons-controller.php' ); @@ -162,6 +163,7 @@ class WC_API extends WC_Legacy_API { include_once( 'api/class-wc-rest-report-sales-controller.php' ); include_once( 'api/class-wc-rest-report-top-sellers-controller.php' ); include_once( 'api/class-wc-rest-reports-controller.php' ); + include_once( 'api/wc-rest-settings-controller.php' ); include_once( 'api/class-wc-rest-tax-classes-controller.php' ); include_once( 'api/class-wc-rest-taxes-controller.php' ); include_once( 'api/class-wc-rest-webhook-deliveries.php' ); @@ -190,6 +192,7 @@ class WC_API extends WC_Legacy_API { 'WC_REST_Report_Sales_Controller', 'WC_REST_Report_Top_Sellers_Controller', 'WC_REST_Reports_Controller', + 'WC_Rest_Settings_Controller', 'WC_REST_Tax_Classes_Controller', 'WC_REST_Taxes_Controller', 'WC_REST_Webhook_Deliveries_Controller', diff --git a/includes/vendor/wp-api-functions.php b/includes/vendor/wp-api-functions.php new file mode 100644 index 00000000000..90c0713a229 --- /dev/null +++ b/includes/vendor/wp-api-functions.php @@ -0,0 +1,199 @@ + null, + 'update_callback' => null, + 'schema' => null, + ); + + $args = wp_parse_args( $args, $defaults ); + + global $wp_rest_additional_fields; + + $object_types = (array) $object_type; + + foreach ( $object_types as $object_type ) { + $wp_rest_additional_fields[ $object_type ][ $attribute ] = $args; + } + } +} + +if ( ! function_exists( 'register_api_field' ) ) { + /** + * Backwards compat shim + */ + function register_api_field( $object_type, $attributes, $args = array() ) { + _deprecated_function( 'register_api_field', 'WPAPI-2.0', 'register_rest_field' ); + register_rest_field( $object_type, $attributes, $args ); + } +} + +if ( ! function_exists( 'rest_validate_request_arg' ) ) { + /** + * Validate a request argument based on details registered to the route. + * + * @param mixed $value + * @param WP_REST_Request $request + * @param string $param + * @return WP_Error|boolean + */ + function rest_validate_request_arg( $value, $request, $param ) { + + $attributes = $request->get_attributes(); + if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { + return true; + } + $args = $attributes['args'][ $param ]; + + if ( ! empty( $args['enum'] ) ) { + if ( ! in_array( $value, $args['enum'] ) ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not one of %s' ), $param, implode( ', ', $args['enum'] ) ) ); + } + } + + if ( 'integer' === $args['type'] && ! is_numeric( $value ) ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s' ), $param, 'integer' ) ); + } + + if ( 'string' === $args['type'] && ! is_string( $value ) ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s' ), $param, 'string' ) ); + } + + if ( isset( $args['format'] ) ) { + switch ( $args['format'] ) { + case 'date-time' : + if ( ! rest_parse_date( $value ) ) { + return new WP_Error( 'rest_invalid_date', __( 'The date you provided is invalid.' ) ); + } + break; + + case 'email' : + if ( ! is_email( $value ) ) { + return new WP_Error( 'rest_invalid_email', __( 'The email address you provided is invalid.' ) ); + } + break; + } + } + + if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) { + if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) { + if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (exclusive)' ), $param, $args['minimum'] ) ); + } else if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (inclusive)' ), $param, $args['minimum'] ) ); + } + } else if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) { + if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (exclusive)' ), $param, $args['maximum'] ) ); + } else if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (inclusive)' ), $param, $args['maximum'] ) ); + } + } else if ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) { + if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { + if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); + } + } else if ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { + if ( $value >= $args['maximum'] || $value < $args['minimum'] ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); + } + } else if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { + if ( $value > $args['maximum'] || $value <= $args['minimum'] ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); + } + } else if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { + if ( $value > $args['maximum'] || $value < $args['minimum'] ) { + return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) ); + } + } + } + } + + return true; + } +} + +if ( ! function_exists( 'rest_sanitize_request_arg' ) ) { + /** + * Sanitize a request argument based on details registered to the route. + * + * @param mixed $value + * @param WP_REST_Request $request + * @param string $param + * @return mixed + */ + function rest_sanitize_request_arg( $value, $request, $param ) { + + $attributes = $request->get_attributes(); + if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { + return $value; + } + $args = $attributes['args'][ $param ]; + + if ( 'integer' === $args['type'] ) { + return (int) $value; + } + + if ( isset( $args['format'] ) ) { + switch ( $args['format'] ) { + case 'date-time' : + return sanitize_text_field( $value ); + + case 'email' : + /* + * sanitize_email() validates, which would be unexpected + */ + return sanitize_text_field( $value ); + + case 'uri' : + return esc_url_raw( $value ); + } + } + + return $value; + } + +} diff --git a/tests/unit-tests/api/settings.php b/tests/unit-tests/api/settings.php new file mode 100644 index 00000000000..86c44b50e74 --- /dev/null +++ b/tests/unit-tests/api/settings.php @@ -0,0 +1,11 @@ +