[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
This commit is contained in:
Chi-Hsuan Huang 2024-09-19 12:22:53 +08:00 committed by GitHub
parent 2fc6528c61
commit dfd7d52d6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 3 deletions

View File

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

View File

@ -143,11 +143,31 @@ 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() {
// 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' );
}