/includes/vendor/wp-rest-functions.php

  1. <?php 
  2. /** 
  3. * @version 2.0-beta15 
  4. */ 
  5.  
  6. if ( ! defined( 'ABSPATH' ) ) { 
  7. exit; 
  8.  
  9. /** 
  10. * core-integration.php 
  11. */ 
  12.  
  13. if ( ! function_exists( 'wp_parse_slug_list' ) ) { 
  14. /** 
  15. * Clean up an array, comma- or space-separated list of slugs. 
  16. * 
  17. * @since 
  18. * 
  19. * @param array|string $list List of slugs. 
  20. * @return array Sanitized array of slugs. 
  21. */ 
  22. function wp_parse_slug_list( $list ) { 
  23. if ( ! is_array( $list ) ) { 
  24. $list = preg_split( '/[\s, ]+/', $list ); 
  25.  
  26. foreach ( $list as $key => $value ) { 
  27. $list[ $key ] = sanitize_title( $value ); 
  28.  
  29. return array_unique( $list ); 
  30.  
  31. if ( ! function_exists( 'rest_get_server' ) ) { 
  32. /** 
  33. * Retrieves the current REST server instance. 
  34. * 
  35. * Instantiates a new instance if none exists already. 
  36. * 
  37. * @since 4.5.0 
  38. * 
  39. * @global WP_REST_Server $wp_rest_server REST server instance. 
  40. * 
  41. * @return WP_REST_Server REST server instance. 
  42. */ 
  43. function rest_get_server() { 
  44. /** @var WP_REST_Server $wp_rest_server */ 
  45. global $wp_rest_server; 
  46.  
  47. if ( empty( $wp_rest_server ) ) { 
  48. /** 
  49. * Filter the REST Server Class. 
  50. * 
  51. * This filter allows you to adjust the server class used by the API, using a 
  52. * different class to handle requests. 
  53. * 
  54. * @since 4.4.0 
  55. * 
  56. * @param string $class_name The name of the server class. Default 'WP_REST_Server'. 
  57. */ 
  58. $wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' ); 
  59. $wp_rest_server = new $wp_rest_server_class; 
  60.  
  61. /** 
  62. * Fires when preparing to serve an API request. 
  63. * 
  64. * Endpoint objects should be created and register their hooks on this action rather 
  65. * than another action to ensure they're only loaded when needed. 
  66. * 
  67. * @since 4.4.0 
  68. * 
  69. * @param WP_REST_Server $wp_rest_server Server object. 
  70. */ 
  71. do_action( 'rest_api_init', $wp_rest_server ); 
  72.  
  73. return $wp_rest_server; 
  74.  
  75. /** 
  76. * plugin.php 
  77. */ 
  78.  
  79. if ( ! function_exists( 'rest_authorization_required_code' ) ) { 
  80. /** 
  81. * Returns a contextual HTTP error code for authorization failure. 
  82. * 
  83. * @return integer 
  84. */ 
  85. function rest_authorization_required_code() { 
  86. return is_user_logged_in() ? 403 : 401; 
  87.  
  88. if ( ! function_exists( 'register_rest_field' ) ) { 
  89. /** 
  90. * Registers a new field on an existing WordPress object type. 
  91. * 
  92. * @global array $wp_rest_additional_fields Holds registered fields, organized 
  93. * by object type. 
  94. * 
  95. * @param string|array $object_type Object(s) the field is being registered 
  96. * to, "post"|"term"|"comment" etc. 
  97. * @param string $attribute The attribute name. 
  98. * @param array $args { 
  99. * Optional. An array of arguments used to handle the registered field. 
  100. * 
  101. * @type string|array|null $get_callback Optional. The callback function used to retrieve the field 
  102. * value. Default is 'null', the field will not be returned in 
  103. * the response. 
  104. * @type string|array|null $update_callback Optional. The callback function used to set and update the 
  105. * field value. Default is 'null', the value cannot be set or 
  106. * updated. 
  107. * @type string|array|null $schema Optional. The callback function used to create the schema for 
  108. * this field. Default is 'null', no schema entry will be returned. 
  109. * } 
  110. */ 
  111. function register_rest_field( $object_type, $attribute, $args = array() ) { 
  112. $defaults = array( 
  113. 'get_callback' => null,  
  114. 'update_callback' => null,  
  115. 'schema' => null,  
  116. ); 
  117.  
  118. $args = wp_parse_args( $args, $defaults ); 
  119.  
  120. global $wp_rest_additional_fields; 
  121.  
  122. $object_types = (array) $object_type; 
  123.  
  124. foreach ( $object_types as $object_type ) { 
  125. $wp_rest_additional_fields[ $object_type ][ $attribute ] = $args; 
  126.  
  127. if ( ! function_exists( 'register_api_field' ) ) { 
  128. /** 
  129. * Backwards compat shim 
  130. */ 
  131. function register_api_field( $object_type, $attributes, $args = array() ) { 
  132. wc_deprecated_function( 'register_api_field', 'WPAPI-2.0', 'register_rest_field' ); 
  133. register_rest_field( $object_type, $attributes, $args ); 
  134.  
  135. if ( ! function_exists( 'rest_validate_request_arg' ) ) { 
  136. /** 
  137. * Validate a request argument based on details registered to the route. 
  138. * 
  139. * @param mixed $value 
  140. * @param WP_REST_Request $request 
  141. * @param string $param 
  142. * @return WP_Error|boolean 
  143. */ 
  144. function rest_validate_request_arg( $value, $request, $param ) { 
  145.  
  146. $attributes = $request->get_attributes(); 
  147. if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { 
  148. return true; 
  149. $args = $attributes['args'][ $param ]; 
  150.  
  151. if ( ! empty( $args['enum'] ) ) { 
  152. if ( ! in_array( $value, $args['enum'] ) ) { 
  153. /** translators: 1: parameter 2: arguments */ 
  154. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not one of %2$s', 'woocommerce' ), $param, implode( ', ', $args['enum'] ) ) ); 
  155.  
  156. if ( 'integer' === $args['type'] && ! is_numeric( $value ) ) { 
  157. /** translators: 1: parameter 2: integer type */ 
  158. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s', 'woocommerce' ), $param, 'integer' ) ); 
  159.  
  160. if ( 'boolean' === $args['type'] && ! rest_is_boolean( $value ) ) { 
  161. /** translators: 1: parameter 2: boolean type */ 
  162. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s', 'woocommerce' ), $value, 'boolean' ) ); 
  163.  
  164. if ( 'string' === $args['type'] && ! is_string( $value ) ) { 
  165. /** translators: 1: parameter 2: string type */ 
  166. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s', 'woocommerce' ), $param, 'string' ) ); 
  167.  
  168. if ( isset( $args['format'] ) ) { 
  169. switch ( $args['format'] ) { 
  170. case 'date-time' : 
  171. if ( ! rest_parse_date( $value ) ) { 
  172. return new WP_Error( 'rest_invalid_date', __( 'The date you provided is invalid.', 'woocommerce' ) ); 
  173. break; 
  174.  
  175. case 'email' : 
  176. if ( ! is_email( $value ) ) { 
  177. return new WP_Error( 'rest_invalid_email', __( 'The email address you provided is invalid.', 'woocommerce' ) ); 
  178. break; 
  179. case 'ipv4' : 
  180. if ( ! rest_is_ip_address( $value ) ) { 
  181. /** translators: %s: IP address */ 
  182. return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not a valid IP address.', 'woocommerce' ), $value ) ); 
  183. break; 
  184.  
  185. if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) { 
  186. if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) { 
  187. if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) { 
  188. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d (exclusive)', 'woocommerce' ), $param, $args['minimum'] ) ); 
  189. } elseif ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) { 
  190. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be greater than %2$d (inclusive)', 'woocommerce' ), $param, $args['minimum'] ) ); 
  191. } elseif ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) { 
  192. if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) { 
  193. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d (exclusive)', 'woocommerce' ), $param, $args['maximum'] ) ); 
  194. } elseif ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) { 
  195. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be less than %2$d (inclusive)', 'woocommerce' ), $param, $args['maximum'] ) ); 
  196. } elseif ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) { 
  197. if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { 
  198. if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) { 
  199. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) ); 
  200. } elseif ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) { 
  201. if ( $value >= $args['maximum'] || $value < $args['minimum'] ) { 
  202. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) ); 
  203. } elseif ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { 
  204. if ( $value > $args['maximum'] || $value <= $args['minimum'] ) { 
  205. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) ); 
  206. } elseif ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) { 
  207. if ( $value > $args['maximum'] || $value < $args['minimum'] ) { 
  208. return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) ); 
  209.  
  210. return true; 
  211.  
  212. if ( ! function_exists( 'rest_sanitize_request_arg' ) ) { 
  213. /** 
  214. * Sanitize a request argument based on details registered to the route. 
  215. * 
  216. * @param mixed $value 
  217. * @param WP_REST_Request $request 
  218. * @param string $param 
  219. * @return mixed 
  220. */ 
  221. function rest_sanitize_request_arg( $value, $request, $param ) { 
  222.  
  223. $attributes = $request->get_attributes(); 
  224. if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { 
  225. return $value; 
  226. $args = $attributes['args'][ $param ]; 
  227.  
  228. if ( 'integer' === $args['type'] ) { 
  229. return (int) $value; 
  230.  
  231. if ( 'boolean' === $args['type'] ) { 
  232. return rest_sanitize_boolean( $value ); 
  233.  
  234. if ( isset( $args['format'] ) ) { 
  235. switch ( $args['format'] ) { 
  236. case 'date-time' : 
  237. return sanitize_text_field( $value ); 
  238.  
  239. case 'email' : 
  240. /** 
  241. * sanitize_email() validates, which would be unexpected 
  242. */ 
  243. return sanitize_text_field( $value ); 
  244.  
  245. case 'uri' : 
  246. return esc_url_raw( $value ); 
  247.  
  248. case 'ipv4' : 
  249. return sanitize_text_field( $value ); 
  250.  
  251. return $value; 
  252.  
  253.  
  254. if ( ! function_exists( 'rest_parse_request_arg' ) ) { 
  255. /** 
  256. * Parse a request argument based on details registered to the route. 
  257. * 
  258. * Runs a validation check and sanitizes the value, primarily to be used via 
  259. * the `sanitize_callback` arguments in the endpoint args registration. 
  260. * 
  261. * @param mixed $value 
  262. * @param WP_REST_Request $request 
  263. * @param string $param 
  264. * @return mixed 
  265. */ 
  266. function rest_parse_request_arg( $value, $request, $param ) { 
  267.  
  268. $is_valid = rest_validate_request_arg( $value, $request, $param ); 
  269.  
  270. if ( is_wp_error( $is_valid ) ) { 
  271. return $is_valid; 
  272.  
  273. $value = rest_sanitize_request_arg( $value, $request, $param ); 
  274.  
  275. return $value; 
  276.  
  277. if ( ! function_exists( 'rest_is_ip_address' ) ) { 
  278. /** 
  279. * Determines if a IPv4 address is valid. 
  280. * 
  281. * Does not handle IPv6 addresses. 
  282. * 
  283. * @param string $ipv4 IP 32-bit address. 
  284. * @return string|false The valid IPv4 address, otherwise false. 
  285. */ 
  286. function rest_is_ip_address( $ipv4 ) { 
  287. $pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.) {3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'; 
  288.  
  289. if ( ! preg_match( $pattern, $ipv4 ) ) { 
  290. return false; 
  291.  
  292. return $ipv4; 
  293.  
  294. /** 
  295. * Changes a boolean-like value into the proper boolean value. 
  296. * 
  297. * @param bool|string|int $value The value being evaluated. 
  298. * @return boolean Returns the proper associated boolean value. 
  299. */ 
  300. if ( ! function_exists( 'rest_sanitize_boolean' ) ) { 
  301. function rest_sanitize_boolean( $value ) { 
  302. // String values are translated to `true`; make sure 'false' is false. 
  303. if ( is_string( $value ) ) { 
  304. $value = strtolower( $value ); 
  305. if ( in_array( $value, array( 'false', '0' ), true ) ) { 
  306. $value = false; 
  307.  
  308. // Everything else will map nicely to boolean. 
  309. return (boolean) $value; 
  310.  
  311. /** 
  312. * Determines if a given value is boolean-like. 
  313. * 
  314. * @param bool|string $maybe_bool The value being evaluated. 
  315. * @return boolean True if a boolean, otherwise false. 
  316. */ 
  317. if ( ! function_exists( 'rest_is_boolean' ) ) { 
  318. function rest_is_boolean( $maybe_bool ) { 
  319. if ( is_bool( $maybe_bool ) ) { 
  320. return true; 
  321.  
  322. if ( is_string( $maybe_bool ) ) { 
  323. $maybe_bool = strtolower( $maybe_bool ); 
  324.  
  325. $valid_boolean_values = array( 
  326. 'false',  
  327. 'true',  
  328. '0',  
  329. '1',  
  330. ); 
  331.  
  332. return in_array( $maybe_bool, $valid_boolean_values, true ); 
  333.  
  334. if ( is_int( $maybe_bool ) ) { 
  335. return in_array( $maybe_bool, array( 0, 1 ), true ); 
  336.  
  337. return false; 
.