PluginProbe
Advanced Ads – Ad Manager & AdSense / 1.6
Advanced Ads – Ad Manager & AdSense v1.6
2.0.26 2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 All 348 releases
advanced-ads / admin / includes / class-notices.php
class-notices.php
395 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * container class for admin notices
5 *
6 * @package WordPress
7 * @subpackage Advanced Ads Plugin
8 * @since 1.4.5
9 */
10 class Advanced_Ads_Admin_Notices {
11
12 /**
13 * maximum number of notices to show at once
14 */
15 const MAX_NOTICES = 2;
16
17 /**
18 * instance of this class.
19 *
20 * @since 1.5.3
21 * @var object
22 */
23 protected static $instance = null;
24
25 /**
26 * options
27 *
28 * @since 1.5.3
29 * @var array
30 */
31 protected $options;
32
33 /**
34 * notices to be displayed
35 *
36 * @since 1.5.3
37 * @var array
38 */
39 protected $notices = array();
40
41 /**
42 * plugin class
43 */
44 private $plugin;
45
46 public function __construct() {
47 $this->plugin = Advanced_Ads_Plugin::get_instance();
48 // load notices
49 $this->load_notices();
50 // display notices
51 $this->display_notices();
52
53 add_action( 'advanced-ads-ad-params-before', array( $this, 'adsense_tutorial' ), 10, 2 );
54 }
55
56 /**
57 * Return an instance of this class.
58 *
59 * @since 1.5.3
60 * @return object A single instance of this class.
61 */
62 public static function get_instance() {
63
64 // If the single instance hasn't been set, set it now.
65 if ( null == self::$instance ) {
66 self::$instance = new self;
67 }
68
69 return self::$instance;
70 }
71
72 /**
73 * load admin notices
74 *
75 * @since 1.4.5
76 * @updated 1.5.3 moved from admin class here
77 */
78 public function load_notices() {
79
80 $options = $this->options();
81 $plugin_options = $this->plugin->options();
82
83 // load notices from queue
84 $this->notices = isset($options['queue']) ? $options['queue'] : array();
85 $notices_before = $this->notices;
86
87 // handle version notices
88 $this->register_version_notices();
89
90 // don’t check non-critical notices if they are disabled
91 if ( ! isset($plugin_options['disable-notices']) ) {
92 // check other notices
93 $this->check_notices();
94 }
95
96 // register notices in db so they get displayed until closed for good
97 if ( $this->notices !== $notices_before ) {
98 $this->add_to_queue( $this->notices );
99 }
100 }
101
102 /**
103 * register update notices
104 *
105 */
106 public function register_version_notices() {
107 $internal_options = $this->plugin->internal_options();
108 $new_options = $internal_options; // in case we udpate options here
109 $plugin_options = $this->plugin->options();
110
111 // set an artifical older version for updates on installed plugins before the notice logic was invented
112 if ( ! isset($internal_options['version']) && $plugin_options !== array() ) {
113 $old_version = '1.4.4';
114 } elseif ( isset($internal_options['version']) ) {
115 $old_version = $internal_options['version'];
116 } else {
117 // empty version for new installations
118 $old_version = 0;
119 }
120
121 if ( isset($internal_options['version']) && ($internal_options['version'] !== ADVADS_VERSION) && $old_version ) {
122 if ( version_compare( $old_version, '1.4.5' ) == -1 ) {
123 $this->notices[] = '1.4.5';
124 }
125 if ( version_compare( $old_version, '1.5.4' ) == -1 ) {
126 $this->notices[] = '1.5.4';
127 }
128 if ( version_compare( $old_version, '1.6' ) == -1 ) {
129 $this->notices[] = '1.6';
130 }
131 }
132 $new_options['version'] = ADVADS_VERSION;
133
134 // update version numbers
135 if ( $internal_options !== $new_options ) {
136 $this->plugin->update_internal_options( $new_options );
137 }
138 }
139
140 /**
141 * check various notices conditions
142 */
143 public function check_notices() {
144 $internal_options = $this->plugin->internal_options();
145 $now = time();
146 $activation = (isset($internal_options['installed'])) ? $internal_options['installed'] : $now; // activation time
147
148 $options = $this->options();
149 $closed = isset($options['closed']) ? $options['closed'] : array();
150 $queue = isset($options['queue']) ? $options['queue'] : array();
151
152 // offer email tutorial right after activation
153 if ( ! $this->is_subscribed() && ( ! in_array( 'nl_first_steps', $queue ) && $activation < $now && ! isset($closed['nl_first_steps'])) ) {
154 $this->notices[] = 'nl_first_steps';
155 }
156 }
157
158 /**
159 * add update notices to the queue of all notices that still needs to be closed
160 *
161 * @since 1.5.3
162 * @param str|arr $notices one or more notices to be added to the queue
163 */
164 public function add_to_queue($notices = 0) {
165 if ( ! $notices ) {
166 return;
167 }
168
169 // get queue from options
170 $options = $this->options();
171 $queue = isset($options['queue']) ? $options['queue'] : array();
172
173 if ( is_array( $notices ) ) {
174 $queue = array_merge( $queue, $notices );
175 } else {
176 $queue[] = $notices;
177 }
178
179 // remove possible duplicated
180 $queue = array_unique( $queue );
181
182 // update db
183 $options['queue'] = $queue;
184 $this->update_options( $options );
185 }
186
187 /**
188 * remove update notice from queue
189 * move notice into "closed"
190 *
191 * @since 1.5.3
192 * @param str $notice notice to be removed from the queue
193 */
194 public function remove_from_queue($notice) {
195 if ( ! isset($notice) ) {
196 return;
197 }
198
199 // get queue from options
200 $options = $this->options();
201 if ( ! isset($options['queue']) ) {
202 return;
203 }
204 $queue = (array) $options['queue'];
205 $closed = isset($options['closed']) ? $options['closed'] : array();
206
207 $key = array_search( $notice, $queue );
208 if ( $key !== false ) {
209 unset($queue[$key]);
210 // close message with timestamp
211 }
212 $closed[$notice] = time();
213
214 // update db
215 $options['queue'] = $queue;
216 $options['closed'] = $closed;
217 $this->update_options( $options );
218 }
219
220 /**
221 *
222 * display notices
223 *
224 */
225 public function display_notices() {
226
227 if ( defined( 'DOING_AJAX' ) ) {
228 return; }
229
230 if ( $this->notices === array() ) {
231 return; }
232
233 // load notices
234 include ADVADS_BASE_PATH . '/admin/includes/notices.php';
235
236 // iterate through notices
237 $count = 0;
238 foreach ( $this->notices as $_notice ) {
239
240 if ( isset($advanced_ads_admin_notices[$_notice]) ) {
241 $notice = $advanced_ads_admin_notices[$_notice];
242 $text = $advanced_ads_admin_notices[$_notice]['text'];
243 $type = isset($advanced_ads_admin_notices[$_notice]['type']) ? $advanced_ads_admin_notices[$_notice]['type'] : '';
244 } else {
245 continue;
246 }
247
248 switch ( $type ) {
249 case 'subscribe' :
250 include ADVADS_BASE_PATH . '/admin/views/notices/subscribe.php';
251 break;
252 default :
253 include ADVADS_BASE_PATH . '/admin/views/notices/error.php';
254 }
255
256 if( ++$count == self::MAX_NOTICES ) {
257 break;
258 }
259 }
260 }
261
262 /**
263 * return notices options
264 *
265 * @since 1.5.3
266 * @return array $options
267 */
268 public function options() {
269 if ( ! isset($this->options) ) {
270 $this->options = get_option( ADVADS_SLUG . '-notices', array() );
271 }
272
273 return $this->options;
274 }
275
276 /**
277 * update notices options
278 *
279 * @since 1.5.3
280 * @param array $options new options
281 */
282 public function update_options(array $options) {
283 // do not allow to clear options
284 if ( $options === array() ) {
285 return;
286 }
287
288 $this->options = $options;
289 update_option( ADVADS_SLUG . '-notices', $options );
290 }
291
292 /**
293 * subscribe to newsletter and autoresponder
294 *
295 * @since 1.5.3
296 * @param string $notice slug of the subscription notice to send the correct reply
297 */
298 public function subscribe($notice) {
299 if ( ! isset($notice) ) {
300 return;
301 }
302
303 global $current_user;
304 $user = wp_get_current_user();
305
306 if ( $user->user_email == '' ) {
307 return sprintf( __( 'You don’t seem to have an email address. Please add one to your <a href="%s">WordPress profile</a>', ADVADS_SLUG ), get_edit_user_link() );
308 }
309
310 $data = array(
311 'email' => $user->user_email,
312 'notice' => $notice
313 );
314
315 $result = wp_remote_post('https://wpadvancedads.com/remote/subscribe.php?source=plugin', array(
316 'method' => 'POST',
317 'timeout' => 20,
318 'redirection' => 5,
319 'httpversion' => '1.1',
320 'blocking' => true,
321 'body' => $data)
322 );
323
324 if ( is_wp_error( $result ) ) {
325 return __( 'How embarrassing. The email server seems to be down. Please try again later.', ADVADS_SLUG );
326 } else {
327 // mark as subscribed and move notice from quere
328 $this->mark_as_subscribed();
329 $this->remove_from_queue( $notice );
330 return true;
331 }
332 }
333
334 /**
335 * check if blog is subscribed to the newsletter
336 */
337 public function is_subscribed() {
338
339 /**
340 * respect previous settings
341 */
342 $options = $this->options();
343 if ( isset($options['is_subscribed'] ) ) {
344 return true;
345 }
346
347 $user_id = get_current_user_id();
348 if( ! $user_id ) {
349 return true;
350 }
351
352 $subscribed = get_user_meta($user_id, 'advanced-ads-subscribed', true);
353 return $subscribed;
354 }
355
356 /**
357 * update information that the current user is subscribed
358 */
359 private function mark_as_subscribed() {
360
361 $user_id = get_current_user_id();
362
363 if( ! $this->is_subscribed() ) {
364 update_user_meta( $user_id, 'advanced-ads-subscribed', true);
365 }
366 }
367
368 /**
369 * add AdSense tutorial notice
370 *
371 * @param obj $ad ad object
372 * @param arr $types ad types
373 */
374 public function adsense_tutorial( $ad, $types = array() ){
375
376 $options = $this->options();
377 $_notice = 'nl_adsense';
378
379 if ( $ad->type !== 'adsense' || isset($options['closed'][ $_notice ] ) ) {
380 return;
381 }
382
383 include ADVADS_BASE_PATH . '/admin/includes/notices.php';
384
385 if ( ! isset( $advanced_ads_admin_notices[ $_notice ] ) ) {
386 return;
387 }
388
389 $notice = $advanced_ads_admin_notices[ $_notice ];
390 $text = $notice['text'];
391 include ADVADS_BASE_PATH . '/admin/views/notices/inline.php';
392 }
393
394 }
395