-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnon-wp-compat.php
100 lines (92 loc) · 2.77 KB
/
non-wp-compat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
if ( ! function_exists( 'wp_parse_args' ) ) {
/**
* Merge user defined arguments into defaults array.
*
* This function is used throughout WordPress to allow for both string or array
* to be merged into another array.
*
* @param string|array|object $args Value to merge with $defaults.
* @param array|string $defaults Optional. Array that serves as the defaults. Default empty.
*
* @return array Merged user defined values with defaults.
*/
function wp_parse_args( $args, $defaults = '' ) {
if ( is_object( $args ) ) {
$r = get_object_vars( $args );
} elseif ( is_array( $args ) ) {
$r =& $args;
} else {
wp_parse_str( $args, $r );
}
if ( is_array( $defaults ) ) {
return array_merge( $defaults, $r );
}
return $r;
}
}
if ( ! function_exists( 'wp_parse_str' ) ) {
/**
* Parses a string into variables to be stored in an array.
*
* @param string $string The string to be parsed.
* @param array $array Variables will be stored in this array.
*/
function wp_parse_str( $string, &$array ) {
parse_str( $string, $array );
if ( get_magic_quotes_gpc() ) {
$array = stripslashes_deep( $array );
}
}
}
if ( ! function_exists( 'stripslashes_deep' ) ) {
/**
* Maps a function to all non-iterable elements of an array or an object.
*
* This is similar to `array_walk_recursive()` but acts upon objects too.
*
* @param mixed $value The array, object, or scalar.
* @param callable $callback The function to map onto $value.
*
* @return mixed The value with the callback applied to all non-arrays and non-objects inside it.
*/
function map_deep( $value, $callback ) {
if ( is_array( $value ) ) {
foreach ( $value as $index => $item ) {
$value[ $index ] = map_deep( $item, $callback );
}
} elseif ( is_object( $value ) ) {
$object_vars = get_object_vars( $value );
foreach ( $object_vars as $property_name => $property_value ) {
$value->$property_name = map_deep( $property_value, $callback );
}
} else {
$value = call_user_func( $callback, $value );
}
return $value;
}
}
if ( ! function_exists( 'stripslashes_deep' ) ) {
/**
* Navigates through an array, object, or scalar, and removes slashes from the values.
*
* @param mixed $value The value to be stripped.
*
* @return mixed Stripped value.
*/
function stripslashes_deep( $value ) {
return map_deep( $value, 'stripslashes_from_strings_only' );
}
}
if ( ! function_exists( 'stripslashes_from_strings_only' ) ) {
/**
* Callback function for `stripslashes_deep()` which strips slashes from strings.
*
* @param mixed $value The array or string to be stripped.
*
* @return mixed $value The stripped value.
*/
function stripslashes_from_strings_only( $value ) {
return is_string( $value ) ? stripslashes( $value ) : $value;
}
}