Completed
Push — renovate/jsdom-16.x ( 553f2a...6948b4 )
by
unknown
77:15 queued 59:05
created

bootstrap.php ➔ urlencode_deep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php //phpcs:ignore Squiz.Commenting.FileComment.Missing
2
3
require_once __DIR__ . '/../../vendor/autoload.php';
4
5
// phpcs:disable
6
// Core WP functions clones
7
8
9
function get_home_url() {
10
	return 'http://example.org';
11
}
12
13
function wp_parse_args( $args, $defaults = '' ) {
14
	if ( is_object( $args ) ) {
15
		$parsed_args = get_object_vars( $args );
16
	} elseif ( is_array( $args ) ) {
17
		$parsed_args =& $args;
18
	} else {
19
		parse_str( $args, $parsed_args );
20
	}
21
22
	if ( is_array( $defaults ) ) {
23
		return array_merge( $defaults, $parsed_args );
24
	}
25
	return $parsed_args;
26
}
27
28
function add_query_arg( ...$args ) {
29
	if ( is_array( $args[0] ) ) {
30 View Code Duplication
		if ( count( $args ) < 2 || false === $args[1] ) {
31
			$uri = $_SERVER['REQUEST_URI'];
32
		} else {
33
			$uri = $args[1];
34
		}
35 View Code Duplication
	} else {
36
		if ( count( $args ) < 3 || false === $args[2] ) {
37
			$uri = $_SERVER['REQUEST_URI'];
38
		} else {
39
			$uri = $args[2];
40
		}
41
	}
42
43
	$frag = strstr( $uri, '#' );
44
	if ( $frag ) {
45
		$uri = substr( $uri, 0, -strlen( $frag ) );
46
	} else {
47
		$frag = '';
48
	}
49
50
	if ( 0 === stripos( $uri, 'http://' ) ) {
51
		$protocol = 'http://';
52
		$uri      = substr( $uri, 7 );
53
	} elseif ( 0 === stripos( $uri, 'https://' ) ) {
54
		$protocol = 'https://';
55
		$uri      = substr( $uri, 8 );
56
	} else {
57
		$protocol = '';
58
	}
59
60
	if ( strpos( $uri, '?' ) !== false ) {
61
		list( $base, $query ) = explode( '?', $uri, 2 );
62
		$base                .= '?';
63
	} elseif ( $protocol || strpos( $uri, '=' ) === false ) {
64
		$base  = $uri . '?';
65
		$query = '';
66
	} else {
67
		$base  = '';
68
		$query = $uri;
69
	}
70
71
	parse_str( $query, $qs );
72
	$qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
73
	if ( is_array( $args[0] ) ) {
74
		foreach ( $args[0] as $k => $v ) {
75
			$qs[ $k ] = $v;
76
		}
77
	} else {
78
		$qs[ $args[0] ] = $args[1];
79
	}
80
81
	foreach ( $qs as $k => $v ) {
82
		if ( $v === false ) {
83
			unset( $qs[ $k ] );
84
		}
85
	}
86
87
	$ret = build_query( $qs );
88
	$ret = trim( $ret, '?' );
89
	$ret = preg_replace( '#=(&|$)#', '$1', $ret );
90
	$ret = $protocol . $base . $ret . $frag;
91
	$ret = rtrim( $ret, '?' );
92
	return $ret;
93
}
94
95
function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) {
96
	$ret = array();
97
98
	foreach ( (array) $data as $k => $v ) {
99
		if ( $urlencode ) {
100
			$k = urlencode( $k );
101
		}
102
		if ( is_int( $k ) && $prefix != null ) {
103
			$k = $prefix . $k;
104
		}
105
		if ( ! empty( $key ) ) {
106
			$k = $key . '%5B' . $k . '%5D';
107
		}
108
		if ( $v === null ) {
109
			continue;
110
		} elseif ( $v === false ) {
111
			$v = '0';
112
		}
113
114
		if ( is_array( $v ) || is_object( $v ) ) {
115
			array_push( $ret, _http_build_query( $v, '', $sep, $k, $urlencode ) );
116
		} elseif ( $urlencode ) {
117
			array_push( $ret, $k . '=' . urlencode( $v ) );
118
		} else {
119
			array_push( $ret, $k . '=' . $v );
120
		}
121
	}
122
123
	if ( null === $sep ) {
124
		$sep = ini_get( 'arg_separator.output' );
125
	}
126
127
	return implode( $sep, $ret );
128
}
129
130
function build_query( $data ) {
131
	return _http_build_query( $data, null, '&', '', false );
132
}
133
134
function map_deep( $value, $callback ) {
135
	if ( is_array( $value ) ) {
136
		foreach ( $value as $index => $item ) {
137
			$value[ $index ] = map_deep( $item, $callback );
138
		}
139
	} elseif ( is_object( $value ) ) {
140
		$object_vars = get_object_vars( $value );
141
		foreach ( $object_vars as $property_name => $property_value ) {
142
			$value->$property_name = map_deep( $property_value, $callback );
143
		}
144
	} else {
145
		$value = call_user_func( $callback, $value );
146
	}
147
148
	return $value;
149
}
150
151
function urlencode_deep( $value ) {
152
	return map_deep( $value, 'urlencode' );
153
}
154