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