@@ -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