Completed
Push — add/handling-connection-errors ( 76997e...1b4c4d )
by
unknown
331:58 queued 323:55
created

bootstrap.php ➔ map_deep()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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