diff --git a/projects/packages/woocommerce-analytics/changelog/add-wca-proxy-speed-module b/projects/packages/woocommerce-analytics/changelog/add-wca-proxy-speed-module new file mode 100644 index 0000000000000..843a1087d645b --- /dev/null +++ b/projects/packages/woocommerce-analytics/changelog/add-wca-proxy-speed-module @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add proxy speed module to enhance proxy API performance diff --git a/projects/packages/woocommerce-analytics/src/class-woocommerce-analytics.php b/projects/packages/woocommerce-analytics/src/class-woocommerce-analytics.php index c72a7363af441..9eef7961e983f 100644 --- a/projects/packages/woocommerce-analytics/src/class-woocommerce-analytics.php +++ b/projects/packages/woocommerce-analytics/src/class-woocommerce-analytics.php @@ -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. @@ -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' ); + } } diff --git a/projects/packages/woocommerce-analytics/src/mu-plugin/woocommerce-analytics-proxy-speed-module.php b/projects/packages/woocommerce-analytics/src/mu-plugin/woocommerce-analytics-proxy-speed-module.php new file mode 100644 index 0000000000000..03a8d65a604fe --- /dev/null +++ b/projects/packages/woocommerce-analytics/src/mu-plugin/woocommerce-analytics-proxy-speed-module.php @@ -0,0 +1,83 @@ +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(); diff --git a/projects/plugins/jetpack/changelog/add-wca-proxy-speed-module b/projects/plugins/jetpack/changelog/add-wca-proxy-speed-module new file mode 100644 index 0000000000000..a7a586e7bf423 --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-wca-proxy-speed-module @@ -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 diff --git a/projects/plugins/jetpack/class.jetpack.php b/projects/plugins/jetpack/class.jetpack.php index 805bfd8982dc9..0cb731b7c68c5 100644 --- a/projects/plugins/jetpack/class.jetpack.php +++ b/projects/plugins/jetpack/class.jetpack.php @@ -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 ); @@ -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(); } @@ -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(); + } } /**