Integration
3 years ago
WordPress
3 years ago
Integration.php
3 years ago
PluginStateHandler.php
3 years ago
PluginStateHandler.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack; |
| 4 | |
| 5 | use NitroPack\Integration\Plugin\AeliaCurrencySwitcher; |
| 6 | use NitroPack\Integration\Plugin\GeoTargetingWP; |
| 7 | |
| 8 | class PluginStateHandler { |
| 9 | const eventHandlersMap = [ |
| 10 | 'woocommerce-aelia-currencyswitcher/woocommerce-aelia-currencyswitcher.php' =>[ |
| 11 | 'activateCallback' => 'HandleAeliaCurrencyActivation', |
| 12 | 'deactivateCallback' => 'HandleAeliaCurrencyDeactivation', |
| 13 | ], |
| 14 | ]; |
| 15 | private static $instance; |
| 16 | |
| 17 | public static function getInstance() { |
| 18 | if (!self::$instance) { |
| 19 | self::$instance = new PluginStateHandler(); |
| 20 | } |
| 21 | return self::$instance; |
| 22 | } |
| 23 | |
| 24 | public static function init() { |
| 25 | add_action('activated_plugin', [self::getInstance(), 'handleActivation'], 10, 1); |
| 26 | add_action('deactivated_plugin', [self::getInstance(), 'handleDeactivation'], 10, 1); |
| 27 | } |
| 28 | |
| 29 | public function handleActivation($plugin) { |
| 30 | if (array_key_exists($plugin, self::eventHandlersMap) && !empty(self::eventHandlersMap[$plugin]['activateCallback'])) { |
| 31 | self::{self::eventHandlersMap[$plugin]['activateCallback']}(); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function handleDeactivation($plugin) { |
| 36 | if (array_key_exists($plugin, self::eventHandlersMap) && !empty(self::eventHandlersMap[$plugin]['deactivateCallback'])) { |
| 37 | self::{self::eventHandlersMap[$plugin]['deactivateCallback']}(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // maybe have these handlers be part of each plugin compatibility class (maybe even have a class PluginCompatibility that they extend). |
| 42 | public static function HandleAeliaCurrencyActivation() { |
| 43 | initVariationCookies(AeliaCurrencySwitcher::customVariationCookies); |
| 44 | } |
| 45 | |
| 46 | public static function HandleAeliaCurrencyDeactivation() { |
| 47 | removeVariationCookies(AeliaCurrencySwitcher::customVariationCookies); |
| 48 | } |
| 49 | |
| 50 | public static function HandleGeowpActivation() { |
| 51 | initVariationCookies(GeoTargetingWP::getCustomVariationCookies()); |
| 52 | } |
| 53 | |
| 54 | public static function HandleGeowpDeactivation() { |
| 55 | removeVariationCookies(GeoTargetingWP::allGeoWpCookies); |
| 56 | } |
| 57 | } |
| 58 |