flush_rewrite_rules() called excessively
-
Hi support
We’ve run into an issue with flush_rewrite_rules() being called excessively when using ApplePay.
The symptoms of the issue is the site showing sporadic 404 for all front-facing URLs.
All admin pages will work as long as they don’t require the REST API as those will also fail with 404s.We first detected this by hooking in like this:
<?php
add_filter('rewrite_rules_array', 'yanco_rewrite_rules_detector');
function yanco_rewrite_rules_detector($rules)
{
// Mail about rewrite being flushed
wp_mail('[email protected]', 'Rewrite Rules Detector', print_r($rules, true));
return $rules;
}This would send us an e-mail when ever the rewrite rules got flushed, this is a sample from a 1-hour timeframe – Notice there’s 18 flushes in just one hour(!) that’s quite excessive.
Then we setup a more in-depth logging by temporarily modifying /wp-includes/rewrite.php as there’s not other real way to know what calles flush_rewrite_rules()
<?php
/**
* Removes rewrite rules and then recreate rewrite rules.
*
* @since 3.0.0
*
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
*
* @param bool $hard Whether to update .htaccess (hard flush) or just update
* rewrite_rules option (soft flush). Default is true (hard).
*/
function flush_rewrite_rules($hard = true)
{
global $wp_rewrite;
if (is_callable(array( $wp_rewrite, 'flush_rules' ))) {
$wp_rewrite->flush_rules($hard);
}
// Get the backtrace
$backtrace = debug_backtrace();
// Log or email the backtrace for debugging
$log_message = "flush_rewrite_rules() was called!\n";
foreach ($backtrace as $trace) {
if (isset($trace['file']) && isset($trace['line'])) {
$log_message .= sprintf("Called in %s on line %d\n", $trace['file'], $trace['line']);
}
}
// Send an email or log the backtrace
wp_mail('[email protected]', 'Rewrite Rules Detector', $log_message);
}Checking each of those entries we could see this backtrace
flush_rewrite_rules() was called!
Called in /home/kbhkollegier/public_html/wp-content/plugins/woocommerce-gateway-stripe/includes/class-wc-stripe-apple-pay-registration.php on line 326
Called in /home/kbhkollegier/public_html/wp-content/plugins/woocommerce-gateway-stripe/includes/class-wc-stripe-apple-pay-registration.php on line 113
Called in /home/kbhkollegier/public_html/wp-includes/class-wp-hook.php on line 324
Called in /home/kbhkollegier/public_html/wp-includes/class-wp-hook.php on line 348
Called in /home/kbhkollegier/public_html/wp-includes/plugin.php on line 517
Called in /home/kbhkollegier/public_html/wp-admin/admin-ajax.php on line 45Diving into that particular file we can tell this is happening:
public function verify_domain_if_configured() {
$secret_key = $this->get_secret_key();
if ( ! $this->is_enabled() || empty( $secret_key ) ) {
return;
}
if ( ! $this->is_available() ) {
return;
}
// Ensure that domain association file will be served.
flush_rewrite_rules();
// The rewrite rule method doesn't work if permalinks are set to Plain.
// Create/update domain association file by copying it from the plugin folder as a fallback.
$this->update_domain_association_file();
// Register the domain with Apple Pay.
$verification_complete = $this->register_domain_with_apple( $secret_key );
// Show/hide notes if necessary.
WC_Stripe_Inbox_Notes::notify_on_apple_pay_domain_verification( $verification_complete );
}If we check the WooCommerce logs for the woocommerce-gateway-stripe we can see a lot of entries like this
2024-09-18T00:00:25+00:00 Fejlret
====Stripe Version: 8.7.0====
====Stripe Plugin API Version: 2024-06-20====
====Start Log====
Your domain has been verified with Apple Pay!
====End Log====Checking just yesterday we can see no less than 674 entries of those, that would be around 28 per hour (!) which looking into the code base all would result in a flush_rewrite_rules().
It would seem there’s some sort of bug in the codebase as triggering those flushes probably should not be happening 28 times per hour.
The page I need help with: [log in to see the link]
- The topic ‘flush_rewrite_rules() called excessively’ is closed to new replies.