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 = parse_url( $url, PHP_URL_HOST ); |
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
|
|
|
|