Ajax.php
3 months ago
Assets.php
1 month ago
Loader.php
1 year ago
Rest.php
1 year ago
SurveyLoader.php
1 year ago
SurveyManager.php
1 month ago
Loader.php
127 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Surveys; |
| 4 | |
| 5 | use Hostinger\WpHelper\Config; |
| 6 | use Hostinger\WpHelper\Constants; |
| 7 | use Hostinger\WpHelper\Requests\Client; |
| 8 | use Hostinger\WpHelper\Utils; |
| 9 | use Hostinger\Surveys\Ajax as SurveysAjax; |
| 10 | |
| 11 | class Loader |
| 12 | { |
| 13 | /** |
| 14 | * @var Loader instance. |
| 15 | */ |
| 16 | private static ?Loader $instance = null; |
| 17 | |
| 18 | /** |
| 19 | * @var array |
| 20 | */ |
| 21 | private array $objects = []; |
| 22 | |
| 23 | /** |
| 24 | * Allow only one instance of class |
| 25 | * |
| 26 | * @return self |
| 27 | */ |
| 28 | public static function getInstance(): self |
| 29 | { |
| 30 | if (null === self::$instance) { |
| 31 | self::$instance = new self(); |
| 32 | } |
| 33 | |
| 34 | return self::$instance; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return void |
| 39 | */ |
| 40 | public function boot(): bool |
| 41 | { |
| 42 | $this->registerModules(); |
| 43 | $this->loadTextDomain(); |
| 44 | |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @return void |
| 50 | */ |
| 51 | public function registerModules(): void |
| 52 | { |
| 53 | // Surveys. |
| 54 | $helper = new Utils(); |
| 55 | $configHandler = new Config(); |
| 56 | $surveyRest = new Rest( |
| 57 | new Client( |
| 58 | $configHandler->getConfigValue('base_rest_uri', Constants::HOSTINGER_REST_URI), |
| 59 | [ |
| 60 | Config::TOKEN_HEADER => $helper::getApiToken(), |
| 61 | Config::DOMAIN_HEADER => $helper->getHostInfo(), |
| 62 | ] |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | $this->objects['ajax'] = new SurveysAjax($helper, $configHandler, $surveyRest); |
| 67 | |
| 68 | // Assets. |
| 69 | $this->objects['assets'] = new Assets(); |
| 70 | $this->objects['assets']->init(); |
| 71 | |
| 72 | $this->objects['survey_loader'] = new SurveyLoader(); |
| 73 | $this->objects['survey_loader']->run(); |
| 74 | |
| 75 | $this->addContainer(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return bool |
| 80 | */ |
| 81 | public function addContainer(): bool |
| 82 | { |
| 83 | if (empty($this->objects)) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | foreach ($this->objects as $object) { |
| 88 | if (property_exists($object, 'surveys')) { |
| 89 | $object->surveys = $this; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return string |
| 98 | */ |
| 99 | public function getPluginInfo(): string |
| 100 | { |
| 101 | $plugin_url = ''; |
| 102 | |
| 103 | $plugins = get_plugins(); |
| 104 | foreach ($plugins as $plugin_path => $plugin_info) { |
| 105 | if (str_contains(__FILE__, 'plugins/' . dirname($plugin_path) . '/')) { |
| 106 | $plugin_dir = dirname($plugin_path); |
| 107 | |
| 108 | return plugins_url($plugin_dir); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $plugin_url; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @return void |
| 117 | */ |
| 118 | public function loadTextDomain(): void |
| 119 | { |
| 120 | load_plugin_textdomain( |
| 121 | 'hostinger-wp-surveys', |
| 122 | false, |
| 123 | dirname(dirname(plugin_basename(__FILE__))) . '/languages/' |
| 124 | ); |
| 125 | } |
| 126 | } |
| 127 |