Completed
Push — fusion-sync/danroundhill/r2061... ( 6c7cf7...0f1caf )
by Jeremy
213:37 queued 206:26
created

jetpack-server-sandbox.php ➔ jetpack_server_sandbox()   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
2
3
/**
4
 * This feature is only useful for Automattic developers.
5
 * It configures Jetpack to talk to staging/sandbox servers
6
 * on WordPress.com instead of production servers.
7
 */
8
9
/**
10
 * @param string $sandbox Sandbox domain
11
 * @param string $url URL of request about to be made
12
 * @param array  $headers Headers of request about to be made
13
 * @return array [ 'url' => new URL, 'host' => new Host ]
14
 */
15
function jetpack_server_sandbox_request_parameters( $sandbox, $url, $headers ) {
16
	$host = '';
17
18
	$url_host = wp_parse_url( $url, PHP_URL_HOST );
0 ignored issues
show
Unused Code introduced by
The call to wp_parse_url() has too many arguments starting with PHP_URL_HOST.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
19
20
	switch ( $url_host ) {
21
	case 'public-api.wordpress.com' :
22
	case 'jetpack.wordpress.com' :
23
	case 'jetpack.com' :
24
	case 'dashboard.wordpress.com' :
25
		$host = isset( $headers['Host'] ) ? $headers['Host'] : $url_host;
26
		$url = preg_replace(
27
			'@^(https?://)' . preg_quote( $url_host, '@' ) . '(?=[/?#].*|$)@',
28
			'${1}' . $sandbox,
29
			$url,
30
			1
31
		);
32
	}
33
34
	return compact( 'url', 'host' );
35
}
36
37
/**
38
 * Modifies parameters of request in order to send the request to the
39
 * server specified by `JETPACK__SANDBOX_DOMAIN`.
40
 *
41
 * Attached to the `requests-requests.before_request` filter.
42
 * @param string &$url URL of request about to be made
43
 * @param array  &$headers Headers of request about to be made
44
 * @return void
45
 */
46
function jetpack_server_sandbox( &$url, &$headers ) {
47
	if ( ! JETPACK__SANDBOX_DOMAIN ) {
48
		return;
49
	}
50
51
	$original_url = $url;
52
53
	$request_parameters = jetpack_server_sandbox_request_parameters( JETPACK__SANDBOX_DOMAIN, $url, $headers );
54
	$url = $request_parameters['url'];
55
	if ( $request_parameters['host'] ) {
56
		$headers['Host'] = $request_parameters['host'];
57
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
58
			error_log( sprintf( "SANDBOXING via '%s': '%s'", JETPACK__SANDBOX_DOMAIN, $original_url ) );
59
		}
60
	}
61
}
62
63
add_action( 'requests-requests.before_request', 'jetpack_server_sandbox', 10, 2 );
64