| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 /** 11 * Core class used to implement a REST request object. 12 * 13 * Contains data from the request, to be passed to the callback. 14 * 15 * Note: This implements ArrayAccess, and acts as an array of parameters when 16 * used in that manner. It does not use ArrayObject (as we cannot rely on SPL), 17 * so be aware it may have non-array behavior in some cases. 18 * 19 * Note: When using features provided by ArrayAccess, be aware that WordPress deliberately 20 * does not distinguish between arguments of the same name for different request methods. 21 * For instance, in a request with `GET id=1` and `POST id=2`, `$request['id']` will equal 22 * 2 (`POST`) not 1 (`GET`). For more precision between request methods, use 23 * WP_REST_Request::get_body_params(), WP_REST_Request::get_url_params(), etc. 24 * 25 * @since 4.4.0 26 * 27 * @link https://www.php.net/manual/en/class.arrayaccess.php 28 * 29 * @phpstan-import-type Endpoint_Arg from WP_REST_Server 30 * 31 * @phpstan-type Attributes array{ 32 * methods?: array<uppercase-string, true>, 33 * callback?: callable|null, 34 * permission_callback?: callable|null, 35 * validate_callback?: callable|null, 36 * args?: array<non-empty-string, Endpoint_Arg>, 37 * accept_json?: bool, 38 * accept_raw?: bool, 39 * show_in_index?: bool, 40 * allow_batch?: array{v1?: bool}|false, 41 * ... 42 * } 43 * @phpstan-type File_Param array{ 44 * name: non-empty-string, 45 * type: string, 46 * size: non-negative-int, 47 * tmp_name: non-empty-string, 48 * error: 0, 49 * full_path?: non-empty-string, 50 * }|array{ 51 * name: string, 52 * type: string, 53 * size: non-negative-int, 54 * tmp_name: string, 55 * error: int<1, 8>, 56 * full_path?: string, 57 * }|array{ 58 * name: array<mixed>, 59 * type: array<mixed>, 60 * size: array<mixed>, 61 * tmp_name: array<mixed>, 62 * error: array<mixed>, 63 * full_path?: array<mixed>, 64 * } 65 * @phpstan-type Parameter_Type 'URL'|'GET'|'POST'|'FILES'|'JSON'|'defaults' 66 * @phpstan-type Params array{ 67 * URL: array<string, mixed>, 68 * GET: array<mixed>, 69 * POST: array<mixed>, 70 * FILES: array<string, File_Param>, 71 * JSON: array<mixed>|string|int|float|bool|null, 72 * defaults: array<string, mixed>, 73 * } 74 * 75 * @phpstan-implements ArrayAccess<string, mixed> 76 */ 77 #[AllowDynamicProperties] 78 class WP_REST_Request implements ArrayAccess { 79 80 /** 81 * HTTP method. 82 * 83 * @since 4.4.0 84 * @var string 85 * @phpstan-var uppercase-string 86 */ 87 protected $method = ''; 88 89 /** 90 * Parameters passed to the request. 91 * 92 * These typically come from the `$_GET`, `$_POST` and `$_FILES` 93 * superglobals when being created from the global scope. 94 * 95 * @since 4.4.0 96 * @var array<string, mixed> Contains GET, POST and FILES keys mapping to arrays of data. 97 * @phpstan-var Params 98 */ 99 protected $params; 100 101 /** 102 * HTTP headers for the request. 103 * 104 * @since 4.4.0 105 * @var array<string, string[]> Map of key to list of values. Key is always lowercase, as per HTTP specification. 106 * @phpstan-var array<lowercase-string, string[]> 107 */ 108 protected $headers = array(); 109 110 /** 111 * Body data. 112 * 113 * @since 4.4.0 114 * @var string|null Binary data from the request, or null if no body has been set. 115 */ 116 protected $body = null; 117 118 /** 119 * Route matched for the request. 120 * 121 * @since 4.4.0 122 * @var string 123 */ 124 protected $route; 125 126 /** 127 * Attributes (options) for the route that was matched. 128 * 129 * This is the options array used when the route was registered, typically 130 * containing the callback as well as the valid methods for the route. 131 * 132 * @since 4.4.0 133 * @var array<string, mixed> Attributes for the request. 134 * @phpstan-var Attributes 135 */ 136 protected $attributes = array(); 137 138 /** 139 * Used to determine if the JSON data has been parsed yet. 140 * 141 * Allows lazy-parsing of JSON data where possible. 142 * 143 * @since 4.4.0 144 * @var bool 145 */ 146 protected $parsed_json = false; 147 148 /** 149 * Used to determine if the body data has been parsed yet. 150 * 151 * @since 4.4.0 152 * @var bool 153 */ 154 protected $parsed_body = false; 155 156 /** 157 * Constructor. 158 * 159 * @since 4.4.0 160 * 161 * @param string $method Optional. Request method. Default empty. 162 * @param string $route Optional. Request route. Default empty. 163 * @param array<string, mixed> $attributes Optional. Request attributes. Default empty array. 164 * 165 * @phpstan-param Attributes $attributes 166 */ 167 public function __construct( $method = '', $route = '', $attributes = array() ) { 168 $this->params = array( 169 'URL' => array(), 170 'GET' => array(), 171 'POST' => array(), 172 'FILES' => array(), 173 174 // See parse_json_params. 175 'JSON' => null, 176 177 'defaults' => array(), 178 ); 179 180 $this->set_method( $method ); 181 $this->set_route( $route ); 182 $this->set_attributes( $attributes ); 183 } 184 185 /** 186 * Retrieves the HTTP method for the request. 187 * 188 * @since 4.4.0 189 * 190 * @return string HTTP method. 191 * 192 * @phpstan-return uppercase-string 193 */ 194 public function get_method() { 195 return $this->method; 196 } 197 198 /** 199 * Sets HTTP method for the request. 200 * 201 * @since 4.4.0 202 * 203 * @param string $method HTTP method. 204 */ 205 public function set_method( $method ) { 206 $this->method = strtoupper( $method ); 207 } 208 209 /** 210 * Retrieves all headers from the request. 211 * 212 * @since 4.4.0 213 * 214 * @return array<string, string[]> Map of key to list of values. Key is always lowercase, as per HTTP specification. 215 * 216 * @phpstan-return array<lowercase-string, string[]> 217 */ 218 public function get_headers() { 219 return $this->headers; 220 } 221 222 /** 223 * Determines if the request is the given method. 224 * 225 * @since 6.8.0 226 * 227 * @param string $method HTTP method. 228 * @return bool Whether the request is of the given method. 229 */ 230 public function is_method( $method ) { 231 return $this->get_method() === strtoupper( $method ); 232 } 233 234 /** 235 * Canonicalizes the header name. 236 * 237 * Ensures that header names are always treated the same regardless of 238 * source. Header names are always case-insensitive. 239 * 240 * Note that we treat `-` (dashes) and `_` (underscores) as the same 241 * character, as per header parsing rules in both Apache and nginx. 242 * 243 * @link https://stackoverflow.com/q/18185366 244 * @link https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#missing-disappearing-http-headers 245 * @link https://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers 246 * 247 * @since 4.4.0 248 * 249 * @param string $key Header name. 250 * @return string Canonicalized name. 251 * 252 * @phpstan-return lowercase-string 253 */ 254 public static function canonicalize_header_name( $key ) { 255 $key = strtolower( $key ); 256 $key = str_replace( '-', '_', $key ); 257 258 return $key; 259 } 260 261 /** 262 * Retrieves the given header from the request. 263 * 264 * If the header has multiple values, they will be concatenated with a comma 265 * as per the HTTP specification. Be aware that some non-compliant headers 266 * (notably cookie headers) cannot be joined this way. 267 * 268 * @since 4.4.0 269 * 270 * @param string $key Header name, will be canonicalized to lowercase. 271 * @return string|null String value if set, null otherwise. 272 */ 273 public function get_header( $key ) { 274 $key = static::canonicalize_header_name( $key ); 275 276 if ( ! isset( $this->headers[ $key ] ) ) { 277 return null; 278 } 279 280 return implode( ',', $this->headers[ $key ] ); 281 } 282 283 /** 284 * Retrieves header values from the request. 285 * 286 * @since 4.4.0 287 * 288 * @param string $key Header name, will be canonicalized to lowercase. 289 * @return string[]|null List of string values if set, null otherwise. 290 */ 291 public function get_header_as_array( $key ) { 292 $key = static::canonicalize_header_name( $key ); 293 294 if ( ! isset( $this->headers[ $key ] ) ) { 295 return null; 296 } 297 298 return $this->headers[ $key ]; 299 } 300 301 /** 302 * Sets the header on request. 303 * 304 * @since 4.4.0 305 * 306 * @param string $key Header name. 307 * @param string|string[] $value Header value, or list of values. 308 */ 309 public function set_header( $key, $value ) { 310 $key = static::canonicalize_header_name( $key ); 311 $value = (array) $value; 312 313 $this->headers[ $key ] = $value; 314 } 315 316 /** 317 * Appends a header value for the given header. 318 * 319 * @since 4.4.0 320 * 321 * @param string $key Header name. 322 * @param string|string[] $value Header value, or list of values. 323 */ 324 public function add_header( $key, $value ) { 325 $key = static::canonicalize_header_name( $key ); 326 $value = (array) $value; 327 328 $this->headers[ $key ] = array_merge( $this->headers[ $key ] ?? array(), $value ); 329 } 330 331 /** 332 * Removes all values for a header. 333 * 334 * @since 4.4.0 335 * 336 * @param string $key Header name. 337 */ 338 public function remove_header( $key ) { 339 $key = static::canonicalize_header_name( $key ); 340 unset( $this->headers[ $key ] ); 341 } 342 343 /** 344 * Sets headers on the request. 345 * 346 * @since 4.4.0 347 * 348 * @param array<string, string|string[]> $headers Map of header name to value, or list of values. 349 * @param bool $override If true, replace the request's headers. Otherwise, merge with existing. 350 */ 351 public function set_headers( $headers, $override = true ) { 352 if ( true === $override ) { 353 $this->headers = array(); 354 } 355 356 foreach ( $headers as $key => $value ) { 357 $this->set_header( $key, $value ); 358 } 359 } 360 361 /** 362 * Retrieves the Content-Type of the request. 363 * 364 * @since 4.4.0 365 * 366 * @return array|null { 367 * The parsed Content-Type header, or null when no valid Content-Type 368 * header was available. 369 * 370 * @type string $value Media type, lowercased, such as `application/json`. 371 * @type string $type Top-level type, such as `application`. 372 * @type string $subtype Subtype, such as `json`. 373 * @type string $parameters Parameters following the media type, such as 374 * `charset=UTF-8`. Empty string if none. 375 * } 376 */ 377 public function get_content_type() { 378 $value = $this->get_header( 'Content-Type' ); 379 if ( empty( $value ) ) { 380 return null; 381 } 382 383 $parameters = ''; 384 if ( strpos( $value, ';' ) ) { 385 list( $value, $parameters ) = explode( ';', $value, 2 ); 386 } 387 388 $value = strtolower( $value ); 389 if ( ! str_contains( $value, '/' ) ) { 390 return null; 391 } 392 393 // Parse type and subtype out. 394 list( $type, $subtype ) = explode( '/', $value, 2 ); 395 396 $data = compact( 'value', 'type', 'subtype', 'parameters' ); 397 $data = array_map( 'trim', $data ); 398 399 return $data; 400 } 401 402 /** 403 * Checks if the request has specified a JSON Content-Type. 404 * 405 * @since 5.6.0 406 * 407 * @return bool True if the Content-Type header is JSON. 408 */ 409 public function is_json_content_type() { 410 $content_type = $this->get_content_type(); 411 412 return isset( $content_type['value'] ) && wp_is_json_media_type( $content_type['value'] ); 413 } 414 415 /** 416 * Retrieves the parameter priority order. 417 * 418 * Used when checking parameters in WP_REST_Request::get_param(). 419 * 420 * @since 4.4.0 421 * 422 * @return string[] Array of types to check, in order of priority. 423 * 424 * @phpstan-return list<Parameter_Type> 425 */ 426 protected function get_parameter_order() { 427 $order = array(); 428 429 if ( $this->is_json_content_type() ) { 430 $order[] = 'JSON'; 431 } 432 433 $this->parse_json_params(); 434 435 // Ensure we parse the body data. 436 $body = $this->get_body(); 437 438 if ( 'POST' !== $this->method && ! empty( $body ) ) { 439 $this->parse_body_params(); 440 } 441 442 $accepts_body_data = array( 'POST', 'PUT', 'PATCH', 'DELETE' ); 443 if ( in_array( $this->method, $accepts_body_data, true ) ) { 444 $order[] = 'POST'; 445 } 446 447 $order[] = 'GET'; 448 $order[] = 'URL'; 449 $order[] = 'defaults'; 450 451 /** 452 * Filters the parameter priority order for a REST API request. 453 * 454 * The order affects which parameters are checked when using WP_REST_Request::get_param() 455 * and family. This acts similarly to PHP's `request_order` setting. 456 * 457 * @since 4.4.0 458 * 459 * @param string[] $order Array of types to check, in order of priority. 460 * @param WP_REST_Request $request The request object. 461 * 462 * @phpstan-param list<Parameter_Type> $order 463 */ 464 return apply_filters( 'rest_request_parameter_order', $order, $this ); 465 } 466 467 /** 468 * Retrieves a parameter from the request. 469 * 470 * @since 4.4.0 471 * 472 * @param string $key Parameter name. 473 * @return mixed|null Value if set, null otherwise. 474 */ 475 public function get_param( $key ) { 476 $order = $this->get_parameter_order(); 477 478 foreach ( $order as $type ) { 479 // Determine if we have the parameter for this type. 480 if ( isset( $this->params[ $type ][ $key ] ) ) { 481 return $this->params[ $type ][ $key ]; 482 } 483 } 484 485 return null; 486 } 487 488 /** 489 * Checks if a parameter exists in the request. 490 * 491 * This allows distinguishing between an omitted parameter, 492 * and a parameter specifically set to null. 493 * 494 * @since 5.3.0 495 * 496 * @param string $key Parameter name. 497 * @return bool True if a param exists for the given key. 498 */ 499 public function has_param( $key ) { 500 $order = $this->get_parameter_order(); 501 502 foreach ( $order as $type ) { 503 if ( is_array( $this->params[ $type ] ) && array_key_exists( $key, $this->params[ $type ] ) ) { 504 return true; 505 } 506 } 507 508 return false; 509 } 510 511 /** 512 * Sets a parameter on the request. 513 * 514 * If the given parameter key exists in any parameter type an update will take place, 515 * otherwise a new param will be created in the first parameter type (respecting 516 * get_parameter_order()). 517 * 518 * @since 4.4.0 519 * 520 * @param string $key Parameter name. 521 * @param mixed $value Parameter value. 522 */ 523 public function set_param( $key, $value ) { 524 $order = $this->get_parameter_order(); 525 $found_key = false; 526 527 foreach ( $order as $type ) { 528 if ( 'defaults' !== $type && is_array( $this->params[ $type ] ) && array_key_exists( $key, $this->params[ $type ] ) ) { 529 $this->params[ $type ][ $key ] = $value; 530 $found_key = true; 531 } 532 } 533 534 if ( ! $found_key ) { 535 $this->params[ $order[0] ][ $key ] = $value; 536 } 537 } 538 539 /** 540 * Retrieves merged parameters from the request. 541 * 542 * The equivalent of get_param(), but returns all parameters for the request. 543 * Handles merging all the available values into a single array. 544 * 545 * @since 4.4.0 546 * 547 * @return array<mixed> Map of key to value. 548 */ 549 public function get_params() { 550 $order = $this->get_parameter_order(); 551 $order = array_reverse( $order, true ); 552 553 $params = array(); 554 foreach ( $order as $type ) { 555 /* 556 * array_merge() / the "+" operator will mess up 557 * numeric keys, so instead do a manual foreach. 558 */ 559 foreach ( (array) $this->params[ $type ] as $key => $value ) { 560 $params[ $key ] = $value; 561 } 562 } 563 564 // Exclude rest_route if pretty permalinks are not enabled. 565 if ( ! get_option( 'permalink_structure' ) ) { 566 unset( $params['rest_route'] ); 567 } 568 569 return $params; 570 } 571 572 /** 573 * Retrieves parameters from the route itself. 574 * 575 * These are parsed from the URL using the regex. 576 * 577 * @since 4.4.0 578 * 579 * @return array<string, mixed> Parameter map of key to value. 580 */ 581 public function get_url_params() { 582 return $this->params['URL']; 583 } 584 585 /** 586 * Sets parameters from the route. 587 * 588 * Typically, this is set after parsing the URL. 589 * 590 * @since 4.4.0 591 * 592 * @param array<string, mixed> $params Parameter map of key to value. 593 */ 594 public function set_url_params( $params ) { 595 $this->params['URL'] = $params; 596 } 597 598 /** 599 * Retrieves parameters from the query string. 600 * 601 * These are the parameters you'd typically find in `$_GET`. 602 * 603 * @since 4.4.0 604 * 605 * @return array<mixed> Parameter map of key to value. 606 */ 607 public function get_query_params() { 608 return $this->params['GET']; 609 } 610 611 /** 612 * Sets parameters from the query string. 613 * 614 * Typically, this is set from `$_GET`. 615 * 616 * @since 4.4.0 617 * 618 * @param array<mixed> $params Parameter map of key to value. 619 */ 620 public function set_query_params( $params ) { 621 $this->params['GET'] = $params; 622 } 623 624 /** 625 * Retrieves parameters from the body. 626 * 627 * These are the parameters you'd typically find in `$_POST`. 628 * 629 * @since 4.4.0 630 * 631 * @return array<mixed> Parameter map of key to value. 632 */ 633 public function get_body_params() { 634 return $this->params['POST']; 635 } 636 637 /** 638 * Sets parameters from the body. 639 * 640 * Typically, this is set from `$_POST`. 641 * 642 * @since 4.4.0 643 * 644 * @param array<mixed> $params Parameter map of key to value. 645 */ 646 public function set_body_params( $params ) { 647 $this->params['POST'] = $params; 648 } 649 650 /** 651 * Retrieves multipart file parameters from the body. 652 * 653 * These are the parameters you'd typically find in `$_FILES`. 654 * 655 * @since 4.4.0 656 * 657 * @return array<string, array<string, mixed>> Parameter map of key to value. 658 * 659 * @phpstan-return array<string, File_Param> 660 */ 661 public function get_file_params() { 662 return $this->params['FILES']; 663 } 664 665 /** 666 * Sets multipart file parameters from the body. 667 * 668 * Typically, this is set from `$_FILES`. 669 * 670 * @since 4.4.0 671 * 672 * @param array<string, array<string, mixed>> $params Parameter map of key to value. 673 * 674 * @phpstan-param array<string, File_Param> $params 675 */ 676 public function set_file_params( $params ) { 677 $this->params['FILES'] = $params; 678 } 679 680 /** 681 * Retrieves the default parameters. 682 * 683 * These are the parameters set in the route registration. 684 * 685 * @since 4.4.0 686 * 687 * @return array<string, mixed> Parameter map of key to value. 688 */ 689 public function get_default_params() { 690 return $this->params['defaults']; 691 } 692 693 /** 694 * Sets default parameters. 695 * 696 * These are the parameters set in the route registration. 697 * 698 * @since 4.4.0 699 * 700 * @param array<string, mixed> $params Parameter map of key to value. 701 */ 702 public function set_default_params( $params ) { 703 $this->params['defaults'] = $params; 704 } 705 706 /** 707 * Retrieves the request body content. 708 * 709 * @since 4.4.0 710 * 711 * @return string|null Binary data from the request body, or null if no body has been set. 712 */ 713 public function get_body() { 714 return $this->body; 715 } 716 717 /** 718 * Sets body content. 719 * 720 * @since 4.4.0 721 * 722 * @param string $data Binary data from the request body. 723 */ 724 public function set_body( $data ) { 725 $this->body = $data; 726 727 // Enable lazy parsing. 728 $this->parsed_json = false; 729 $this->parsed_body = false; 730 $this->params['JSON'] = null; 731 } 732 733 /** 734 * Retrieves the parameters from a JSON-formatted body. 735 * 736 * @since 4.4.0 737 * 738 * @return array<mixed>|string|int|float|bool|null Parameter map of key to value, or null if the request 739 * has no valid JSON body. A JSON body that is not an 740 * object or array decodes to the scalar it contains. 741 */ 742 public function get_json_params() { 743 // Ensure the parameters have been parsed out. 744 $this->parse_json_params(); 745 746 return $this->params['JSON']; 747 } 748 749 /** 750 * Parses the JSON parameters. 751 * 752 * Avoids parsing the JSON data until we need to access it. 753 * 754 * @since 4.4.0 755 * @since 4.7.0 Returns error instance if value cannot be decoded. 756 * @return true|WP_Error True if the JSON data was passed or no JSON data was provided, WP_Error if invalid JSON was passed. 757 */ 758 protected function parse_json_params() { 759 if ( $this->parsed_json ) { 760 return true; 761 } 762 763 $this->parsed_json = true; 764 765 // Check that we actually got JSON. 766 if ( ! $this->is_json_content_type() ) { 767 return true; 768 } 769 770 $body = $this->get_body(); 771 if ( empty( $body ) ) { 772 return true; 773 } 774 775 $params = json_decode( $body, true ); 776 777 /* 778 * Check for a parsing error. 779 */ 780 if ( null === $params && JSON_ERROR_NONE !== json_last_error() ) { 781 // Ensure subsequent calls receive error instance. 782 $this->parsed_json = false; 783 784 $error_data = array( 785 'status' => WP_Http::BAD_REQUEST, 786 'json_error_code' => json_last_error(), 787 'json_error_message' => json_last_error_msg(), 788 ); 789 790 return new WP_Error( 'rest_invalid_json', __( 'Invalid JSON body passed.' ), $error_data ); 791 } 792 793 $this->params['JSON'] = $params; 794 795 return true; 796 } 797 798 /** 799 * Parses the request body parameters. 800 * 801 * Parses out URL-encoded bodies for request methods that aren't supported 802 * natively by PHP. 803 * 804 * @since 4.4.0 805 */ 806 protected function parse_body_params() { 807 if ( $this->parsed_body ) { 808 return; 809 } 810 811 $this->parsed_body = true; 812 813 /* 814 * Check that we got URL-encoded. Treat a missing Content-Type as 815 * URL-encoded for maximum compatibility. 816 */ 817 $content_type = $this->get_content_type(); 818 819 if ( ! empty( $content_type ) && 'application/x-www-form-urlencoded' !== $content_type['value'] ) { 820 return; 821 } 822 823 parse_str( $this->get_body(), $params ); 824 825 /* 826 * Add to the POST parameters stored internally. If a user has already 827 * set these manually (via `set_body_params`), don't override them. 828 */ 829 $this->params['POST'] = array_merge( $params, $this->params['POST'] ); 830 } 831 832 /** 833 * Retrieves the route that matched the request. 834 * 835 * @since 4.4.0 836 * 837 * @return string Route matching regex. 838 */ 839 public function get_route() { 840 return $this->route; 841 } 842 843 /** 844 * Sets the route that matched the request. 845 * 846 * @since 4.4.0 847 * 848 * @param string $route Route matching regex. 849 */ 850 public function set_route( $route ) { 851 $this->route = $route; 852 } 853 854 /** 855 * Retrieves the attributes for the request. 856 * 857 * These are the options for the route that was matched. 858 * 859 * @since 4.4.0 860 * 861 * @return array<string, mixed> Attributes for the request. 862 * 863 * @phpstan-return Attributes 864 */ 865 public function get_attributes() { 866 return $this->attributes; 867 } 868 869 /** 870 * Sets the attributes for the request. 871 * 872 * @since 4.4.0 873 * 874 * @param array<string, mixed> $attributes Attributes for the request. 875 * 876 * @phpstan-param Attributes $attributes 877 */ 878 public function set_attributes( $attributes ) { 879 $this->attributes = $attributes; 880 } 881 882 /** 883 * Sanitizes (where possible) the params on the request. 884 * 885 * This is primarily based off the sanitize_callback param on each registered 886 * argument. 887 * 888 * @since 4.4.0 889 * 890 * @return true|WP_Error True if parameters were sanitized, WP_Error if an error occurred during sanitization. 891 */ 892 public function sanitize_params() { 893 $attributes = $this->get_attributes(); 894 895 // No arguments set, skip sanitizing. 896 if ( empty( $attributes['args'] ) ) { 897 return true; 898 } 899 900 $order = $this->get_parameter_order(); 901 902 $invalid_params = array(); 903 $invalid_details = array(); 904 905 foreach ( $order as $type ) { 906 if ( empty( $this->params[ $type ] ) ) { 907 continue; 908 } 909 910 foreach ( $this->params[ $type ] as $key => $value ) { 911 if ( ! isset( $attributes['args'][ $key ] ) ) { 912 continue; 913 } 914 915 $param_args = $attributes['args'][ $key ]; 916 917 // If the arg has a type but no sanitize_callback attribute, default to rest_parse_request_arg. 918 if ( ! array_key_exists( 'sanitize_callback', $param_args ) && ! empty( $param_args['type'] ) ) { 919 $param_args['sanitize_callback'] = 'rest_parse_request_arg'; 920 } 921 // If there's still no sanitize_callback, nothing to do here. 922 if ( empty( $param_args['sanitize_callback'] ) ) { 923 continue; 924 } 925 926 /** @var mixed|WP_Error $sanitized_value */ 927 $sanitized_value = call_user_func( $param_args['sanitize_callback'], $value, $this, $key ); 928 929 if ( is_wp_error( $sanitized_value ) ) { 930 $invalid_params[ $key ] = implode( ' ', $sanitized_value->get_error_messages() ); 931 $invalid_details[ $key ] = rest_convert_error_to_response( $sanitized_value )->get_data(); 932 } else { 933 $this->params[ $type ][ $key ] = $sanitized_value; 934 } 935 } 936 } 937 938 if ( $invalid_params ) { 939 return new WP_Error( 940 'rest_invalid_param', 941 /* translators: %s: List of invalid parameters. */ 942 sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), 943 array( 944 'status' => 400, 945 'params' => $invalid_params, 946 'details' => $invalid_details, 947 ) 948 ); 949 } 950 951 return true; 952 } 953 954 /** 955 * Checks whether this request is valid according to its attributes. 956 * 957 * @since 4.4.0 958 * 959 * @return true|WP_Error True if there are no parameters to validate or if all pass validation, 960 * WP_Error if required parameters are missing. 961 */ 962 public function has_valid_params() { 963 // If JSON data was passed, check for errors. 964 $json_error = $this->parse_json_params(); 965 if ( is_wp_error( $json_error ) ) { 966 return $json_error; 967 } 968 969 $attributes = $this->get_attributes(); 970 $required = array(); 971 972 $args = empty( $attributes['args'] ) ? array() : $attributes['args']; 973 974 foreach ( $args as $key => $arg ) { 975 $param = $this->get_param( $key ); 976 if ( isset( $arg['required'] ) && true === $arg['required'] && null === $param ) { 977 $required[] = $key; 978 } 979 } 980 981 if ( ! empty( $required ) ) { 982 return new WP_Error( 983 'rest_missing_callback_param', 984 /* translators: %s: List of required parameters. */ 985 sprintf( __( 'Missing parameter(s): %s' ), implode( ', ', $required ) ), 986 array( 987 'status' => 400, 988 'params' => $required, 989 ) 990 ); 991 } 992 993 /* 994 * Check the validation callbacks for each registered arg. 995 * 996 * This is done after required checking as required checking is cheaper. 997 */ 998 $invalid_params = array(); 999 $invalid_details = array(); 1000 1001 foreach ( $args as $key => $arg ) { 1002 1003 $param = $this->get_param( $key ); 1004 1005 if ( null !== $param && ! empty( $arg['validate_callback'] ) ) { 1006 /** @var bool|\WP_Error $valid_check */ 1007 $valid_check = call_user_func( $arg['validate_callback'], $param, $this, $key ); 1008 1009 if ( false === $valid_check ) { 1010 $invalid_params[ $key ] = __( 'Invalid parameter.' ); 1011 } 1012 1013 if ( is_wp_error( $valid_check ) ) { 1014 $invalid_params[ $key ] = implode( ' ', $valid_check->get_error_messages() ); 1015 $invalid_details[ $key ] = rest_convert_error_to_response( $valid_check )->get_data(); 1016 } 1017 } 1018 } 1019 1020 if ( $invalid_params ) { 1021 return new WP_Error( 1022 'rest_invalid_param', 1023 /* translators: %s: List of invalid parameters. */ 1024 sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), 1025 array( 1026 'status' => 400, 1027 'params' => $invalid_params, 1028 'details' => $invalid_details, 1029 ) 1030 ); 1031 } 1032 1033 if ( isset( $attributes['validate_callback'] ) ) { 1034 $valid_check = call_user_func( $attributes['validate_callback'], $this ); 1035 1036 if ( is_wp_error( $valid_check ) ) { 1037 return $valid_check; 1038 } 1039 1040 if ( false === $valid_check ) { 1041 // A WP_Error instance is preferred, but false is supported for parity with the per-arg validate_callback. 1042 return new WP_Error( 'rest_invalid_params', __( 'Invalid parameters.' ), array( 'status' => 400 ) ); 1043 } 1044 } 1045 1046 return true; 1047 } 1048 1049 /** 1050 * Checks if a parameter is set. 1051 * 1052 * @since 4.4.0 1053 * 1054 * @param string $offset Parameter name. 1055 * @return bool Whether the parameter is set. 1056 */ 1057 #[ReturnTypeWillChange] 1058 public function offsetExists( $offset ) { 1059 $order = $this->get_parameter_order(); 1060 1061 foreach ( $order as $type ) { 1062 if ( isset( $this->params[ $type ][ $offset ] ) ) { 1063 return true; 1064 } 1065 } 1066 1067 return false; 1068 } 1069 1070 /** 1071 * Retrieves a parameter from the request. 1072 * 1073 * @since 4.4.0 1074 * 1075 * @param string $offset Parameter name. 1076 * @return mixed|null Value if set, null otherwise. 1077 */ 1078 #[ReturnTypeWillChange] 1079 public function offsetGet( $offset ) { 1080 return $this->get_param( $offset ); 1081 } 1082 1083 /** 1084 * Sets a parameter on the request. 1085 * 1086 * @since 4.4.0 1087 * 1088 * @param string $offset Parameter name. 1089 * @param mixed $value Parameter value. 1090 */ 1091 #[ReturnTypeWillChange] 1092 public function offsetSet( $offset, $value ) { 1093 $this->set_param( $offset, $value ); 1094 } 1095 1096 /** 1097 * Removes a parameter from the request. 1098 * 1099 * @since 4.4.0 1100 * 1101 * @param string $offset Parameter name. 1102 */ 1103 #[ReturnTypeWillChange] 1104 public function offsetUnset( $offset ) { 1105 $order = $this->get_parameter_order(); 1106 1107 // Remove the offset from every group. 1108 foreach ( $order as $type ) { 1109 unset( $this->params[ $type ][ $offset ] ); 1110 } 1111 } 1112 1113 /** 1114 * Retrieves a WP_REST_Request object from a full URL. 1115 * 1116 * @since 4.5.0 1117 * 1118 * @param string $url URL with protocol, domain, path and query args. 1119 * @return WP_REST_Request|false WP_REST_Request object on success, false on failure. 1120 */ 1121 public static function from_url( $url ) { 1122 $bits = parse_url( $url ); 1123 $query_params = array(); 1124 1125 if ( ! empty( $bits['query'] ) ) { 1126 wp_parse_str( $bits['query'], $query_params ); 1127 } 1128 1129 $api_root = rest_url(); 1130 if ( get_option( 'permalink_structure' ) && str_starts_with( $url, $api_root ) ) { 1131 // Pretty permalinks on, and URL is under the API root. 1132 $api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) ); 1133 $route = parse_url( $api_url_part, PHP_URL_PATH ); 1134 } elseif ( ! empty( $query_params['rest_route'] ) ) { 1135 // ?rest_route=... set directly. 1136 $route = $query_params['rest_route']; 1137 unset( $query_params['rest_route'] ); 1138 } 1139 1140 $request = false; 1141 if ( ! empty( $route ) ) { 1142 $request = new WP_REST_Request( 'GET', $route ); 1143 $request->set_query_params( $query_params ); 1144 } 1145 1146 /** 1147 * Filters the REST API request generated from a URL. 1148 * 1149 * @since 4.5.0 1150 * 1151 * @param WP_REST_Request|false $request Generated request object, or false if URL 1152 * could not be parsed. 1153 * @param string $url URL the request was generated from. 1154 */ 1155 return apply_filters( 'rest_request_from_url', $request, $url ); 1156 } 1157 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Nov 25 08:20:01 2026 | Cross-referenced by PHPXref |