Нет описания

misc.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. /**
  3. * File for our cool Carousel in the footer
  4. *
  5. * @category Child Plugin
  6. * @version v0.1.0
  7. * @since v0.1.0
  8. * @author iClyde <kontakt@iclyde.pl>
  9. */
  10. // Namespace
  11. namespace Inisev\Subs;
  12. // Disallow direct access
  13. if (defined('ABSPATH')) {
  14. /**
  15. * Main class for handling the Carousel
  16. */
  17. if (!class_exists('Inisev\Subs\Inisev_Carousel')) {
  18. class Inisev_Carousel {
  19. // Should hide it for good i.e. styles may be broken?
  20. private $error = 0;
  21. // Slugs of plugins
  22. private $usm_premium = 'usm-premium/usm_premium_icons.php';
  23. private $usm_slug = 'ultimate-social-media-icons/ultimate_social_media_icons.php';
  24. private $bmi_premium = 'backup-backup-pro/backup-backup-pro.php';
  25. private $bmi_slug = 'backup-backup/backup-backup.php';
  26. private $cdp_premium = 'copy-delete-posts-premium/copy-delete-posts-premium.php';
  27. private $cdp_slug = 'copy-delete-posts/copy-delete-posts.php';
  28. private $mpu_slug = 'pop-up-pop-up/pop-up-pop-up.php';
  29. /*
  30. * Compile some variables for "future us"
  31. * Such as slug of current plugin, root dir of plugin
  32. */
  33. function __construct($root_file, $root_dir) {
  34. // This roots
  35. $this->_root_file = $root_file;
  36. $this->_root_dir = $root_dir;
  37. // Add handler for Ajax request
  38. if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
  39. // Check if slug is defined
  40. if (isset($_POST['slug']) && !empty($_POST['slug'])) {
  41. // Handle the request
  42. add_action('wp_ajax_inisev_installation', [&$this, 'handle_installation']);
  43. }
  44. // Stop for POST
  45. return;
  46. }
  47. // WordPress globals
  48. global $menu;
  49. // Make sure WP_PLUGIN_DIR is defined
  50. if (!defined('WP_PLUGIN_DIR')) return $this->fail(1);
  51. if (!function_exists('trailingslashit')) return $this->fail(2);
  52. if (!defined('DIRECTORY_SEPARATOR')) define('DIRECTORY_SEPARATOR', '/');
  53. // That's in case the developer moved this file somewhere else
  54. $tmp_slug = trailingslashit($this->_root_dir);
  55. $tmp_root = trailingslashit(WP_PLUGIN_DIR);
  56. $tmp_name = explode(DIRECTORY_SEPARATOR, substr($tmp_slug, strlen($tmp_root)));
  57. // Make the "probably" slug name
  58. $this->page = sanitize_text_field($_GET['page']);
  59. $this->slug = $tmp_name[0];
  60. $this->root = $tmp_root . $this->slug;
  61. // Make lowercase slug
  62. $this->slug_low = $this->makelower($this->slug);
  63. // We don't need those anymore
  64. unset($tmp_slug, $tmp_root, $tmp_name);
  65. // Check if the guess is correct enough
  66. if (!is_dir($this->root)) return $this->fail(3);
  67. // Check if the script requires to be in hook
  68. if (!function_exists('current_action')) return $this->fail(4);
  69. $this->hooked = (current_action() == '' ? false : true);
  70. // Add hook if it's required
  71. if (!$this->hooked) {
  72. // Hook the script to init
  73. add_action('admin_menu', [&$this, 'setup'], PHP_INT_MAX);
  74. } else {
  75. // The child plugin is already hooked, check if correctly
  76. if (current_action() == 'admin_menu' || isset($menu)) {
  77. // If the hook is correct continue
  78. $this->setup();
  79. } else {
  80. // Hook the script to init if it's not hooked to it already
  81. add_action('admin_menu', [&$this, 'setup'], PHP_INT_MAX);
  82. }
  83. }
  84. }
  85. /*
  86. * Main setup of this child plugin
  87. */
  88. public function setup() {
  89. // WordPress Global Variables
  90. global $menu;
  91. // Make sure $menu exists
  92. if (!isset($menu) || !is_array($menu)) return $this->fail(5);
  93. // Get menu slug name
  94. if (!$this->menu_name($menu)) return false;
  95. if ($this->page === $this->menu && !defined('INISEV_CAROUSEL')) {
  96. // Initialize Carousel constant
  97. define('INISEV_CAROUSEL', true);
  98. // Root URL for assets
  99. $this->url = trailingslashit(plugins_url(null, $this->_root_file));
  100. // Load styles
  101. wp_enqueue_script('inisev-carousel-script', ($this->url . 'assets/index.min.js'), [], filemtime($this->_root_dir . '/assets/index.min.js'), true);
  102. wp_enqueue_style('inisev-carousel-style', ($this->url . 'assets/style.min.css'), [], filemtime($this->_root_dir . '/assets/style.min.css'));
  103. // Print the footer
  104. add_action('in_admin_footer', [&$this, '_print'], 1);
  105. }
  106. }
  107. /*
  108. * This function may be used for debugging purposes
  109. */
  110. private function fail($code = false) {
  111. if ($code === false) {
  112. // Return error code if specified as request ($code === false)
  113. return $this->error;
  114. } else {
  115. // Set the error code and return
  116. // error_log($code);
  117. $this->error = $code;
  118. return false;
  119. }
  120. }
  121. /*
  122. * Helper function remove _ -/ characters and make lowercase
  123. */
  124. private function makelower($str) {
  125. $str = str_replace('_', '', $str);
  126. $str = str_replace('-', '', $str);
  127. $str = str_replace('/', '', $str);
  128. $str = str_replace('\/', '', $str);
  129. $str = str_replace(' ', '', $str);
  130. $str = strtolower($str);
  131. return $str;
  132. }
  133. /*
  134. * This function will find slug of menu page
  135. */
  136. private function menu_name(&$menu) {
  137. // Find the menu slug
  138. // IMPORTANT: It requires the plugin to use own icon (own assets)
  139. foreach ($menu as $priority => $details) {
  140. if (is_array($details) && sizeof($details) >= 6) {
  141. for ($i = 0; $i < sizeof($details); ++$i) {
  142. if ($this->makelower($details[$i]) == $this->slug_low) {
  143. $this->menu = $details[2];
  144. break;
  145. }
  146. }
  147. if (isset($this->menu)) break;
  148. }
  149. }
  150. // MyPopUps exception
  151. if (!isset($this->menu)) {
  152. $mpu = ['wpmypopups', 'mypopups', 'popuppopup'];
  153. if (in_array($this->slug_low, $mpu)) {
  154. $this->menu = 'wp-mypopups';
  155. }
  156. }
  157. if (!isset($this->menu)) {
  158. $bmi = ['backupbackup', 'backup-backup', 'backup-migration', 'backupmigration'];
  159. if (in_array($this->slug_low, $bmi)) {
  160. $this->menu = 'backup-migration';
  161. }
  162. }
  163. if (!isset($this->menu)) {
  164. $hhr = ['httpsremover', 'httphttpsremover'];
  165. if (in_array($this->slug_low, $hhr)) {
  166. $this->menu = 'httphttpsRemoval';
  167. }
  168. }
  169. if (!isset($this->menu)) {
  170. $wpc = ['wp-clone', 'wp-clone', 'wpclonebywpacademy'];
  171. if (in_array($this->slug_low, $wpc)) {
  172. $this->menu = 'wp-clone';
  173. }
  174. }
  175. // Make sure it found something
  176. if (isset($this->menu)) return true;
  177. else return $this->fail(6);
  178. }
  179. /*
  180. * Helper: Include file
  181. */
  182. private function _include($path) {
  183. include_once trailingslashit($this->_root_dir) . 'views/' . $path . '.php';
  184. }
  185. /*
  186. * Helper: Get asset URL
  187. */
  188. private function get_asset($file) {
  189. return $this->url . $file;
  190. }
  191. /*
  192. * Helper: Get asset and print URL
  193. */
  194. private function _asset($file) {
  195. echo $this->get_asset('views/' . $file);
  196. }
  197. /*
  198. * Upgrade plugin, this function probably will never be fired
  199. */
  200. private function upgrade_plugin($plugin_slug) {
  201. // Include upgrader
  202. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  203. wp_cache_flush();
  204. // Initialize & upgrade the plugin
  205. $upgrader = new \Plugin_Upgrader();
  206. $upgraded = $upgrader->upgrade($plugin_slug);
  207. // Return status or WP Error
  208. return $upgraded;
  209. }
  210. /*
  211. * Check if plugin is installed by slug
  212. */
  213. private function is_plugin_installed($slug) {
  214. // Get all plugins
  215. $all_plugins = get_plugins();
  216. // Make sure all slugs are in lowercase.
  217. foreach ($all_plugins as $plug => $v) {
  218. // Once something match return success
  219. if (strtolower($plug) == strtolower($slug)) return true;
  220. }
  221. // If nothing just fail
  222. return false;
  223. // When I exactly know the letter case...
  224. // if (!empty($all_plugins[$slug])) return true;
  225. // else return false;
  226. }
  227. /*
  228. * Install the plugin by slug
  229. */
  230. private function install_plugin($plugin_zip) {
  231. // Include upgrader
  232. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  233. wp_cache_flush();
  234. // Initialize WP upgrader & install the plugin
  235. $upgrader = new \Plugin_Upgrader();
  236. $installed = $upgrader->install($plugin_zip);
  237. // Return status or WP Error
  238. return $installed;
  239. }
  240. /*
  241. * Install file
  242. */
  243. private function install($slug, $directory_slug) {
  244. // Prepare the URLs and full slug
  245. $plugin_slug = $slug;
  246. $plugin_zip = 'https://downloads.wordpress.org/plugin/' . $directory_slug . '.latest-stable.zip';
  247. // Make sure the plugin is not installed
  248. if ($this->is_plugin_installed($plugin_slug)) {
  249. // Upgrade the plugin if it's installed somehow
  250. $this->upgrade_plugin($plugin_slug);
  251. $installed = true;
  252. // Install instead
  253. } else $installed = $this->install_plugin($plugin_zip);
  254. // Check if there was any error
  255. if (!is_wp_error($installed) && $installed) {
  256. $activate = activate_plugin($plugin_slug);
  257. if (is_null($activate)) {
  258. $url = admin_url('', 'admin');
  259. // CDP has special alert when installed with quick-install module
  260. if ($_POST['slug'] === 'cdp') {
  261. update_option('_cdp_cool_installation', true);
  262. update_option('_cdp_redirect', true);
  263. $url = admin_url() . 'admin.php?page=copy-delete-posts';
  264. }
  265. // Redirection for MPU
  266. if ($_POST['slug'] === 'mpu') {
  267. update_option('wp_mypopups_do_activation_redirect', true);
  268. $url = admin_url() . 'admin.php?page=wp-mypopups';
  269. }
  270. // Redirection for USM
  271. if ($_POST['slug'] === 'usm') {
  272. update_option('sfsi_plugin_do_activation_redirect', true);
  273. $url = admin_url() . 'admin.php?page=sfsi-options';
  274. }
  275. // Redirection for BMI
  276. if ($_POST['slug'] === 'bmi') {
  277. update_option('_bmi_redirect', true);
  278. $url = admin_url() . 'admin.php?page=backup-migration';
  279. }
  280. // Send success
  281. wp_send_json_success([ 'installed' => true, 'url' => $url ]);
  282. // I don't know what happened here and if it's even possible
  283. } else wp_send_json_error();
  284. // Send fail
  285. } else wp_send_json_error();
  286. }
  287. /*
  288. * Add/print the Carousel
  289. */
  290. public function _print() {
  291. try {
  292. include_once trailingslashit($this->_root_dir) . 'views/index.php';
  293. } catch (\Exception $e) {
  294. return $this->fail(7);
  295. } catch (\Exception $e) {
  296. return $this->fail(8);
  297. }
  298. }
  299. /*
  300. * Handle ajax request
  301. */
  302. public function handle_installation() {
  303. // Handle the slug and install the plugin
  304. $slug = sanitize_text_field($_POST['slug']);
  305. if ($slug === 'usm') {
  306. $this->install($this->usm_slug, 'ultimate-social-media-icons');
  307. } elseif ($slug === 'bmi') {
  308. $this->install($this->bmi_slug, 'backup-backup');
  309. } elseif ($slug === 'cdp') {
  310. $this->install($this->cdp_slug, 'copy-delete-posts');
  311. } elseif ($slug === 'mpu') {
  312. $this->install($this->mpu_slug, 'pop-up-pop-up');
  313. // Anything else error
  314. } else wp_send_json_error();
  315. }
  316. }
  317. }
  318. // Disallow usage of multiple Carousels + allow only GET requests
  319. if (!defined('INISEV_CAROUSEL')) {
  320. // Make sure settings/menu page slug exsits
  321. if (!empty($_GET['page']) || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  322. // Initialize the Carousel
  323. $carousel = new Inisev_Carousel(__FILE__, __DIR__);
  324. }
  325. }
  326. }