PluginProbe
Metricool – Social media and site statistics / 2.0.2
Metricool – Social media and site statistics v2.0.2
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / bootstrap / Plugin.php

Plugin.php in Metricool – Social media and site statistics 2.0.2, at bootstrap/Plugin.php

173 lines 6.0 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\Bootstrap;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 use Metricool\Http;
12 use Metricool\Providers;
13 use Metricool\Controllers;
14 use Metricool\Managers\FeatureManager;
15 use Metricool\Managers\ProviderManager;
16 use Metricool\Managers\EndpointManager;
17 use Metricool\Managers\ControllerManager;
18
19 class Plugin
20 {
21 private FeatureManager $featureManager;
22 private ProviderManager $providerManager;
23 private EndpointManager $endpointManager;
24 private ControllerManager $controllerManager;
25
26 /**
27 * Plugin constructor
28 */
29 public function __construct()
30 {
31 $app = App::getInstance();
32
33 $this->featureManager = $app->make(FeatureManager::class);
34 $this->providerManager = $app->make(ProviderManager::class);
35 $this->endpointManager = $app->make(EndpointManager::class);
36 $this->controllerManager = $app->make(ControllerManager::class);
37 }
38
39 /**
40 * Boot the plugin
41 */
42 public function boot(): void
43 {
44 $pluginBaseFile = (plugin_basename(dirname(__DIR__)) . DIRECTORY_SEPARATOR . plugin_basename(dirname(__DIR__)) . '.php');
45 register_activation_hook($pluginBaseFile, [$this, 'activation']);
46 register_deactivation_hook($pluginBaseFile, [$this, 'deactivation']);
47 register_uninstall_hook($pluginBaseFile, 'Metricool\Bootstrap\Plugin::uninstall');
48
49 add_action('plugins_loaded', [$this, 'loadPluginTextDomain']);
50 add_action('plugins_loaded', [$this, 'registerProviders']); // Provide functionality to the plugin
51 add_action('metricool_providers_loaded', [$this->featureManager, 'registerFeatures']); // Makes sure features exist when Controllers need them
52 add_action('metricool_features_loaded', [$this, 'registerControllers']); // Control the functionality of the plugin
53 add_action('rest_api_init', [$this, 'registerEndpoints']);
54 add_action('admin_init', [$this, 'fireActivationHook']);
55 }
56
57 /**
58 * Load the plugin text domain for translations
59 */
60 public function loadPluginTextDomain(): void
61 {
62 load_plugin_textdomain('metricool');
63 }
64
65 /**
66 * Method that fires on activation. It creates a flag in the database
67 * options table to indicate that the plugin is being activated. Flag is
68 * used by {@see fireActivationHook} to run the activation hook only once.
69 */
70 public function activation(): void
71 {
72 global $pagenow;
73
74 // Set the flag on activation
75 update_option('metricool_activation_flag', true, false);
76 update_option('metricool_activation_source_page', sanitize_text_field($pagenow), false);
77
78 // Flush rewrite rules to ensure the new routes are available
79 add_action('shutdown', 'flush_rewrite_rules');
80 }
81
82 /**
83 * Method fires the activation hook. But only if the plugin is being
84 * activated. The flag is set in the database options table
85 * {@see activation} and is used to determine if the plugin is being
86 * activated. This method removes the flag after it has been used.
87 */
88 public function fireActivationHook(): void
89 {
90 if (get_option('metricool_activation_flag', false) === false) {
91 return;
92 }
93
94 // Get the source page where the activation was triggered from
95 $source = get_option('metricool_activation_source_page', 'unknown');
96
97 // Remove the activation flag so the action doesn't run again. Do it
98 // before the action so its deleted before anything can go wrong.
99 delete_option('metricool_activation_flag');
100 delete_option('metricool_activation_source_page');
101
102 // Gives possibility to hook into the activation process
103 do_action('metricool_activation', $source); // !important
104 }
105
106 /**
107 * Method that fires on deactivation
108 */
109 public function deactivation(): void
110 {
111 // Silence is golden
112 }
113
114 /**
115 * Method that fires on uninstall
116 */
117 public static function uninstall(): void
118 {
119 $uninstallInstance = App::getInstance()->make('\Metricool\Support\Helpers\Uninstall');
120 $uninstallInstance->handlePluginUninstall();
121 }
122
123 /**
124 * Register Plugin providers. First step in the booting process of the
125 * plugin. Is hooked into plugins_loaded to make sure we only boot the
126 * plugin after all other plugins are loaded. This plugin depends on the
127 * providerManager to fire the metricool_providers_loaded action.
128 * @uses do_action metricool_providers_loaded
129 */
130 public function registerProviders(): void
131 {
132 $this->providerManager->register([
133 Providers\MetricoolApiProvider::class,
134 ]);
135 }
136
137 /**
138 * Register Controllers. Hooked into metricool_features_loaded to make sure
139 * features are available to the Controllers.
140 * @uses do_action metricool_controllers_loaded
141 */
142 public function registerControllers(): void
143 {
144 $this->controllerManager->register([
145 Controllers\CapabilityController::class,
146 Controllers\MigrationsController::class,
147 Controllers\UpgradeController::class,
148 Controllers\AdminController::class,
149 Controllers\DashboardController::class,
150 Controllers\TrackingScriptController::class,
151 Controllers\SharePostController::class,
152 ]);
153 }
154
155 /**
156 * Register the plugins REST API endpoint instances. Hooked into
157 * rest_api_init to make sure the REST API is available.
158 * @uses do_action metricool_endpoints_loaded
159 */
160 public function registerEndpoints(): void
161 {
162 $this->endpointManager->register([
163 Http\Endpoints\ConnectedBrandsEndpoint::class,
164 Http\Endpoints\ConnectedNetworksEndpoint::class,
165 Http\Endpoints\DistributionEndpoint::class,
166 Http\Endpoints\AnalyticsEndpoint::class,
167 Http\Endpoints\RealtimeEndpoint::class,
168 Http\Endpoints\OtherPluginsEndpoints::class,
169 Http\Endpoints\LogoutEndpoint::class,
170 ]);
171 }
172 }
173