Feature
7 months ago
Integration
5 months ago
Interfaces
1 year ago
Util
1 year ago
WordPress
5 months ago
ModuleHandler.php
11 months ago
PluginStateHandler.php
11 months ago
PluginStateHandler.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack; |
| 4 | |
| 5 | use NitroPack\Integration\Plugin\AeliaCurrencySwitcher; |
| 6 | |
| 7 | class PluginStateHandler { |
| 8 | const eventHandlersMap = [ |
| 9 | // 'woocommerce-aelia-currencyswitcher/woocommerce-aelia-currencyswitcher.php' => [ |
| 10 | // 'activateCallback' => 'HandleAeliaCurrencyActivation', |
| 11 | // 'deactivateCallback' => 'HandleAeliaCurrencyDeactivation', |
| 12 | // ], |
| 13 | ]; |
| 14 | private static $instance; |
| 15 | |
| 16 | public static function getInstance() { |
| 17 | if (!self::$instance) { |
| 18 | self::$instance = new PluginStateHandler(); |
| 19 | } |
| 20 | return self::$instance; |
| 21 | } |
| 22 | |
| 23 | public static function init() { |
| 24 | add_action('activated_plugin', [self::getInstance(), 'handleActivation'], 10, 1); |
| 25 | add_action('deactivated_plugin', [self::getInstance(), 'handleDeactivation'], 10, 1); |
| 26 | add_action('update_option_active_plugins', [self::getInstance(), 'onUpdatedActivePluginsList'], 10, 3); |
| 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 | public function onUpdatedActivePluginsList($old_value, $value, $option) { |
| 41 | if ($old_value === $value) return; |
| 42 | |
| 43 | $activated_plugins = array_diff($value, $old_value); |
| 44 | $deactivated_plugins = array_diff($old_value, $value); |
| 45 | |
| 46 | if (in_array('woocommerce/woocommerce.php', $activated_plugins)) { |
| 47 | nitropack_event("platform_change", null, array("platform" => 'WooCommerce')); |
| 48 | } else if (in_array('woocommerce/woocommerce.php', $deactivated_plugins)) { |
| 49 | nitropack_event("platform_change", null, array("platform" => 'WordPress')); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 |