| @@ -4,8 +4,11 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package Timetics |
| 6 | 6 | */ |
| 7 | 7 | namespace Timetics\Base; |
| 8 | + | |
| 9 | +defined( 'ABSPATH' ) || exit; | |
| 10 | + | |
| 8 | 11 | use Timetics\Utils\Singleton; |
| 9 | 12 | |
| 10 | 13 | class Custom_Endpoint { |
| 11 | 14 | use Singleton; |
| @@ -16,8 +19,9 @@ | ||
| 16 | 19 | * @return void |
| 17 | 20 | */ |
| 18 | 21 | public function init() { |
| 19 | 22 | add_action( 'init', [$this, 'register'] ); |
| 23 | + add_action( 'init', [$this, 'maybe_flush_rules'], 99 ); | |
| 20 | 24 | } |
| 21 | 25 | |
| 22 | 26 | /** |
| 23 | 27 | * Register all custom endpoints |
| @@ -29,11 +33,22 @@ | ||
| 29 | 33 | |
| 30 | 34 | foreach ( $endpoints as $endpoint ) { |
| 31 | 35 | add_rewrite_endpoint( $endpoint, EP_ALL ); |
| 32 | 36 | } |
| 37 | + } | |
| 33 | 38 | |
| 34 | - // Flush rewrite rules after register all custom endpoints. | |
| 35 | - flush_rewrite_rules( true ); | |
| 39 | + /** | |
| 40 | + * Flush rewrite rules once per plugin version (after upgrade or fresh install). | |
| 41 | + * | |
| 42 | + * @return void | |
| 43 | + */ | |
| 44 | + public function maybe_flush_rules() { | |
| 45 | + $stored = get_option( 'timetics_rewrite_version' ); | |
| 46 | + | |
| 47 | + if ( defined( 'TIMETICS_VERSION' ) && $stored !== TIMETICS_VERSION ) { | |
| 48 | + flush_rewrite_rules( false ); | |
| 49 | + update_option( 'timetics_rewrite_version', TIMETICS_VERSION, false ); | |
| 50 | + } | |
| 36 | 51 | } |
| 37 | 52 | |
| 38 | 53 | /** |
| 39 | 54 | * Get all endpoints |