home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / class-wp-http-response.php < prev    next >
Encoding:
PHP Script  |  2017-10-03  |  2.8 KB  |  154 lines

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