blocks
6 years ago
build
6 years ago
fonts
8 years ago
genericons
6 years ago
lib
6 years ago
social-logos
7 years ago
accessible-focus.js
8 years ago
class.jetpack-provision.php
6 years ago
connect-button.js
6 years ago
crowdsignal-shortcode.js
7 years ago
crowdsignal-survey.js
7 years ago
facebook-embed.js
7 years ago
footer.php
7 years ago
gallery-settings.js
7 years ago
genericons.php
11 years ago
header.php
7 years ago
idc-notice.js
7 years ago
jetpack-admin.js
7 years ago
jetpack-connection-banner.js
6 years ago
jetpack-jitm.js
7 years ago
jetpack-modules.js
7 years ago
jetpack-modules.models.js
6 years ago
jetpack-modules.views.js
7 years ago
jetpack-server-sandbox.php
6 years ago
jetpack-strings.php
6 years ago
jquery.jetpack-resize.js
7 years ago
jquery.spin.js
8 years ago
polldaddy-shortcode.js
6 years ago
postmessage.js
8 years ago
social-logos.php
9 years ago
spin.js
8 years ago
twitter-timeline.js
6 years ago
jetpack-server-sandbox.php
64 lines
| 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 ); |
| 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 |