PluginProbe
Metricool – Social media and site statistics / trunk
Metricool – Social media and site statistics vtrunk
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Controllers / UpgradeController.php

UpgradeController.php in Metricool – Social media and site statistics trunk, at app/Controllers/UpgradeController.php

71 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Metricool\Controllers;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 use Metricool\Interfaces\ControllerInterface;
12 use Metricool\Support\Helpers\Storages\EnvironmentConfig;
13
14 class UpgradeController implements ControllerInterface
15 {
16 public const LEGACY_VERSION = '1.27';
17
18 private EnvironmentConfig $env;
19
20 public function __construct(EnvironmentConfig $env)
21 {
22 $this->env = $env;
23 }
24
25 public function register(): void
26 {
27 add_action('metricool_controllers_loaded', [$this, 'checkForUpgrades']);
28 }
29
30 /**
31 * Fire an action when the plugin is upgraded from one version to another.
32 *
33 * @internal Note the starting underscore in the option name. This is to
34 * prevent the option from being deleted when a user logs out. As if
35 * it is a private Metricool option.
36 *
37 * @hooked metricool_controllers_loaded to make sure Controllers can hook
38 * into metricool_plugin_version_upgrade. Even this one.
39 *
40 * @uses do_action metricool_plugin_version_upgrade Only hook into this
41 * action if the upgrade script is related to the responsibility of the
42 * hooking class.
43 */
44 public function checkForUpgrades(): void
45 {
46 $previousSavedVersion = (string) get_option('_metricool_current_version', '');
47 $currentVersion = $this->env->getString('plugin.version');
48
49 if ($previousSavedVersion === $currentVersion) {
50 return; // Nothing to do
51 }
52
53 // Legacy upgrade
54 if (empty($previousSavedVersion) && $this->isLegacyUpgrade()) {
55 $previousSavedVersion = self::LEGACY_VERSION;
56 do_action('metricool_plugin_legacy_upgrade', $currentVersion);
57 }
58
59 do_action('metricool_plugin_version_upgrade', $previousSavedVersion, $currentVersion);
60 update_option('_metricool_current_version', $currentVersion, false);
61 }
62
63 /**
64 * Determine if this is an upgrade from the legacy plugin by checking for the existence of the old metricool_id option.
65 */
66 private function isLegacyUpgrade(): bool
67 {
68 return get_option('metricool_profile_id', false) !== false;
69 }
70 }
71