PopulatorData
9 years ago
Activator.php
9 years ago
Analytics.php
9 years ago
Changelog.php
9 years ago
Database.php
9 years ago
Env.php
9 years ago
Hooks.php
9 years ago
Initializer.php
9 years ago
Localizer.php
9 years ago
Menu.php
9 years ago
Migrator.php
9 years ago
Populator.php
9 years ago
Renderer.php
9 years ago
RequirementsChecker.php
9 years ago
Shortcodes.php
9 years ago
Widget.php
9 years ago
index.php
9 years ago
Initializer.php
204 lines
| 1 | <?php |
| 2 | namespace MailPoet\Config; |
| 3 | |
| 4 | use MailPoet\API; |
| 5 | use MailPoet\Cron\CronTrigger; |
| 6 | use MailPoet\Router; |
| 7 | use MailPoet\Util\ConflictResolver; |
| 8 | use MailPoet\WP\Notice as WPNotice; |
| 9 | |
| 10 | if(!defined('ABSPATH')) exit; |
| 11 | |
| 12 | require_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 13 | |
| 14 | class Initializer { |
| 15 | |
| 16 | protected $plugin_initialized = false; |
| 17 | |
| 18 | function __construct($params = array( |
| 19 | 'file' => '', |
| 20 | 'version' => '1.0.0' |
| 21 | )) { |
| 22 | Env::init($params['file'], $params['version']); |
| 23 | } |
| 24 | |
| 25 | function init() { |
| 26 | $requirements_check_results = $this->checkRequirements(); |
| 27 | |
| 28 | // abort initialization if PDO extension is missing |
| 29 | if(!$requirements_check_results[RequirementsChecker::TEST_PDO_EXTENSION] || |
| 30 | !$requirements_check_results[RequirementsChecker::TEST_VENDOR_SOURCE] |
| 31 | ) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | $this->setupDB(); |
| 36 | |
| 37 | // activation function |
| 38 | register_activation_hook( |
| 39 | Env::$file, |
| 40 | array( |
| 41 | 'MailPoet\Config\Activator', |
| 42 | 'activate' |
| 43 | ) |
| 44 | ); |
| 45 | |
| 46 | add_action('plugins_loaded', array( |
| 47 | $this, |
| 48 | 'setup' |
| 49 | )); |
| 50 | add_action('init', array( |
| 51 | $this, |
| 52 | 'onInit' |
| 53 | )); |
| 54 | add_action('widgets_init', array( |
| 55 | $this, |
| 56 | 'setupWidget' |
| 57 | )); |
| 58 | add_action('wp_loaded', array( |
| 59 | $this, |
| 60 | 'setupHooks' |
| 61 | )); |
| 62 | } |
| 63 | |
| 64 | function checkRequirements() { |
| 65 | $requrements = new RequirementsChecker(); |
| 66 | return $requrements->checkAllRequirements(); |
| 67 | } |
| 68 | |
| 69 | function setupDB() { |
| 70 | $database = new Database(); |
| 71 | $database->init(); |
| 72 | } |
| 73 | |
| 74 | function setup() { |
| 75 | try { |
| 76 | $this->maybeDbUpdate(); |
| 77 | $this->setupRenderer(); |
| 78 | $this->setupLocalizer(); |
| 79 | $this->setupMenu(); |
| 80 | $this->setupAnalytics(); |
| 81 | $this->setupChangelog(); |
| 82 | $this->setupShortcodes(); |
| 83 | $this->setupImages(); |
| 84 | $this->setupCronTrigger(); |
| 85 | $this->setupConflictResolver(); |
| 86 | |
| 87 | $this->plugin_initialized = true; |
| 88 | } catch(\Exception $e) { |
| 89 | $this->handleFailedInitialization($e); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | function onInit() { |
| 94 | if(!$this->plugin_initialized) { |
| 95 | define('MAILPOET_INITIALIZED', false); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | try { |
| 100 | $this->setupAPI(); |
| 101 | $this->setupRouter(); |
| 102 | $this->setupPages(); |
| 103 | do_action('mailpoet_initialized', MAILPOET_VERSION); |
| 104 | } catch(\Exception $e) { |
| 105 | $this->handleFailedInitialization($e); |
| 106 | } |
| 107 | |
| 108 | define('MAILPOET_INITIALIZED', true); |
| 109 | } |
| 110 | |
| 111 | function maybeDbUpdate() { |
| 112 | $current_db_version = get_option('mailpoet_db_version', false); |
| 113 | |
| 114 | // if current db version and plugin version differ |
| 115 | if(version_compare($current_db_version, Env::$version) !== 0) { |
| 116 | Activator::activate(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | function setupWidget() { |
| 121 | if(!$this->plugin_initialized) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | try { |
| 126 | $widget = new Widget($this->renderer); |
| 127 | $widget->init(); |
| 128 | } catch(\Exception $e) { |
| 129 | $this->handleFailedInitialization($e); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | function setupRenderer() { |
| 134 | $caching = !WP_DEBUG; |
| 135 | $debugging = WP_DEBUG; |
| 136 | $this->renderer = new Renderer($caching, $debugging); |
| 137 | } |
| 138 | |
| 139 | function setupLocalizer() { |
| 140 | $localizer = new Localizer($this->renderer); |
| 141 | $localizer->init(); |
| 142 | } |
| 143 | |
| 144 | function setupMenu() { |
| 145 | $menu = new Menu($this->renderer, Env::$assets_url); |
| 146 | $menu->init(); |
| 147 | } |
| 148 | |
| 149 | function setupAnalytics() { |
| 150 | $analytics = new Analytics(); |
| 151 | $analytics->init(); |
| 152 | } |
| 153 | |
| 154 | function setupChangelog() { |
| 155 | $changelog = new Changelog(); |
| 156 | $changelog->init(); |
| 157 | } |
| 158 | |
| 159 | function setupPages() { |
| 160 | $pages = new \MailPoet\Settings\Pages(); |
| 161 | $pages->init(); |
| 162 | } |
| 163 | |
| 164 | function setupShortcodes() { |
| 165 | $shortcodes = new Shortcodes(); |
| 166 | $shortcodes->init(); |
| 167 | } |
| 168 | |
| 169 | function setupHooks() { |
| 170 | $hooks = new Hooks(); |
| 171 | $hooks->init(); |
| 172 | } |
| 173 | |
| 174 | function setupAPI() { |
| 175 | $api = new API\API(); |
| 176 | $api->init(); |
| 177 | } |
| 178 | |
| 179 | function setupRouter() { |
| 180 | $router = new Router\Router(); |
| 181 | $router->init(); |
| 182 | } |
| 183 | |
| 184 | function setupCronTrigger() { |
| 185 | // setup cron trigger only outside of cli environment |
| 186 | if(php_sapi_name() !== 'cli') { |
| 187 | $cron_trigger = new CronTrigger(); |
| 188 | $cron_trigger->init(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | function setupImages() { |
| 193 | add_image_size('mailpoet_newsletter_max', 1320); |
| 194 | } |
| 195 | |
| 196 | function setupConflictResolver() { |
| 197 | $conflict_resolver = new ConflictResolver(); |
| 198 | $conflict_resolver->init(); |
| 199 | } |
| 200 | |
| 201 | function handleFailedInitialization($message) { |
| 202 | return WPNotice::displayError($message); |
| 203 | } |
| 204 | } |