Нет описания

jetpack-server-sandbox.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * This feature is only useful for Automattic developers.
  4. * It configures Jetpack to talk to staging/sandbox servers
  5. * on WordPress.com instead of production servers.
  6. *
  7. * @package automattic/jetpack
  8. */
  9. /**
  10. * Provides sandbox request parameters.
  11. *
  12. * @param string $sandbox Sandbox domain.
  13. * @param string $url URL of request about to be made.
  14. * @param array $headers Headers of request about to be made.
  15. * @return array [ 'url' => new URL, 'host' => new Host ].
  16. */
  17. function jetpack_server_sandbox_request_parameters( $sandbox, $url, $headers ) {
  18. $host = '';
  19. $url_host = wp_parse_url( $url, PHP_URL_HOST );
  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. return compact( 'url', 'host' );
  34. }
  35. /**
  36. * Modifies parameters of request in order to send the request to the
  37. * server specified by `JETPACK__SANDBOX_DOMAIN`.
  38. *
  39. * Attached to the `requests-requests.before_request` filter.
  40. *
  41. * @param string $url URL of request about to be made.
  42. * @param array $headers Headers of request about to be made.
  43. * @return void
  44. */
  45. function jetpack_server_sandbox( &$url, &$headers ) {
  46. if ( ! JETPACK__SANDBOX_DOMAIN ) {
  47. return;
  48. }
  49. $original_url = $url;
  50. $request_parameters = jetpack_server_sandbox_request_parameters( JETPACK__SANDBOX_DOMAIN, $url, $headers );
  51. $url = $request_parameters['url'];
  52. if ( $request_parameters['host'] ) {
  53. $headers['Host'] = $request_parameters['host'];
  54. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  55. error_log( sprintf( "SANDBOXING via '%s': '%s'", JETPACK__SANDBOX_DOMAIN, $original_url ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
  56. }
  57. }
  58. }
  59. add_action( 'requests-requests.before_request', 'jetpack_server_sandbox', 10, 2 );