| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SimplyBook\Bootstrap; |
| 6 |
|
| 7 |
use SimplyBook\Managers\FeatureManager; |
| 8 |
use SimplyBook\Managers\EndpointManager; |
| 9 |
use SimplyBook\Managers\ProviderManager; |
| 10 |
use SimplyBook\Managers\ListenerManager; |
| 11 |
use SimplyBook\Support\Helpers\Uninstall; |
| 12 |
use SimplyBook\Managers\AbilitiesManager; |
| 13 |
use SimplyBook\Managers\ControllerManager; |
| 14 |
|
| 15 |
/** |
| 16 |
* @SuppressWarnings("PHPMD.CouplingBetweenObjects") |
| 17 |
* @SuppressWarnings("PHPMD.TooManyPublicMethods") |
| 18 |
* |
| 19 |
* This class is responsible for booting the plugin and as such needs to |
| 20 |
* interact with, and expose entry points to, many other classes. Splitting it |
| 21 |
* up to reduce coupling or the number of public methods would not make it |
| 22 |
* more readable or maintainable. |
| 23 |
*/ |
| 24 |
final class Plugin |
| 25 |
{ |
| 26 |
private FeatureManager $featureManager; |
| 27 |
private ProviderManager $providerManager; |
| 28 |
private EndpointManager $endpointManager; |
| 29 |
private ControllerManager $controllerManager; |
| 30 |
private AbilitiesManager $abilitiesManager; |
| 31 |
private ListenerManager $listenerManager; |
| 32 |
|
| 33 |
/** |
| 34 |
* Plugin constructor |
| 35 |
*/ |
| 36 |
public function __construct() |
| 37 |
{ |
| 38 |
$app = App::getInstance(); |
| 39 |
|
| 40 |
$this->featureManager = $app->make(FeatureManager::class); |
| 41 |
$this->providerManager = $app->make(ProviderManager::class); |
| 42 |
$this->endpointManager = $app->make(EndpointManager::class); |
| 43 |
$this->controllerManager = $app->make(ControllerManager::class); |
| 44 |
$this->abilitiesManager = $app->make(AbilitiesManager::class); |
| 45 |
$this->listenerManager = $app->make(ListenerManager::class); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Boot the plugin |
| 50 |
*/ |
| 51 |
public function boot(): void |
| 52 |
{ |
| 53 |
$pluginBaseFile = (plugin_basename(dirname(__DIR__)) . DIRECTORY_SEPARATOR . plugin_basename(dirname(__DIR__)) . '.php'); |
| 54 |
register_activation_hook($pluginBaseFile, [$this, 'activation']); |
| 55 |
register_deactivation_hook($pluginBaseFile, [$this, 'deactivation']); |
| 56 |
register_uninstall_hook($pluginBaseFile, 'SimplyBook\Bootstrap\Plugin::uninstall'); |
| 57 |
|
| 58 |
add_action('plugins_loaded', [$this, 'loadPluginTextDomain']); |
| 59 |
add_action('plugins_loaded', [$this, 'registerProviders']); // Provide functionality to the plugin |
| 60 |
add_action('simplybook_plugin_providers_loaded', [$this, 'registerFeatures']); // Makes sure features exist when Controllers need them |
| 61 |
add_action('simplybook_plugin_features_loaded', [$this, 'registerControllers']); // Control the functionality of the plugin |
| 62 |
add_action('simplybook_plugin_controllers_loaded', [$this, 'registerListeners']); |
| 63 |
add_action('init', [$this, 'registerAbilities'], 1); // Loaded on init so Features and Controllers can register Abilities and translations can be used |
| 64 |
add_action('rest_api_init', [$this, 'registerEndpoints']); |
| 65 |
add_action('admin_init', [$this, 'fireActivationHook']); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Load the plugin text domain for translations |
| 70 |
*/ |
| 71 |
public function loadPluginTextDomain(): void |
| 72 |
{ |
| 73 |
load_plugin_textdomain('simplybook'); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Method that fires on activation. It creates a flag in the database |
| 78 |
* options table to indicate that the plugin is being activated. Flag is |
| 79 |
* used by {@see fireActivationHook} to run the activation hook only once. |
| 80 |
*/ |
| 81 |
public function activation(): void |
| 82 |
{ |
| 83 |
global $pagenow; |
| 84 |
|
| 85 |
// Remember activation time |
| 86 |
update_option('simplybook_activation_unix_timestamp', time(), false); |
| 87 |
|
| 88 |
// Set the flag on activation |
| 89 |
update_option('simplybook_activation_flag', true, false); |
| 90 |
update_option('simplybook_activation_source_page', sanitize_text_field($pagenow), false); |
| 91 |
|
| 92 |
// Flush rewrite rules to ensure the new routes are available |
| 93 |
add_action('shutdown', 'flush_rewrite_rules'); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Method fires the activation hook. But only if the plugin is being |
| 98 |
* activated. The flag is set in the database options table |
| 99 |
* {@see activation} and is used to determine if the plugin is being |
| 100 |
* activated. This method removes the flag after it has been used. |
| 101 |
*/ |
| 102 |
public function fireActivationHook(): void |
| 103 |
{ |
| 104 |
if (get_option('simplybook_activation_flag', false) === false) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// Get the source page where the activation was triggered from |
| 109 |
$source = get_option('simplybook_activation_source_page', 'unknown'); |
| 110 |
|
| 111 |
// Remove the activation flag so the action doesn't run again. Do it |
| 112 |
// before the action so its deleted before anything can go wrong. |
| 113 |
delete_option('simplybook_activation_flag'); |
| 114 |
delete_option('simplybook_activation_source_page'); |
| 115 |
|
| 116 |
// Gives possibility to hook into the activation process |
| 117 |
do_action('simplybook_activation', $source); // !important |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Method that fires on deactivation |
| 122 |
*/ |
| 123 |
public function deactivation(): void |
| 124 |
{ |
| 125 |
do_action('simplybook_deactivation'); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Method that fires on uninstall |
| 130 |
*/ |
| 131 |
public static function uninstall(): void |
| 132 |
{ |
| 133 |
$uninstallInstance = new Uninstall(); |
| 134 |
$uninstallInstance->handlePluginUninstall(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Register Plugin providers. First step in the booting process of the |
| 139 |
* plugin. Is hooked into plugins_loaded to make sure we only boot the |
| 140 |
* plugin after all other plugins are loaded. This plugin depends on the |
| 141 |
* providerManager to fire the simplybook_providers_loaded action. |
| 142 |
*/ |
| 143 |
public function registerProviders(): void |
| 144 |
{ |
| 145 |
$this->providerManager->register([ |
| 146 |
\SimplyBook\Providers\SimplyBookApiProvider::class, |
| 147 |
]); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Register the plugin features. Hooked into simplybook_providers_loaded to |
| 152 |
* make sure providers are available to the Features. |
| 153 |
*/ |
| 154 |
public function registerFeatures(): void |
| 155 |
{ |
| 156 |
$this->featureManager->register([ |
| 157 |
\SimplyBook\Features\Onboarding\OnboardingFeature::class, |
| 158 |
\SimplyBook\Features\TaskManagement\TaskManagementFeature::class, |
| 159 |
\SimplyBook\Features\Notifications\NotificationsFeature::class, |
| 160 |
]); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Register Controllers. Hooked into simplybook_features_loaded to make sure |
| 165 |
* features are available to the Controllers. |
| 166 |
*/ |
| 167 |
public function registerControllers(): void |
| 168 |
{ |
| 169 |
$this->controllerManager->register([ |
| 170 |
\SimplyBook\Controllers\DashboardController::class, |
| 171 |
\SimplyBook\Controllers\AdminController::class, |
| 172 |
\SimplyBook\Controllers\SettingsController::class, |
| 173 |
\SimplyBook\Controllers\CapabilityController::class, |
| 174 |
\SimplyBook\Controllers\ScheduleController::class, |
| 175 |
\SimplyBook\Controllers\WidgetController::class, |
| 176 |
\SimplyBook\Controllers\BlockController::class, |
| 177 |
\SimplyBook\Controllers\DesignSettingsController::class, |
| 178 |
\SimplyBook\Controllers\ServicesController::class, |
| 179 |
\SimplyBook\Controllers\ServiceProvidersController::class, |
| 180 |
\SimplyBook\Controllers\ReviewController::class, |
| 181 |
\SimplyBook\Controllers\TrialExpirationController::class, |
| 182 |
\SimplyBook\Controllers\WidgetTrackingController::class, |
| 183 |
\SimplyBook\Controllers\OnboardingNoticeController::class, |
| 184 |
\SimplyBook\Controllers\BookingPageController::class, |
| 185 |
\SimplyBook\Controllers\CompanyInfoController::class, |
| 186 |
\SimplyBook\Controllers\UpgradeController::class, |
| 187 |
]); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Register the plugins REST API endpoint instances. Hooked into |
| 192 |
* rest_api_init to make sure the REST API is available. |
| 193 |
*/ |
| 194 |
public function registerEndpoints(): void |
| 195 |
{ |
| 196 |
$this->endpointManager->register([ |
| 197 |
\SimplyBook\Http\Endpoints\LoginUrlEndpoint::class, |
| 198 |
\SimplyBook\Http\Endpoints\ServicesEndpoint::class, |
| 199 |
\SimplyBook\Http\Endpoints\ServicesProvidersEndpoint::class, |
| 200 |
\SimplyBook\Http\Endpoints\SettingEndpoint::class, |
| 201 |
\SimplyBook\Http\Endpoints\WidgetEndpoint::class, |
| 202 |
\SimplyBook\Http\Endpoints\DomainEndpoint::class, |
| 203 |
\SimplyBook\Http\Endpoints\RemotePluginsEndpoint::class, |
| 204 |
\SimplyBook\Http\Endpoints\WaitForRegistrationEndpoint::class, |
| 205 |
\SimplyBook\Http\Endpoints\RelatedPluginEndpoint::class, |
| 206 |
\SimplyBook\Http\Endpoints\BlockEndpoint::class, |
| 207 |
\SimplyBook\Http\Endpoints\LogOutEndpoint::class, |
| 208 |
\SimplyBook\Http\Endpoints\TipsTricksEndpoint::class, |
| 209 |
\SimplyBook\Http\Endpoints\StatisticsEndpoint::class, |
| 210 |
\SimplyBook\Http\Endpoints\SubscriptionEndpoint::class, |
| 211 |
\SimplyBook\Http\Endpoints\SubscriptionWidgetEndpoint::class, |
| 212 |
\SimplyBook\Http\Endpoints\PublicThemeListEndpoint::class, |
| 213 |
\SimplyBook\Http\Endpoints\ThemeColorEndpoint::class, |
| 214 |
\SimplyBook\Http\Endpoints\NoticesDismissEndpoint::class, |
| 215 |
]); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Register the plugins listeners. The base plugin does not have any |
| 220 |
* listeners yet. Currently used to hook listeners from Features into |
| 221 |
* the registering process. |
| 222 |
*/ |
| 223 |
public function registerListeners(): void |
| 224 |
{ |
| 225 |
$this->listenerManager->register([]); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Register the plugins Abilities with the WP Abilities API. Hooked into |
| 230 |
* init so Features and Controllers can register their own Abilities via |
| 231 |
* the simplybook_plugin_abilities filter and translations can be used. |
| 232 |
*/ |
| 233 |
public function registerAbilities(): void |
| 234 |
{ |
| 235 |
$this->abilitiesManager->register([ |
| 236 |
\SimplyBook\Abilities\Design\ListDesignSettingsAbility::class, |
| 237 |
\SimplyBook\Abilities\Design\UpdateDesignSettingsAbility::class, |
| 238 |
\SimplyBook\Abilities\Providers\ListProvidersAbility::class, |
| 239 |
\SimplyBook\Abilities\Providers\UpdateProviderAbility::class, |
| 240 |
\SimplyBook\Abilities\Schedule\GetAvailableSlotsAbility::class, |
| 241 |
\SimplyBook\Abilities\Schedule\GetScheduleAbility::class, |
| 242 |
\SimplyBook\Abilities\Services\ListServicesAbility::class, |
| 243 |
\SimplyBook\Abilities\Services\UpdateServiceAbility::class, |
| 244 |
\SimplyBook\Abilities\Statistics\CountRecentBookingsAbility::class, |
| 245 |
\SimplyBook\Abilities\Statistics\GetStatisticsAbility::class, |
| 246 |
\SimplyBook\Abilities\Tasks\ListTasksAbility::class, |
| 247 |
\SimplyBook\Abilities\Tasks\UpdateTaskStatusAbility::class, |
| 248 |
]); |
| 249 |
} |
| 250 |
} |
| 251 |
|