| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | * Proposal for GPCS variables
|
|---|
| 4 | *
|
|---|
| 5 | * @see: http://core.trac.wordpress.org/ticket/22325
|
|---|
| 6 | *
|
|---|
| 7 | * @author Mike Schinkel <mike@newclarity.net>
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Retrieves value of $_GET[$key]
|
|---|
| 12 | *
|
|---|
| 13 | * @example
|
|---|
| 14 | *
|
|---|
| 15 | * For URL http://example.com?test=50:
|
|---|
| 16 | * Retrieve value with: _GET( 'test', 25 ) with a default value of 25
|
|---|
| 17 | *
|
|---|
| 18 | * @param string $key
|
|---|
| 19 | * @param mixed $default
|
|---|
| 20 | *
|
|---|
| 21 | * @return null|mixed Defaults to null if $key not in $_GET.
|
|---|
| 22 | */
|
|---|
| 23 | function _GET( $key, $default = null ) {
|
|---|
| 24 | if ( WP_DEBUG && is_array( $key ) )
|
|---|
| 25 | trigger_error( sprintf( __( '_GET() does not support array keys.' ) ), E_USER_WARNING );
|
|---|
| 26 |
|
|---|
| 27 | return WP_GPCS::access( 'GET', $key, $default );
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Retrieves value of $_POST[$key]
|
|---|
| 32 | *
|
|---|
| 33 | * Note: See $key can be an array, see example:
|
|---|
| 34 | *
|
|---|
| 35 | * @example
|
|---|
| 36 | *
|
|---|
| 37 | * For HTML <input type="text" name="test" /> w/default value of 25:
|
|---|
| 38 | * Retrieve value with: _POST( 'test', 25 )
|
|---|
| 39 | *
|
|---|
| 40 | * For HTML <input type="text" name="test[abc][123][xyz]" />
|
|---|
| 41 | * Retrieve value with: _POST( array( 'test', 'abc','123','xyz' ) )
|
|---|
| 42 | *
|
|---|
| 43 | * @param string|array $key
|
|---|
| 44 | * @param mixed $default
|
|---|
| 45 | *
|
|---|
| 46 | * @return null|mixed
|
|---|
| 47 | */
|
|---|
| 48 | function _POST( $key, $default = null ) {
|
|---|
| 49 | return WP_GPCS::access( 'POST', $key, $default );
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Retrieves value of $_REQUEST[$key]
|
|---|
| 54 | *
|
|---|
| 55 | * Note: See $key can be an array, see example:
|
|---|
| 56 | *
|
|---|
| 57 | * @example
|
|---|
| 58 | *
|
|---|
| 59 | * For HTML <input type="text" name="test" /> w/default value of 25:
|
|---|
| 60 | * Retrieve value with: _REQUEST( 'test', 25 )
|
|---|
| 61 | *
|
|---|
| 62 | * For HTML <input type="text" name="test[abc][123][xyz]" />
|
|---|
| 63 | * Retrieve value with: _REQUEST( array( 'test', 'abc','123','xyz' ) )
|
|---|
| 64 | *
|
|---|
| 65 | * @param string|array $key
|
|---|
| 66 | * @param mixed $default
|
|---|
| 67 | *
|
|---|
| 68 | * @return null|mixed
|
|---|
| 69 | */
|
|---|
| 70 | function _REQUEST( $key, $default = null ) {
|
|---|
| 71 | return WP_GPCS::access( 'REQUEST', $key, $default );
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Tests for existance of $key as an array element in $_GET, or if count($_GET)>0 when $key is null.
|
|---|
| 76 | *
|
|---|
| 77 | * @param null|string|array $key Key value
|
|---|
| 78 | *
|
|---|
| 79 | * @return bool
|
|---|
| 80 | */
|
|---|
| 81 | function has_GET( $key = null ) {
|
|---|
| 82 | return WP_GPCS::has( 'GET', $key );
|
|---|
| 83 | }
|
|---|
| 84 | /**
|
|---|
| 85 | * Tests for existance of $key as an array element in $_POST, or if count($_POST)>0 when $key is null.
|
|---|
| 86 | *
|
|---|
| 87 | * @param null|string|array $key Key value
|
|---|
| 88 | *
|
|---|
| 89 | * @return bool
|
|---|
| 90 | */
|
|---|
| 91 | function has_POST( $key = null ) {
|
|---|
| 92 | return WP_GPCS::has( 'POST', $key );
|
|---|
| 93 | }
|
|---|
| 94 | /**
|
|---|
| 95 | * Tests for existance of $key as an array element in $_REQUEST, or if count($_REQUEST)>0 when $key is null.
|
|---|
| 96 | *
|
|---|
| 97 | * @param null|string|array $key Key value
|
|---|
| 98 | *
|
|---|
| 99 | * @return bool
|
|---|
| 100 | */
|
|---|
| 101 | function has_REQUEST( $key = null ) {
|
|---|
| 102 | return WP_GPCS::has( 'REQUEST', $key );
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Sets a new value for $key in $_GET.
|
|---|
| 107 | *
|
|---|
| 108 | * @param string|array $key
|
|---|
| 109 | * @param mixed $value
|
|---|
| 110 | */
|
|---|
| 111 | function set_GET( $key, $value ) {
|
|---|
| 112 | WP_GPCS::set( 'GET', $key, $value );
|
|---|
| 113 | }
|
|---|
| 114 | /**
|
|---|
| 115 | * Sets a new value for $key in $_POST.
|
|---|
| 116 | *
|
|---|
| 117 | * @param string|array $key Key value
|
|---|
| 118 | * @param mixed $value
|
|---|
| 119 | */
|
|---|
| 120 | function set_POST( $key, $value ) {
|
|---|
| 121 | WP_GPCS::set( 'POST', $key, $value );
|
|---|
| 122 | }
|
|---|
| 123 | /**
|
|---|
| 124 | * Sets a new value for $key in $_REQUEST.
|
|---|
| 125 | *
|
|---|
| 126 | * @param string|array $key Key value
|
|---|
| 127 | * @param mixed $value
|
|---|
| 128 | */
|
|---|
| 129 | function set_REQUEST( $key, $value ) {
|
|---|
| 130 | WP_GPCS::set( 'REQUEST', $key, $value );
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Helper class to manage Global variable access.
|
|---|
| 136 | */
|
|---|
| 137 | class WP_GPCS {
|
|---|
| 138 | private static $_vars;
|
|---|
| 139 | static function on_load() {
|
|---|
| 140 | self::$_vars = array(
|
|---|
| 141 | 'GET' => $_GET,
|
|---|
| 142 | 'POST' => $_POST,
|
|---|
| 143 | 'REQUEST' => $_REQUEST,
|
|---|
| 144 | );
|
|---|
| 145 | }
|
|---|
| 146 | /**
|
|---|
| 147 | * Helper function to support accessing $_GET, $_POST and $_REQUEST
|
|---|
| 148 | *
|
|---|
| 149 | * Retrieves value of $_POST[$key], $_GET[$key] or $_REQUEST[$key]
|
|---|
| 150 | *
|
|---|
| 151 | * Note: $key can be an array, see example:
|
|---|
| 152 | *
|
|---|
| 153 | * @example
|
|---|
| 154 | *
|
|---|
| 155 | * For HTML <input type="text" name="test" /> w/default value of 25:
|
|---|
| 156 | * Retrieve value with: WP_GPCS::access( $_POST, 'POST', 'test', 25 )
|
|---|
| 157 | *
|
|---|
| 158 | * For HTML <input type="text" name="test[abc][123][xyz]" />
|
|---|
| 159 | * Retrieve value with: WP_GPCS::access( $_POST, 'POST', array( 'test', 'abc','123','xyz' ) )
|
|---|
| 160 | *
|
|---|
| 161 | * @param string $type Pass in 'POST', 'GET' or 'REQUEST'
|
|---|
| 162 | * @param mixed $key Key value, may be array
|
|---|
| 163 | * @param null $default
|
|---|
| 164 | *
|
|---|
| 165 | * @return null
|
|---|
| 166 | */
|
|---|
| 167 | static function access( $type, $key, $default = null ) {
|
|---|
| 168 | if ( ( is_string( $key ) || is_numeric( $key ) ) ) {
|
|---|
| 169 | return isset( self::$_vars[$type][$key] ) ? self::$_vars[$type][$key] : $default;
|
|---|
| 170 | } else if ( is_array( $key ) ) {
|
|---|
| 171 | $value = self::$_vars[$type];
|
|---|
| 172 | foreach( $key as $subkey ) {
|
|---|
| 173 | if ( isset( $value[$subkey] ) ) {
|
|---|
| 174 | $value = $value[$subkey];
|
|---|
| 175 | } else {
|
|---|
| 176 | return $default;
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 | return $value;
|
|---|
| 180 | } else if ( WP_DEBUG ){
|
|---|
| 181 | trigger_error( sprintf( __( 'The key [%s] passed to _%s() is not a string, numeric or array.' ), $key, $type ), E_USER_WARNING );
|
|---|
| 182 | }
|
|---|
| 183 | return $default;
|
|---|
| 184 | }
|
|---|
| 185 | /**
|
|---|
| 186 | * Helper function to test for existance of $key as an array element in $_POST, $_GET or $_REQUEST
|
|---|
| 187 | *
|
|---|
| 188 | * @param string $type Pass in 'POST', 'GET' or 'REQUEST'
|
|---|
| 189 | * @param null|string|array $key Key value, may be array
|
|---|
| 190 | *
|
|---|
| 191 | * @return bool
|
|---|
| 192 | */
|
|---|
| 193 | static function has( $type, $key = null ) {
|
|---|
| 194 | if ( is_null( $key ) ) {
|
|---|
| 195 | return 0 < count( self::$_vars[$type] );
|
|---|
| 196 | } else if ( ( is_string( $key ) || is_numeric( $key ) ) && isset( self::$_vars[$key] ) ) {
|
|---|
| 197 | return true;
|
|---|
| 198 | } else if ( is_array( $key ) ) {
|
|---|
| 199 | $value = self::$_vars[$type];
|
|---|
| 200 | foreach( $key as $subkey ) {
|
|---|
| 201 | if ( ! isset( $value[$subkey] ) ) {
|
|---|
| 202 | return false;
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 | return true;
|
|---|
| 206 | }
|
|---|
| 207 | return false;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * Helper function to set a new value for $key in $_POST, $_GET or $_REQUEST
|
|---|
| 213 | *
|
|---|
| 214 | * @param string $type Pass in 'POST', 'GET' or 'REQUEST'
|
|---|
| 215 | * @param string|array $key Key value, may be array
|
|---|
| 216 | * @param mixed $value
|
|---|
| 217 | */
|
|---|
| 218 | static function set( $type, $key, $value ) {
|
|---|
| 219 | if ( ( is_string( $key ) || is_numeric( $key ) ) ) {
|
|---|
| 220 | self::$_vars[$type][$key] = $value;
|
|---|
| 221 | } else if ( is_array( $key ) ) {
|
|---|
| 222 | $old_value = &self::$_vars[$type];
|
|---|
| 223 | foreach( $key as $index => $subkey ) {
|
|---|
| 224 | $old_value[$subkey] = null;
|
|---|
| 225 | $old_value = &$old_value[$subkey];
|
|---|
| 226 | }
|
|---|
| 227 | $old_value = $value;
|
|---|
| 228 | } else if ( WP_DEBUG ){
|
|---|
| 229 | trigger_error( sprintf( __( 'The key [%s] passed to set_%s() is not a string, numeric or array.' ), $key, $type ), E_USER_WARNING );
|
|---|
| 230 | }
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 | WP_GPCS::on_load();
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 | // TEST CODE FOLLOWS.
|
|---|
| 237 | // TODO: DELETE IF INCLUDED INTO WORDPRESS CORE.
|
|---|
| 238 | ?>
|
|---|
| 239 | <form method="post" action="?foo=123&bar=abc">
|
|---|
| 240 | <input type="text" name="test[abc][123][xyz]" value="100" />
|
|---|
| 241 | <input type="submit" value="Go!" />
|
|---|
| 242 | </form>
|
|---|
| 243 | <?php
|
|---|
| 244 |
|
|---|
| 245 | include 'wp-load.php';
|
|---|
| 246 |
|
|---|
| 247 | echo "\n<pre>\n";
|
|---|
| 248 |
|
|---|
| 249 | if ( has_POST() ) {
|
|---|
| 250 | echo "\n----------\n";
|
|---|
| 251 | echo '$_POST["test"]: ';
|
|---|
| 252 | print_r( _POST('test') );
|
|---|
| 253 | echo "\n----------\n";
|
|---|
| 254 | echo '$_POST["test"]["abc"]["123"]: ';
|
|---|
| 255 | print_r( _POST( array( 'test','abc','123' ) ) );
|
|---|
| 256 | echo "\n----------\n";
|
|---|
| 257 | echo '$_POST["test"][abc][123][xyz]: ' . _POST( array( 'test', 'abc','123','xyz' ) );
|
|---|
| 258 | echo "\n----[This from Cache]------\n";
|
|---|
| 259 | echo '$_POST["test"][abc][123][xyz]: ' . _POST( array( 'test', 'abc','123','xyz' ) );
|
|---|
| 260 | set_POST( 'test', 123456 );
|
|---|
| 261 | echo "\n---[After set(number)]-------\n";
|
|---|
| 262 | print_r( _POST('test') );
|
|---|
| 263 | set_POST( array( 'test','abc','123' ), array( 'xyz' => 'ABC' ) );
|
|---|
| 264 | echo "\n---[After set(array)]-------\n";
|
|---|
| 265 | print_r( _POST('test') );
|
|---|
| 266 | echo "\n----------\n";
|
|---|
| 267 | }
|
|---|
| 268 | if ( has_GET() ) {
|
|---|
| 269 | echo '$_GET["foo"]: ' . _GET('foo');
|
|---|
| 270 | echo "\n----[This from Cache]------\n";
|
|---|
| 271 | echo '$_GET["foo"]: ' . _GET('foo');
|
|---|
| 272 | echo "\n----------\n";
|
|---|
| 273 | echo '$_GET["bar"]: ' . _GET('bar');
|
|---|
| 274 | echo "\n----------\n";
|
|---|
| 275 | echo '$_GET["baz"]: ' . _GET('baz', '1001' );
|
|---|
| 276 | if ( ! has_GET('banana') ) {
|
|---|
| 277 | echo "\n----------\nWe have no bananas today.\n";
|
|---|
| 278 | }
|
|---|
| 279 | }
|
|---|
| 280 | echo "\n</pre>\n";
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|