/wp-content/plugins/woocommerce/includes/vendor/wp-rest-functions.php

https://gitlab.com/hunt9310/ras · PHP · 275 lines · 143 code · 34 blank · 98 comment · 67 complexity · f2e38b3a7d1e4ff0e9fa7021bb634556 MD5 · raw file

  1. <?php
  2. /**
  3. * @version 2.0-beta13.1
  4. */
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * core-integration.php
  10. */
  11. if ( ! function_exists( 'wp_parse_slug_list' ) ) {
  12. /**
  13. * Clean up an array, comma- or space-separated list of slugs.
  14. *
  15. * @since
  16. *
  17. * @param array|string $list List of slugs.
  18. * @return array Sanitized array of slugs.
  19. */
  20. function wp_parse_slug_list( $list ) {
  21. if ( ! is_array( $list ) ) {
  22. $list = preg_split( '/[\s,]+/', $list );
  23. }
  24. foreach ( $list as $key => $value ) {
  25. $list[ $key ] = sanitize_title( $value );
  26. }
  27. return array_unique( $list );
  28. }
  29. }
  30. if ( ! function_exists( 'rest_get_server' ) ) {
  31. /**
  32. * Retrieves the current REST server instance.
  33. *
  34. * Instantiates a new instance if none exists already.
  35. *
  36. * @since 4.5.0
  37. *
  38. * @global WP_REST_Server $wp_rest_server REST server instance.
  39. *
  40. * @return WP_REST_Server REST server instance.
  41. */
  42. function rest_get_server() {
  43. /* @var WP_REST_Server $wp_rest_server */
  44. global $wp_rest_server;
  45. if ( empty( $wp_rest_server ) ) {
  46. /**
  47. * Filter the REST Server Class.
  48. *
  49. * This filter allows you to adjust the server class used by the API, using a
  50. * different class to handle requests.
  51. *
  52. * @since 4.4.0
  53. *
  54. * @param string $class_name The name of the server class. Default 'WP_REST_Server'.
  55. */
  56. $wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
  57. $wp_rest_server = new $wp_rest_server_class;
  58. /**
  59. * Fires when preparing to serve an API request.
  60. *
  61. * Endpoint objects should be created and register their hooks on this action rather
  62. * than another action to ensure they're only loaded when needed.
  63. *
  64. * @since 4.4.0
  65. *
  66. * @param WP_REST_Server $wp_rest_server Server object.
  67. */
  68. do_action( 'rest_api_init', $wp_rest_server );
  69. }
  70. return $wp_rest_server;
  71. }
  72. }
  73. /**
  74. * plugin.php
  75. */
  76. if ( ! function_exists( 'rest_authorization_required_code' ) ) {
  77. /**
  78. * Returns a contextual HTTP error code for authorization failure.
  79. *
  80. * @return integer
  81. */
  82. function rest_authorization_required_code() {
  83. return is_user_logged_in() ? 403 : 401;
  84. }
  85. }
  86. if ( ! function_exists( 'register_rest_field' ) ) {
  87. /**
  88. * Registers a new field on an existing WordPress object type.
  89. *
  90. * @global array $wp_rest_additional_fields Holds registered fields, organized
  91. * by object type.
  92. *
  93. * @param string|array $object_type Object(s) the field is being registered
  94. * to, "post"|"term"|"comment" etc.
  95. * @param string $attribute The attribute name.
  96. * @param array $args {
  97. * Optional. An array of arguments used to handle the registered field.
  98. *
  99. * @type string|array|null $get_callback Optional. The callback function used to retrieve the field
  100. * value. Default is 'null', the field will not be returned in
  101. * the response.
  102. * @type string|array|null $update_callback Optional. The callback function used to set and update the
  103. * field value. Default is 'null', the value cannot be set or
  104. * updated.
  105. * @type string|array|null $schema Optional. The callback function used to create the schema for
  106. * this field. Default is 'null', no schema entry will be returned.
  107. * }
  108. */
  109. function register_rest_field( $object_type, $attribute, $args = array() ) {
  110. $defaults = array(
  111. 'get_callback' => null,
  112. 'update_callback' => null,
  113. 'schema' => null,
  114. );
  115. $args = wp_parse_args( $args, $defaults );
  116. global $wp_rest_additional_fields;
  117. $object_types = (array) $object_type;
  118. foreach ( $object_types as $object_type ) {
  119. $wp_rest_additional_fields[ $object_type ][ $attribute ] = $args;
  120. }
  121. }
  122. }
  123. if ( ! function_exists( 'register_api_field' ) ) {
  124. /**
  125. * Backwards compat shim
  126. */
  127. function register_api_field( $object_type, $attributes, $args = array() ) {
  128. _deprecated_function( 'register_api_field', 'WPAPI-2.0', 'register_rest_field' );
  129. register_rest_field( $object_type, $attributes, $args );
  130. }
  131. }
  132. if ( ! function_exists( 'rest_validate_request_arg' ) ) {
  133. /**
  134. * Validate a request argument based on details registered to the route.
  135. *
  136. * @param mixed $value
  137. * @param WP_REST_Request $request
  138. * @param string $param
  139. * @return WP_Error|boolean
  140. */
  141. function rest_validate_request_arg( $value, $request, $param ) {
  142. $attributes = $request->get_attributes();
  143. if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) {
  144. return true;
  145. }
  146. $args = $attributes['args'][ $param ];
  147. if ( ! empty( $args['enum'] ) ) {
  148. if ( ! in_array( $value, $args['enum'] ) ) {
  149. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not one of %s', 'woocommerce' ), $param, implode( ', ', $args['enum'] ) ) );
  150. }
  151. }
  152. if ( 'integer' === $args['type'] && ! is_numeric( $value ) ) {
  153. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s', 'woocommerce' ), $param, 'integer' ) );
  154. }
  155. if ( 'string' === $args['type'] && ! is_string( $value ) ) {
  156. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s', 'woocommerce' ), $param, 'string' ) );
  157. }
  158. if ( isset( $args['format'] ) ) {
  159. switch ( $args['format'] ) {
  160. case 'date-time' :
  161. if ( ! rest_parse_date( $value ) ) {
  162. return new WP_Error( 'rest_invalid_date', __( 'The date you provided is invalid.', 'woocommerce' ) );
  163. }
  164. break;
  165. case 'email' :
  166. if ( ! is_email( $value ) ) {
  167. return new WP_Error( 'rest_invalid_email', __( 'The email address you provided is invalid.', 'woocommerce' ) );
  168. }
  169. break;
  170. }
  171. }
  172. if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) {
  173. if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
  174. if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
  175. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (exclusive)', 'woocommerce' ), $param, $args['minimum'] ) );
  176. } else if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
  177. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (inclusive)', 'woocommerce' ), $param, $args['minimum'] ) );
  178. }
  179. } else if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
  180. if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
  181. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (exclusive)', 'woocommerce' ), $param, $args['maximum'] ) );
  182. } else if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
  183. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (inclusive)', 'woocommerce' ), $param, $args['maximum'] ) );
  184. }
  185. } else if ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) {
  186. if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
  187. if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) {
  188. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (exclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) );
  189. }
  190. } else if ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
  191. if ( $value >= $args['maximum'] || $value < $args['minimum'] ) {
  192. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (exclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) );
  193. }
  194. } else if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
  195. if ( $value > $args['maximum'] || $value <= $args['minimum'] ) {
  196. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (inclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) );
  197. }
  198. } else if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
  199. if ( $value > $args['maximum'] || $value < $args['minimum'] ) {
  200. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (inclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) );
  201. }
  202. }
  203. }
  204. }
  205. return true;
  206. }
  207. }
  208. if ( ! function_exists( 'rest_sanitize_request_arg' ) ) {
  209. /**
  210. * Sanitize a request argument based on details registered to the route.
  211. *
  212. * @param mixed $value
  213. * @param WP_REST_Request $request
  214. * @param string $param
  215. * @return mixed
  216. */
  217. function rest_sanitize_request_arg( $value, $request, $param ) {
  218. $attributes = $request->get_attributes();
  219. if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) {
  220. return $value;
  221. }
  222. $args = $attributes['args'][ $param ];
  223. if ( 'integer' === $args['type'] ) {
  224. return (int) $value;
  225. }
  226. if ( isset( $args['format'] ) ) {
  227. switch ( $args['format'] ) {
  228. case 'date-time' :
  229. return sanitize_text_field( $value );
  230. case 'email' :
  231. /*
  232. * sanitize_email() validates, which would be unexpected
  233. */
  234. return sanitize_text_field( $value );
  235. case 'uri' :
  236. return esc_url_raw( $value );
  237. }
  238. }
  239. return $value;
  240. }
  241. }