Skip to content

Commit

Permalink
Additional check on the rewrite URL to test if rewrites needs to be f…
Browse files Browse the repository at this point in the history
…lushed
  • Loading branch information
carstingaxion committed Sep 15, 2024
1 parent c37112f commit e72d25c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions includes/core/classes/endpoints/class-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function init(): void {
add_rewrite_rule( $reg_ex_pattern, $rewrite_url, 'top' );

// Trigger a flush of rewrite rules.
$this->maybe_flush_rewrite_rules( $reg_ex_pattern );
$this->maybe_flush_rewrite_rules( $reg_ex_pattern, $rewrite_url );

// Allow the custom query variable by filtering the public query vars.
add_filter( 'query_vars', array( $this, 'allow_query_vars' ) );
Expand Down Expand Up @@ -246,6 +246,10 @@ public function get_rewrite_atts(): array {

/**
* Creates a flag option to indicate that rewrite rules need to be flushed.
*
* This method checks if the generated rewrite rules already exist in the DB,
* and if its rewrite URL matches the recently generated rewrite URL.
* If any of this checks fail, the option to flush the rewrite rules will be set.
*
* This method DOES NO checks if the 'gatherpress_flush_rewrite_rules_flag' option
* exists. It just adds the option and sets it to true. This flag
Expand All @@ -254,11 +258,13 @@ public function get_rewrite_atts(): array {
* @since 1.0.0
*
* @param string $reg_ex_pattern The regular expression pattern for matching the custom endpoint URL structure.
* @param string $rewrite_url The URL structure for handling matched requests via query vars.
* @return void
*/
private function maybe_flush_rewrite_rules( string $reg_ex_pattern ): void {
private function maybe_flush_rewrite_rules( string $reg_ex_pattern, string $rewrite_url ): void {
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules[ $reg_ex_pattern ] ) ) {

if ( ! isset( $rules[ $reg_ex_pattern ] ) || $rules[ $reg_ex_pattern ] !== $rewrite_url ) {
// Event_Setup->maybe_create_flush_rewrite_rules_flag // @todo maybe make this a public method ?!
// @see https://github.com/GatherPress/gatherpress/blob/3d91f2bcb30b5d02ebf459cd5a42d4f43bc05ea5/includes/core/classes/class-settings.php#L760C1-L761C63 .
add_option( 'gatherpress_flush_rewrite_rules_flag', true );
Expand Down

0 comments on commit e72d25c

Please sign in to comment.