You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

166 lines
3.0 KiB

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