Skip to content

Commit 092579d

Browse files
kangzjmatticbot
authored andcommitted
WooCommerce Analytics: Fix proxy speed module installation with proper error/path handling (#45801)
* Enhance WooCommerce Analytics: Add checks for mu-plugin directory writability and source file existence, improve error logging, and ensure proper file copying. * Add changelog * Update projects/packages/woocommerce-analytics/changelog/fix-woocomerce-analytics-proxy-speed-copy Co-authored-by: Copilot <[email protected]> * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * Use copy directly * WooCommerce Analytics: Use WP_Filesystem API consistently --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Jasper Kang <[email protected]> Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/19650795756 Upstream-Ref: Automattic/jetpack@88386db
1 parent 80fc426 commit 092579d

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.12.3-alpha] - unreleased
9+
10+
This is an alpha version! The changes listed here are not final.
11+
12+
### Fixed
13+
- Fix proxy speed module installation using WP_Filesystem API with proper error handling
14+
815
## [0.12.2] - 2025-11-24
916
### Fixed
1017
- Improve compatibility with old WooCommerce versions. [#46003]
@@ -216,6 +223,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
216223
- Fix namespace issue with WooCommerce class reference. [#35857]
217224
- General: bail early when WooCommerce is not active. [#36278]
218225

226+
[0.12.3-alpha]: https://github.com/Automattic/woocommerce-analytics/compare/v0.12.2...v0.12.3-alpha
219227
[0.12.2]: https://github.com/Automattic/woocommerce-analytics/compare/v0.12.1...v0.12.2
220228
[0.12.1]: https://github.com/Automattic/woocommerce-analytics/compare/v0.12.0...v0.12.1
221229
[0.12.0]: https://github.com/Automattic/woocommerce-analytics/compare/v0.11.0...v0.12.0

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@automattic/woocommerce-analytics",
3-
"version": "0.12.2",
3+
"version": "0.12.3-alpha",
44
"private": true,
55
"description": "WooCommerce Analytics package to track frontend events",
66
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/woocommerce-analytics/#readme",

src/class-woocommerce-analytics.php

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Woocommerce_Analytics {
2121
/**
2222
* Package version.
2323
*/
24-
const PACKAGE_VERSION = '0.12.2';
24+
const PACKAGE_VERSION = '0.12.3-alpha';
2525

2626
/**
2727
* Proxy speed module version.
@@ -174,12 +174,11 @@ public static function register_rest_routes() {
174174
* Maybe add proxy speed module.
175175
*/
176176
public static function maybe_add_proxy_speed_module() {
177-
if ( ! function_exists( 'WP_Filesystem' ) ) {
178-
require_once ABSPATH . 'wp-admin/includes/file.php';
177+
if ( ! self::init_filesystem() ) {
178+
return;
179179
}
180180

181-
// Initialize the WP filesystem.
182-
WP_Filesystem();
181+
global $wp_filesystem;
183182

184183
// Create the mu-plugin directory if it doesn't exist.
185184
if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
@@ -191,36 +190,85 @@ public static function maybe_add_proxy_speed_module() {
191190
return;
192191
}
193192

193+
// Check if the mu-plugin directory is writable.
194+
if ( ! $wp_filesystem->is_writable( WPMU_PLUGIN_DIR ) ) {
195+
if ( function_exists( 'wc_get_logger' ) ) {
196+
wc_get_logger()->debug( 'WooCommerce Analytics proxy speed module not installed: mu-plugins directory is not writable.', array( 'source' => 'woocommerce-analytics' ) );
197+
}
198+
return;
199+
}
200+
194201
if ( get_option( 'woocommerce_analytics_proxy_speed_module_version' ) === self::PROXY_SPEED_MODULE_VERSION ) {
195202
// No need to copy the files again.
196203
return;
197204
}
198205

199-
update_option( 'woocommerce_analytics_proxy_speed_module_version', self::PROXY_SPEED_MODULE_VERSION );
200206
$mu_plugin_src_file = __DIR__ . '/mu-plugin/woocommerce-analytics-proxy-speed-module.php';
201-
$mu_plugin_dest_file = WPMU_PLUGIN_DIR . '/woocommerce-analytics-proxy-speed-module.php';
202-
$results = copy( $mu_plugin_src_file, $mu_plugin_dest_file );
207+
$mu_plugin_dest_file = trailingslashit( WPMU_PLUGIN_DIR ) . 'woocommerce-analytics-proxy-speed-module.php';
208+
209+
// Verify source file exists before attempting to copy.
210+
if ( ! file_exists( $mu_plugin_src_file ) ) {
211+
if ( function_exists( 'wc_get_logger' ) ) {
212+
wc_get_logger()->error( 'WooCommerce Analytics proxy speed module source file not found.', array( 'source' => 'woocommerce-analytics' ) );
213+
}
214+
return;
215+
}
216+
217+
$content = $wp_filesystem->get_contents( $mu_plugin_src_file );
218+
if ( false === $content ) {
219+
if ( function_exists( 'wc_get_logger' ) ) {
220+
wc_get_logger()->error( 'Failed to read the WooCommerce Analytics proxy speed module source file.', array( 'source' => 'woocommerce-analytics' ) );
221+
}
222+
return;
223+
}
203224

204-
if ( ! $results ) {
225+
if ( ! $wp_filesystem->put_contents( $mu_plugin_dest_file, $content ) ) {
205226
if ( function_exists( 'wc_get_logger' ) ) {
206-
wc_get_logger()->error( 'Failed to copy the WooCommerce Analytics proxy speed module files.', array( 'source' => 'woocommerce-analytics' ) );
227+
wc_get_logger()->error( 'Failed to copy the WooCommerce Analytics proxy speed module file.', array( 'source' => 'woocommerce-analytics' ) );
207228
}
229+
return;
208230
}
231+
232+
update_option( 'woocommerce_analytics_proxy_speed_module_version', self::PROXY_SPEED_MODULE_VERSION );
209233
}
210234

211235
/**
212236
* Maybe removes the proxy speed module. This should be invoked when the plugin is deactivated.
213237
*/
214238
public static function maybe_remove_proxy_speed_module() {
239+
if ( ! self::init_filesystem() ) {
240+
return;
241+
}
242+
243+
global $wp_filesystem;
244+
215245
/**
216246
* Clean up MU plugin.
217247
*/
218-
$file_path = WPMU_PLUGIN_DIR . '/woocommerce-analytics-proxy-speed-module.php';
248+
$file_path = trailingslashit( WPMU_PLUGIN_DIR ) . 'woocommerce-analytics-proxy-speed-module.php';
219249

220-
if ( file_exists( $file_path ) ) {
221-
wp_delete_file( $file_path );
250+
if ( $wp_filesystem->exists( $file_path ) && $wp_filesystem->is_writable( $file_path ) ) {
251+
$wp_filesystem->delete( $file_path );
222252
}
223253

224254
delete_option( 'woocommerce_analytics_proxy_speed_module_version' );
225255
}
256+
257+
/**
258+
* Initialize the WP filesystem.
259+
*
260+
* @return bool True if filesystem is initialized, false otherwise.
261+
*/
262+
private static function init_filesystem() {
263+
if ( ! function_exists( 'WP_Filesystem' ) ) {
264+
require_once ABSPATH . 'wp-admin/includes/file.php';
265+
}
266+
267+
// Initialize the WP filesystem.
268+
ob_start();
269+
$initialized = WP_Filesystem();
270+
ob_end_clean();
271+
272+
return $initialized;
273+
}
226274
}

0 commit comments

Comments
 (0)