From dfd7d52d6b51af139bac062c8977c3444295da29 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Thu, 19 Sep 2024 12:22:53 +0800 Subject: [PATCH] [beta tester] Simulate core error early in the request lifecycle before WP fully loaded. (#51330) * Improve remote logging tool to simulate core error early in the request lifecycle before wp fully loaded * Disable wp error handler when simulate_woocommerce_error --- .../dev-simulate-error-before-wp-loaded | 4 +++ .../woocommerce-beta-tester.php | 27 ++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 plugins/woocommerce-beta-tester/changelog/dev-simulate-error-before-wp-loaded 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' ); }