/wp-includes/class-wp-http-response.php

  1. <?php 
  2. /** 
  3. * HTTP API: WP_HTTP_Response class 
  4. * 
  5. * @package WordPress 
  6. * @subpackage HTTP 
  7. * @since 4.4.0 
  8. */ 
  9.  
  10. /** 
  11. * Core class used to prepare HTTP responses. 
  12. * 
  13. * @since 4.4.0 
  14. */ 
  15. class WP_HTTP_Response { 
  16.  
  17. /** 
  18. * Response data. 
  19. * 
  20. * @since 4.4.0 
  21. * @access public 
  22. * @var mixed 
  23. */ 
  24. public $data; 
  25.  
  26. /** 
  27. * Response headers. 
  28. * 
  29. * @since 4.4.0 
  30. * @access public 
  31. * @var array 
  32. */ 
  33. public $headers; 
  34.  
  35. /** 
  36. * Response status. 
  37. * 
  38. * @since 4.4.0 
  39. * @access public 
  40. * @var int 
  41. */ 
  42. public $status; 
  43.  
  44. /** 
  45. * Constructor. 
  46. * 
  47. * @since 4.4.0 
  48. * @access public 
  49. * 
  50. * @param mixed $data Response data. Default null. 
  51. * @param int $status Optional. HTTP status code. Default 200. 
  52. * @param array $headers Optional. HTTP header map. Default empty array. 
  53. */ 
  54. public function __construct( $data = null, $status = 200, $headers = array() ) { 
  55. $this->data = $data; 
  56. $this->set_status( $status ); 
  57. $this->set_headers( $headers ); 
  58.  
  59. /** 
  60. * Retrieves headers associated with the response. 
  61. * 
  62. * @since 4.4.0 
  63. * @access public 
  64. * 
  65. * @return array Map of header name to header value. 
  66. */ 
  67. public function get_headers() { 
  68. return $this->headers; 
  69.  
  70. /** 
  71. * Sets all header values. 
  72. * 
  73. * @since 4.4.0 
  74. * @access public 
  75. * 
  76. * @param array $headers Map of header name to header value. 
  77. */ 
  78. public function set_headers( $headers ) { 
  79. $this->headers = $headers; 
  80.  
  81. /** 
  82. * Sets a single HTTP header. 
  83. * 
  84. * @since 4.4.0 
  85. * @access public 
  86. * 
  87. * @param string $key Header name. 
  88. * @param string $value Header value. 
  89. * @param bool $replace Optional. Whether to replace an existing header of the same name. 
  90. * Default true. 
  91. */ 
  92. public function header( $key, $value, $replace = true ) { 
  93. if ( $replace || ! isset( $this->headers[ $key ] ) ) { 
  94. $this->headers[ $key ] = $value; 
  95. } else { 
  96. $this->headers[ $key ] .= ', ' . $value; 
  97.  
  98. /** 
  99. * Retrieves the HTTP return code for the response. 
  100. * 
  101. * @since 4.4.0 
  102. * @access public 
  103. * 
  104. * @return int The 3-digit HTTP status code. 
  105. */ 
  106. public function get_status() { 
  107. return $this->status; 
  108.  
  109. /** 
  110. * Sets the 3-digit HTTP status code. 
  111. * 
  112. * @since 4.4.0 
  113. * @access public 
  114. * 
  115. * @param int $code HTTP status. 
  116. */ 
  117. public function set_status( $code ) { 
  118. $this->status = absint( $code ); 
  119.  
  120. /** 
  121. * Retrieves the response data. 
  122. * 
  123. * @since 4.4.0 
  124. * @access public 
  125. * 
  126. * @return mixed Response data. 
  127. */ 
  128. public function get_data() { 
  129. return $this->data; 
  130.  
  131. /** 
  132. * Sets the response data. 
  133. * 
  134. * @since 4.4.0 
  135. * @access public 
  136. * 
  137. * @param mixed $data Response data. 
  138. */ 
  139. public function set_data( $data ) { 
  140. $this->data = $data; 
  141.  
  142. /** 
  143. * Retrieves the response data for JSON serialization. 
  144. * 
  145. * It is expected that in most implementations, this will return the same as get_data(),  
  146. * however this may be different if you want to do custom JSON data handling. 
  147. * 
  148. * @since 4.4.0 
  149. * @access public 
  150. * 
  151. * @return mixed Any JSON-serializable value. 
  152. */ 
  153. public function jsonSerialize() { 
  154. return $this->get_data(); 
.