
<form method="post" action="?foo=123&bar=abc">
  <input type="text" name="test[abc][123][xyz]" value="100" />
  <input type="submit" value="Go!" />
</form>
<?php

include 'wp-load.php';

echo "\n<pre>\n";

if ( has_POST() ) {
  echo "\n----------\n";
  echo '$_POST["test"]: ';
  print_r( _POST('test') );
  echo "\n----------\n";
  echo '$_POST["test"]["abc"]["123"]: ';
  print_r( _POST( array( 'test','abc','123' ) ) );
  echo "\n----------\n";
  echo '$_POST["test"][abc][123][xyz]: ' . _POST( array( 'test', 'abc','123','xyz' ) );
  echo "\n----------\n";
}
if ( has_GET() ) {
  echo '$_GET["foo"]: ' . _GET('foo');
  echo "\n----------\n";
  echo '$_GET["bar"]: ' . _GET('bar');
  echo "\n----------\n";
  echo '$_GET["baz"]: ' . _GET('baz', '1001' );
  if ( ! has_GET('banana') ) {
    echo "\n----------\nWe have no bananas today.\n";
  }
}
echo "\n</pre>\n";


/**
 * Retrieves value of $_GET[$key]
 *
 * @example
 *
 *   For URL http://example.com?test=50:
 *   Retrieve value with: _GET( 'test', 25 ) with a default value of 25
 *
 * @param string $key
 * @param mixed $default
 *
 * @return null|mixed Defaults to null if $key not in $_GET.
 */
function _GET( $key, $default = null ) {
  if ( WP_DEBUG && is_array( $key ) )
    trigger_error( sprintf( __( '_GET() does not support array keys.' ) ), E_USER_WARNING );

  return _GPCS( $_GET, 'GET', $key, $default );
}

/**
 * Retrieves value of $_POST[$key]
 *
 * Note: See $key can be an array, see example:
 *
 * @example
 *
 *   For HTML <input type="text" name="test" /> w/default value of 25:
 *   Retrieve value with: _POST( 'test', 25 )
 *
 *   For HTML <input type="text" name="test[abc][123][xyz]" />
 *   Retrieve value with: _POST( array( 'test', 'abc','123','xyz' ) )
 *
 * @param string|array $key
 * @param mixed $default
 *
 * @return null|mixed
 */
function _POST( $key, $default = null ) {
  return _GPCS( $_POST, 'POST', $key, $default );
}

/**
 * Retrieves value of $_REQUEST[$key]
 *
 * Note: See $key can be an array, see example:
 *
 * @example
 *
 *   For HTML <input type="text" name="test" /> w/default value of 25:
 *   Retrieve value with: _REQUEST( 'test', 25 )
 *
 *   For HTML <input type="text" name="test[abc][123][xyz]" />
 *   Retrieve value with: _REQUEST( array( 'test', 'abc','123','xyz' ) )
 *
 * @param string|array $key
 * @param mixed $default
 *
 * @return null|mixed
 */
function _REQUEST( $key, $default = null ) {
  return _GPCS( $_REQUEST, 'REQUEST', $key, $default );
}

/**
 * Tests for existance of $key as an array element in $_GET, or if count($_GET)>0 when $key is null.
 *
 * @param null|string|array $key Key value
 *
 * @return bool
 */
function has_GET( $key = null ) {
  return _has_GPCS( $_GET, 'GET', $key );
}
/**
 * Tests for existance of $key as an array element in $_POST, or if count($_POST)>0 when $key is null.
 *
 * @param null|string|array $key Key value
 *
 * @return bool
 */
function has_POST( $key = null ) {
  return _has_GPCS( $_POST, 'POST', $key );
}
/**
 * Tests for existance of $key as an array element in $_REQUEST, or if count($_REQUEST)>0 when $key is null.
 *
 * @param null|string|array $key Key value
 *
 * @return bool
 */
function has_REQUEST( $key = null ) {
  return _has_GPCS( $_REQUEST, 'REQUEST', $key );
}


/**
 * Helper function to support accessing $_GET, $_POST and $_REQUEST
 *
 * Retrieves value of $_POST[$key], $_GET[$key] or $_REQUEST[$key]
 *
 * Note: $key can be an array, see example:
 *
 * @example
 *
 *   For HTML <input type="text" name="test" /> w/default value of 25:
 *   Retrieve value with: _GPCS( $_POST, 'POST', 'test', 25 )
 *
 *   For HTML <input type="text" name="test[abc][123][xyz]" />
 *   Retrieve value with: _GPCS( $_POST, 'POST', array( 'test', 'abc','123','xyz' ) )
 *
 * @param array $array Pass in $_POST, $_GET or $_REQUEST
 * @param string $type Pass in 'POST', 'GET' or 'REQUEST'
 * @param mixed $key Key value, may be array
 * @param null $default
 *
 * @return null
 */
function _GPCS( $array, $type, $key, $default = null ) {
  if ( ( is_string( $key ) || is_numeric( $key ) ) ) {
    if ( isset( $array[$key] ) )
      return $array[$key];
    else
      return $default;
  } else if ( is_array( $key ) ) {
    $value = $array;
    foreach( $key as $subkey ) {
      if ( isset( $value[$subkey] ) ) {
        $value = $value[$subkey];
      } else {
        return $default;
      }
    }
    return $value;
  } else if ( WP_DEBUG ){
    trigger_error( sprintf( __( 'The key [%s] passed to _%s() is not a string, numeric or array.' ), $key, $type ), E_USER_WARNING );
  }
  return $default;
}


/**
 * Tests for existance of $key as an array element in $_POST, $_GET or $_REQUEST or if count($_*)>0 when $key is null
 *
 * @param array $array Pass in $_POST, $_GET or $_REQUEST
 * @param string $type Pass in 'POST', 'GET' or 'REQUEST'
 * @param null|string|array $key Key value, may be array
 *
 * @return bool
 */
function _has_GPCS( $array, $type, $key = null ) {
  if ( is_null( $key ) ) {
    return 0 < count($array);
  } else if ( ( is_string( $key ) || is_numeric( $key ) ) ) {
    return isset( $array[$key] );
  } else if ( is_array( $key ) ) {
    $value = $array;
    foreach( $key as $subkey ) {
      if ( ! isset( $value[$subkey] ) ) {
        return false;
      }
    }
    return true;
  }
  return false;
}
