summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYury German <blueknight@gentoo.org>2019-05-22 01:01:36 -0400
committerYury German <blueknight@gentoo.org>2019-05-22 01:01:36 -0400
commit0914c92da22824025992c368c745546e41fbeb84 (patch)
tree965f6adf3b725e56d559fe4a93eff02281499dcc /plugins/jetpack/_inc/jetpack-server-sandbox.php
parentDeleting plugins for update (diff)
downloadblogs-gentoo-0914c92da22824025992c368c745546e41fbeb84.tar.gz
blogs-gentoo-0914c92da22824025992c368c745546e41fbeb84.tar.bz2
blogs-gentoo-0914c92da22824025992c368c745546e41fbeb84.zip
Adding Plugins
Updating the following akismet.4.1.2, google-authenticator.0.52, jetpack.7.3.1 Signed-off-by: Yury German <blueknight@gentoo.org>
Diffstat (limited to 'plugins/jetpack/_inc/jetpack-server-sandbox.php')
-rw-r--r--plugins/jetpack/_inc/jetpack-server-sandbox.php63
1 files changed, 63 insertions, 0 deletions
diff --git a/plugins/jetpack/_inc/jetpack-server-sandbox.php b/plugins/jetpack/_inc/jetpack-server-sandbox.php
new file mode 100644
index 00000000..a0c90798
--- /dev/null
+++ b/plugins/jetpack/_inc/jetpack-server-sandbox.php
@@ -0,0 +1,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 );