PluginProbe
Premmerce / 1.3.23
Premmerce v1.3.23
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.23, at src/Addons/AddonsManager.php

348 lines 8.1 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
57 public function init()
58 {
59
60 $this->addonsStatuses = $this->getOptions();
61
62 add_action('plugins_loaded', array($this, 'runActive'));
63
64 add_filter('plugin_action_links', function ($actions, $plugin, $data) {
65 $plugins = array_flip($this->addons);
66 if (in_array($plugin, $this->addons) && $this->isActive($plugins[ $plugin ])) {
67 unset($actions['activate']);
68 $actions[] = '<p>' . sprintf(__('Please deactivate %s addon before activating this plugin', 'premmerce'), $data['Title']) . '</p>';
69 }
70
71 return $actions;
72 }, 10, 3);
73
74 add_action('admin_post_premmerce_addon_action', function () {
75 if (! current_user_can('manage_options') || ! check_admin_referer('premmerce_addon_action')) {
76 exit;
77 }
78
79 $class = urldecode($_POST['slug']);
80 $action = urldecode($_POST['addon_action']);
81
82 if ($this->exists($class)) {
83 switch ($action) {
84 case 'activate':
85 $this->activate(array($class));
86 break;
87 case 'deactivate':
88 $this->deactivate(array($class));
89 break;
90 }
91 }
92
93 wp_redirect($_SERVER['HTTP_REFERER']);
94 die;
95 });
96 }
97
98 /**
99 * @return array
100 */
101 public function getAddons()
102 {
103 return $this->addons;
104 }
105
106 /**
107 * @param string $class
108 *
109 * @return mixed
110 */
111 public function get($class)
112 {
113 if ($this->exists($class)) {
114 return $this->instantiate($class);
115 }
116 }
117
118 public function exists($class)
119 {
120 return array_key_exists($class, $this->addons);
121 }
122
123 /**
124 * @param string $plugin
125 *
126 * @return string
127 */
128 public function getPluginFullPath($plugin)
129 {
130 return $this->addonsBasePath . '/' . $plugin;
131 }
132
133 /**
134 * Run active plugins
135 */
136 public function runActive()
137 {
138
139 foreach ($this->getActivePlugins() as $class) {
140 $plugin = $this->addons[ $class ];
141 if (!is_plugin_active($plugin)) {
142 $this->callAddonMethod($class, 'run');
143 } else {
144 //deactivate addon if same plugin is active
145 $this->setStatus(array($class), false);
146 }
147 }
148
149 }
150
151 /**
152 * Activate plugins
153 *
154 * @param array $addons
155 */
156 public function activate(array $addons)
157 {
158
159 foreach ($addons as $addon) {
160 if (!$this->isActive($addon)) {
161 $this->callAddonMethod($addon, 'activate');
162 }
163 }
164
165 $this->setStatus($addons, true);
166 }
167
168 /**
169 * Deactivate plugins
170 *
171 * @param array $plugins
172 */
173 public function deactivate(array $plugins)
174 {
175 foreach ($plugins as $plugin) {
176 if ($this->isActive($plugin)) {
177 $this->callAddonMethod($plugin, 'deactivate');
178 }
179 }
180 $this->setStatus($plugins, false);
181 }
182
183 /**
184 * @param string $plugin
185 * @param string $method
186 *
187 * @return mixed
188 */
189 private function callAddonMethod($plugin, $method)
190 {
191 if ($plugin = $this->get($plugin)) {
192 if (method_exists($plugin, $method)) {
193 call_user_func(array($plugin, $method));
194 }
195 };
196
197 return $plugin;
198 }
199
200 /**
201 * Set status to array of plugins
202 *
203 * @param array $plugins
204 * @param bool $status
205 */
206 private function setStatus(array $plugins, $status)
207 {
208 $options = $this->addonsStatuses;
209
210 foreach ($plugins as $plugin) {
211 $options[ $plugin ] = $status;
212 }
213
214 update_option(self::OPTION, $options);
215 }
216
217 /**
218 * Check that plugin is active
219 *
220 * @param $plugin
221 *
222 * @return mixed
223 */
224 public function isActive($plugin)
225 {
226 return $this->addonsStatuses[ $plugin ];
227 }
228
229 /**
230 * Get data from plugin annotations
231 *
232 * @param string $plugin
233 *
234 * @return array
235 */
236 public function getPluginData($plugin)
237 {
238 $pluginFullPath = $this->getPluginFullPath($plugin);
239
240 $default = array(
241 'Name' => 'Plugin Name',
242 'PluginURI' => 'Plugin URI',
243 'Version' => 'Version',
244 'Description' => 'Description',
245 'Author' => 'Author',
246 'AuthorURI' => 'Author URI',
247 'TextDomain' => 'Text Domain',
248 'DomainPath' => 'Domain Path',
249 );
250 $data = get_file_data($pluginFullPath, $default, 'plugin');
251
252
253 return $data;
254 }
255
256 /**
257 * Array of active plugins
258 *
259 * @return array
260 */
261 private function getActivePlugins()
262 {
263 $options = $this->getOptions();
264
265 $options = array_filter($options);
266
267 return array_keys($options);
268 }
269
270 /**
271 * Cleaned plugins options merged with defaults
272 *
273 * @return array
274 */
275 private function getOptions()
276 {
277 $all = $this->getDefaults();
278
279 $options = get_option(self::OPTION, array());
280
281 foreach ($options as $key => $status) {
282 if (!array_key_exists($key, $all)) {
283 unset($options[ $key ]);
284 }
285 }
286
287 $options = array_merge($all, $options);
288
289 return array_merge($all, $options);
290 }
291
292 /**
293 * Default option for handled plugins
294 * @return array
295 */
296 private function getDefaults()
297 {
298 $all = array_keys($this->getAddons());
299
300 $keys = array_fill(0, count($all), false);
301
302 $all = array_combine($all, $keys);
303
304 return $all;
305 }
306
307 /**
308 * @param $class
309 *
310 * @return mixed
311 */
312 private function instantiate($class)
313 {
314 $path = $this->addons[ $class ];
315
316 if (!isset($this->instances[ $path ])) {
317 $fullPath = $this->getPluginFullPath($path);
318
319 $this->loadAddonTextDomain($class);
320
321 $autoloadPath = dirname($fullPath) . '/vendor/autoload.php';
322 if (file_exists($autoloadPath)) {
323 require_once $autoloadPath;
324 }
325
326 $plugin = new $class($fullPath);
327 $this->instances[ $path ] = $plugin;
328 }
329
330 return $this->instances[ $path ];
331 }
332
333 private function loadAddonTextDomain($class)
334 {
335
336 if (isset($this->textDomains[ $class ])) {
337
338 $domain = $this->textDomains[ $class ];
339
340 $path = $this->addons[ $class ];
341
342 $lang = $this->addonsRelativePath . '/' . dirname($path) . '/languages';
343
344 load_plugin_textdomain($domain, false, $lang);
345 }
346 }
347 }
348