class-wp-http-response.php 2.9 KB

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