Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add proxy speed module to enhance proxy API performance
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class Woocommerce_Analytics {
*/
const PACKAGE_VERSION = '0.8.0';

/**
* Proxy speed module version.
*
* @var string
*/
const PROXY_SPEED_MODULE_VERSION = '1.0.0';

/**
* Initializer.
* Used to configure the WooCommerce Analytics package.
Expand Down Expand Up @@ -131,4 +138,58 @@ public static function register_rest_routes() {
$controller = new WC_Analytics_Tracking_Proxy();
$controller->register_routes();
}

/**
* Maybe add proxy speed module.
*/
public static function maybe_add_proxy_speed_module() {
if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}

// Initialize the WP filesystem.
WP_Filesystem();

// Create the mu-plugin directory if it doesn't exist.
if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
wp_mkdir_p( WPMU_PLUGIN_DIR );
}

// If the mu-plugin directory doesn't exist, we can't copy the files.
if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
return;
}

if ( get_option( 'woocommerce_analytics_proxy_speed_module_version' ) === self::PROXY_SPEED_MODULE_VERSION ) {
// No need to copy the files again.
return;
}

update_option( 'woocommerce_analytics_proxy_speed_module_version', self::PROXY_SPEED_MODULE_VERSION );
$mu_plugin_src_file = __DIR__ . '/mu-plugin/woocommerce-analytics-proxy-speed-module.php';
$mu_plugin_dest_file = WPMU_PLUGIN_DIR . '/woocommerce-analytics-proxy-speed-module.php';
$results = copy( $mu_plugin_src_file, $mu_plugin_dest_file );

if ( ! $results ) {
if ( function_exists( 'wc_get_logger' ) ) {
wc_get_logger()->error( 'Failed to copy the WooCommerce Analytics proxy speed module files.', array( 'source' => 'woocommerce-analytics' ) );
}
}
}

/**
* Maybe removes the proxy speed module. This should be invoked when the plugin is deactivated.
*/
public static function maybe_remove_proxy_speed_module() {
/**
* Clean up MU plugin.
*/
$file_path = WPMU_PLUGIN_DIR . '/woocommerce-analytics-proxy-speed-module.php';

if ( file_exists( $file_path ) ) {
wp_delete_file( $file_path );
}

delete_option( 'woocommerce_analytics_proxy_speed_module_version' );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Plugin Name: WooCommerce Analytics - Proxy Speed Module
* Description: Speeds up WooCommerce Analytics' proxy for avoiding ad blockers.
* Plugin URI: https://woocommerce.com
* Author: WooCommerce
* Version: 1.0.0
* Author URI: https://woocommerce.com
*
* Text Domain: woocommerce-analytics
*
* Inspired by: https://github.com/plausible/wordpress/blob/092b97b247f45bf347ae32f9614f20a81d9e10c0/mu-plugin/plausible-proxy-speed-module.php
*/
class WooCommerceAnalyticsProxySpeed {
/**
* Path of the proxy request.
*
* @var string
*/
const PROXY_REQUEST_PATH = 'woocommerce-analytics/v1/track';

/**
* Allowed plugin files for proxy request.
*
* @var array
*/
private $allowed_plugin_files = array( 'woocommerce.php', 'woocommerce-analytics.php', 'jetpack.php' );

/**
* Add filters and actions.
*
* @return void
*/
public function init() {
add_filter( 'option_active_plugins', array( $this, 'filter_active_plugins' ) );
}

/**
* Filter the list of active plugins for custom endpoint requests.
*
* @param array $active_plugins The list of active plugins.
*
* @return array The filtered list of active plugins.
*/
public function filter_active_plugins( $active_plugins ) {
if ( ! $this->is_proxy_request() || ! is_array( $active_plugins ) ) {
return $active_plugins;
}

$filtered_plugins = array();

foreach ( $active_plugins as $plugin ) {
foreach ( $this->allowed_plugin_files as $allowed_plugin_file ) {
if ( strpos( $plugin, $allowed_plugin_file ) !== false ) {
$filtered_plugins[] = $plugin;
break;
}
}
}

return $filtered_plugins;
}

/**
* Helper method to retrieve Request URI. Checks several globals.
*
* @return mixed
*/
private function get_request_uri() {
return isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}

/**
* Check if current request is a proxy request.
*
* @return bool
*/
private function is_proxy_request() {
return strpos( $this->get_request_uri(), self::PROXY_REQUEST_PATH ) !== false;
}
}

( new WooCommerceAnalyticsProxySpeed() )->init();
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-wca-proxy-speed-module
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Integrate calls to add and remove the proxy speed module in Jetpack plugin initialization and deactivation processes
9 changes: 9 additions & 0 deletions projects/plugins/jetpack/class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Automattic\Jetpack\Sync\Sender;
use Automattic\Jetpack\Terms_Of_Service;
use Automattic\Jetpack\Tracking;
use Automattic\Woocommerce_Analytics;

if ( ! defined( 'ABSPATH' ) ) {
exit( 0 );
Expand Down Expand Up @@ -2623,6 +2624,10 @@ public static function plugin_activation( $network_wide ) {
Sync_Actions::do_only_first_initial_sync();
}

if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
Woocommerce_Analytics::maybe_add_proxy_speed_module();
}

self::plugin_initialize();
}

Expand Down Expand Up @@ -2803,6 +2808,10 @@ public static function plugin_deactivation() {
self::disconnect();
Jetpack_Options::delete_option( 'version' );
}

if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
Woocommerce_Analytics::maybe_remove_proxy_speed_module();
}
}

/**
Expand Down
Loading