[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-http-response.php (source)

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


Generated : Fri Oct 30 08:20:34 2026 Cross-referenced by PHPXref