PluginProbe ʕ •ᴥ•ʔ
Redirection / 1.0.8
Redirection v1.0.8
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0
redirect-redirection / includes / banner / misc.php
redirect-redirection / includes / banner Last commit date
assets 4 years ago views 4 years ago misc.php 4 years ago
misc.php
473 lines
1 <?php
2
3 /**
4 * File for our cool Carousel in the footer
5 *
6 * @category Child Plugin
7 * @version v0.2.0
8 * @since v0.1.0
9 * @author iClyde <kontakt@iclyde.pl>
10 */
11
12 // Namespace
13 namespace Inisev\Subs;
14
15 // Disallow direct access
16 if (defined('ABSPATH')) {
17
18 /**
19 * Main class for handling the Carousel
20 */
21 if (!class_exists('Inisev\Subs\Inisev_Carousel')) {
22 class Inisev_Carousel {
23
24 // Should hide it for good i.e. styles may be broken?
25 private $error = 0;
26
27 // Slugs of plugins
28 private $usm_premium = 'usm-premium/usm_premium_icons.php';
29 private $usm_slug = 'ultimate-social-media-icons/ultimate_social_media_icons.php';
30 private $bmi_premium = 'backup-backup-pro/backup-backup-pro.php';
31 private $bmi_slug = 'backup-backup/backup-backup.php';
32 private $cdp_premium = 'copy-delete-posts-premium/copy-delete-posts-premium.php';
33 private $cdp_slug = 'copy-delete-posts/copy-delete-posts.php';
34 private $mpu_slug = 'pop-up-pop-up/pop-up-pop-up.php';
35 private $redi_slug = 'redirect-redirection/redirect-redirection.php';
36
37 /*
38 * Compile some variables for "future us"
39 * Such as slug of current plugin, root dir of plugin
40 */
41 function __construct($root_file, $root_dir) {
42
43 // This roots
44 $this->_root_file = $root_file;
45 $this->_root_dir = $root_dir;
46
47 // Add handler for Ajax request
48 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
49
50 // Check if slug is defined
51 if (isset($_POST['slug']) && !empty($_POST['slug'])) {
52
53 // Handle the request
54 add_action('wp_ajax_inisev_installation', [&$this, 'handle_installation']);
55
56 }
57
58 // Stop for POST
59 return;
60
61 }
62
63 // WordPress globals
64 global $menu;
65
66 // Make sure WP_PLUGIN_DIR is defined
67 if (!defined('WP_PLUGIN_DIR')) return $this->fail(1);
68 if (!function_exists('trailingslashit')) return $this->fail(2);
69 if (!defined('DIRECTORY_SEPARATOR')) define('DIRECTORY_SEPARATOR', '/');
70
71 // That's in case the developer moved this file somewhere else
72 $tmp_slug = trailingslashit($this->_root_dir);
73 $tmp_root = trailingslashit(WP_PLUGIN_DIR);
74 $tmp_name = explode(DIRECTORY_SEPARATOR, substr($tmp_slug, strlen($tmp_root)));
75
76 // Make the "probably" slug name
77 $this->page = sanitize_text_field($_GET['page']);
78 $this->slug = $tmp_name[0];
79 $this->root = $tmp_root . $this->slug;
80
81 // Make lowercase slug
82 $this->slug_low = $this->makelower($this->slug);
83
84 // We don't need those anymore
85 unset($tmp_slug, $tmp_root, $tmp_name);
86
87 // Check if the guess is correct enough
88 if (!is_dir($this->root)) return $this->fail(3);
89
90 // Check if the script requires to be in hook
91 if (!function_exists('current_action')) return $this->fail(4);
92 $this->hooked = (current_action() == '' ? false : true);
93
94 // Add hook if it's required
95 if (!$this->hooked) {
96
97 // Hook the script to init
98 add_action('admin_menu', [&$this, 'setup'], PHP_INT_MAX);
99
100 } else {
101
102 // The child plugin is already hooked, check if correctly
103 if (current_action() == 'admin_menu' || isset($menu)) {
104
105 // If the hook is correct continue
106 $this->setup();
107
108 } else {
109
110 // Hook the script to init if it's not hooked to it already
111 add_action('admin_menu', [&$this, 'setup'], PHP_INT_MAX);
112
113 }
114
115 }
116
117 }
118
119 /*
120 * Main setup of this child plugin
121 */
122 public function setup() {
123
124 // WordPress Global Variables
125 global $menu;
126
127 // Make sure $menu exists
128 if (!isset($menu) || !is_array($menu)) return $this->fail(5);
129
130 // Get menu slug name
131 if (!$this->menu_name($menu)) return false;
132
133 if (/*$this->page === $this->menu && */!defined('INISEV_CAROUSEL')) {
134
135 // Initialize Carousel constant
136 define('INISEV_CAROUSEL', true);
137
138 // Root URL for assets
139 $this->url = trailingslashit(plugins_url(null, $this->_root_file));
140
141 // Load styles
142 wp_enqueue_script('inisev-carousel-script', ($this->url . 'assets/index.min.js'), [], filemtime($this->_root_dir . '/assets/index.min.js'), true);
143 wp_enqueue_style('inisev-carousel-style', ($this->url . 'assets/style.min.css'), [], filemtime($this->_root_dir . '/assets/style.min.css'));
144
145 // Print the footer
146 if (!has_action('ins_global_print_carrousel')) {
147 add_action('ins_global_print_carrousel', [&$this, '_print'], 1);
148 }
149
150 }
151
152 }
153
154 /*
155 * This function may be used for debugging purposes
156 */
157 private function fail($code = false) {
158
159 if ($code === false) {
160
161 // Return error code if specified as request ($code === false)
162 return $this->error;
163
164 } else {
165
166 // Set the error code and return
167 // error_log($code);
168 $this->error = $code;
169 return false;
170
171 }
172
173 }
174
175 /*
176 * Helper function remove _ -/ characters and make lowercase
177 */
178 private function makelower($str) {
179
180 $str = str_replace('_', '', $str);
181 $str = str_replace('-', '', $str);
182 $str = str_replace('/', '', $str);
183 $str = str_replace('\/', '', $str);
184 $str = str_replace(' ', '', $str);
185 $str = strtolower($str);
186
187 return $str;
188
189 }
190
191 /*
192 * This function will find slug of menu page
193 */
194 private function menu_name(&$menu) {
195
196 // Find the menu slug
197 // IMPORTANT: It requires the plugin to use own icon (own assets)
198 foreach ($menu as $priority => $details) {
199 if (is_array($details) && sizeof($details) >= 6) {
200 for ($i = 0; $i < sizeof($details); ++$i) {
201 if ($this->makelower($details[$i]) == $this->slug_low) {
202 $this->menu = $details[2];
203 break;
204 }
205 }
206 if (isset($this->menu)) break;
207 }
208 }
209
210 // MyPopUps exception
211 if (!isset($this->menu)) {
212 $mpu = ['wpmypopups', 'mypopups', 'popuppopup'];
213 if (in_array($this->slug_low, $mpu)) {
214 $this->menu = 'wp-mypopups';
215 }
216 }
217
218 if (!isset($this->menu)) {
219 $bmi = ['backupbackup', 'backup-backup', 'backup-migration', 'backupmigration'];
220 if (in_array($this->slug_low, $bmi)) {
221 $this->menu = 'backup-migration';
222 }
223 }
224
225 if (!isset($this->menu)) {
226 $hhr = ['httpsremover', 'httphttpsremover'];
227 if (in_array($this->slug_low, $hhr)) {
228 $this->menu = 'httphttpsRemoval';
229 }
230 }
231
232 if (!isset($this->menu)) {
233 $wpc = ['wp-clone', 'wp-clone', 'wpclonebywpacademy'];
234 if (in_array($this->slug_low, $wpc)) {
235 $this->menu = 'wp-clone';
236 }
237 }
238
239 // Make sure it found something
240 if (isset($this->menu)) return true;
241 else return true;
242 // else return $this->fail(6);
243
244 }
245
246 /*
247 * Helper: Include file
248 */
249 private function _include($path) {
250
251 include_once trailingslashit($this->_root_dir) . 'views/' . $path . '.php';
252
253 }
254
255 /*
256 * Helper: Get asset URL
257 */
258 private function get_asset($file) {
259
260 return $this->url . $file;
261
262 }
263
264 /*
265 * Helper: Get asset and print URL
266 */
267 private function _asset($file) {
268
269 echo $this->get_asset('views/' . $file);
270
271 }
272
273 /*
274 * Upgrade plugin, this function probably will never be fired
275 */
276 private function upgrade_plugin($plugin_slug) {
277
278 // Include upgrader
279 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
280 wp_cache_flush();
281
282 // Initialize & upgrade the plugin
283 $upgrader = new \Plugin_Upgrader();
284 $upgraded = $upgrader->upgrade($plugin_slug);
285
286 // Return status or WP Error
287 return $upgraded;
288
289 }
290
291 /*
292 * Check if plugin is installed by slug
293 */
294 private function is_plugin_installed($slug) {
295
296 // Get all plugins
297 $all_plugins = get_plugins();
298
299 // Make sure all slugs are in lowercase.
300 foreach ($all_plugins as $plug => $v) {
301
302 // Once something match return success
303 if (strtolower($plug) == strtolower($slug)) return true;
304
305 }
306
307 // If nothing just fail
308 return false;
309
310 // When I exactly know the letter case...
311 // if (!empty($all_plugins[$slug])) return true;
312 // else return false;
313
314 }
315
316 /*
317 * Install the plugin by slug
318 */
319 private function install_plugin($plugin_zip) {
320
321 // Include upgrader
322 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
323 wp_cache_flush();
324
325 // Initialize WP upgrader & install the plugin
326 $upgrader = new \Plugin_Upgrader();
327 $installed = $upgrader->install($plugin_zip);
328
329 // Return status or WP Error
330 return $installed;
331
332 }
333
334 /*
335 * Install file
336 */
337 private function install($slug, $directory_slug) {
338
339 // Prepare the URLs and full slug
340 $plugin_slug = $slug;
341 $plugin_zip = 'https://downloads.wordpress.org/plugin/' . $directory_slug . '.latest-stable.zip';
342
343 // Make sure the plugin is not installed
344 if ($this->is_plugin_installed($plugin_slug)) {
345
346 // Upgrade the plugin if it's installed somehow
347 $this->upgrade_plugin($plugin_slug);
348 $installed = true;
349
350 // Install instead
351 } else $installed = $this->install_plugin($plugin_zip);
352
353 // Check if there was any error
354 if (!is_wp_error($installed) && $installed) {
355 $activate = activate_plugin($plugin_slug);
356
357 if (is_null($activate)) {
358
359 $url = admin_url('', 'admin');
360
361 // CDP has special alert when installed with quick-install module
362 if ($_POST['slug'] === 'cdp') {
363 update_option('_cdp_cool_installation', true);
364 update_option('_cdp_redirect', true);
365 $url = admin_url() . 'admin.php?page=copy-delete-posts';
366 }
367
368 // Redirection for MPU
369 if ($_POST['slug'] === 'mpu') {
370 update_option('wp_mypopups_do_activation_redirect', true);
371 $url = admin_url() . 'admin.php?page=wp-mypopups';
372 }
373
374 // Redirection for USM
375 if ($_POST['slug'] === 'usm') {
376 update_option('sfsi_plugin_do_activation_redirect', true);
377 $url = admin_url() . 'admin.php?page=sfsi-options';
378 }
379
380 // Redirection for BMI
381 if ($_POST['slug'] === 'bmi') {
382 update_option('_bmi_redirect', true);
383 $url = admin_url() . 'admin.php?page=backup-migration';
384 }
385
386 // Redirection for RED
387 if ($_POST['slug'] === 'redi') {
388 update_option('irrp_activation_redirect', true);
389 $url = admin_url() . 'admin.php?page=irrp-redirection';
390 }
391
392 // Send success
393 wp_send_json_success([ 'installed' => true, 'url' => $url ]);
394
395 // I don't know what happened here and if it's even possible
396 } else wp_send_json_error();
397
398 // Send fail
399 } else wp_send_json_error();
400
401 }
402
403 /*
404 * Add/print the Carousel
405 */
406 public function _print() {
407
408 try {
409
410 include_once trailingslashit($this->_root_dir) . 'views/index.php';
411
412 } catch (\Exception $e) {
413
414 return $this->fail(7);
415
416 } catch (\Exception $e) {
417
418 return $this->fail(8);
419
420 }
421
422 }
423
424 /*
425 * Handle ajax request
426 */
427 public function handle_installation() {
428
429 // Handle the slug and install the plugin
430 $slug = sanitize_text_field($_POST['slug']);
431 if ($slug === 'usm') {
432
433 $this->install($this->usm_slug, 'ultimate-social-media-icons');
434
435 } elseif ($slug === 'bmi') {
436
437 $this->install($this->bmi_slug, 'backup-backup');
438
439 } elseif ($slug === 'cdp') {
440
441 $this->install($this->cdp_slug, 'copy-delete-posts');
442
443 } elseif ($slug === 'mpu') {
444
445 $this->install($this->mpu_slug, 'pop-up-pop-up');
446
447 } elseif ($slug == 'redi') {
448
449 $this->install($this->redi_slug, 'redirect-redirection');
450
451 // Anything else error
452 } else wp_send_json_error();
453
454 }
455
456 }
457 }
458
459 // Disallow usage of multiple Carousels + allow only GET requests
460 if (!defined('INISEV_CAROUSEL')) {
461
462 // Make sure settings/menu page slug exsits
463 if (!empty($_GET['page']) || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
464
465 // Initialize the Carousel
466 $carousel = new Inisev_Carousel(__FILE__, __DIR__);
467
468 }
469
470 }
471
472 }
473