misc.php
305 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * File for handling the "Make a backup" banner |
| 6 | * |
| 7 | * @category Child Plugin |
| 8 | * @version v0.1.0 |
| 9 | * @since v0.1.0 |
| 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 Backup Banner |
| 20 | */ |
| 21 | if (!class_exists('Inisev\Subs\BMI_Backup_Banner')) { |
| 22 | class BMI_Backup_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 $name; // Display name |
| 31 | |
| 32 | /** |
| 33 | * Local URLs |
| 34 | */ |
| 35 | private $root_url; // Root URL for plugin's dir |
| 36 | private $assets_url; // Root URL for banner assets |
| 37 | private $plugin_menu_url; // Plugin's settings menu |
| 38 | private $option_name = '_bmi_backup_banner'; // Option name for this module |
| 39 | |
| 40 | public $expiring_at = null; // Expiration timestamp for the site, if available |
| 41 | |
| 42 | /** |
| 43 | * __construct: |
| 44 | * Compile some variables for "future use" |
| 45 | * |
| 46 | * @param string $root_file __FILE__ of plugin's main file |
| 47 | * @param string $root_dir __DIR__ of plugin's main file |
| 48 | * @param string $individual_slug Individual slug - mostly plugin's slug |
| 49 | * @param string $display_name The name that will be displayed in the banner |
| 50 | * @param string $plugin_menu_url Plugin menu slug |
| 51 | */ |
| 52 | function __construct($root_file, $root_dir, $individual_slug, $display_name, $plugin_menu_url) { |
| 53 | |
| 54 | $this->file = $root_file; |
| 55 | $this->root = $root_dir; |
| 56 | $this->slug = $individual_slug; |
| 57 | $this->name = $display_name; |
| 58 | |
| 59 | $this->plugin_menu_url = admin_url('admin.php?page=' . $plugin_menu_url); |
| 60 | |
| 61 | $this->root_url = plugin_dir_url($this->file); |
| 62 | $this->assets_url = $this->root_url . 'modules/backup-banner/assets/'; |
| 63 | |
| 64 | $option = get_option($this->option_name); |
| 65 | // Set the first time this code is loaded |
| 66 | if ($option === false) { |
| 67 | update_option($this->option_name, [ |
| 68 | 'dismissed' => false, |
| 69 | 'dismissed_at' => null, |
| 70 | 'first_loaded_at' => time() |
| 71 | ]); |
| 72 | } else if ($option['dismissed'] === false && (!isset($option['first_loaded_at']))) { |
| 73 | $option['first_loaded_at'] = time(); |
| 74 | update_option($this->option_name, $option); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | // Add handler for Ajax request |
| 79 | $request_method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper(sanitize_text_field(wp_unslash($_SERVER['REQUEST_METHOD']))) : ''; |
| 80 | if ('POST' === $request_method) { |
| 81 | |
| 82 | // Check if token is defined |
| 83 | if ( isset( $_POST['token'] ) ) { |
| 84 | $token = sanitize_text_field( wp_unslash( $_POST['token'] ) ); |
| 85 | if ( $token === 'bmi_backup_banner' ) { |
| 86 | |
| 87 | // Handle the request |
| 88 | add_action('wp_ajax_dismiss_bmi_backup_banner', [&$this, 'dismiss_banner']); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Stop for POST |
| 93 | return; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | add_action('wp_loaded', [&$this, 'init_backup_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 esc_url($this->assets_url . $file); |
| 134 | |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * can_be_displayed - check if the banner should be displayed |
| 139 | * |
| 140 | * @return bool true if banner can be displayed |
| 141 | */ |
| 142 | private function can_be_displayed() { |
| 143 | |
| 144 | $site_url = get_site_url(); |
| 145 | $option = get_option($this->option_name); |
| 146 | |
| 147 | if (isset($option['dismissed']) && $option['dismissed'] === true) { |
| 148 | if ($this->is_site_near_expiration()) { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | if (strpos($site_url, 'tastewp') === false || $option['first_loaded_at'] > time() - 5 * MINUTE_IN_SECONDS) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | if (!function_exists('is_plugin_active')) { |
| 160 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 161 | } |
| 162 | |
| 163 | if (!\is_plugin_active('backup-backup/backup-backup.php')) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * add_assets - adds required assets by the banner |
| 172 | * |
| 173 | * @return void |
| 174 | */ |
| 175 | public function add_assets() { |
| 176 | |
| 177 | if (!defined('BMI_BACKUP_BANNER_ASSETS_LOADED')) { |
| 178 | define('BMI_BACKUP_BANNER_ASSETS_LOADED', true); |
| 179 | wp_enqueue_script('bmi-backup-banner-script', $this->__asset('js/script.js'), [], filemtime($this->__dir_asset('js/script.js')), true); |
| 180 | wp_enqueue_style('bmi-backup-banner-style', $this->__asset('css/style.css'), [], filemtime($this->__dir_asset('css/style.css'))); |
| 181 | wp_localize_script('bmi-backup-banner-script', 'bmi_backup_banner', [ |
| 182 | 'dismiss_nonce' => wp_create_nonce('bmi_backup_banner_dismiss'), |
| 183 | 'plugin_url' => $this->plugin_menu_url, |
| 184 | 'expiring_at' => $this->expiring_at |
| 185 | ]); |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * display_banner - loads the HTML and prints it in the header only once |
| 192 | * |
| 193 | * @return void |
| 194 | */ |
| 195 | public function display_banner() { |
| 196 | |
| 197 | if (!defined('BMI_BACKUP_BANNER_HTML_LOADED')) { |
| 198 | define('BMI_BACKUP_BANNER_HTML_LOADED', true); |
| 199 | include_once __DIR__ . '/views/banner.php'; |
| 200 | } |
| 201 | |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * dismiss_banner - Handles all POST actions |
| 206 | * |
| 207 | * @return void returns JSON response to browser |
| 208 | */ |
| 209 | public function dismiss_banner() { |
| 210 | |
| 211 | if (check_ajax_referer('bmi_backup_banner_dismiss', 'nonce', false) === false) { |
| 212 | wp_send_json_error(); |
| 213 | } |
| 214 | |
| 215 | if (!current_user_can('manage_options')) { |
| 216 | wp_send_json_error(); |
| 217 | } |
| 218 | |
| 219 | if (isset($_POST['shouldRedirect'])) $shouldRedirect = sanitize_text_field($_POST['shouldRedirect']); |
| 220 | else $shouldRedirect = false; |
| 221 | |
| 222 | update_option($this->option_name, [ |
| 223 | 'dismissed' => true, |
| 224 | 'dismissed_at' => time(), |
| 225 | ]); |
| 226 | |
| 227 | if ($shouldRedirect == 'true') { |
| 228 | wp_send_json_success([ |
| 229 | 'redirect' => $this->plugin_menu_url |
| 230 | ]); |
| 231 | } else { |
| 232 | wp_send_json_success(); |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * init_backup_banner - initialization when the user is authenticated already |
| 239 | * |
| 240 | * @return void |
| 241 | */ |
| 242 | public function init_backup_banner() { |
| 243 | |
| 244 | if ($this->can_be_displayed()) { |
| 245 | add_action('admin_enqueue_scripts', [&$this, 'add_assets']); |
| 246 | add_action('admin_notices', [&$this, 'display_banner']); |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Check if site is near expiration. |
| 253 | * |
| 254 | * Checks if the site is expiring in less than 5 hours. |
| 255 | * |
| 256 | * @return bool True if the site is expiring in less than 5 hours, false otherwise. |
| 257 | */ |
| 258 | public function is_site_near_expiration() { |
| 259 | |
| 260 | $site_url = get_site_url(); |
| 261 | if (strpos($site_url, 'tastewp') === false) { |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | $api_url = 'https://tastewp.com/api/z'; |
| 266 | $headers = [ |
| 267 | 'Origin' => home_url(), |
| 268 | 'Referer' => home_url(), |
| 269 | 'Accept' => 'application/json' |
| 270 | ]; |
| 271 | |
| 272 | $response = wp_remote_get($api_url, [ |
| 273 | 'headers' => $headers, |
| 274 | 'timeout' => 5 |
| 275 | ]); |
| 276 | |
| 277 | if (is_wp_error($response)) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | if (wp_remote_retrieve_response_code($response) !== 200) { |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | $body = wp_remote_retrieve_body($response); |
| 286 | $data = json_decode($body, true); |
| 287 | if (!is_array($data) || !isset($data['e'])) { |
| 288 | return false; |
| 289 | } |
| 290 | $expiration_timestamp = strtotime($data['e']); |
| 291 | if ($expiration_timestamp === false) { |
| 292 | return false; |
| 293 | } |
| 294 | $time_until_expiration = $expiration_timestamp - time(); |
| 295 | if ($time_until_expiration >= 0 && $time_until_expiration <= 5 * HOUR_IN_SECONDS) { |
| 296 | $this->expiring_at = $expiration_timestamp; |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | } |
| 305 |