wp/wp-includes/rest-api/class-wp-rest-request.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
   109 	 * @param string $route      Optional. Request route. Default empty.
   109 	 * @param string $route      Optional. Request route. Default empty.
   110 	 * @param array  $attributes Optional. Request attributes. Default empty array.
   110 	 * @param array  $attributes Optional. Request attributes. Default empty array.
   111 	 */
   111 	 */
   112 	public function __construct( $method = '', $route = '', $attributes = array() ) {
   112 	public function __construct( $method = '', $route = '', $attributes = array() ) {
   113 		$this->params = array(
   113 		$this->params = array(
   114 			'URL'   => array(),
   114 			'URL'      => array(),
   115 			'GET'   => array(),
   115 			'GET'      => array(),
   116 			'POST'  => array(),
   116 			'POST'     => array(),
   117 			'FILES' => array(),
   117 			'FILES'    => array(),
   118 
   118 
   119 			// See parse_json_params.
   119 			// See parse_json_params.
   120 			'JSON'  => null,
   120 			'JSON'     => null,
   121 
   121 
   122 			'defaults' => array(),
   122 			'defaults' => array(),
   123 		);
   123 		);
   124 
   124 
   125 		$this->set_method( $method );
   125 		$this->set_method( $method );
   172 	 * @link https://stackoverflow.com/q/18185366
   172 	 * @link https://stackoverflow.com/q/18185366
   173 	 * @link https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#missing-disappearing-http-headers
   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
   174 	 * @link https://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers
   175 	 *
   175 	 *
   176 	 * @since 4.4.0
   176 	 * @since 4.4.0
   177 	 * @static
       
   178 	 *
   177 	 *
   179 	 * @param string $key Header name.
   178 	 * @param string $key Header name.
   180 	 * @return string Canonicalized name.
   179 	 * @return string Canonicalized name.
   181 	 */
   180 	 */
   182 	public static function canonicalize_header_name( $key ) {
   181 	public static function canonicalize_header_name( $key ) {
   233 	 *
   232 	 *
   234 	 * @param string $key   Header name.
   233 	 * @param string $key   Header name.
   235 	 * @param string $value Header value, or list of values.
   234 	 * @param string $value Header value, or list of values.
   236 	 */
   235 	 */
   237 	public function set_header( $key, $value ) {
   236 	public function set_header( $key, $value ) {
   238 		$key = $this->canonicalize_header_name( $key );
   237 		$key   = $this->canonicalize_header_name( $key );
   239 		$value = (array) $value;
   238 		$value = (array) $value;
   240 
   239 
   241 		$this->headers[ $key ] = $value;
   240 		$this->headers[ $key ] = $value;
   242 	}
   241 	}
   243 
   242 
   248 	 *
   247 	 *
   249 	 * @param string $key   Header name.
   248 	 * @param string $key   Header name.
   250 	 * @param string $value Header value, or list of values.
   249 	 * @param string $value Header value, or list of values.
   251 	 */
   250 	 */
   252 	public function add_header( $key, $value ) {
   251 	public function add_header( $key, $value ) {
   253 		$key = $this->canonicalize_header_name( $key );
   252 		$key   = $this->canonicalize_header_name( $key );
   254 		$value = (array) $value;
   253 		$value = (array) $value;
   255 
   254 
   256 		if ( ! isset( $this->headers[ $key ] ) ) {
   255 		if ( ! isset( $this->headers[ $key ] ) ) {
   257 			$this->headers[ $key ] = array();
   256 			$this->headers[ $key ] = array();
   258 		}
   257 		}
   366 		 * @since 4.4.0
   365 		 * @since 4.4.0
   367 		 *
   366 		 *
   368 		 * @param array           $order {
   367 		 * @param array           $order {
   369 		 *    An array of types to check, in order of priority.
   368 		 *    An array of types to check, in order of priority.
   370 		 *
   369 		 *
   371 		 *    @param string $type The type to check.
   370 		 * @param string $type The type to check.
   372 		 * }
   371 		 * }
   373 		 * @param WP_REST_Request $this The request object.
   372 		 * @param WP_REST_Request $this The request object.
   374 		 */
   373 		 */
   375 		return apply_filters( 'rest_request_parameter_order', $order, $this );
   374 		return apply_filters( 'rest_request_parameter_order', $order, $this );
   376 	}
   375 	}
   403 	 *
   402 	 *
   404 	 * @param string $key   Parameter name.
   403 	 * @param string $key   Parameter name.
   405 	 * @param mixed  $value Parameter value.
   404 	 * @param mixed  $value Parameter value.
   406 	 */
   405 	 */
   407 	public function set_param( $key, $value ) {
   406 	public function set_param( $key, $value ) {
   408 		$order = $this->get_parameter_order();
   407 		$order                             = $this->get_parameter_order();
   409 		$this->params[ $order[0] ][ $key ] = $value;
   408 		$this->params[ $order[0] ][ $key ] = $value;
   410 	}
   409 	}
   411 
   410 
   412 	/**
   411 	/**
   413 	 * Retrieves merged parameters from the request.
   412 	 * Retrieves merged parameters from the request.
   585 	 */
   584 	 */
   586 	public function set_body( $data ) {
   585 	public function set_body( $data ) {
   587 		$this->body = $data;
   586 		$this->body = $data;
   588 
   587 
   589 		// Enable lazy parsing.
   588 		// Enable lazy parsing.
   590 		$this->parsed_json = false;
   589 		$this->parsed_json    = false;
   591 		$this->parsed_body = false;
   590 		$this->parsed_body    = false;
   592 		$this->params['JSON'] = null;
   591 		$this->params['JSON'] = null;
   593 	}
   592 	}
   594 
   593 
   595 	/**
   594 	/**
   596 	 * Retrieves the parameters from a JSON-formatted body.
   595 	 * Retrieves the parameters from a JSON-formatted body.
   648 
   647 
   649 			$error_data = array(
   648 			$error_data = array(
   650 				'status' => WP_Http::BAD_REQUEST,
   649 				'status' => WP_Http::BAD_REQUEST,
   651 			);
   650 			);
   652 			if ( function_exists( 'json_last_error' ) ) {
   651 			if ( function_exists( 'json_last_error' ) ) {
   653 				$error_data['json_error_code'] = json_last_error();
   652 				$error_data['json_error_code']    = json_last_error();
   654 				$error_data['json_error_message'] = json_last_error_msg();
   653 				$error_data['json_error_message'] = json_last_error_msg();
   655 			}
   654 			}
   656 
   655 
   657 			return new WP_Error( 'rest_invalid_json', __( 'Invalid JSON body passed.' ), $error_data );
   656 			return new WP_Error( 'rest_invalid_json', __( 'Invalid JSON body passed.' ), $error_data );
   658 		}
   657 		}
   800 				}
   799 				}
   801 			}
   800 			}
   802 		}
   801 		}
   803 
   802 
   804 		if ( $invalid_params ) {
   803 		if ( $invalid_params ) {
   805 			return new WP_Error( 'rest_invalid_param', sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), array( 'status' => 400, 'params' => $invalid_params ) );
   804 			return new WP_Error(
       
   805 				'rest_invalid_param',
       
   806 				sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ),
       
   807 				array(
       
   808 					'status' => 400,
       
   809 					'params' => $invalid_params,
       
   810 				)
       
   811 			);
   806 		}
   812 		}
   807 
   813 
   808 		return true;
   814 		return true;
   809 	}
   815 	}
   810 
   816 
   822 		if ( is_wp_error( $json_error ) ) {
   828 		if ( is_wp_error( $json_error ) ) {
   823 			return $json_error;
   829 			return $json_error;
   824 		}
   830 		}
   825 
   831 
   826 		$attributes = $this->get_attributes();
   832 		$attributes = $this->get_attributes();
   827 		$required = array();
   833 		$required   = array();
   828 
   834 
   829 		// No arguments set, skip validation.
   835 		// No arguments set, skip validation.
   830 		if ( empty( $attributes['args'] ) ) {
   836 		if ( empty( $attributes['args'] ) ) {
   831 			return true;
   837 			return true;
   832 		}
   838 		}
   838 				$required[] = $key;
   844 				$required[] = $key;
   839 			}
   845 			}
   840 		}
   846 		}
   841 
   847 
   842 		if ( ! empty( $required ) ) {
   848 		if ( ! empty( $required ) ) {
   843 			return new WP_Error( 'rest_missing_callback_param', sprintf( __( 'Missing parameter(s): %s' ), implode( ', ', $required ) ), array( 'status' => 400, 'params' => $required ) );
   849 			return new WP_Error(
       
   850 				'rest_missing_callback_param',
       
   851 				sprintf( __( 'Missing parameter(s): %s' ), implode( ', ', $required ) ),
       
   852 				array(
       
   853 					'status' => 400,
       
   854 					'params' => $required,
       
   855 				)
       
   856 			);
   844 		}
   857 		}
   845 
   858 
   846 		/*
   859 		/*
   847 		 * Check the validation callbacks for each registered arg.
   860 		 * Check the validation callbacks for each registered arg.
   848 		 *
   861 		 *
   866 				}
   879 				}
   867 			}
   880 			}
   868 		}
   881 		}
   869 
   882 
   870 		if ( $invalid_params ) {
   883 		if ( $invalid_params ) {
   871 			return new WP_Error( 'rest_invalid_param', sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), array( 'status' => 400, 'params' => $invalid_params ) );
   884 			return new WP_Error(
       
   885 				'rest_invalid_param',
       
   886 				sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ),
       
   887 				array(
       
   888 					'status' => 400,
       
   889 					'params' => $invalid_params,
       
   890 				)
       
   891 			);
   872 		}
   892 		}
   873 
   893 
   874 		return true;
   894 		return true;
   875 
   895 
   876 	}
   896 	}
   936 	}
   956 	}
   937 
   957 
   938 	/**
   958 	/**
   939 	 * Retrieves a WP_REST_Request object from a full URL.
   959 	 * Retrieves a WP_REST_Request object from a full URL.
   940 	 *
   960 	 *
   941 	 * @static
       
   942 	 * @since 4.5.0
   961 	 * @since 4.5.0
   943 	 *
   962 	 *
   944 	 * @param string $url URL with protocol, domain, path and query args.
   963 	 * @param string $url URL with protocol, domain, path and query args.
   945 	 * @return WP_REST_Request|false WP_REST_Request object on success, false on failure.
   964 	 * @return WP_REST_Request|false WP_REST_Request object on success, false on failure.
   946 	 */
   965 	 */
   947 	public static function from_url( $url ) {
   966 	public static function from_url( $url ) {
   948 		$bits = parse_url( $url );
   967 		$bits         = parse_url( $url );
   949 		$query_params = array();
   968 		$query_params = array();
   950 
   969 
   951 		if ( ! empty( $bits['query'] ) ) {
   970 		if ( ! empty( $bits['query'] ) ) {
   952 			wp_parse_str( $bits['query'], $query_params );
   971 			wp_parse_str( $bits['query'], $query_params );
   953 		}
   972 		}
   954 
   973 
   955 		$api_root = rest_url();
   974 		$api_root = rest_url();
   956 		if ( get_option( 'permalink_structure' ) && 0 === strpos( $url, $api_root ) ) {
   975 		if ( get_option( 'permalink_structure' ) && 0 === strpos( $url, $api_root ) ) {
   957 			// Pretty permalinks on, and URL is under the API root.
   976 			// Pretty permalinks on, and URL is under the API root.
   958 			$api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) );
   977 			$api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) );
   959 			$route = parse_url( $api_url_part, PHP_URL_PATH );
   978 			$route        = parse_url( $api_url_part, PHP_URL_PATH );
   960 		} elseif ( ! empty( $query_params['rest_route'] ) ) {
   979 		} elseif ( ! empty( $query_params['rest_route'] ) ) {
   961 			// ?rest_route=... set directly
   980 			// ?rest_route=... set directly
   962 			$route = $query_params['rest_route'];
   981 			$route = $query_params['rest_route'];
   963 			unset( $query_params['rest_route'] );
   982 			unset( $query_params['rest_route'] );
   964 		}
   983 		}