/** * Class Jetpack_Constants * * Testing constants is hard. Once you define a constant, it's defined. Jetpack_Constants is an * abstraction layer so that unit tests can set "constants" for tests. * * To test your code, you'll need to swap out `defined( 'CONSTANT' )` with `Jetpack_Constants::is_defined( 'CONSTANT' )` * and replace `CONSTANT` with `Jetpack_Constants::get_constant( 'CONSTANT' )`. Then in the unit test, you can set the * constant with `Jetpack::set_constant( 'CONSTANT', $value )` and then clean up after each test with something like * this: * * function tearDown() { * Jetpack_Constants::clear_constants(); * } */ class Jetpack_Constants { static $set_constants = array(); /** * Checks if a "constant" has been set in Jetpack_Constants, and if not, * checks if the constant was defined with define( 'name', 'value ). * * @param $name string The name of the constant * * @return bool */ public static function is_defined( $name ) { return array_key_exists( $name, self::$set_constants ) ? true : defined( $name ); } /** * Checks if a "constant" has been set in Jetpack_Constants * and has the value of true * * @param $name string The name of the constant * * @return bool */ public static function is_true( $name ) { return self::is_defined( $name) && self::get_constant( $name ); } /** * Attempts to retrieve the "constant" from Jetpack_Constants, and if it hasn't been set, * then attempts to get the constant with the constant() function. * * @param $name * * @return mixed null if the constant does not exist or the value of the constant. */ public static function get_constant( $name ) { if ( array_key_exists( $name, self::$set_constants ) ) { return self::$set_constants[ $name ]; } return defined( $name ) ? constant( $name ) : null; } /** * Sets the value of the "constant" within Jetpack_Constants. * * @param $name string The name of the "constant" * @param $value string The value of the "constant" */ public static function set_constant( $name, $value ) { self::$set_constants[ $name ] = $value; } /** * Will unset a "constant" from Jetpack_Constants if the constant exists. * * @param $name string The name of the "constant" * * @return bool Whether the constant was removed. */ public static function clear_single_constant( $name ) { if ( ! array_key_exists( $name, self::$set_constants ) ) { return false; } unset( self::$set_constants[ $name ] ); return true; } /** * Resets all of the constants within Jetpack_Constants. */ public static function clear_constants() { self::$set_constants = array(); } } /** * HTTP response class * * Contains a response from Requests::request() * @package Requests */ /** * HTTP response class * * Contains a response from Requests::request() * @package Requests */ class Requests_Response { /** * Constructor */ public function __construct() { $this->headers = new Requests_Response_Headers(); $this->cookies = new Requests_Cookie_Jar(); } /** * Response body * * @var string */ public $body = ''; /** * Raw HTTP data from the transport * * @var string */ public $raw = ''; /** * Headers, as an associative array * * @var Requests_Response_Headers Array-like object representing headers */ public $headers = array(); /** * Status code, false if non-blocking * * @var integer|boolean */ public $status_code = false; /** * Protocol version, false if non-blocking * @var float|boolean */ public $protocol_version = false; /** * Whether the request succeeded or not * * @var boolean */ public $success = false; /** * Number of redirects the request used * * @var integer */ public $redirects = 0; /** * URL requested * * @var string */ public $url = ''; /** * Previous requests (from redirects) * * @var array Array of Requests_Response objects */ public $history = array(); /** * Cookies from the request * * @var Requests_Cookie_Jar Array-like object representing a cookie jar */ public $cookies = array(); /** * Is the response a redirect? * * @return boolean True if redirect (3xx status), false if not. */ public function is_redirect() { $code = $this->status_code; return in_array($code, array(300, 301, 302, 303, 307)) || $code > 307 && $code < 400; } /** * Throws an exception if the request was not successful * * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`) * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404}) * @param boolean $allow_redirects Set to false to throw on a 3xx as well */ public function throw_for_status($allow_redirects = true) { if ($this->is_redirect()) { if (!$allow_redirects) { throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this); } } elseif (!$this->success) { $exception = Requests_Exception_HTTP::get_class($this->status_code); throw new $exception(null, $this); } } }