misc.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Show notice about new feature: Google Drive Storage |
| 5 | * |
| 6 | * @category Child Plugin |
| 7 | * @version v0.1.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 GDrive Banner |
| 20 | */ |
| 21 | if (!class_exists('Inisev\Subs\BMI_Banners_GDrive')) { |
| 22 | class BMI_Banners_GDrive { |
| 23 | |
| 24 | /** |
| 25 | * Variable declarations |
| 26 | */ |
| 27 | public $name = null; |
| 28 | public $nonce = null; |
| 29 | public $plugin_menu_url = null; |
| 30 | public $assets_js = null; |
| 31 | public $assets_css = null; |
| 32 | |
| 33 | /** |
| 34 | * Initializes the module |
| 35 | */ |
| 36 | function __construct($display_name, $plugin_menu_url) { |
| 37 | |
| 38 | $this->name = $display_name; |
| 39 | $this->nonce = wp_create_nonce('bmi_grive_action'); |
| 40 | $this->plugin_menu_url = admin_url('admin.php?page=' . $plugin_menu_url); |
| 41 | |
| 42 | $this->assets_js = __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR; |
| 43 | $this->assets_css = __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR; |
| 44 | |
| 45 | if (get_option('bmi_gdrive_banner_dismissed', false)) return false; |
| 46 | add_action('wp_ajax_bmi_gdrive_banner', [&$this, 'handle_banner_action']); |
| 47 | $this->display_banner_if_possible(); |
| 48 | |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Checks if banner should be displayed and adds action |
| 53 | */ |
| 54 | public function display_banner_if_possible() { |
| 55 | |
| 56 | if (!$this->check_page()) return; |
| 57 | if (!$this->check_version()) return; |
| 58 | |
| 59 | add_action('admin_notices', [&$this, 'include_banner'], 100); |
| 60 | |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Handles banner Action (close) |
| 65 | */ |
| 66 | public function handle_banner_action() { |
| 67 | if (check_ajax_referer('bmi_grive_action', 'nonce', false) === false) return wp_send_json_error(); |
| 68 | if (!current_user_can('manage_options')) return wp_send_json_error(); |
| 69 | |
| 70 | $mode = sanitize_text_field($_POST['mode']); |
| 71 | if ($mode == 'dismiss') update_option('bmi_gdrive_banner_dismissed', true); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns URL to image |
| 77 | */ |
| 78 | public function __img($filename) { |
| 79 | |
| 80 | $path = __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'imgs' . DIRECTORY_SEPARATOR . $filename; |
| 81 | $url = plugins_url($this->path, BMI_ROOT_DIR); |
| 82 | |
| 83 | return $url; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Print banner HTML |
| 89 | */ |
| 90 | public function include_banner() { |
| 91 | |
| 92 | wp_enqueue_style('bmi-gdrive-banner-style', plugin_dir_url($this->assets_css) . 'css/styles.css', [], filemtime($this->assets_css . 'styles.css')); |
| 93 | wp_enqueue_script('bmi-gdrive-banner-script', plugin_dir_url($this->assets_js) . 'js/script.js', [], filemtime($this->assets_js . 'script.js'), true); |
| 94 | require_once __DIR__ . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'banner.php'; |
| 95 | |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Checks if the banner should be displayed on particular page |
| 100 | */ |
| 101 | public function check_page() { |
| 102 | |
| 103 | global $pagenow; |
| 104 | |
| 105 | if (!is_admin()) return false; |
| 106 | if (!current_user_can('manage_options')) return false; |
| 107 | |
| 108 | $allowed_pages = ['index.php', 'plugins.php']; |
| 109 | if (!(in_array($pagenow, $allowed_pages) || strpos($_SERVER['REQUEST_URI'], 'admin.php?page=backup-migration'))) return false; |
| 110 | |
| 111 | return true; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Check if the plugin was updated |
| 117 | * There is no reason to display it for new users, but only to these who awaited that feature |
| 118 | */ |
| 119 | public function check_version() { |
| 120 | |
| 121 | // Get applied hotfixes |
| 122 | $hotfixes = get_option('bmi_hotfixes', array()); |
| 123 | |
| 124 | // New installs should have 0 hotfixes |
| 125 | if (sizeof($hotfixes) == 0) { |
| 126 | |
| 127 | // For new users there is no need to display the banner |
| 128 | update_option('bmi_gdrive_banner_dismissed', true); |
| 129 | return false; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } |