Skip to content

Commit c74ee8f

Browse files
authored
Simplify the steps for getting to the global variable.
1 parent 71030b1 commit c74ee8f

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/wp-includes/class-wp.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -784,35 +784,38 @@ public function main( $query_args = '' ) {
784784
* Retrieve a value from $_REQUEST according to a $path and/or $schama.
785785
*/
786786
public static function request( $path, $default = null, $schema = false ) {
787-
return self::_superglobal_access_helper( 'request', $path, $default, $schema );
787+
return self::_superglobal_access_helper( '_REQUEST', $path, $default, $schema );
788788
}
789789

790790
/**
791791
* Retrieve a value from $_GET according to a $path and/or $schama.
792792
*/
793793
public static function get( $path, $default = null, $schema = false ) {
794-
return self::_superglobal_access_helper( 'get', $path, $default, $schema );
794+
return self::_superglobal_access_helper( '_GET', $path, $default, $schema );
795795
}
796796

797797
/**
798798
* Retrieve a value from $_POST according to a $path and/or $schama.
799799
*/
800800
public static function post( $path, $default = null, $schema = false ) {
801-
return self::_superglobal_access_helper( 'post', $path, $default, $schema );
801+
return self::_superglobal_access_helper( '_POST', $path, $default, $schema );
802802
}
803803

804804
/**
805805
*
806-
* @param string $var The global to access, sans underscore prefix.
806+
* @param string $var The global to access.
807807
* @param string|array $path The path to the value to fetch. See _wp_array_get().
808808
* @param mixed|null $default The default value if $path is not set.
809809
* @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().
810810
* @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.
811811
*/
812812
protected static function _superglobal_access_helper( $var, $path, $default = null, $schema = false ) {
813-
$var = ltrim( $var, '_' );
813+
if ( ! isset( $GLOBALS[ $var ] ) ) {
814+
return $default;
815+
}
816+
814817
$path = is_array( $path ) ? $path : array( $path );
815-
$value = _wp_array_get( $GLOBALS[ strtoupper( "_{$var}" ) ], $path, null );
818+
$value = _wp_array_get( $GLOBALS[ $var ], $path, null );
816819

817820
if ( is_null( $value ) ) {
818821
return $default;
@@ -823,7 +826,8 @@ protected static function _superglobal_access_helper( $var, $path, $default = nu
823826
// Coerce it into the appropriate type.
824827
if ( $schema ) {
825828
$schema = is_string( $schema ) ? array( 'type' => $schema ) : $schema;
826-
$value = rest_sanitize_value_from_schema( $value, $schema, "WP::{$var}()" );
829+
$caller = strtolower( ltrim( $var, '_' ) );
830+
$value = rest_sanitize_value_from_schema( $value, $schema, "WP::{$caller}()" );
827831
}
828832

829833
return $value;

0 commit comments

Comments
 (0)