[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/rest-api/ -> class-wp-rest-request.php (source)

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


Generated: Sat Sep 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1