PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/rest-api/class-wp-rest-request.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 1016 lines | 376 code | 131 blank | 509 comment | 48 complexity | 72681e16d0d6130597d5fbe6d1d021bc MD5 | raw file
  1. <?php
  2. /**
  3. * REST API: WP_REST_Request class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement a REST request object.
  11. *
  12. * Contains data from the request, to be passed to the callback.
  13. *
  14. * Note: This implements ArrayAccess, and acts as an array of parameters when
  15. * used in that manner. It does not use ArrayObject (as we cannot rely on SPL),
  16. * so be aware it may have non-array behaviour in some cases.
  17. *
  18. * Note: When using features provided by ArrayAccess, be aware that WordPress deliberately
  19. * does not distinguish between arguments of the same name for different request methods.
  20. * For instance, in a request with `GET id=1` and `POST id=2`, `$request['id']` will equal
  21. * 2 (`POST`) not 1 (`GET`). For more precision between request methods, use
  22. * WP_REST_Request::get_body_params(), WP_REST_Request::get_url_params(), etc.
  23. *
  24. * @since 4.4.0
  25. *
  26. * @link https://www.php.net/manual/en/class.arrayaccess.php
  27. */
  28. class WP_REST_Request implements ArrayAccess {
  29. /**
  30. * HTTP method.
  31. *
  32. * @since 4.4.0
  33. * @var string
  34. */
  35. protected $method = '';
  36. /**
  37. * Parameters passed to the request.
  38. *
  39. * These typically come from the `$_GET`, `$_POST` and `$_FILES`
  40. * superglobals when being created from the global scope.
  41. *
  42. * @since 4.4.0
  43. * @var array Contains GET, POST and FILES keys mapping to arrays of data.
  44. */
  45. protected $params;
  46. /**
  47. * HTTP headers for the request.
  48. *
  49. * @since 4.4.0
  50. * @var array Map of key to value. Key is always lowercase, as per HTTP specification.
  51. */
  52. protected $headers = array();
  53. /**
  54. * Body data.
  55. *
  56. * @since 4.4.0
  57. * @var string Binary data from the request.
  58. */
  59. protected $body = null;
  60. /**
  61. * Route matched for the request.
  62. *
  63. * @since 4.4.0
  64. * @var string
  65. */
  66. protected $route;
  67. /**
  68. * Attributes (options) for the route that was matched.
  69. *
  70. * This is the options array used when the route was registered, typically
  71. * containing the callback as well as the valid methods for the route.
  72. *
  73. * @since 4.4.0
  74. * @var array Attributes for the request.
  75. */
  76. protected $attributes = array();
  77. /**
  78. * Used to determine if the JSON data has been parsed yet.
  79. *
  80. * Allows lazy-parsing of JSON data where possible.
  81. *
  82. * @since 4.4.0
  83. * @var bool
  84. */
  85. protected $parsed_json = false;
  86. /**
  87. * Used to determine if the body data has been parsed yet.
  88. *
  89. * @since 4.4.0
  90. * @var bool
  91. */
  92. protected $parsed_body = false;
  93. /**
  94. * Constructor.
  95. *
  96. * @since 4.4.0
  97. *
  98. * @param string $method Optional. Request method. Default empty.
  99. * @param string $route Optional. Request route. Default empty.
  100. * @param array $attributes Optional. Request attributes. Default empty array.
  101. */
  102. public function __construct( $method = '', $route = '', $attributes = array() ) {
  103. $this->params = array(
  104. 'URL' => array(),
  105. 'GET' => array(),
  106. 'POST' => array(),
  107. 'FILES' => array(),
  108. // See parse_json_params.
  109. 'JSON' => null,
  110. 'defaults' => array(),
  111. );
  112. $this->set_method( $method );
  113. $this->set_route( $route );
  114. $this->set_attributes( $attributes );
  115. }
  116. /**
  117. * Retrieves the HTTP method for the request.
  118. *
  119. * @since 4.4.0
  120. *
  121. * @return string HTTP method.
  122. */
  123. public function get_method() {
  124. return $this->method;
  125. }
  126. /**
  127. * Sets HTTP method for the request.
  128. *
  129. * @since 4.4.0
  130. *
  131. * @param string $method HTTP method.
  132. */
  133. public function set_method( $method ) {
  134. $this->method = strtoupper( $method );
  135. }
  136. /**
  137. * Retrieves all headers from the request.
  138. *
  139. * @since 4.4.0
  140. *
  141. * @return array Map of key to value. Key is always lowercase, as per HTTP specification.
  142. */
  143. public function get_headers() {
  144. return $this->headers;
  145. }
  146. /**
  147. * Canonicalizes the header name.
  148. *
  149. * Ensures that header names are always treated the same regardless of
  150. * source. Header names are always case insensitive.
  151. *
  152. * Note that we treat `-` (dashes) and `_` (underscores) as the same
  153. * character, as per header parsing rules in both Apache and nginx.
  154. *
  155. * @link https://stackoverflow.com/q/18185366
  156. * @link https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#missing-disappearing-http-headers
  157. * @link https://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers
  158. *
  159. * @since 4.4.0
  160. *
  161. * @param string $key Header name.
  162. * @return string Canonicalized name.
  163. */
  164. public static function canonicalize_header_name( $key ) {
  165. $key = strtolower( $key );
  166. $key = str_replace( '-', '_', $key );
  167. return $key;
  168. }
  169. /**
  170. * Retrieves the given header from the request.
  171. *
  172. * If the header has multiple values, they will be concatenated with a comma
  173. * as per the HTTP specification. Be aware that some non-compliant headers
  174. * (notably cookie headers) cannot be joined this way.
  175. *
  176. * @since 4.4.0
  177. *
  178. * @param string $key Header name, will be canonicalized to lowercase.
  179. * @return string|null String value if set, null otherwise.
  180. */
  181. public function get_header( $key ) {
  182. $key = $this->canonicalize_header_name( $key );
  183. if ( ! isset( $this->headers[ $key ] ) ) {
  184. return null;
  185. }
  186. return implode( ',', $this->headers[ $key ] );
  187. }
  188. /**
  189. * Retrieves header values from the request.
  190. *
  191. * @since 4.4.0
  192. *
  193. * @param string $key Header name, will be canonicalized to lowercase.
  194. * @return array|null List of string values if set, null otherwise.
  195. */
  196. public function get_header_as_array( $key ) {
  197. $key = $this->canonicalize_header_name( $key );
  198. if ( ! isset( $this->headers[ $key ] ) ) {
  199. return null;
  200. }
  201. return $this->headers[ $key ];
  202. }
  203. /**
  204. * Sets the header on request.
  205. *
  206. * @since 4.4.0
  207. *
  208. * @param string $key Header name.
  209. * @param string $value Header value, or list of values.
  210. */
  211. public function set_header( $key, $value ) {
  212. $key = $this->canonicalize_header_name( $key );
  213. $value = (array) $value;
  214. $this->headers[ $key ] = $value;
  215. }
  216. /**
  217. * Appends a header value for the given header.
  218. *
  219. * @since 4.4.0
  220. *
  221. * @param string $key Header name.
  222. * @param string $value Header value, or list of values.
  223. */
  224. public function add_header( $key, $value ) {
  225. $key = $this->canonicalize_header_name( $key );
  226. $value = (array) $value;
  227. if ( ! isset( $this->headers[ $key ] ) ) {
  228. $this->headers[ $key ] = array();
  229. }
  230. $this->headers[ $key ] = array_merge( $this->headers[ $key ], $value );
  231. }
  232. /**
  233. * Removes all values for a header.
  234. *
  235. * @since 4.4.0
  236. *
  237. * @param string $key Header name.
  238. */
  239. public function remove_header( $key ) {
  240. $key = $this->canonicalize_header_name( $key );
  241. unset( $this->headers[ $key ] );
  242. }
  243. /**
  244. * Sets headers on the request.
  245. *
  246. * @since 4.4.0
  247. *
  248. * @param array $headers Map of header name to value.
  249. * @param bool $override If true, replace the request's headers. Otherwise, merge with existing.
  250. */
  251. public function set_headers( $headers, $override = true ) {
  252. if ( true === $override ) {
  253. $this->headers = array();
  254. }
  255. foreach ( $headers as $key => $value ) {
  256. $this->set_header( $key, $value );
  257. }
  258. }
  259. /**
  260. * Retrieves the content-type of the request.
  261. *
  262. * @since 4.4.0
  263. *
  264. * @return array|null Map containing 'value' and 'parameters' keys
  265. * or null when no valid content-type header was
  266. * available.
  267. */
  268. public function get_content_type() {
  269. $value = $this->get_header( 'content-type' );
  270. if ( empty( $value ) ) {
  271. return null;
  272. }
  273. $parameters = '';
  274. if ( strpos( $value, ';' ) ) {
  275. list( $value, $parameters ) = explode( ';', $value, 2 );
  276. }
  277. $value = strtolower( $value );
  278. if ( false === strpos( $value, '/' ) ) {
  279. return null;
  280. }
  281. // Parse type and subtype out.
  282. list( $type, $subtype ) = explode( '/', $value, 2 );
  283. $data = compact( 'value', 'type', 'subtype', 'parameters' );
  284. $data = array_map( 'trim', $data );
  285. return $data;
  286. }
  287. /**
  288. * Retrieves the parameter priority order.
  289. *
  290. * Used when checking parameters in get_param().
  291. *
  292. * @since 4.4.0
  293. *
  294. * @return string[] Array of types to check, in order of priority.
  295. */
  296. protected function get_parameter_order() {
  297. $order = array();
  298. $content_type = $this->get_content_type();
  299. if ( isset( $content_type['value'] ) && 'application/json' === $content_type['value'] ) {
  300. $order[] = 'JSON';
  301. }
  302. $this->parse_json_params();
  303. // Ensure we parse the body data.
  304. $body = $this->get_body();
  305. if ( 'POST' !== $this->method && ! empty( $body ) ) {
  306. $this->parse_body_params();
  307. }
  308. $accepts_body_data = array( 'POST', 'PUT', 'PATCH', 'DELETE' );
  309. if ( in_array( $this->method, $accepts_body_data, true ) ) {
  310. $order[] = 'POST';
  311. }
  312. $order[] = 'GET';
  313. $order[] = 'URL';
  314. $order[] = 'defaults';
  315. /**
  316. * Filters the parameter order.
  317. *
  318. * The order affects which parameters are checked when using get_param() and family.
  319. * This acts similarly to PHP's `request_order` setting.
  320. *
  321. * @since 4.4.0
  322. *
  323. * @param string[] $order Array of types to check, in order of priority.
  324. * @param WP_REST_Request $this The request object.
  325. */
  326. return apply_filters( 'rest_request_parameter_order', $order, $this );
  327. }
  328. /**
  329. * Retrieves a parameter from the request.
  330. *
  331. * @since 4.4.0
  332. *
  333. * @param string $key Parameter name.
  334. * @return mixed|null Value if set, null otherwise.
  335. */
  336. public function get_param( $key ) {
  337. $order = $this->get_parameter_order();
  338. foreach ( $order as $type ) {
  339. // Determine if we have the parameter for this type.
  340. if ( isset( $this->params[ $type ][ $key ] ) ) {
  341. return $this->params[ $type ][ $key ];
  342. }
  343. }
  344. return null;
  345. }
  346. /**
  347. * Checks if a parameter exists in the request.
  348. *
  349. * This allows distinguishing between an omitted parameter,
  350. * and a parameter specifically set to null.
  351. *
  352. * @since 5.3.0
  353. *
  354. * @param string $key Parameter name.
  355. *
  356. * @return bool True if a param exists for the given key.
  357. */
  358. public function has_param( $key ) {
  359. $order = $this->get_parameter_order();
  360. foreach ( $order as $type ) {
  361. if ( array_key_exists( $key, $this->params[ $type ] ) ) {
  362. return true;
  363. }
  364. }
  365. return false;
  366. }
  367. /**
  368. * Sets a parameter on the request.
  369. *
  370. * @since 4.4.0
  371. *
  372. * @param string $key Parameter name.
  373. * @param mixed $value Parameter value.
  374. */
  375. public function set_param( $key, $value ) {
  376. $order = $this->get_parameter_order();
  377. $this->params[ $order[0] ][ $key ] = $value;
  378. }
  379. /**
  380. * Retrieves merged parameters from the request.
  381. *
  382. * The equivalent of get_param(), but returns all parameters for the request.
  383. * Handles merging all the available values into a single array.
  384. *
  385. * @since 4.4.0
  386. *
  387. * @return array Map of key to value.
  388. */
  389. public function get_params() {
  390. $order = $this->get_parameter_order();
  391. $order = array_reverse( $order, true );
  392. $params = array();
  393. foreach ( $order as $type ) {
  394. // array_merge() / the "+" operator will mess up
  395. // numeric keys, so instead do a manual foreach.
  396. foreach ( (array) $this->params[ $type ] as $key => $value ) {
  397. $params[ $key ] = $value;
  398. }
  399. }
  400. return $params;
  401. }
  402. /**
  403. * Retrieves parameters from the route itself.
  404. *
  405. * These are parsed from the URL using the regex.
  406. *
  407. * @since 4.4.0
  408. *
  409. * @return array Parameter map of key to value.
  410. */
  411. public function get_url_params() {
  412. return $this->params['URL'];
  413. }
  414. /**
  415. * Sets parameters from the route.
  416. *
  417. * Typically, this is set after parsing the URL.
  418. *
  419. * @since 4.4.0
  420. *
  421. * @param array $params Parameter map of key to value.
  422. */
  423. public function set_url_params( $params ) {
  424. $this->params['URL'] = $params;
  425. }
  426. /**
  427. * Retrieves parameters from the query string.
  428. *
  429. * These are the parameters you'd typically find in `$_GET`.
  430. *
  431. * @since 4.4.0
  432. *
  433. * @return array Parameter map of key to value
  434. */
  435. public function get_query_params() {
  436. return $this->params['GET'];
  437. }
  438. /**
  439. * Sets parameters from the query string.
  440. *
  441. * Typically, this is set from `$_GET`.
  442. *
  443. * @since 4.4.0
  444. *
  445. * @param array $params Parameter map of key to value.
  446. */
  447. public function set_query_params( $params ) {
  448. $this->params['GET'] = $params;
  449. }
  450. /**
  451. * Retrieves parameters from the body.
  452. *
  453. * These are the parameters you'd typically find in `$_POST`.
  454. *
  455. * @since 4.4.0
  456. *
  457. * @return array Parameter map of key to value.
  458. */
  459. public function get_body_params() {
  460. return $this->params['POST'];
  461. }
  462. /**
  463. * Sets parameters from the body.
  464. *
  465. * Typically, this is set from `$_POST`.
  466. *
  467. * @since 4.4.0
  468. *
  469. * @param array $params Parameter map of key to value.
  470. */
  471. public function set_body_params( $params ) {
  472. $this->params['POST'] = $params;
  473. }
  474. /**
  475. * Retrieves multipart file parameters from the body.
  476. *
  477. * These are the parameters you'd typically find in `$_FILES`.
  478. *
  479. * @since 4.4.0
  480. *
  481. * @return array Parameter map of key to value
  482. */
  483. public function get_file_params() {
  484. return $this->params['FILES'];
  485. }
  486. /**
  487. * Sets multipart file parameters from the body.
  488. *
  489. * Typically, this is set from `$_FILES`.
  490. *
  491. * @since 4.4.0
  492. *
  493. * @param array $params Parameter map of key to value.
  494. */
  495. public function set_file_params( $params ) {
  496. $this->params['FILES'] = $params;
  497. }
  498. /**
  499. * Retrieves the default parameters.
  500. *
  501. * These are the parameters set in the route registration.
  502. *
  503. * @since 4.4.0
  504. *
  505. * @return array Parameter map of key to value
  506. */
  507. public function get_default_params() {
  508. return $this->params['defaults'];
  509. }
  510. /**
  511. * Sets default parameters.
  512. *
  513. * These are the parameters set in the route registration.
  514. *
  515. * @since 4.4.0
  516. *
  517. * @param array $params Parameter map of key to value.
  518. */
  519. public function set_default_params( $params ) {
  520. $this->params['defaults'] = $params;
  521. }
  522. /**
  523. * Retrieves the request body content.
  524. *
  525. * @since 4.4.0
  526. *
  527. * @return string Binary data from the request body.
  528. */
  529. public function get_body() {
  530. return $this->body;
  531. }
  532. /**
  533. * Sets body content.
  534. *
  535. * @since 4.4.0
  536. *
  537. * @param string $data Binary data from the request body.
  538. */
  539. public function set_body( $data ) {
  540. $this->body = $data;
  541. // Enable lazy parsing.
  542. $this->parsed_json = false;
  543. $this->parsed_body = false;
  544. $this->params['JSON'] = null;
  545. }
  546. /**
  547. * Retrieves the parameters from a JSON-formatted body.
  548. *
  549. * @since 4.4.0
  550. *
  551. * @return array Parameter map of key to value.
  552. */
  553. public function get_json_params() {
  554. // Ensure the parameters have been parsed out.
  555. $this->parse_json_params();
  556. return $this->params['JSON'];
  557. }
  558. /**
  559. * Parses the JSON parameters.
  560. *
  561. * Avoids parsing the JSON data until we need to access it.
  562. *
  563. * @since 4.4.0
  564. * @since 4.7.0 Returns error instance if value cannot be decoded.
  565. * @return true|WP_Error True if the JSON data was passed or no JSON data was provided, WP_Error if invalid JSON was passed.
  566. */
  567. protected function parse_json_params() {
  568. if ( $this->parsed_json ) {
  569. return true;
  570. }
  571. $this->parsed_json = true;
  572. // Check that we actually got JSON.
  573. $content_type = $this->get_content_type();
  574. if ( empty( $content_type ) || 'application/json' !== $content_type['value'] ) {
  575. return true;
  576. }
  577. $body = $this->get_body();
  578. if ( empty( $body ) ) {
  579. return true;
  580. }
  581. $params = json_decode( $body, true );
  582. /*
  583. * Check for a parsing error.
  584. */
  585. if ( null === $params && JSON_ERROR_NONE !== json_last_error() ) {
  586. // Ensure subsequent calls receive error instance.
  587. $this->parsed_json = false;
  588. $error_data = array(
  589. 'status' => WP_Http::BAD_REQUEST,
  590. 'json_error_code' => json_last_error(),
  591. 'json_error_message' => json_last_error_msg(),
  592. );
  593. return new WP_Error( 'rest_invalid_json', __( 'Invalid JSON body passed.' ), $error_data );
  594. }
  595. $this->params['JSON'] = $params;
  596. return true;
  597. }
  598. /**
  599. * Parses the request body parameters.
  600. *
  601. * Parses out URL-encoded bodies for request methods that aren't supported
  602. * natively by PHP. In PHP 5.x, only POST has these parsed automatically.
  603. *
  604. * @since 4.4.0
  605. */
  606. protected function parse_body_params() {
  607. if ( $this->parsed_body ) {
  608. return;
  609. }
  610. $this->parsed_body = true;
  611. /*
  612. * Check that we got URL-encoded. Treat a missing content-type as
  613. * URL-encoded for maximum compatibility.
  614. */
  615. $content_type = $this->get_content_type();
  616. if ( ! empty( $content_type ) && 'application/x-www-form-urlencoded' !== $content_type['value'] ) {
  617. return;
  618. }
  619. parse_str( $this->get_body(), $params );
  620. /*
  621. * Add to the POST parameters stored internally. If a user has already
  622. * set these manually (via `set_body_params`), don't override them.
  623. */
  624. $this->params['POST'] = array_merge( $params, $this->params['POST'] );
  625. }
  626. /**
  627. * Retrieves the route that matched the request.
  628. *
  629. * @since 4.4.0
  630. *
  631. * @return string Route matching regex.
  632. */
  633. public function get_route() {
  634. return $this->route;
  635. }
  636. /**
  637. * Sets the route that matched the request.
  638. *
  639. * @since 4.4.0
  640. *
  641. * @param string $route Route matching regex.
  642. */
  643. public function set_route( $route ) {
  644. $this->route = $route;
  645. }
  646. /**
  647. * Retrieves the attributes for the request.
  648. *
  649. * These are the options for the route that was matched.
  650. *
  651. * @since 4.4.0
  652. *
  653. * @return array Attributes for the request.
  654. */
  655. public function get_attributes() {
  656. return $this->attributes;
  657. }
  658. /**
  659. * Sets the attributes for the request.
  660. *
  661. * @since 4.4.0
  662. *
  663. * @param array $attributes Attributes for the request.
  664. */
  665. public function set_attributes( $attributes ) {
  666. $this->attributes = $attributes;
  667. }
  668. /**
  669. * Sanitizes (where possible) the params on the request.
  670. *
  671. * This is primarily based off the sanitize_callback param on each registered
  672. * argument.
  673. *
  674. * @since 4.4.0
  675. *
  676. * @return true|WP_Error True if parameters were sanitized, WP_Error if an error occurred during sanitization.
  677. */
  678. public function sanitize_params() {
  679. $attributes = $this->get_attributes();
  680. // No arguments set, skip sanitizing.
  681. if ( empty( $attributes['args'] ) ) {
  682. return true;
  683. }
  684. $order = $this->get_parameter_order();
  685. $invalid_params = array();
  686. foreach ( $order as $type ) {
  687. if ( empty( $this->params[ $type ] ) ) {
  688. continue;
  689. }
  690. foreach ( $this->params[ $type ] as $key => $value ) {
  691. if ( ! isset( $attributes['args'][ $key ] ) ) {
  692. continue;
  693. }
  694. $param_args = $attributes['args'][ $key ];
  695. // If the arg has a type but no sanitize_callback attribute, default to rest_parse_request_arg.
  696. if ( ! array_key_exists( 'sanitize_callback', $param_args ) && ! empty( $param_args['type'] ) ) {
  697. $param_args['sanitize_callback'] = 'rest_parse_request_arg';
  698. }
  699. // If there's still no sanitize_callback, nothing to do here.
  700. if ( empty( $param_args['sanitize_callback'] ) ) {
  701. continue;
  702. }
  703. $sanitized_value = call_user_func( $param_args['sanitize_callback'], $value, $this, $key );
  704. if ( is_wp_error( $sanitized_value ) ) {
  705. $invalid_params[ $key ] = $sanitized_value->get_error_message();
  706. } else {
  707. $this->params[ $type ][ $key ] = $sanitized_value;
  708. }
  709. }
  710. }
  711. if ( $invalid_params ) {
  712. return new WP_Error(
  713. 'rest_invalid_param',
  714. /* translators: %s: List of invalid parameters. */
  715. sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ),
  716. array(
  717. 'status' => 400,
  718. 'params' => $invalid_params,
  719. )
  720. );
  721. }
  722. return true;
  723. }
  724. /**
  725. * Checks whether this request is valid according to its attributes.
  726. *
  727. * @since 4.4.0
  728. *
  729. * @return bool|WP_Error True if there are no parameters to validate or if all pass validation,
  730. * WP_Error if required parameters are missing.
  731. */
  732. public function has_valid_params() {
  733. // If JSON data was passed, check for errors.
  734. $json_error = $this->parse_json_params();
  735. if ( is_wp_error( $json_error ) ) {
  736. return $json_error;
  737. }
  738. $attributes = $this->get_attributes();
  739. $required = array();
  740. // No arguments set, skip validation.
  741. if ( empty( $attributes['args'] ) ) {
  742. return true;
  743. }
  744. foreach ( $attributes['args'] as $key => $arg ) {
  745. $param = $this->get_param( $key );
  746. if ( isset( $arg['required'] ) && true === $arg['required'] && null === $param ) {
  747. $required[] = $key;
  748. }
  749. }
  750. if ( ! empty( $required ) ) {
  751. return new WP_Error(
  752. 'rest_missing_callback_param',
  753. /* translators: %s: List of required parameters. */
  754. sprintf( __( 'Missing parameter(s): %s' ), implode( ', ', $required ) ),
  755. array(
  756. 'status' => 400,
  757. 'params' => $required,
  758. )
  759. );
  760. }
  761. /*
  762. * Check the validation callbacks for each registered arg.
  763. *
  764. * This is done after required checking as required checking is cheaper.
  765. */
  766. $invalid_params = array();
  767. foreach ( $attributes['args'] as $key => $arg ) {
  768. $param = $this->get_param( $key );
  769. if ( null !== $param && ! empty( $arg['validate_callback'] ) ) {
  770. $valid_check = call_user_func( $arg['validate_callback'], $param, $this, $key );
  771. if ( false === $valid_check ) {
  772. $invalid_params[ $key ] = __( 'Invalid parameter.' );
  773. }
  774. if ( is_wp_error( $valid_check ) ) {
  775. $invalid_params[ $key ] = $valid_check->get_error_message();
  776. }
  777. }
  778. }
  779. if ( $invalid_params ) {
  780. return new WP_Error(
  781. 'rest_invalid_param',
  782. /* translators: %s: List of invalid parameters. */
  783. sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ),
  784. array(
  785. 'status' => 400,
  786. 'params' => $invalid_params,
  787. )
  788. );
  789. }
  790. return true;
  791. }
  792. /**
  793. * Checks if a parameter is set.
  794. *
  795. * @since 4.4.0
  796. *
  797. * @param string $offset Parameter name.
  798. * @return bool Whether the parameter is set.
  799. */
  800. public function offsetExists( $offset ) {
  801. $order = $this->get_parameter_order();
  802. foreach ( $order as $type ) {
  803. if ( isset( $this->params[ $type ][ $offset ] ) ) {
  804. return true;
  805. }
  806. }
  807. return false;
  808. }
  809. /**
  810. * Retrieves a parameter from the request.
  811. *
  812. * @since 4.4.0
  813. *
  814. * @param string $offset Parameter name.
  815. * @return mixed|null Value if set, null otherwise.
  816. */
  817. public function offsetGet( $offset ) {
  818. return $this->get_param( $offset );
  819. }
  820. /**
  821. * Sets a parameter on the request.
  822. *
  823. * @since 4.4.0
  824. *
  825. * @param string $offset Parameter name.
  826. * @param mixed $value Parameter value.
  827. */
  828. public function offsetSet( $offset, $value ) {
  829. $this->set_param( $offset, $value );
  830. }
  831. /**
  832. * Removes a parameter from the request.
  833. *
  834. * @since 4.4.0
  835. *
  836. * @param string $offset Parameter name.
  837. */
  838. public function offsetUnset( $offset ) {
  839. $order = $this->get_parameter_order();
  840. // Remove the offset from every group.
  841. foreach ( $order as $type ) {
  842. unset( $this->params[ $type ][ $offset ] );
  843. }
  844. }
  845. /**
  846. * Retrieves a WP_REST_Request object from a full URL.
  847. *
  848. * @since 4.5.0
  849. *
  850. * @param string $url URL with protocol, domain, path and query args.
  851. * @return WP_REST_Request|false WP_REST_Request object on success, false on failure.
  852. */
  853. public static function from_url( $url ) {
  854. $bits = parse_url( $url );
  855. $query_params = array();
  856. if ( ! empty( $bits['query'] ) ) {
  857. wp_parse_str( $bits['query'], $query_params );
  858. }
  859. $api_root = rest_url();
  860. if ( get_option( 'permalink_structure' ) && 0 === strpos( $url, $api_root ) ) {
  861. // Pretty permalinks on, and URL is under the API root.
  862. $api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) );
  863. $route = parse_url( $api_url_part, PHP_URL_PATH );
  864. } elseif ( ! empty( $query_params['rest_route'] ) ) {
  865. // ?rest_route=... set directly.
  866. $route = $query_params['rest_route'];
  867. unset( $query_params['rest_route'] );
  868. }
  869. $request = false;
  870. if ( ! empty( $route ) ) {
  871. $request = new WP_REST_Request( 'GET', $route );
  872. $request->set_query_params( $query_params );
  873. }
  874. /**
  875. * Filters the request generated from a URL.
  876. *
  877. * @since 4.5.0
  878. *
  879. * @param WP_REST_Request|false $request Generated request object, or false if URL
  880. * could not be parsed.
  881. * @param string $url URL the request was generated from.
  882. */
  883. return apply_filters( 'rest_request_from_url', $request, $url );
  884. }
  885. }