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