PluginProbe
Premmerce / 1.3.12
Premmerce v1.3.12
trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.19 1.3.2 1.3.20 1.3.21 1.3.22 1.3.23 1.3.3 1.3.4 1.3.5 1.3.6 All 28 releases
premmerce / src / Addons / AddonsManager.php

AddonsManager.php in Premmerce 1.3.12, at src/Addons/AddonsManager.php

337 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace Premmerce\Premmerce\Addons;
2
3 use Premmerce\ProductComparison\ProductComparisonPlugin;
4 use Premmerce\SDK\V2\FileManager\FileManager;
5 use Premmerce\Toolkit\PremmerceToolkitPlugin;
6
7 class AddonsManager
8 {
9 /**
10 * @var string
11 */
12 private $addonsBasePath;
13
14 private $addonsRelativePath;
15
16 private $addonsDirname = 'addons';
17
18 const OPTION = 'premmerce_premium_plugins';
19
20 const OPTION_ACTIVATION = 'premmerce_premium_plugins_activate';
21
22 /**
23 * @var array
24 */
25 private $addons = array(
26 ProductComparisonPlugin::class => 'premmerce-product-comparison/premmerce-product-comparison.php',
27 PremmerceToolkitPlugin::class => 'premmerce-woocommerce-toolkit/premmerce-toolkit.php',
28 );
29
30 private $textDomains = array(
31 ProductComparisonPlugin::class => 'premmerce-product-comparison',
32 PremmerceToolkitPlugin::class => 'premmerce-toolkit',
33 );
34
35 /**
36 * @var array
37 */
38 private $instances = array();
39
40 /**
41 * @var array
42 */
43 private $addonsStatuses = array();
44
45 /**
46 * Container constructor.
47 *
48 * @param FileManager $fileManager
49 */
50 public function __construct(FileManager $fileManager)
51 {
52 $this->addonsBasePath = $fileManager->getPluginDirectory() . $this->addonsDirname;
53 $this->addonsRelativePath = $fileManager->getPluginName() . '/' . $this->addonsDirname;
54 }
55
56 public function init()
57 {
58 $this->addonsStatuses = $this->getOptions();
59
60 add_action('plugins_loaded', array($this, 'runActive'));
61
62 add_filter('plugin_action_links', function ($actions, $plugin, $data) {
63 $plugins = array_flip($this->addons);
64 if (in_array($plugin, $this->addons) && $this->isActive($plugins[ $plugin ])) {
65 unset($actions['activate']);
66 $actions[] = '<p>' . sprintf(__('Please deactivate %s addon before activating this plugin', 'premmerce'), $data['Title']) . '</p>';
67 }
68
69 return $actions;
70 }, 10, 3);
71
72 add_action('admin_post_premmerce_addon_action', function () {
73 $class = urldecode($_POST['slug']);
74 $action = urldecode($_POST['addon_action']);
75
76 if ($this->exists($class)) {
77 switch ($action) {
78 case 'activate':
79 $this->activate(array($class));
80 break;
81 case 'deactivate':
82 $this->deactivate(array($class));
83 break;
84 }
85 }
86
87 wp_redirect($_SERVER['HTTP_REFERER']);
88 die;
89 });
90 }
91
92 /**
93 * @return array
94 */
95 public function getAddons()
96 {
97 return $this->addons;
98 }
99
100 /**
101 * @param string $class
102 *
103 * @return mixed
104 */
105 public function get($class)
106 {
107 if ($this->exists($class)) {
108 return $this->instantiate($class);
109 }
110 }
111
112 public function exists($class)
113 {
114 return array_key_exists($class, $this->addons);
115 }
116
117 /**
118 * @param string $plugin
119 *
120 * @return string
121 */
122 public function getPluginFullPath($plugin)
123 {
124 return $this->addonsBasePath . '/' . $plugin;
125 }
126
127 /**
128 * Run active plugins
129 */
130 public function runActive()
131 {
132 foreach ($this->getActivePlugins() as $class) {
133 $plugin = $this->addons[ $class ];
134 if (!is_plugin_active($plugin)) {
135 $this->callAddonMethod($class, 'run');
136 } else {
137 //deactivate addon if same plugin is active
138 $this->setStatus(array($class), false);
139 }
140 }
141 }
142
143 /**
144 * Activate plugins
145 *
146 * @param array $addons
147 */
148 public function activate(array $addons)
149 {
150 foreach ($addons as $addon) {
151 if (!$this->isActive($addon)) {
152 $this->callAddonMethod($addon, 'activate');
153 }
154 }
155
156 $this->setStatus($addons, true);
157 }
158
159 /**
160 * Deactivate plugins
161 *
162 * @param array $plugins
163 */
164 public function deactivate(array $plugins)
165 {
166 foreach ($plugins as $plugin) {
167 if ($this->isActive($plugin)) {
168 $this->callAddonMethod($plugin, 'deactivate');
169 }
170 }
171 $this->setStatus($plugins, false);
172 }
173
174 /**
175 * @param string $plugin
176 * @param string $method
177 *
178 * @return mixed
179 */
180 private function callAddonMethod($plugin, $method)
181 {
182 if ($plugin = $this->get($plugin)) {
183 if (method_exists($plugin, $method)) {
184 call_user_func(array($plugin, $method));
185 }
186 };
187
188 return $plugin;
189 }
190
191 /**
192 * Set status to array of plugins
193 *
194 * @param array $plugins
195 * @param bool $status
196 */
197 private function setStatus(array $plugins, $status)
198 {
199 $options = $this->addonsStatuses;
200
201 foreach ($plugins as $plugin) {
202 $options[ $plugin ] = $status;
203 }
204
205 update_option(self::OPTION, $options);
206 }
207
208 /**
209 * Check that plugin is active
210 *
211 * @param $plugin
212 *
213 * @return mixed
214 */
215 public function isActive($plugin)
216 {
217 return $this->addonsStatuses[ $plugin ];
218 }
219
220 /**
221 * Get data from plugin annotations
222 *
223 * @param string $plugin
224 *
225 * @return array
226 */
227 public function getPluginData($plugin)
228 {
229 $pluginFullPath = $this->getPluginFullPath($plugin);
230
231 $default = array(
232 'Name' => 'Plugin Name',
233 'PluginURI' => 'Plugin URI',
234 'Version' => 'Version',
235 'Description' => 'Description',
236 'Author' => 'Author',
237 'AuthorURI' => 'Author URI',
238 'TextDomain' => 'Text Domain',
239 'DomainPath' => 'Domain Path',
240 );
241 $data = get_file_data($pluginFullPath, $default, 'plugin');
242
243
244 return $data;
245 }
246
247 /**
248 * Array of active plugins
249 *
250 * @return array
251 */
252 private function getActivePlugins()
253 {
254 $options = $this->getOptions();
255
256 $options = array_filter($options);
257
258 return array_keys($options);
259 }
260
261 /**
262 * Cleaned plugins options merged with defaults
263 *
264 * @return array
265 */
266 private function getOptions()
267 {
268 $all = $this->getDefaults();
269
270 $options = get_option(self::OPTION, array());
271
272 foreach ($options as $key => $status) {
273 if (!array_key_exists($key, $all)) {
274 unset($options[ $key ]);
275 }
276 }
277
278 $options = array_merge($all, $options);
279
280 return array_merge($all, $options);
281 }
282
283 /**
284 * Default option for handled plugins
285 * @return array
286 */
287 private function getDefaults()
288 {
289 $all = array_keys($this->getAddons());
290
291 $keys = array_fill(0, count($all), false);
292
293 $all = array_combine($all, $keys);
294
295 return $all;
296 }
297
298 /**
299 * @param $class
300 *
301 * @return mixed
302 */
303 private function instantiate($class)
304 {
305 $path = $this->addons[ $class ];
306
307 if (!isset($this->instances[ $path ])) {
308 $fullPath = $this->getPluginFullPath($path);
309
310 $this->loadAddonTextDomain($class);
311
312 $autoloadPath = dirname($fullPath) . '/vendor/autoload.php';
313 if (file_exists($autoloadPath)) {
314 require_once $autoloadPath;
315 }
316
317 $plugin = new $class($fullPath);
318 $this->instances[ $path ] = $plugin;
319 }
320
321 return $this->instances[ $path ];
322 }
323
324 private function loadAddonTextDomain($class)
325 {
326 if (isset($this->textDomains[ $class ])) {
327 $domain = $this->textDomains[ $class ];
328
329 $path = $this->addons[ $class ];
330
331 $lang = $this->addonsRelativePath . '/' . dirname($path) . '/languages';
332
333 load_plugin_textdomain($domain, false, $lang);
334 }
335 }
336 }
337