ActionDispatcher.php
1 month ago
AmplitudeLoader.php
1 month ago
AmplitudeManager.php
1 month ago
Rest.php
1 month ago
AmplitudeLoader.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Amplitude; |
| 4 | |
| 5 | use Hostinger\WpHelper\Config; |
| 6 | use Hostinger\WpHelper\Constants; |
| 7 | use Hostinger\WpHelper\Requests\Client; |
| 8 | use Hostinger\WpHelper\Utils as Helper; |
| 9 | |
| 10 | class AmplitudeLoader |
| 11 | { |
| 12 | /** |
| 13 | * @var AmplitudeLoader instance. |
| 14 | */ |
| 15 | private static ?AmplitudeLoader $instance = null; |
| 16 | |
| 17 | /** |
| 18 | * @var array |
| 19 | */ |
| 20 | private array $objects = []; |
| 21 | |
| 22 | /** |
| 23 | * Allow only one instance of class |
| 24 | * |
| 25 | * @return self |
| 26 | */ |
| 27 | public static function getInstance(): self |
| 28 | { |
| 29 | if (null === self::$instance) { |
| 30 | self::$instance = new self(); |
| 31 | } |
| 32 | |
| 33 | return self::$instance; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @return void |
| 38 | */ |
| 39 | public function boot(): bool |
| 40 | { |
| 41 | $this->registerModules(); |
| 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @return void |
| 48 | */ |
| 49 | public function registerModules(): void |
| 50 | { |
| 51 | // Action Dispatcher. |
| 52 | $helper = new Helper(); |
| 53 | $config = new Config(); |
| 54 | $client = new Client( |
| 55 | $config->getConfigValue('base_rest_uri', Constants::HOSTINGER_REST_URI), |
| 56 | [ |
| 57 | Config::TOKEN_HEADER => $helper->getApiToken(), |
| 58 | Config::DOMAIN_HEADER => $helper->getHostInfo(), |
| 59 | ] |
| 60 | ); |
| 61 | |
| 62 | $this->objects['action_dispatcher'] = new ActionDispatcher($helper, $config, $client); |
| 63 | |
| 64 | // Amplitude Manager. |
| 65 | $this->objects['amplitude_rest'] = new Rest(); |
| 66 | $this->objects['amplitude_rest']->init(); |
| 67 | |
| 68 | $this->addContainer(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return bool |
| 73 | */ |
| 74 | public function addContainer(): bool |
| 75 | { |
| 76 | if (empty($this->objects)) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | foreach ($this->objects as $object) { |
| 81 | if (property_exists($object, 'amplitude')) { |
| 82 | $object->setAmplitude($this); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return string |
| 91 | */ |
| 92 | public function getPluginInfo(): string |
| 93 | { |
| 94 | $plugin_url = ''; |
| 95 | |
| 96 | $plugins = get_plugins(); |
| 97 | foreach ($plugins as $plugin_path => $plugin_info) { |
| 98 | if (str_contains(__FILE__, 'plugins/' . dirname($plugin_path) . '/')) { |
| 99 | $plugin_dir = dirname($plugin_path); |
| 100 | |
| 101 | return plugins_url($plugin_dir); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return $plugin_url; |
| 106 | } |
| 107 | } |
| 108 |