{"id":18021808,"date":"2024-09-18T08:39:54","date_gmt":"2024-09-18T08:39:54","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/flush_rewrite_rules-called-excessively\/"},"modified":"2024-09-18T08:39:54","modified_gmt":"2024-09-18T08:39:54","slug":"flush_rewrite_rules-called-excessively","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/flush_rewrite_rules-called-excessively\/","title":{"rendered":"flush_rewrite_rules() called excessively"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hi support<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ve run into an issue with <strong>flush_rewrite_rules()<\/strong> being called excessively when using ApplePay.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The symptoms of the issue is the site showing sporadic 404 for all front-facing URLs.<br \/>All admin pages will work as long as they don&#8217;t require the REST API as those will also fail with 404s.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We first detected this by hooking in like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br \/><br \/>add_filter('rewrite_rules_array', 'yanco_rewrite_rules_detector');<br \/>function yanco_rewrite_rules_detector($rules)<br \/>{<br \/>    \/\/ Mail about rewrite being flushed<br \/>    wp_mail('REDACTED@EMAIL-REDACTED.COM', 'Rewrite Rules Detector', print_r($rules, true));<br \/><br \/>    return $rules;<br \/>}<br \/><br \/><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This would send us an e-mail when ever the rewrite rules got flushed, this is a sample from a 1-hour timeframe &#8211; Notice there&#8217;s 18 flushes in just one hour(!) that&#8217;s quite excessive.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-imgur wp-block-embed-imgur\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"imgur-embed-pub\" lang=\"en\" data-id=\"a\/MC0eg2f\"><a href=\"https:\/\/imgur.com\/a\/MC0eg2f\">View post on imgur.com<\/a><\/blockquote><script async src=\"\/\/s.imgur.com\/min\/embed.js\" charset=\"utf-8\"><\/script>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Then we setup a more in-depth logging by temporarily modifying <strong>\/wp-includes\/rewrite.php<\/strong> as there&#8217;s not other real way to know what calles <strong>flush_rewrite_rules()<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br \/>\/**<br \/> * Removes rewrite rules and then recreate rewrite rules.<br \/> *<br \/> * @since 3.0.0<br \/> *<br \/> * @global WP_Rewrite $wp_rewrite WordPress rewrite component.<br \/> *<br \/> * @param bool $hard Whether to update .htaccess (hard flush) or just update<br \/> *                   rewrite_rules option (soft flush). Default is true (hard).<br \/> *\/<br \/>function flush_rewrite_rules($hard = true)<br \/>{<br \/>    global $wp_rewrite;<br \/><br \/>    if (is_callable(array( $wp_rewrite, 'flush_rules' ))) {<br \/>        $wp_rewrite-&gt;flush_rules($hard);<br \/>    }<br \/><br \/>    \/\/ Get the backtrace<br \/>    $backtrace = debug_backtrace();<br \/><br \/>    \/\/ Log or email the backtrace for debugging<br \/>    $log_message = \"flush_rewrite_rules() was called!\\n\";<br \/>    foreach ($backtrace as $trace) {<br \/>        if (isset($trace&#091;'file']) &amp;&amp; isset($trace&#091;'line'])) {<br \/>            $log_message .= sprintf(\"Called in %s on line %d\\n\", $trace&#091;'file'], $trace&#091;'line']);<br \/>        }<br \/>    }<br \/><br \/>    \/\/ Send an email or log the backtrace<br \/>    wp_mail('REDACTED@EMAIL-REDACTED.COM', 'Rewrite Rules Detector', $log_message);<br \/>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Checking each of those entries we could see this backtrace<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flush_rewrite_rules() was called! <br \/>Called in \/home\/kbhkollegier\/public_html\/wp-content\/plugins\/<strong>woocommerce-gateway-stripe\/includes\/class-wc-stripe-apple-pay-registration.php<\/strong> on line 326 <br \/>Called in \/home\/kbhkollegier\/public_html\/wp-content\/plugins\/<strong>woocommerce-gateway-stripe\/includes\/class-wc-stripe-apple-pay-registration.php<\/strong> on line 113 <br \/>Called in \/home\/kbhkollegier\/public_html\/wp-includes\/class-wp-hook.php on line 324 <br \/>Called in \/home\/kbhkollegier\/public_html\/wp-includes\/class-wp-hook.php on line 348 <br \/>Called in \/home\/kbhkollegier\/public_html\/wp-includes\/plugin.php on line 517 <br \/>Called in \/home\/kbhkollegier\/public_html\/wp-admin\/admin-ajax.php on line 45<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Diving into that particular file we can tell this is happening:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function verify_domain_if_configured() {<br \/>\t\t$secret_key = $this-&gt;get_secret_key();<br \/><br \/>\t\tif ( ! $this-&gt;is_enabled() || empty( $secret_key ) ) {<br \/>\t\t\treturn;<br \/>\t\t}<br \/><br \/>\t\tif ( ! $this-&gt;is_available() ) {<br \/>\t\t\treturn;<br \/>\t\t}<br \/><br \/>\t\t\/\/ Ensure that domain association file will be served.<br \/>\t\t<strong>flush_rewrite_rules();<\/strong><br \/><br \/>\t\t\/\/ The rewrite rule method doesn't work if permalinks are set to Plain.<br \/>\t\t\/\/ Create\/update domain association file by copying it from the plugin folder as a fallback.<br \/>\t\t$this-&gt;update_domain_association_file();<br \/><br \/>\t\t\/\/ Register the domain with Apple Pay.<br \/>\t\t$verification_complete = $this-&gt;register_domain_with_apple( $secret_key );<br \/><br \/>\t\t\/\/ Show\/hide notes if necessary.<br \/>\t\tWC_Stripe_Inbox_Notes::notify_on_apple_pay_domain_verification( $verification_complete );<br \/>\t}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If we check the WooCommerce logs for the <strong>woocommerce-gateway-stripe<\/strong> we can see a lot of entries like this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2024-09-18T00:00:25+00:00 Fejlret <br \/>====Stripe Version: 8.7.0====<br \/>====Stripe Plugin API Version: 2024-06-20====<br \/>====Start Log====<br \/>Your domain has been verified with Apple Pay!<br \/>====End Log====<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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 <strong>flush_rewrite_rules()<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> It would seem there&#8217;s some sort of bug in the codebase as triggering those flushes probably should not be happening 28 times per hour.<\/p>\n","protected":false},"template":"","class_list":["post-18021808","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/18021808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/18021808\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=18021808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}