Make WordPress Core

Opened 9 years ago

Closed 7 years ago

Last modified 4 years ago

#29107 closed defect (bug) (fixed)

WP_Rewrite::flush_rules() should not delete 'rewrite_rules' option

Reported by: sergeybiryukov's profile SergeyBiryukov Owned by: swissspidy's profile swissspidy
Milestone: 4.5 Priority: normal
Severity: normal Version: 2.0.1
Component: Rewrite Rules Keywords: has-patch has-unit-tests commit
Focuses: Cc:


If permalinks are disabled, there's an extra query on each page load:

SELECT option_value FROM trunk_options WHERE option_name = 'rewrite_rules' LIMIT 1

It comes from WP::parse_request(), which calls WP_Rewrite::wp_rewrite_rules().

Normally, it should be covered by wp_load_alloptions(), but:

  1. The rewrite_rules option is not created on installation.
  2. If you enable permalinks and disable them again, WP_Rewrite::flush_rules() deletes the option.
  3. WP_Rewrite::wp_rewrite_rules() attempts to recreate it, but fails due to the check in update_option().

I think we should replace rewrite_rules option with an empty value instead of deleting it.

Introduced in [3373].

Attachments (4)

29107.patch (1.0 KB) - added by SergeyBiryukov 9 years ago.
29107-tests.diff (730 bytes) - added by jesin 9 years ago.
Tests if rewrite_rules is an array and is not empty
29107.diff (1.7 KB) - added by voldemortensen 9 years ago.
29107.2.diff (1.5 KB) - added by swissspidy 7 years ago.

Download all attachments as: .zip

Change History (14)

#2 @nacin
9 years ago

Can we confirm (perhaps with a unit test) that an empty option gets updated to a populated option when a structure is set and rules are flushed?

#3 @SergeyBiryukov
9 years ago

  • Keywords needs-unit-tests added

9 years ago

Tests if rewrite_rules is an array and is not empty

9 years ago

#4 @voldemortensen
9 years ago

  • Keywords needs-unit-tests removed

Refreshed patch and combined unit tests.

#5 @chriscct7
7 years ago

  • Keywords needs-refresh has-unit-tests added

7 years ago

#6 @swissspidy
7 years ago

  • Keywords needs-refresh removed
  • Milestone changed from Awaiting Review to 4.5

29107.2.diff is an updated patch that applies cleanly against trunk.

In what ways does this affect #11384?

#7 @swissspidy
7 years ago

  • Keywords commit added

#8 @swissspidy
7 years ago

  • Owner set to swissspidy
  • Resolution set to fixed
  • Status changed from new to closed

In 36254:

Rewrite: Ensure WP_Rewrite::flush_rules() does not delete the 'rewrite_rules' option.

Instead, the option gets updated to an empty string.
Adds unit tests.

Props SergeyBiryukov, jesin, voldemortensen.
Fixes #29107.

#9 @swissspidy
7 years ago

#11384 was marked as a duplicate.

#10 @johnjamesjacoby
4 years ago

update_home_siteurl() and site-info.php both continue to call delete_option( 'rewrite_rules' ).

Is it possible to confirm whether those usages may cause this issue to reappear?

Also, delete_option( 'rewrite_rules' ) has been used for a very long time as the way for plugins to easily (and maybe poorly) queue up a rewrite rule flush on the subsequent page load. A quick search on GitHub shows over 47,000 usages across all repositories of people using it, mostly for this reason.

Is that enough in the wild for core to provide a helper function (like queue_rewrite_rule_updates() or something) that everyone can change to using instead of incorrectly deleting the option and causing this extra query to reappear?

Or... instead of caring if the option is empty vs. missing, is there a different approach core can take to tuck this concern under the rug, and internally add rewrite_rules to the notoptions array before it's queried independently, or something less sinister?

Last edited 4 years ago by johnjamesjacoby (previous) (diff)
Note: See TracTickets for help on using tickets.