PluginProbe
Code Snippets / trunk
Code Snippets vtrunk
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
← All changes | php/Core/Upgrader.php +34 -0 3.10.1 → trunk View file →
@@ -5,8 +5,9 @@
5 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();
@@ -51,8 +58,35 @@
51 58 $this->do_multisite_upgrades();
52 59 }
53 60
54 61 $this->do_site_upgrades();
62 + }
63 +
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 );
55 89 }
56 90
57 91 /**
58 92 * Perform upgrades for the current site