| 1 | <?php |
| 2 | /** |
| 3 | * WordPress Request API. |
| 4 | * |
| 5 | * @package WordPress |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * WordPress Request class. |
| 10 | * |
| 11 | * Core class used to implement unslashed GET, POST, COOKIE, SERVER, REQUEST functionality. |
| 12 | * |
| 13 | * @since 5.0.0 |
| 14 | */ |
| 15 | final class WP_Request { |
| 16 | /** |
| 17 | * Unslashed GET superglobal. |
| 18 | * |
| 19 | * @since 5.0.0 |
| 20 | * @var array |
| 21 | */ |
| 22 | private $get = array(); |
| 23 | |
| 24 | /** |
| 25 | * Unslashed POST superglobal. |
| 26 | * |
| 27 | * @since 5.0.0 |
| 28 | * @var array |
| 29 | */ |
| 30 | private $post = array(); |
| 31 | |
| 32 | /** |
| 33 | * Unslashed COOKIE superglobal. |
| 34 | * |
| 35 | * @since 5.0.0 |
| 36 | * @var array |
| 37 | */ |
| 38 | private $cookie = array(); |
| 39 | |
| 40 | /** |
| 41 | * Unslashed server superglobal. |
| 42 | * |
| 43 | * @since 5.0.0 |
| 44 | * @var array |
| 45 | */ |
| 46 | private $server = array(); |
| 47 | |
| 48 | /** |
| 49 | * Unslashed REQUEST superglobal. |
| 50 | * |
| 51 | * @since 5.0.0 |
| 52 | * @var array |
| 53 | */ |
| 54 | private $request = array(); |
| 55 | |
| 56 | /** |
| 57 | * Static instance of this class. |
| 58 | * |
| 59 | * @since 5.0.0 |
| 60 | * @var $this |
| 61 | */ |
| 62 | private static $instance = null; |
| 63 | |
| 64 | /** |
| 65 | * Class constructor. Set superglobals. |
| 66 | * |
| 67 | * @since 5.0.0 |
| 68 | */ |
| 69 | private function __construct() { |
| 70 | $this->get = $_GET; |
| 71 | $this->post = $_POST; |
| 72 | $this->cookie = $_COOKIE; |
| 73 | $this->server = $_SERVER; |
| 74 | $this->request = $_REQUEST; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Return an instance of the WP_Request class. |
| 79 | * |
| 80 | * @since 5.0.0 |
| 81 | * |
| 82 | * @return WP_Request An instance of this class. |
| 83 | */ |
| 84 | public static function getInstance() { |
| 85 | if( null === self::$instance ) { |
| 86 | self::$instance = new WP_Request(); |
| 87 | } |
| 88 | return self::$instance; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Declared to prevent cloning of the class. |
| 93 | * |
| 94 | * @since 5.0.0 |
| 95 | */ |
| 96 | private function __clone() {} |
| 97 | |
| 98 | /** |
| 99 | * Declared to prevent unserializing of the class. |
| 100 | * |
| 101 | * @since 5.0.0 |
| 102 | */ |
| 103 | private function __wakeup() {} |
| 104 | |
| 105 | /** |
| 106 | * Retrieve an unslashed GET variable. |
| 107 | * |
| 108 | * @since 5.0.0 |
| 109 | * @param string $key GET variable string. |
| 110 | * @return mixed GET variable if one is set. Otherwise null. |
| 111 | */ |
| 112 | public function get( $key ) { |
| 113 | return $this->retrieve( 'get', $key ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Retrieve an unslashed POST variable. |
| 118 | * |
| 119 | * @since 5.0.0 |
| 120 | * @param string $key POST variable string. |
| 121 | * @return mixed POST variable if one is set. Otherwise null. |
| 122 | */ |
| 123 | public function post( $key ) { |
| 124 | return $this->retrieve( 'post', $key ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Retrieve an unslashed COOKIE variable. |
| 129 | * |
| 130 | * @since 5.0.0 |
| 131 | * @param string $key COOKIE variable string. |
| 132 | * @return mixed COOKIE variable if one is set. Otherwise null. |
| 133 | */ |
| 134 | public function cookie( $key ) { |
| 135 | return $this->retrieve( 'cookie', $key ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Retrieve an unslashed SERVER variable. |
| 140 | * |
| 141 | * @since 5.0.0 |
| 142 | * @param string $key SERVER variable string. |
| 143 | * @return mixed SERVER variable if one is set. Otherwise null. |
| 144 | */ |
| 145 | public function server( $key ) { |
| 146 | return $this->retrieve( 'server', $key ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Retrieve an unslashed REQUEST variable. |
| 151 | * |
| 152 | * @since 5.0.0 |
| 153 | * @param string $key REQUEST variable string. |
| 154 | * @return mixed REQUEST variable if one is set. Otherwise null. |
| 155 | */ |
| 156 | public function request( $key ) { |
| 157 | return $this->retrieve( 'request', $key ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Retrieve an unslashed variable. |
| 162 | * |
| 163 | * @since 5.0.0 |
| 164 | * @param string $kind Which of the globals to retrieve: get, post, cookie, server, or request. |
| 165 | * @param string $key Variable string. |
| 166 | * @return mixed Variable if one is set. Otherwise null. |
| 167 | */ |
| 168 | private function retrieve( $kind, $key ) { |
| 169 | return is_string( $key ) && isset( $this->$kind[ $key ] ) ? $this->$kind[ $key ] : null; |
| 170 | } |
| 171 | } |