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