misc.php
264 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * File for handling new BB banner |
| 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 new BB banner |
| 20 | */ |
| 21 | if (!class_exists('Inisev\Subs\New_BB_Banner')) { |
| 22 | class New_BB_Banner { |
| 23 | |
| 24 | /** |
| 25 | * Local variables |
| 26 | */ |
| 27 | private $root; // __ROOT__ of plugin's root |
| 28 | private $file; // __FILE__ of plugin's root |
| 29 | private $slug; // Plugin's slug |
| 30 | private $is_bmi_exists = false; // BMI plugin exists |
| 31 | private $name; |
| 32 | |
| 33 | /** |
| 34 | * Local URLs |
| 35 | */ |
| 36 | private $root_url; // Root URL for plugin's dir |
| 37 | private $assets_url; // Root URL for banner assets |
| 38 | private $plugin_menu_url; // Plugin's settings menu |
| 39 | public $option_name = '_new_bb_banner'; // Option name for this module |
| 40 | public $using_since; // Check since user uses this plugin |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * __construct: |
| 45 | * Compile some variables for "future use" |
| 46 | * Such as slug of current plugin, root dir of plugin |
| 47 | * |
| 48 | * @param string $root_file __FILE__ of plugin's main file |
| 49 | * @param string $root_dir __DIR__ of plugin's main file |
| 50 | * @param string $individual_slug Individual slug - mostly plugin's slug |
| 51 | * @param string $display_name The name that will be displayed in the banner |
| 52 | * @param string $plugin_menu_url Plugin menu slug example.com/wp-admin/admin.php?page=<this slug here> |
| 53 | */ |
| 54 | function __construct($root_file, $root_dir, $individual_slug, $display_name, $plugin_menu_url) { |
| 55 | |
| 56 | $this->file = $root_file; |
| 57 | $this->root = $root_dir; |
| 58 | $this->slug = $individual_slug; |
| 59 | $this->name = $display_name; |
| 60 | |
| 61 | $this->plugin_menu_url = admin_url('admin.php?page=' . $plugin_menu_url); |
| 62 | |
| 63 | $this->root_url = plugin_dir_url($this->file); |
| 64 | $this->assets_url = $this->root_url . 'modules/new-bb-banner/assets/'; |
| 65 | $this->is_bmi_exists = is_dir(WP_PLUGIN_DIR . '/backup-backup') || is_dir(WP_PLUGIN_DIR . '/backup-backup-pro'); |
| 66 | |
| 67 | |
| 68 | $time = time(); |
| 69 | $option = get_option($this->option_name, [ |
| 70 | 'dismissed' => false, |
| 71 | 'dismissed_at' => 0, |
| 72 | 'using_since' => $time |
| 73 | ]); |
| 74 | |
| 75 | if ($option['using_since'] == $time) { |
| 76 | update_option($this->option_name, $option); |
| 77 | } |
| 78 | |
| 79 | $this->using_since = $option['using_since']; |
| 80 | |
| 81 | |
| 82 | // Add handler for Ajax request |
| 83 | if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') { |
| 84 | |
| 85 | // Check if slug is defined |
| 86 | if (isset($_POST['token']) && $_POST['token'] === 'new_bb_banner') { |
| 87 | |
| 88 | // Handle the request |
| 89 | add_action('wp_ajax_dismiss_new_bb_banner', [&$this, 'dismiss_banner']); |
| 90 | } |
| 91 | |
| 92 | // Stop for POST |
| 93 | return; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | add_action('wp_loaded', [&$this, 'init_new_bb_banner']); |
| 98 | |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * __asset - Loads assets |
| 103 | * |
| 104 | * @param string $file path relative |
| 105 | * @return string file URL |
| 106 | */ |
| 107 | private function __asset($file) { |
| 108 | |
| 109 | return $this->assets_url . $file; |
| 110 | |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * __dir_asset - Loads assets |
| 115 | * |
| 116 | * @param string $file path relative |
| 117 | * @return string absolute path |
| 118 | */ |
| 119 | private function __dir_asset($file) { |
| 120 | |
| 121 | return __DIR__ . '/assets' . '/' . $file; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * _asset - Loads assets and automatically echo |
| 127 | * |
| 128 | * @param string $file path relative |
| 129 | * @echo string file URL |
| 130 | */ |
| 131 | private function _asset($file) { |
| 132 | |
| 133 | echo $this->assets_url . $file; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * _dir_asset - Loads assets and automatically echo |
| 139 | * |
| 140 | * @param string $file path relative |
| 141 | * @echo string absolute path |
| 142 | */ |
| 143 | private function _dir_asset($file) { |
| 144 | |
| 145 | echo __DIR__ . '/assets' . '/' . $file; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * can_be_displayed - check if the banner should be displayed |
| 151 | * |
| 152 | * @return bool true if banner can be displayed |
| 153 | */ |
| 154 | private function can_be_displayed() { |
| 155 | |
| 156 | $option = get_option($this->option_name, [ |
| 157 | 'dismissed' => false, |
| 158 | 'dismissed_at' => 0 |
| 159 | ]); |
| 160 | |
| 161 | if ($option['dismissed'] === true) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | $site_url = get_site_url(); |
| 166 | $site_url = str_replace('http://', '', $site_url); |
| 167 | $site_url = str_replace('https://', '', $site_url); |
| 168 | $site_url = str_replace('www.', '', $site_url); |
| 169 | |
| 170 | // IF site match regex then display |
| 171 | if (preg_match('/^[a-c]/i', $site_url)) { |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | if ($this->slug !== 'backup-backup'){ |
| 176 | // WILL BE USED IN OTHER PLUGINS |
| 177 | } |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * add_assets - adds required assests by the banner |
| 184 | * |
| 185 | * @return void |
| 186 | */ |
| 187 | public function add_assets() { |
| 188 | |
| 189 | wp_enqueue_script('new-bb-banner-script', $this->__asset('js/script.js'), [], filemtime($this->__dir_asset('js/script.js')), true); |
| 190 | wp_enqueue_style('new-bb-banner-style', $this->__asset('css/style.css'), [], filemtime($this->__dir_asset('css/style.css'))); |
| 191 | wp_localize_script('new-bb-banner-script', 'new_bb_banner', [ |
| 192 | 'dismiss_nonce' => wp_create_nonce('new_bb_banner_dismiss'), |
| 193 | 'is_backup_pro_exists' => is_dir(WP_PLUGIN_DIR . '/backup-backup-pro'), |
| 194 | // 'is_backup_pro_exists' => false, // For Testing Purpose |
| 195 | 'current_plugin' => $this->slug, // Will be used for future use |
| 196 | 'is_bmi_exists' => $this->is_bmi_exists |
| 197 | ]); |
| 198 | |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * display_banner - loads the HTML and prints it in the header only once |
| 203 | * |
| 204 | * @return void |
| 205 | */ |
| 206 | public function display_banner() { |
| 207 | |
| 208 | if (!defined('NEW_BB_BANNER_H_HTML_LOADED')) { |
| 209 | define('NEW_BB_BANNER_H_HTML_LOADED', true); |
| 210 | include_once __DIR__ . '/views/banner.php'; |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * dismiss_banner - Handles all POST actions |
| 217 | * |
| 218 | * @param string $_POST['slug'] - the unique slug |
| 219 | * @param string $_POST['mode'] - the unique action remind/dismiss |
| 220 | * |
| 221 | * @return void returns JSON response to browser |
| 222 | */ |
| 223 | public function dismiss_banner() { |
| 224 | if (check_ajax_referer('new_bb_banner_dismiss', 'nonce', false) === false) { |
| 225 | wp_send_json_error(); |
| 226 | } |
| 227 | |
| 228 | if (isset($_POST['shouldRedirectToBMI'])) $shouldRedirectToBMI = sanitize_text_field($_POST['shouldRedirectToBMI']); |
| 229 | else $shouldRedirectToBMI = false; |
| 230 | |
| 231 | update_option($this->option_name, [ |
| 232 | 'dismissed' => true, |
| 233 | 'dismissed_at' => time(), |
| 234 | 'using_since' => $this->using_since |
| 235 | ]); |
| 236 | |
| 237 | if ($shouldRedirectToBMI == 'true') { |
| 238 | wp_send_json_success([ |
| 239 | 'redirect' => $this->plugin_menu_url |
| 240 | ]); |
| 241 | } else { |
| 242 | wp_send_json_success(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * init_new_bb_banner - initialization when the user is authenticated already |
| 248 | * |
| 249 | * @return void |
| 250 | */ |
| 251 | public function init_new_bb_banner() { |
| 252 | |
| 253 | if ($this->can_be_displayed()) { |
| 254 | add_action('admin_enqueue_scripts', [&$this, 'add_assets']); |
| 255 | add_action('admin_notices', [&$this, 'display_banner']); |
| 256 | } |
| 257 | |
| 258 | } |
| 259 | |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | } |
| 264 |