| @@ -1,12 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Code_Snippets\Core; |
| 4 | 4 | |
| 5 | -use Code_Snippets\Client\Welcome_API; | |
| 5 | +use Code_Snippets\Client\Welcome_Client; | |
| 6 | 6 | use Code_Snippets\Model\Snippet; |
| 7 | 7 | use WP_User; |
| 8 | 8 | use function Code_Snippets\clean_snippets_cache; |
| 9 | +use function Code_Snippets\flush_versioned_cache_groups; | |
| 9 | 10 | use function Code_Snippets\code_snippets; |
| 10 | 11 | use function Code_Snippets\save_snippet; |
| 11 | 12 | |
| 12 | 13 | /** |
| @@ -14,8 +15,13 @@ | ||
| 14 | 15 | */ |
| 15 | 16 | class Upgrader { |
| 16 | 17 | |
| 17 | 18 | /** |
| 19 | + * Option recording the plugin version that last wrote to the cache. | |
| 20 | + */ | |
| 21 | + private const CACHE_VERSION_OPTION = 'code_snippets_cache_version'; | |
| 22 | + | |
| 23 | + /** | |
| 18 | 24 | * Instance of database class |
| 19 | 25 | * |
| 20 | 26 | * @var DB |
| 21 | 27 | */ |
| @@ -44,8 +50,9 @@ | ||
| 44 | 50 | /** |
| 45 | 51 | * Run the upgrade functions |
| 46 | 52 | */ |
| 47 | 53 | public function run() { |
| 54 | + $this->handle_version_change(); | |
| 48 | 55 | |
| 49 | 56 | // Always run multisite upgrades, even if not on the main site, as sub-sites depend on the network snippet table. |
| 50 | 57 | if ( is_multisite() ) { |
| 51 | 58 | $this->do_multisite_upgrades(); |
| @@ -54,8 +61,35 @@ | ||
| 54 | 61 | $this->do_site_upgrades(); |
| 55 | 62 | } |
| 56 | 63 | |
| 57 | 64 | /** |
| 65 | + * Discard cached data belonging to a different version of the plugin. | |
| 66 | + * | |
| 67 | + * The do_site_upgrades() method only acts when the version has gone up, so | |
| 68 | + * it never fires on a rollback. Cached data has to be dealt with in both | |
| 69 | + * directions, so it is tracked separately here. | |
| 70 | + * | |
| 71 | + * A dedicated option is used rather than the one that method maintains, | |
| 72 | + * because that option is left untouched on a downgrade, which would leave | |
| 73 | + * this flushing on every request for as long as the older version ran. | |
| 74 | + * | |
| 75 | + * @return void | |
| 76 | + */ | |
| 77 | + private function handle_version_change(): void { | |
| 78 | + $previous_version = (string) get_option( self::CACHE_VERSION_OPTION, '' ); | |
| 79 | + | |
| 80 | + if ( $previous_version === $this->current_version ) { | |
| 81 | + return; | |
| 82 | + } | |
| 83 | + | |
| 84 | + // Recorded before flushing so that a cache backend which cannot flush | |
| 85 | + // groups does not leave this repeating on every request. | |
| 86 | + update_option( self::CACHE_VERSION_OPTION, $this->current_version, false ); | |
| 87 | + | |
| 88 | + flush_versioned_cache_groups( $previous_version ); | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 58 | 92 | * Perform upgrades for the current site |
| 59 | 93 | */ |
| 60 | 94 | private function do_site_upgrades() { |
| 61 | 95 | $table_name = $this->db->table; |
| @@ -104,9 +138,9 @@ | ||
| 104 | 138 | add_action( 'init', [ $this, 'create_sample_content' ] ); |
| 105 | 139 | } |
| 106 | 140 | |
| 107 | 141 | clean_snippets_cache( $table_name ); |
| 108 | - Welcome_API::clear_cache(); | |
| 142 | + Welcome_Client::clear_cache(); | |
| 109 | 143 | } |
| 110 | 144 | |
| 111 | 145 | /** |
| 112 | 146 | * Create example snippets. |