diff --git a/plugins/woocommerce-beta-tester/changelog/dev-simulate-error-before-wp-loaded b/plugins/woocommerce-beta-tester/changelog/dev-simulate-error-before-wp-loaded new file mode 100644 index 00000000000..874acda860a --- /dev/null +++ b/plugins/woocommerce-beta-tester/changelog/dev-simulate-error-before-wp-loaded @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Improve remote logging tool to simulate core error early in the request lifecycle before wp fully loaded diff --git a/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php b/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php index bc1d9a76d72..f6bdbf24e8d 100644 --- a/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php +++ b/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php @@ -143,10 +143,30 @@ add_action( /** * Simulate a WooCommerce error for remote logging testing. * - * @throws Exception A simulated WooCommerce error if the option is set. + * This function adds a filter to the 'woocommerce_template_path' hook + * that throws an exception, then triggers the filter by calling WC()->template_path(). + * + * @throws Exception A simulated WooCommerce error for testing purposes. */ function simulate_woocommerce_error() { - throw new Exception( 'Simulated WooCommerce error for remote logging test' ); + // Return if WooCommerce is not loaded. + if ( ! function_exists( 'WC' ) || ! class_exists( 'WooCommerce' ) ) { + return; + } + + // Define a constant to prevent the error from being caught by the WP Error Handler. + if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) { + define( 'WP_SANDBOX_SCRAPING', true ); + } + + add_filter( + 'woocommerce_template_path', + function() { + throw new Exception( 'Simulated WooCommerce error for remote logging test' ); + } + ); + + WC()->template_path(); } $simulate_error = get_option( 'wc_beta_tester_simulate_woocommerce_php_error', false ); @@ -155,7 +175,8 @@ if ( $simulate_error ) { delete_option( 'wc_beta_tester_simulate_woocommerce_php_error' ); if ( 'core' === $simulate_error ) { - add_action( 'woocommerce_loaded', 'simulate_woocommerce_error' ); + // Hook into the plugin_loaded action to simulate the error early before WP fully initializes. + add_action( 'plugin_loaded', 'simulate_woocommerce_error' ); } elseif ( 'beta-tester' === $simulate_error ) { throw new Exception( 'Test PHP exception from WooCommerce Beta Tester' ); }