Skip to content

Commit a7f0218

Browse files
committed
Use a transient to determine if we should flush the rewrite rules.
1 parent acdbc27 commit a7f0218

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

classes/suggested-tasks/providers/class-permalink-structure.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ class Permalink_Structure extends Tasks_Interactive {
5454
*/
5555
public function init() {
5656
\add_action( 'wp_ajax_prpl_interactive_task_submit_core-permalink-structure', [ $this, 'handle_interactive_task_specific_submit' ] );
57+
\add_action( 'init', [ $this, 'maybe_flush_rewrite_rules' ] );
58+
}
59+
60+
/**
61+
* Maybe flush rewrite rules.
62+
*
63+
* Checks for a transient flag and flushes rewrite rules if needed.
64+
* This is more performant than flushing immediately during the AJAX request.
65+
*
66+
* @return void
67+
*/
68+
public function maybe_flush_rewrite_rules() {
69+
if ( \get_transient( 'prpl_flush_rewrite_rules' ) ) {
70+
\flush_rewrite_rules();
71+
\delete_transient( 'prpl_flush_rewrite_rules' );
72+
}
5773
}
5874

5975
/**
@@ -261,8 +277,9 @@ public function handle_interactive_task_specific_submit() {
261277
// Update the permalink structure.
262278
\update_option( 'permalink_structure', $permalink_structure );
263279

264-
// Flush rewrite rules to apply the new permalink structure.
265-
\flush_rewrite_rules();
280+
// Set a transient to flush rewrite rules on the next page load.
281+
// This is more performant than flushing immediately during the AJAX request.
282+
\set_transient( 'prpl_flush_rewrite_rules', 1, HOUR_IN_SECONDS );
266283

267284
\wp_send_json_success( [ 'message' => \esc_html__( 'Permalink structure updated.', 'progress-planner' ) ] );
268285
}

0 commit comments

Comments
 (0)