summaryrefslogtreecommitdiff
blob: a0c907987628274468d617db0ed44ec8aa213ab4 (plain)
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
<?php

/**
 * This feature is only useful for Automattic developers.
 * It configures Jetpack to talk to staging/sandbox servers
 * on WordPress.com instead of production servers.
 */

/**
 * @param string $sandbox Sandbox domain
 * @param string $url URL of request about to be made
 * @param array  $headers Headers of request about to be made
 * @return array [ 'url' => new URL, 'host' => new Host ]
 */
function jetpack_server_sandbox_request_parameters( $sandbox, $url, $headers ) {
	$host = '';

	$url_host = parse_url( $url, PHP_URL_HOST );

	switch ( $url_host ) {
	case 'public-api.wordpress.com' :
	case 'jetpack.wordpress.com' :
	case 'jetpack.com' :
	case 'dashboard.wordpress.com' :
		$host = isset( $headers['Host'] ) ? $headers['Host'] : $url_host;
		$url = preg_replace(
			'@^(https?://)' . preg_quote( $url_host, '@' ) . '(?=[/?#].*|$)@',
			'\\1' . $sandbox,
			$url,
			1
		);
	}

	return compact( 'url', 'host' );
}

/**
 * Modifies parameters of request in order to send the request to the
 * server specified by `JETPACK__SANDBOX_DOMAIN`.
 *
 * Attached to the `requests-requests.before_request` filter.
 * @param string &$url URL of request about to be made
 * @param array  &$headers Headers of request about to be made
 * @return void
 */
function jetpack_server_sandbox( &$url, &$headers ) {
	if ( ! JETPACK__SANDBOX_DOMAIN ) {
		return;
	}

	$original_url = $url;

	$request_parameters = jetpack_server_sandbox_request_parameters( JETPACK__SANDBOX_DOMAIN, $url, $headers );
	$url = $request_parameters['url'];
	if ( $request_parameters['host'] ) {
		$headers['Host'] = $request_parameters['host'];
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( sprintf( "SANDBOXING via '%s': '%s'", JETPACK__SANDBOX_DOMAIN, $original_url ) );
		}
	}
}

add_action( 'requests-requests.before_request', 'jetpack_server_sandbox', 10, 2 );