Skip to content

Commit 71030b1

Browse files
authored
Add initial WP::request() & friends API method with unslashing, optional default, optional sanitization.
1 parent 1f7977d commit 71030b1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/wp-includes/class-wp.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,4 +779,53 @@ public function main( $query_args = '' ) {
779779
*/
780780
do_action_ref_array( 'wp', array( &$this ) );
781781
}
782+
783+
/**
784+
* Retrieve a value from $_REQUEST according to a $path and/or $schama.
785+
*/
786+
public static function request( $path, $default = null, $schema = false ) {
787+
return self::_superglobal_access_helper( 'request', $path, $default, $schema );
788+
}
789+
790+
/**
791+
* Retrieve a value from $_GET according to a $path and/or $schama.
792+
*/
793+
public static function get( $path, $default = null, $schema = false ) {
794+
return self::_superglobal_access_helper( 'get', $path, $default, $schema );
795+
}
796+
797+
/**
798+
* Retrieve a value from $_POST according to a $path and/or $schama.
799+
*/
800+
public static function post( $path, $default = null, $schema = false ) {
801+
return self::_superglobal_access_helper( 'post', $path, $default, $schema );
802+
}
803+
804+
/**
805+
*
806+
* @param string $var The global to access, sans underscore prefix.
807+
* @param string|array $path The path to the value to fetch. See _wp_array_get().
808+
* @param mixed|null $default The default value if $path is not set.
809+
* @param string|arrau $schema The primitive type of the value to return, or a Schema defining the value of the item. See rest_sanitize_value_from_schema().
810+
* @return mixed|WP_Error The request value, and if a $schema is passed, the sanitized value or a WP_Error instance if the value cannot be safely sanitized.
811+
*/
812+
protected static function _superglobal_access_helper( $var, $path, $default = null, $schema = false ) {
813+
$var = ltrim( $var, '_' );
814+
$path = is_array( $path ) ? $path : array( $path );
815+
$value = _wp_array_get( $GLOBALS[ strtoupper( "_{$var}" ) ], $path, null );
816+
817+
if ( is_null( $value ) ) {
818+
return $default;
819+
}
820+
821+
$value = wp_unslash( $value );
822+
823+
// Coerce it into the appropriate type.
824+
if ( $schema ) {
825+
$schema = is_string( $schema ) ? array( 'type' => $schema ) : $schema;
826+
$value = rest_sanitize_value_from_schema( $value, $schema, "WP::{$var}()" );
827+
}
828+
829+
return $value;
830+
}
782831
}

0 commit comments

Comments
 (0)