class-wp-http-response.php 2.9 KB

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