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