Skip to content
This repository was archived by the owner on May 6, 2023. It is now read-only.

Commit 140f10c

Browse files
committed
Store plugin version to detect update
(Hook "upgrader_process_complete" is unreliable) Added link to disable admin notice in settings
1 parent 14dd9e4 commit 140f10c

File tree

1 file changed

+24
-64
lines changed

1 file changed

+24
-64
lines changed

src/Plugin.php

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace GraphQLAPI\GraphQLAPI;
66

7-
use WP_Upgrader;
87
use PoP\Engine\ComponentLoader;
98
use GraphQLAPI\GraphQLAPI\PluginConfiguration;
109
use GraphQLAPI\GraphQLAPI\Blocks\AbstractBlock;
@@ -49,9 +48,10 @@ class Plugin
4948
public const NAMESPACE = __NAMESPACE__;
5049

5150
/**
52-
* Transient to indicate if plugin was just updated
51+
* Store the plugin version in the Options table, to track when
52+
* the plugin is installed/updated
5353
*/
54-
public const TRANSIENT_PLUGIN_UPDATED = 'graphql-api-plugin-updated';
54+
public const OPTION_PLUGIN_VERSION = 'graphql-api-plugin-version';
5555

5656
/**
5757
* Plugin set-up, executed immediately when loading the plugin.
@@ -90,26 +90,26 @@ public function setup(): void
9090
*
9191
* @see https://developer.wordpress.org/reference/functions/register_activation_hook/#process-flow
9292
*/
93-
$isPluginJustActivatedOptionName = 'graphql-api-activated-plugin';
9493
\register_activation_hook(
9594
\GRAPHQL_API_PLUGIN_FILE,
96-
function () use ($isPluginJustActivatedOptionName): void {
97-
// Flag to indicate that the plugin has been activated
98-
\add_option($isPluginJustActivatedOptionName, true);
95+
function (): void {
96+
// By removing the option (in case it already exists from a previously-installed version),
97+
// the next request will know the plugin was just installed
98+
\update_option(self::OPTION_PLUGIN_VERSION, false);
9999
// This is the proper activation logic
100100
$this->activate();
101101
}
102102
);
103103
\add_action(
104104
'admin_init',
105-
function () use ($isPluginJustActivatedOptionName): void {
106-
// If the flag is there, it's the first screen after activating the plugin
107-
$isPluginJustActivated = \get_option($isPluginJustActivatedOptionName) == true;
105+
function (): void {
106+
// If there is no version stored, it's the first screen after activating the plugin
107+
$isPluginJustActivated = \get_option(self::OPTION_PLUGIN_VERSION) === false;
108108
if (!$isPluginJustActivated) {
109109
return;
110110
}
111-
// Delete the flag
112-
\delete_option($isPluginJustActivatedOptionName);
111+
// Update to the current version
112+
\update_option(self::OPTION_PLUGIN_VERSION, \GRAPHQL_API_VERSION);
113113
// Required logic after plugin is activated
114114
\flush_rewrite_rules();
115115
}
@@ -119,46 +119,38 @@ function () use ($isPluginJustActivatedOptionName): void {
119119
*/
120120
\add_action(
121121
'admin_init',
122-
function () use ($isPluginJustActivatedOptionName): void {
123-
// On updates only: Show a link to view the new version's release notes
122+
function (): void {
123+
// Do not execute when doing Ajax, since we can't show the one-time
124+
// admin notice to the user then
124125
if (\wp_doing_ajax()) {
125126
return;
126127
}
127-
// Check if there is a transient indicating that the plugin was updated
128-
$pluginUpdatedTransient = \get_transient(self::TRANSIENT_PLUGIN_UPDATED);
129-
if (!$pluginUpdatedTransient) {
128+
// Check if the plugin has been updated: if the stored version in the DB
129+
// and the current plugin's version are different
130+
$storedVersion = \get_option(self::OPTION_PLUGIN_VERSION, \GRAPHQL_API_VERSION);
131+
if ($storedVersion == \GRAPHQL_API_VERSION) {
130132
return;
131133
}
132-
delete_transient(self::TRANSIENT_PLUGIN_UPDATED);
133-
// Only if not disabled by the user
134+
// Update to the current version
135+
\update_option(self::OPTION_PLUGIN_VERSION, \GRAPHQL_API_VERSION);
136+
// Admin notice: Check if it is enabled
134137
$userSettingsManager = UserSettingsManagerFacade::getInstance();
135138
if (!$userSettingsManager->getSetting(
136139
PluginManagementFunctionalityModuleResolver::GENERAL,
137140
PluginManagementFunctionalityModuleResolver::OPTION_ADD_RELEASE_NOTES_ADMIN_NOTICE
138141
)) {
139142
return;
140143
}
141-
// If updating either MAJOR or MINOR versions, show admin notice. No need for PATCH versions
144+
// Show admin notice only when updating MAJOR or MINOR versions. No need for PATCH versions
142145
$currentMinorReleaseVersion = $this->getMinorReleaseVersion(\GRAPHQL_API_VERSION);
143-
$previousMinorReleaseVersion = $this->getMinorReleaseVersion($pluginUpdatedTransient);
146+
$previousMinorReleaseVersion = $this->getMinorReleaseVersion($storedVersion);
144147
if ($currentMinorReleaseVersion == $previousMinorReleaseVersion) {
145148
return;
146149
}
147150
// All checks passed, show the release notes
148151
$this->showReleaseNotesInAdminNotice();
149152
}
150153
);
151-
/**
152-
* On updates only: Show a link to view the new version's release notes
153-
*
154-
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/upgrader_process_complete
155-
*/
156-
add_action(
157-
'upgrader_process_complete',
158-
[$this, 'checkIsPluginUpgraded'],
159-
10,
160-
2
161-
);
162154

163155
/**
164156
* Wait until "plugins_loaded" to initialize the plugin, because:
@@ -170,38 +162,6 @@ function () use ($isPluginJustActivatedOptionName): void {
170162
\add_action('plugins_loaded', [$this, 'boot'], 15);
171163
}
172164

173-
/**
174-
* This function runs when WordPress completes its upgrade process
175-
* If this plugin is updated, it sets a transient flag
176-
*
177-
* @param array<string, mixed> $options
178-
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/upgrader_process_complete
179-
*/
180-
public function checkIsPluginUpgraded(WP_Upgrader $upgrader_object, array $options): void
181-
{
182-
// If an update has taken place and the updated type is plugins and the plugins element exists,
183-
// or an install of a new version of the plugin
184-
$destinationName = null;
185-
if (is_array($upgrader_object->result)) {
186-
$destinationName = $upgrader_object->result['destination_name'];
187-
}
188-
if ($options['type'] == 'plugin' && (
189-
(
190-
$options['action'] == 'update'
191-
&& isset($options['plugins'])
192-
&& in_array(\GRAPHQL_API_BASE_NAME, $options['plugins'])
193-
) || (
194-
$options['action'] == 'install'
195-
&& $destinationName == \GRAPHQL_API_PLUGIN_NAME
196-
)
197-
)) {
198-
// Set a transient to record that the plugin has just been updated
199-
// The value is the plugin's current version when the upgrade happens,
200-
// i.e. the version to be replaced
201-
\set_transient(self::TRANSIENT_PLUGIN_UPDATED, \GRAPHQL_API_VERSION);
202-
}
203-
}
204-
205165
/**
206166
* Add a notice with a link to the latest release note,
207167
* to open in a modal window

0 commit comments

Comments
 (0)