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