[wp-trac] [WordPress Trac] #22325: Abstract GPCS away from the superglobals

WordPress Trac noreply at wordpress.org
Fri Nov 2 18:55:42 UTC 2012


#22325: Abstract GPCS away from the superglobals
-------------------------+-----------------------------
 Reporter:  rmccue       |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Future Release
Component:  General      |     Version:
 Severity:  minor        |  Resolution:
 Keywords:               |
-------------------------+-----------------------------

Comment (by CaptainN):

 Here's a quick version I through together (without testing):

 {{{
 <?php
 $wp_get = $_GET;
 $wp_post = $_POST;
 $wp_request = $_REQUEST;
 $wp_cookie = $_COOKIE;
 $wp_server = $_SERVER;

 function wp_gpcs( $gprcs, $key )
 {
         global $wp_get, $wp_post, $wp_request, $wp_cookie, $wp_server;

         // return the whole array if no $key specified
         if ( is_null( $key ) )
                 return $$gprcs;

         // return the value of the key
         if ( isset( $$gprcs[ $key ] ) )
                 return $$gprcs[ $Key ];

         // if not set, return NULL - this differs from standard PHP
         // behavior, which would throw a notice.
         else
                 return NULL;

         // :TODO: Expand with support for multi-dimensional arrays.
 }

 function _get( $key = NULL ) {
         return wp_gpcs( 'wp_get', $key );
 }
 function _post( $key = NULL ) {
         return wp_gpcs( 'wp_post', $key );
 }
 function _request( $key = NULL ) {
         return wp_gpcs( 'wp_request', $key );
 }
 function _cookie( $key = NULL ) {
         return wp_gpcs( 'wp_cookie', $key );
 }
 function _server( $key = NULL ) {
         return wp_gpcs( 'wp_server', $key );
 }
 }}}

 That whole thing could be wrapped in a class to make it much cleaner, but
 this is just a quick and dirty example.

 This would have be be inserted before the WordPress magic quotes
 operations. I changed the part where it would normally throw a notice (or
 error, I don't remember which it does), because I don't actually know how
 to do that while retaining the call stack, etc. This may be better anyway.
 I also didn't implement multidimensional support, but it wouldn't be too
 hard (using func_get_args and family).

 [comment:14 MikeSchinkel]: You have a point, it would be nice (and
 simulating the PHP error system may be too problematic anyway). So how do
 you mix the default value specification in the method signature that
 retains the multidimensional capability of PHP forms
 (name="level1[level2]"). I'd use the second (third, etc.) arguments as a
 way to access the indexes in the array (_get('level1','level2');), but
 you've used them to specify the default value.

 Also, how would you know that you've set the default value? You'd still
 have to check that. Maybe more methods are called for?

 {{{
 // default value and set in the GPCS method
 if ( _get( 'myVar', 'default value' ) != 'default value' ) {
     // validation passes
 }

 // If we return NULL:
 // to check if it's set at all (for checkboxes and radios)
 if ( !is_null( _get( 'myVar' ) ) ) {
     // It's set!
 }

 // a function to check for a default value with NULL checks
 if ( _check( _get( 'myVar' ), 'default value' )  {
     // the value is validated
 }

 // and if you need to output the value with a default
 echo _default( _get( 'myVar' ), 'default value' );




 // returns value of $default if $value is null, $value otherwise
 function _default( $value, $default ) {
     return ( is_null( $value ) )? $default: $value;
 }

 // returns false if the value is either null, or matches $default
 function _check( $value, $default ) {
     return ( is_null( $value ) || $value == $default )? false: true;
 }

 // :NOTE: The above is psuedo code. I didn't run or test any of it.

 }}}

 That way we can have the multidimensional support, and a reduction of
 boilerplate with no errors or notices being thrown, and the GPCS methods
 work almost the same as PHP does (except without annoying notices).

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/22325#comment:16>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list