wp/wp-includes/class-wp-http-requests-response.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 <?php
       
     2 /**
       
     3  * HTTP API: WP_HTTP_Requests_Response class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage HTTP
       
     7  * @since 4.6.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core wrapper object for a Requests_Response for standardisation.
       
    12  *
       
    13  * @since 4.6.0
       
    14  *
       
    15  * @see WP_HTTP_Response
       
    16  */
       
    17 class WP_HTTP_Requests_Response extends WP_HTTP_Response {
       
    18 	/**
       
    19 	 * Requests Response object.
       
    20 	 *
       
    21 	 * @since 4.6.0
       
    22 	 * @var Requests_Response
       
    23 	 */
       
    24 	protected $response;
       
    25 
       
    26 	/**
       
    27 	 * Filename the response was saved to.
       
    28 	 *
       
    29 	 * @since 4.6.0
       
    30 	 * @var string|null
       
    31 	 */
       
    32 	protected $filename;
       
    33 
       
    34 	/**
       
    35 	 * Constructor.
       
    36 	 *
       
    37 	 * @since 4.6.0
       
    38 	 *
       
    39 	 * @param Requests_Response $response HTTP response.
       
    40 	 * @param string            $filename Optional. File name. Default empty.
       
    41 	 */
       
    42 	public function __construct( Requests_Response $response, $filename = '' ) {
       
    43 		$this->response = $response;
       
    44 		$this->filename = $filename;
       
    45 	}
       
    46 
       
    47 	/**
       
    48 	 * Retrieves the response object for the request.
       
    49 	 *
       
    50 	 * @since 4.6.0
       
    51 	 *
       
    52 	 * @return Requests_Response HTTP response.
       
    53 	 */
       
    54 	public function get_response_object() {
       
    55 		return $this->response;
       
    56 	}
       
    57 
       
    58 	/**
       
    59 	 * Retrieves headers associated with the response.
       
    60 	 *
       
    61 	 * @since 4.6.0
       
    62 	 *
       
    63 	 * @see \Requests_Utility_CaseInsensitiveDictionary
       
    64 	 *
       
    65 	 * @return \Requests_Utility_CaseInsensitiveDictionary Map of header name to header value.
       
    66 	 */
       
    67 	public function get_headers() {
       
    68 		// Ensure headers remain case-insensitive.
       
    69 		$converted = new Requests_Utility_CaseInsensitiveDictionary();
       
    70 
       
    71 		foreach ( $this->response->headers->getAll() as $key => $value ) {
       
    72 			if ( count( $value ) === 1 ) {
       
    73 				$converted[ $key ] = $value[0];
       
    74 			} else {
       
    75 				$converted[ $key ] = $value;
       
    76 			}
       
    77 		}
       
    78 
       
    79 		return $converted;
       
    80 	}
       
    81 
       
    82 	/**
       
    83 	 * Sets all header values.
       
    84 	 *
       
    85 	 * @since 4.6.0
       
    86 	 *
       
    87 	 * @param array $headers Map of header name to header value.
       
    88 	 */
       
    89 	public function set_headers( $headers ) {
       
    90 		$this->response->headers = new Requests_Response_Headers( $headers );
       
    91 	}
       
    92 
       
    93 	/**
       
    94 	 * Sets a single HTTP header.
       
    95 	 *
       
    96 	 * @since 4.6.0
       
    97 	 *
       
    98 	 * @param string $key     Header name.
       
    99 	 * @param string $value   Header value.
       
   100 	 * @param bool   $replace Optional. Whether to replace an existing header of the same name.
       
   101 	 *                        Default true.
       
   102 	 */
       
   103 	public function header( $key, $value, $replace = true ) {
       
   104 		if ( $replace ) {
       
   105 			unset( $this->response->headers[ $key ] );
       
   106 		}
       
   107 
       
   108 		$this->response->headers[ $key ] = $value;
       
   109 	}
       
   110 
       
   111 	/**
       
   112 	 * Retrieves the HTTP return code for the response.
       
   113 	 *
       
   114 	 * @since 4.6.0
       
   115 	 *
       
   116 	 * @return int The 3-digit HTTP status code.
       
   117 	 */
       
   118 	public function get_status() {
       
   119 		return $this->response->status_code;
       
   120 	}
       
   121 
       
   122 	/**
       
   123 	 * Sets the 3-digit HTTP status code.
       
   124 	 *
       
   125 	 * @since 4.6.0
       
   126 	 *
       
   127 	 * @param int $code HTTP status.
       
   128 	 */
       
   129 	public function set_status( $code ) {
       
   130 		$this->response->status_code = absint( $code );
       
   131 	}
       
   132 
       
   133 	/**
       
   134 	 * Retrieves the response data.
       
   135 	 *
       
   136 	 * @since 4.6.0
       
   137 	 *
       
   138 	 * @return mixed Response data.
       
   139 	 */
       
   140 	public function get_data() {
       
   141 		return $this->response->body;
       
   142 	}
       
   143 
       
   144 	/**
       
   145 	 * Sets the response data.
       
   146 	 *
       
   147 	 * @since 4.6.0
       
   148 	 *
       
   149 	 * @param mixed $data Response data.
       
   150 	 */
       
   151 	public function set_data( $data ) {
       
   152 		$this->response->body = $data;
       
   153 	}
       
   154 
       
   155 	/**
       
   156 	 * Retrieves cookies from the response.
       
   157 	 *
       
   158 	 * @since 4.6.0
       
   159 	 *
       
   160 	 * @return WP_HTTP_Cookie[] List of cookie objects.
       
   161 	 */
       
   162 	public function get_cookies() {
       
   163 		$cookies = array();
       
   164 		foreach ( $this->response->cookies as $cookie ) {
       
   165 			$cookies[] = new WP_Http_Cookie( array(
       
   166 				'name'    => $cookie->name,
       
   167 				'value'   => urldecode( $cookie->value ),
       
   168 				'expires' => isset( $cookie->attributes['expires'] ) ? $cookie->attributes['expires'] : null,
       
   169 				'path'    => isset( $cookie->attributes['path'] ) ? $cookie->attributes['path'] : null,
       
   170 				'domain'  => isset( $cookie->attributes['domain'] ) ? $cookie->attributes['domain'] : null,
       
   171 			));
       
   172 		}
       
   173 
       
   174 		return $cookies;
       
   175 	}
       
   176 
       
   177 	/**
       
   178 	 * Converts the object to a WP_Http response array.
       
   179 	 *
       
   180 	 * @since 4.6.0
       
   181 	 *
       
   182 	 * @return array WP_Http response array, per WP_Http::request().
       
   183 	 */
       
   184 	public function to_array() {
       
   185 		return array(
       
   186 			'headers' => $this->get_headers(),
       
   187 			'body' => $this->get_data(),
       
   188 			'response' => array(
       
   189 				'code'    => $this->get_status(),
       
   190 				'message' => get_status_header_desc( $this->get_status() ),
       
   191 			),
       
   192 			'cookies' => $this->get_cookies(),
       
   193 			'filename' => $this->filename,
       
   194 		);
       
   195 	}
       
   196 }