| 1 | |
|---|
| 2 | <form method="post" action="?foo=123&bar=abc"> |
|---|
| 3 | <input type="text" name="test[abc][123][xyz]" value="100" /> |
|---|
| 4 | <input type="submit" value="Go!" /> |
|---|
| 5 | </form> |
|---|
| 6 | <?php |
|---|
| 7 | |
|---|
| 8 | include 'wp-load.php'; |
|---|
| 9 | |
|---|
| 10 | echo "\n<pre>\n"; |
|---|
| 11 | |
|---|
| 12 | if ( has_POST() ) { |
|---|
| 13 | echo "\n----------\n"; |
|---|
| 14 | echo '$_POST["test"]: '; |
|---|
| 15 | print_r( _POST('test') ); |
|---|
| 16 | echo "\n----------\n"; |
|---|
| 17 | echo '$_POST["test"]["abc"]["123"]: '; |
|---|
| 18 | print_r( _POST( array( 'test','abc','123' ) ) ); |
|---|
| 19 | echo "\n----------\n"; |
|---|
| 20 | echo '$_POST["test"][abc][123][xyz]: ' . _POST( array( 'test', 'abc','123','xyz' ) ); |
|---|
| 21 | echo "\n----------\n"; |
|---|
| 22 | } |
|---|
| 23 | if ( has_GET() ) { |
|---|
| 24 | echo '$_GET["foo"]: ' . _GET('foo'); |
|---|
| 25 | echo "\n----------\n"; |
|---|
| 26 | echo '$_GET["bar"]: ' . _GET('bar'); |
|---|
| 27 | echo "\n----------\n"; |
|---|
| 28 | echo '$_GET["baz"]: ' . _GET('baz', '1001' ); |
|---|
| 29 | if ( ! has_GET('banana') ) { |
|---|
| 30 | echo "\n----------\nWe have no bananas today.\n"; |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | echo "\n</pre>\n"; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Retrieves value of $_GET[$key] |
|---|
| 38 | * |
|---|
| 39 | * @example |
|---|
| 40 | * |
|---|
| 41 | * For URL http://example.com?test=50: |
|---|
| 42 | * Retrieve value with: _GET( 'test', 25 ) with a default value of 25 |
|---|
| 43 | * |
|---|
| 44 | * @param string $key |
|---|
| 45 | * @param mixed $default |
|---|
| 46 | * |
|---|
| 47 | * @return null|mixed Defaults to null if $key not in $_GET. |
|---|
| 48 | */ |
|---|
| 49 | function _GET( $key, $default = null ) { |
|---|
| 50 | if ( WP_DEBUG && is_array( $key ) ) |
|---|
| 51 | trigger_error( sprintf( __( '_GET() does not support array keys.' ) ), E_USER_WARNING ); |
|---|
| 52 | |
|---|
| 53 | return _GPCS( $_GET, 'GET', $key, $default ); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Retrieves value of $_POST[$key] |
|---|
| 58 | * |
|---|
| 59 | * Note: See $key can be an array, see example: |
|---|
| 60 | * |
|---|
| 61 | * @example |
|---|
| 62 | * |
|---|
| 63 | * For HTML <input type="text" name="test" /> w/default value of 25: |
|---|
| 64 | * Retrieve value with: _POST( 'test', 25 ) |
|---|
| 65 | * |
|---|
| 66 | * For HTML <input type="text" name="test[abc][123][xyz]" /> |
|---|
| 67 | * Retrieve value with: _POST( array( 'test', 'abc','123','xyz' ) ) |
|---|
| 68 | * |
|---|
| 69 | * @param string|array $key |
|---|
| 70 | * @param mixed $default |
|---|
| 71 | * |
|---|
| 72 | * @return null|mixed |
|---|
| 73 | */ |
|---|
| 74 | function _POST( $key, $default = null ) { |
|---|
| 75 | return _GPCS( $_POST, 'POST', $key, $default ); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Retrieves value of $_REQUEST[$key] |
|---|
| 80 | * |
|---|
| 81 | * Note: See $key can be an array, see example: |
|---|
| 82 | * |
|---|
| 83 | * @example |
|---|
| 84 | * |
|---|
| 85 | * For HTML <input type="text" name="test" /> w/default value of 25: |
|---|
| 86 | * Retrieve value with: _REQUEST( 'test', 25 ) |
|---|
| 87 | * |
|---|
| 88 | * For HTML <input type="text" name="test[abc][123][xyz]" /> |
|---|
| 89 | * Retrieve value with: _REQUEST( array( 'test', 'abc','123','xyz' ) ) |
|---|
| 90 | * |
|---|
| 91 | * @param string|array $key |
|---|
| 92 | * @param mixed $default |
|---|
| 93 | * |
|---|
| 94 | * @return null|mixed |
|---|
| 95 | */ |
|---|
| 96 | function _REQUEST( $key, $default = null ) { |
|---|
| 97 | return _GPCS( $_REQUEST, 'REQUEST', $key, $default ); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Tests for existance of $key as an array element in $_GET, or if count($_GET)>0 when $key is null. |
|---|
| 102 | * |
|---|
| 103 | * @param null|string|array $key Key value |
|---|
| 104 | * |
|---|
| 105 | * @return bool |
|---|
| 106 | */ |
|---|
| 107 | function has_GET( $key = null ) { |
|---|
| 108 | return _has_GPCS( $_GET, 'GET', $key ); |
|---|
| 109 | } |
|---|
| 110 | /** |
|---|
| 111 | * Tests for existance of $key as an array element in $_POST, or if count($_POST)>0 when $key is null. |
|---|
| 112 | * |
|---|
| 113 | * @param null|string|array $key Key value |
|---|
| 114 | * |
|---|
| 115 | * @return bool |
|---|
| 116 | */ |
|---|
| 117 | function has_POST( $key = null ) { |
|---|
| 118 | return _has_GPCS( $_POST, 'POST', $key ); |
|---|
| 119 | } |
|---|
| 120 | /** |
|---|
| 121 | * Tests for existance of $key as an array element in $_REQUEST, or if count($_REQUEST)>0 when $key is null. |
|---|
| 122 | * |
|---|
| 123 | * @param null|string|array $key Key value |
|---|
| 124 | * |
|---|
| 125 | * @return bool |
|---|
| 126 | */ |
|---|
| 127 | function has_REQUEST( $key = null ) { |
|---|
| 128 | return _has_GPCS( $_REQUEST, 'REQUEST', $key ); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * Helper function to support accessing $_GET, $_POST and $_REQUEST |
|---|
| 134 | * |
|---|
| 135 | * Retrieves value of $_POST[$key], $_GET[$key] or $_REQUEST[$key] |
|---|
| 136 | * |
|---|
| 137 | * Note: $key can be an array, see example: |
|---|
| 138 | * |
|---|
| 139 | * @example |
|---|
| 140 | * |
|---|
| 141 | * For HTML <input type="text" name="test" /> w/default value of 25: |
|---|
| 142 | * Retrieve value with: _GPCS( $_POST, 'POST', 'test', 25 ) |
|---|
| 143 | * |
|---|
| 144 | * For HTML <input type="text" name="test[abc][123][xyz]" /> |
|---|
| 145 | * Retrieve value with: _GPCS( $_POST, 'POST', array( 'test', 'abc','123','xyz' ) ) |
|---|
| 146 | * |
|---|
| 147 | * @param array $array Pass in $_POST, $_GET or $_REQUEST |
|---|
| 148 | * @param string $type Pass in 'POST', 'GET' or 'REQUEST' |
|---|
| 149 | * @param mixed $key Key value, may be array |
|---|
| 150 | * @param null $default |
|---|
| 151 | * |
|---|
| 152 | * @return null |
|---|
| 153 | */ |
|---|
| 154 | function _GPCS( $array, $type, $key, $default = null ) { |
|---|
| 155 | if ( ( is_string( $key ) || is_numeric( $key ) ) ) { |
|---|
| 156 | if ( isset( $array[$key] ) ) |
|---|
| 157 | return $array[$key]; |
|---|
| 158 | else |
|---|
| 159 | return $default; |
|---|
| 160 | } else if ( is_array( $key ) ) { |
|---|
| 161 | $value = $array; |
|---|
| 162 | foreach( $key as $subkey ) { |
|---|
| 163 | if ( isset( $value[$subkey] ) ) { |
|---|
| 164 | $value = $value[$subkey]; |
|---|
| 165 | } else { |
|---|
| 166 | return $default; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | return $value; |
|---|
| 170 | } else if ( WP_DEBUG ){ |
|---|
| 171 | trigger_error( sprintf( __( 'The key [%s] passed to _%s() is not a string, numeric or array.' ), $key, $type ), E_USER_WARNING ); |
|---|
| 172 | } |
|---|
| 173 | return $default; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | * Tests for existance of $key as an array element in $_POST, $_GET or $_REQUEST or if count($_*)>0 when $key is null |
|---|
| 179 | * |
|---|
| 180 | * @param array $array Pass in $_POST, $_GET or $_REQUEST |
|---|
| 181 | * @param string $type Pass in 'POST', 'GET' or 'REQUEST' |
|---|
| 182 | * @param null|string|array $key Key value, may be array |
|---|
| 183 | * |
|---|
| 184 | * @return bool |
|---|
| 185 | */ |
|---|
| 186 | function _has_GPCS( $array, $type, $key = null ) { |
|---|
| 187 | if ( is_null( $key ) ) { |
|---|
| 188 | return 0 < count($array); |
|---|
| 189 | } else if ( ( is_string( $key ) || is_numeric( $key ) ) ) { |
|---|
| 190 | return isset( $array[$key] ); |
|---|
| 191 | } else if ( is_array( $key ) ) { |
|---|
| 192 | $value = $array; |
|---|
| 193 | foreach( $key as $subkey ) { |
|---|
| 194 | if ( ! isset( $value[$subkey] ) ) { |
|---|
| 195 | return false; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | return true; |
|---|
| 199 | } |
|---|
| 200 | return false; |
|---|
| 201 | } |
|---|