PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / team-updraft / common-libs / src / updraft-notices / updraft-notices.php

updraft-notices.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at vendor/team-updraft/common-libs/src/updraft-notices/updraft-notices.php

279 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access allowed');
4
5 abstract class Updraft_Notices_1_3 {
6
7 protected $widget_title;
8
9 protected $notices_content;
10
11 // These variables are just short-hands to be used in advert content.
12 protected $dashboard_top = array('top');
13
14 protected $dashboard_top_or_report = array('top', 'report', 'report-plain');
15
16 protected $dashboard_top_or_report_or_widget = array('top', 'report', 'report-plain', 'widget');
17
18 protected $dashboard_bottom_or_report = array('bottom', 'report', 'report-plain');
19
20 protected $anywhere = array('top', 'bottom', 'report', 'report-plain');
21
22 protected $autobackup = array('autobackup');
23
24 protected $autobackup_bottom_or_report = array('autobackup', 'bottom', 'report', 'report-plain');
25
26 /**
27 * Global adverts that appear in all products will be returned to the child to display
28 *
29 * @return array
30 */
31 protected function populate_notices_content() {
32 return array();
33 }
34
35 /**
36 * Call this method to setup the notices.
37 */
38 abstract protected function notices_init();
39
40 /**
41 * Checks if the plugin is installed and checks status if needed.
42 *
43 * @param null $product - Plugin to check
44 * @param boolean $also_require_active - bool to indicate if active status is required or not
45 *
46 * @return boolean Returns true, if plugin is installed otherwise false
47 */
48 protected function is_plugin_installed($product = null, $also_require_active = false) {
49 if ($also_require_active) return class_exists($product);
50 if (!function_exists('get_plugins')) include_once(ABSPATH.'wp-admin/includes/plugin.php');
51 $plugins = get_plugins();
52 foreach ($plugins as $value) {
53 if ($value['TextDomain'] == $product) {
54 // We have found the plugin so return false so that we do not display this advert.
55 return false;
56 }
57 }
58 return true;
59 }
60
61 /**
62 * Checks if translation is needed or not
63 *
64 * @param string $plugin_base_dir - Base directory of plugin
65 * @param string $product_name - Plugin name
66 *
67 * @return boolean Returns true if translation is needed, otherwise false
68 */
69 protected function translation_needed($plugin_base_dir, $product_name) {
70 $wplang = get_locale();
71 if (strlen($wplang) < 1 || 'en_US' == $wplang || 'en_GB' == $wplang) return false;
72 if (defined('WP_LANG_DIR') && is_file(WP_LANG_DIR.'/plugins/'.$product_name.'-'.$wplang.'.mo')) return false;
73 if (is_file($plugin_base_dir.'/languages/'.$product_name.'-'.$wplang.'.mo')) return false;
74 return true;
75 }
76
77 /**
78 * Generates the start of a HTML URL
79 *
80 * @param boolean $html_allowed - indicates if HTML is allowed or not
81 * @param string $url - the URL
82 * @param boolean $https - the protocol to use
83 * @param string $website_home - the product website name
84 *
85 * @return string returns a partial HTML URL
86 */
87 protected function url_start($html_allowed, $url, $https = false, $website_home = null) {
88 $proto = ($https) ? 'https' : 'http';
89 if (strpos($url, $website_home) !== false) {
90 return $html_allowed ? "<a href=".apply_filters(str_replace('.', '_', $website_home).'_link', $proto.'://'.$url).'>' : '';
91 } else {
92 return $html_allowed ? '<a href="'.$proto.'://'.$url.'">' : '';
93 }
94 }
95
96 /**
97 * Generate the end of a HTML URL
98 *
99 * @param boolean $html_allowed - indicates if HTML is allowed or not
100 * @param string $url - the URL
101 * @param boolean $https - the protocol to use
102 *
103 * @return string returns a partial HTML URL
104 */
105 protected function url_end($html_allowed, $url, $https = false) {
106 $proto = $https ? 'https' : 'http';
107 return $html_allowed ? '</a>' : ' ('.$proto.'://'.$url.')';
108 }
109
110 /**
111 * Renders notice
112 *
113 * @param mixed $notice - a specific notice to render or false
114 * @param string $position - position of the notice
115 * @param boolean $return_instead_of_echo - indicates if we should echo notice or return as string
116 *
117 * @return mixed Returns string or echos notice
118 */
119 public function do_notice($notice = false, $position = 'top', $return_instead_of_echo = false) {
120
121 $this->notices_init();
122
123 if (false === $notice) $notice = apply_filters('updraft_notices_force_id', false, $this);
124
125 $notice_content = $this->get_notice_data($notice, $position);
126
127 if (false != $notice_content) {
128 return $this->render_specified_notice($notice_content, $return_instead_of_echo, $position);
129 }
130 }
131
132 /**
133 * This method will return a notice ready for display.
134 *
135 * @param boolean $notice - a specific notice to render or false
136 * @param string $position - position of the notice
137 *
138 * @return array returns notice data
139 */
140 protected function get_notice_data($notice = false, $position = 'top') {
141
142 // If a specific notice has been passed to this method then return that notice.
143 if ($notice) {
144 if (!isset($this->notices_content[$notice])) return false;
145
146 // Does the notice support the position specified?
147 if (isset($this->notices_content[$notice]['supported_positions']) && !in_array($position, $this->notices_content[$notice]['supported_positions'])) return false;
148
149 // First check if the advert passed can be displayed and hasn't been dismissed, we do this by checking what dismissed value we should be checking.
150 $dismiss_time = $this->notices_content[$notice]['dismiss_time'];
151
152 $dismiss = $this->check_notice_dismissed($dismiss_time);
153
154 if ($dismiss) return false;
155
156 // If the advert has a validity function, then require the advert to be valid
157 if (!empty($this->notices_content[$notice]['validity_function']) && !call_user_func(array($this, $this->notices_content[$notice]['validity_function']))) return false;
158
159 return $this->notices_content[$notice];
160 }
161
162 // Create an array to add non-seasonal adverts to so that if a seasonal advert can't be returned we can choose a random advert from this array.
163 $available_notices = array();
164
165 // If Advert wasn't passed then next we should check to see if a seasonal advert can be returned.
166 foreach ($this->notices_content as $notice_id => $notice_data) {
167 // Does the notice support the position specified?
168 if (isset($this->notices_content[$notice_id]['supported_positions']) && !in_array($position, $this->notices_content[$notice_id]['supported_positions'])) continue;
169
170 // If the advert has a validity function, then require the advert to be valid.
171 if (!empty($notice_data['validity_function']) && !call_user_func(array($this, $notice_data['validity_function']))) continue;
172
173
174 if (isset($notice_data['valid_from']) && isset($notice_data['valid_to'])) {
175 if ($this->skip_seasonal_notices($notice_data)) return $notice_data;
176 } else {
177 $dismiss_time = $this->notices_content[$notice_id]['dismiss_time'];
178 $dismiss = $this->check_notice_dismissed($dismiss_time);
179
180 if (!$dismiss) $available_notices[$notice_id] = $notice_data;
181 }
182 }
183
184 if (empty($available_notices)) return false;
185
186 // If a seasonal advert can't be returned then we will return a random advert.
187
188 // Here we give a 25% chance for the rate advert to be returned before selecting a random advert from the entire collection which also includes the rate advert
189 if (0 == rand(0, 3) && isset($available_notices['rate'])) return $available_notices['rate'];
190
191 // Using shuffle here as something like rand which produces a random number and uses that as the array index fails, this is because in future an advert may not be numbered and could have a string as its key which will then cause errors.
192 shuffle($available_notices);
193 return $available_notices[0];
194 }
195
196 /**
197 * Skip seasonal notices
198 *
199 * @param array $notice_data - an array of data for the chosen notice
200 *
201 * @return boolean
202 */
203 protected function skip_seasonal_notices($notice_data) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
204 return false;
205 }
206
207 /**
208 * Returns affiliate ID
209 *
210 * @return mixed Returns affiliate ID
211 */
212 public function get_affiliate_id() {
213 return $this->self_affiliate_id;
214 }
215
216 /**
217 * Checks if the notice has been dismissed
218 *
219 * @param string $dismiss_time - dismiss time
220 *
221 * @return mixed
222 */
223 abstract protected function check_notice_dismissed($dismiss_time);
224
225 /**
226 * This function will add the seasonal notice WordPress dashboard widget.
227 *
228 * @return void
229 */
230 public function seasonal_widget() {
231 global $wp_meta_boxes;
232
233 $notice = $this->do_notice(false, 'widget', true);
234 if (!$notice) return;
235
236 $widget_id = 'updraft-seasonal-widget';
237
238 /**
239 * We only want to display one seasonal notice across all our plugins,
240 * so here we have a random chance to allow this plugin to replace the existing notice with it's own
241 */
242 $random_number = rand(1, 50);
243
244 if (isset($wp_meta_boxes['dashboard']['normal']['core'][$widget_id]) && 25 >= $random_number) return;
245
246 if (isset($wp_meta_boxes['dashboard']['normal']['core'][$widget_id])) {
247 unset($wp_meta_boxes['dashboard']['normal']['core'][$widget_id]);
248 }
249
250 $this->widget_enqueue();
251
252 if (!isset($wp_meta_boxes['dashboard']['normal']['core'][$widget_id])) {
253 // Initialization of the WordPress dashboard widget.
254 wp_add_dashboard_widget($widget_id, $this->widget_title, array($this, 'widget_view'), null, compact('notice'));
255 }
256
257 }
258
259 /**
260 * This function will add the dashboard widget output.
261 *
262 * @param Int $post post ID
263 * @param Array $callback_args callback args
264 *
265 * @return void
266 */
267 public function widget_view($post, $callback_args) {
268 extract($callback_args['args']);
269 echo $notice; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
270 }
271
272 /**
273 * This function will enqueue the WordPress dashboard widget styles or scripts.
274 *
275 * @return void
276 */
277 abstract protected function widget_enqueue();
278 }
279