PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents / 3.4.0
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents v3.4.0
4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / EmbedPress / Includes / Classes / EmbedPress_Notice.php
embedpress / EmbedPress / Includes / Classes Last commit date
Elementor_Enhancer.php 4 years ago EmbedPress_Core_Installer.php 6 years ago EmbedPress_Notice.php 4 years ago EmbedPress_Plugin_Usage_Tracker.php 4 years ago Feature_Enhancer.php 4 years ago Helper.php 4 years ago
EmbedPress_Notice.php
1026 lines
1 <?php
2 namespace EmbedPress\Includes\Classes;
3
4 if (!defined('ABSPATH')) {
5 exit;
6 } // Exit if accessed directly.
7
8 class EmbedPress_Notice {
9 /**
10 * Admin Notice Key
11 *
12 * @var array
13 */
14 const ADMIN_UPDATE_NOTICE_KEY = 'wpdeveloper_notices_seen';
15 public $text_domain = 'embedpress';
16 /**
17 * All Data
18 * @var array
19 */
20 private $data = array();
21 private $properties = array(
22 'links', 'message', 'thumbnail',
23 );
24 private $methods = array(
25 'message', 'thumbnail', 'classes'
26 );
27 /**
28 * cne_day == current_notice_end_day
29 *
30 * @var integer
31 */
32 public $cne_time = '2 day';
33 public $maybe_later_time = '7 day';
34 public $finish_time = [];
35 /**
36 * Plugin Name
37 *
38 * @var string
39 */
40 private $plugin_name;
41 /**
42 * Plugin File Name
43 * @var string
44 */
45 private $plugin_file;
46 /**
47 * First Install Version Of The Plugin
48 *
49 * @var string
50 */
51 private $version;
52 /**
53 * Saved Data in DB
54 * @var array
55 */
56 private $options_data;
57 /**
58 * Current Timestamp
59 * @var integer
60 */
61 public $timestamp;
62 /**
63 * Primary Notice Action
64 *
65 * @var string
66 */
67 private $do_notice_action;
68 /**
69 * Default Options Set
70 *
71 * @var array
72 */
73 public $options_args = array();
74 /**
75 * Notice ID for users.
76 * @var string
77 */
78 private $notice_id;
79 /**
80 * Upsale Notice Arguments
81 * @var array
82 */
83 public $upsale_args;
84
85 /**
86 * Revoke this function when the object is created.
87 *
88 * @param string $plugin_file
89 * @param string $version
90 */
91 public function __construct( $plugin_file = '', $version = '' ) {
92 $this->plugin_file = $plugin_file;
93 $this->plugin_name = basename( $plugin_file, '.php' );
94 $this->version = $version;
95 $this->timestamp = intval( current_time( 'timestamp' ) );
96 $this->notice_id = 'wpdeveloper_notice_' . str_replace( '.', '_', $this->version );
97
98 $this->do_notice_action = 'wpdeveloper_notices_for_' . $this->plugin_name;
99 new EmbedPress_Core_Installer( $this->plugin_name );
100
101 }
102 /**
103 * Initiate The Plugin
104 * @return void
105 */
106 public function init(){
107 add_action( 'init', array( $this, 'first_install_track') );
108 add_action( 'deactivate_' . $this->plugin_file, array( $this, 'first_install_end' ) );
109 add_action( 'init', array( $this, 'hooks' ) );
110 }
111 /**
112 * All Hooks
113 * @return void
114 */
115 public function hooks(){
116 add_action( 'wpdeveloper_notice_clicked_for_' . $this->plugin_name, array( $this, 'clicked' ) );
117 add_action( 'wp_ajax_wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name, array( $this, 'upsale_notice_dissmiss' ) );
118 add_action( 'wp_ajax_wpdeveloper_notice_dissmiss_for_' . $this->plugin_name, array( $this, 'notice_dissmiss' ) );
119 add_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name, array( $this, 'before' ) );
120 add_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name, array( $this, 'after' ) );
121 add_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name, array( $this, 'before_upsale' ) );
122 add_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name, array( $this, 'after' ) );
123 add_action( $this->do_notice_action, array( $this, 'content' ) );
124 if( current_user_can( 'install_plugins' ) ) {
125 if( isset( $_GET['plugin'] ) && $_GET['plugin'] == $this->plugin_name ) {
126 if( isset( $_GET['tab'] ) && $_GET['tab'] === 'plugin-information' ) {
127 return;
128 }
129 do_action( 'wpdeveloper_notice_clicked_for_' . $this->plugin_name );
130 /**
131 * Redirect User To the Current URL, but without set query arguments.
132 */
133 wp_safe_redirect( $this->redirect_to() );
134 }
135 $return_notice = $this->next_notice();
136 $current_notice = current( $return_notice );
137 $next_notice = next( $return_notice );
138
139 $deserve_notice = $this->deserve_notice( $current_notice );
140 $options_data = $this->get_options_data();
141 $user_notices = $this->get_user_notices();
142
143 $notice_time = isset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] )
144 ? $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] : $this->timestamp;
145 $next_notice_time = $next_notice ? $options_data[ $this->plugin_name ]['notice_will_show'][ $next_notice ] : $this->timestamp;
146 $current_notice_end = $this->makeTime( $notice_time, $this->cne_time );
147
148 if( ! $deserve_notice ) {
149 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
150 $this->update_options_data( $options_data[ $this->plugin_name ] );
151 }
152
153 if( $deserve_notice ) {
154 /**
155 * TODO: automatic maybe later setup with time.
156 */
157 if( ( $this->timestamp >= $current_notice_end ) || ( $this->timestamp > $next_notice_time ) ) {
158 $this->maybe_later( $current_notice );
159 $notice_time = false;
160 }
161
162 if( isset( $this->finish_time[ $current_notice ] ) ) {
163 if( $this->timestamp >= strtotime( $this->finish_time[ $current_notice ] ) ) {
164 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
165 $this->update_options_data( $options_data[ $this->plugin_name ] );
166 $notice_time = false;
167 }
168 }
169
170 if( $notice_time != false ) {
171 if( $notice_time <= $this->timestamp ) {
172 if( $current_notice === 'upsale' ) {
173 $upsale_args = $this->get_upsale_args();
174 if( empty( $upsale_args ) ) {
175 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
176 $this->update_options_data( $options_data[ $this->plugin_name ] );
177 } else {
178 if( isset( $upsale_args['condition'], $upsale_args['condition']['by'] ) ) {
179 switch( $upsale_args['condition']['by'] ) {
180 case 'class' :
181 if( isset( $upsale_args['condition']['class'] ) && class_exists( $upsale_args['condition']['class'] ) ) {
182 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
183 $this->update_options_data( $options_data[ $this->plugin_name ] );
184 return;
185 }
186 break;
187 case 'function' :
188 if( isset( $upsale_args['condition']['function'] ) && function_exists( $upsale_args['condition']['function'] ) ) {
189 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
190 $this->update_options_data( $options_data[ $this->plugin_name ] );
191 return;
192 }
193 break;
194 }
195 }
196 if( isset( $upsale_args['slug'], $upsale_args['file'] ) ) {
197 if ( ! function_exists( 'get_plugins' ) ) {
198 include ABSPATH . '/wp-admin/includes/plugin.php';
199 }
200 $plugins = get_plugins();
201 $pkey = $upsale_args['slug'] . '/' . $upsale_args['file'];
202 if( isset( $plugins[ $pkey ] ) ) {
203 $this->update( $current_notice );
204 return;
205 }
206 }
207 add_action( 'admin_notices', array( $this, 'upsale_notice' ) );
208 }
209 } else {
210 add_action( 'admin_notices', array( $this, 'admin_notices' ) );
211 }
212 }
213 }
214 }
215 }
216 }
217 /**
218 * Make time using timestamp and a string like 2 Hour, 2 Day, 30 Minutes, 1 Week, 1 year
219 * @param integer $current
220 * @param string $time
221 * @return integer
222 */
223 public function makeTime( $current, $time ) {
224 return intval( strtotime( date('r', $current) . " +$time" ) );
225 }
226 /**
227 * Automatice Maybe Later.
228 * @param string $notice
229 * @return void
230 */
231 private function maybe_later( $notice ){
232 if( empty( $notice ) ) {
233 return;
234 }
235 $options_data = $this->get_options_data();
236 $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] = $this->makeTime( $this->timestamp, $this->maybe_later_time );
237 $this->update_options_data( $options_data[ $this->plugin_name ] );
238 }
239 /**
240 * When links are clicked, this function will invoked.
241 * @return void
242 */
243 public function clicked(){
244 if( isset( $_GET['plugin'] ) ) {
245 $plugin = sanitize_text_field( $_GET['plugin'] );
246 if( $plugin === $this->plugin_name ) {
247 $options_data = $this->get_options_data();
248 $clicked_from = current( $this->next_notice() );
249 if( isset( $_GET['plugin_action'] ) ) {
250 $plugin_action = sanitize_text_field( $_GET['plugin_action'] );
251 }
252 if( isset( $_GET['dismiss'] ) ) {
253 $dismiss = sanitize_text_field( $_GET['dismiss'] );
254 }
255 if( isset( $_GET['later'] ) ) {
256 $later = sanitize_text_field( $_GET['later'] );
257 }
258
259 $later_time = '';
260
261 switch( $clicked_from ) {
262
263 case 'opt_in' :
264 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
265 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
266 break;
267
268 case 'first_install' :
269 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
270 break;
271
272 case 'update' :
273 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
274 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
275 break;
276
277 case 'review' :
278 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
279 break;
280
281 case 'upsale' :
282 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
283 break;
284 }
285
286 if( isset( $later ) && $later == true ) {
287 $options_data[ $this->plugin_name ]['notice_will_show'][ $clicked_from ] = $later_time;
288 }
289 if( isset( $dismiss ) && $dismiss == true ) {
290 update_user_meta( get_current_user_id(), $this->plugin_name . '_' . $clicked_from, true );
291 $this->update( $clicked_from );
292 }
293 $this->update_options_data( $options_data[ $this->plugin_name ] );
294 }
295 }
296 }
297 /**
298 * For Redirecting Current Page without Arguments!
299 *
300 * @return void
301 */
302 private function redirect_to(){
303 $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
304 $query_string = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
305 parse_str( $query_string, $current_url );
306
307 $unset_array = array( 'dismiss', 'plugin', '_wpnonce', 'later', 'plugin_action', 'marketing_optin' );
308
309 foreach( $unset_array as $value ) {
310 if( isset( $current_url[ $value ] ) ) {
311 unset( $current_url[ $value ] );
312 }
313 }
314
315 $current_url = http_build_query($current_url);
316 $redirect_url = $request_uri . '?' . $current_url;
317 return $redirect_url;
318 }
319 /**
320 * Before Notice
321 * @return void
322 */
323 public function before(){
324 $current_notice = current( $this->next_notice() );
325 $classes = 'notice notice-info put-dismiss-notice';
326 if( isset( $this->data['classes'] ) ) {
327 if( isset( $this->data['classes'][ $current_notice ] ) ) {
328 $classes = $this->data['classes'][ $current_notice ];
329 }
330 }
331
332 if( $this->has_thumbnail( $current_notice ) ) {
333 $classes .= 'notice-has-thumbnail';
334 }
335
336 if( ! $this->has_notice( $current_notice ) ) {
337 return;
338 }
339
340 echo '<div data-notice="'. $current_notice .'" class="'. $classes .' wpdeveloper-'. $current_notice .'-notice">';
341 }
342 /**
343 * After Notice
344 * @return void
345 */
346 public function after(){
347 $current_notice = current( $this->next_notice() );
348 if( ! $this->has_notice( $current_notice ) ) {
349 return;
350 }
351 echo '</div>';
352 }
353 /**
354 * Content generation & Hooks Funciton.
355 * @return void
356 */
357 public function content(){
358 $options_data = $this->get_options_data();
359 $notice = current( $this->next_notice() );
360 ?>
361 <style>
362 .wpdeveloper-review-notice {
363 padding: 10px;
364 background-color: #fff;
365 border-radius: 3px;
366 margin: 15px;
367 border-left: 4px solid transparent;
368 display: -webkit-box;
369 display: -ms-flexbox;
370 display: flex;
371 -webkit-box-align: center;
372 -ms-flex-align: center;
373 align-items: center;
374 }
375 .wpdeveloper-review-notice:after {
376 content: "";
377 display: table;
378 clear: both;
379 }
380 .wpdeveloper-update-notice{
381 margin-top: 20px;
382 }
383 .wpdeveloper-update-notice .notice-dismiss {
384 top: auto;
385 }
386 .wpdeveloper-notice-thumbnail {
387 width: 40px;
388 float: left;
389 padding: 5px 5px 5px 10px;
390 text-align: center;
391 border-right: 4px solid transparent;
392 }
393 .wpdeveloper-notice-thumbnail img {
394 width: 100%;
395 opacity: 0.85;
396 -webkit-transition: all 0.3s;
397 -o-transition: all 0.3s;
398 transition: all 0.3s;
399 }
400 .wpdeveloper-notice-thumbnail img:hover {
401 opacity: 1;
402 }
403
404 .wpdeveloper-notice-link {
405 margin: 8px 0 0 0;
406 padding: 0;
407 }
408 .wpdeveloper-notice-link li {
409 display: inline-block;
410 margin-right: 15px;
411 }
412 .wpdeveloper-notice-link li a {
413 display: inline-block;
414 color: #10738b;
415 text-decoration: none;
416 padding-left: 26px;
417 position: relative;
418 }
419 .wpdeveloper-notice-link li a span {
420 position: absolute;
421 left: 0;
422 top: -2px;
423 }
424 .wpdeveloper-notice-message {
425 padding: 10px;
426 }
427 .wpdeveloper-upsale-notice .wpdeveloper-notice-message {
428 display: -webkit-box;
429 display: -ms-flexbox;
430 display: flex;
431 -webkit-box-align: center;
432 -ms-flex-align: center;
433 align-items: center;
434 padding: 10px 0;
435 }
436 .wpdeveloper-upsale-notice .wpdeveloper-notice-message + .notice-dismiss {
437 top: 10px;
438 }
439 .wpdeveloper-upsale-notice #plugin-install-core {
440 margin-left: 10px;
441 }
442 .notice.notice-has-thumbnail {
443 padding-left: 0;
444 display: flex;
445 align-items: center;
446 }
447 .wpdeveloper-upsale-notice {
448 display: -webkit-box;
449 display: -ms-flexbox;
450 display: flex;
451 }
452 .wpdeveloper-upsale-notice .wpdeveloper-notice-thumbnail {
453 padding: 10px;
454 width: 40px;
455 }
456 .wpdeveloper-upsale-notice .wpdeveloper-notice-thumbnail img {
457 width: 32px;
458 }
459 .toplevel_page_eael-settings .wp-menu-image img {
460 max-width: 20px;
461 padding-top: 8px !important;
462 }
463 .wpdeveloper-upsale-notice .wpdeveloper-notice-message .button {
464 margin-left: 15px;
465 }
466
467 #embedpress-settings-wrapper a.embedpress-settings-link {
468 color: #0073aa;
469 }
470
471 </style>
472 <?php
473 switch( $notice ) {
474 case 'opt_in' :
475 do_action('wpdeveloper_optin_notice_for_' . $this->plugin_name );
476 break;
477 case 'first_install' :
478 if( $options_data[ $this->plugin_name ]['first_install'] !== 'deactivated' ) {
479 do_action( 'wpdeveloper_first_install_notice_for_' . $this->plugin_name );
480 $this->get_thumbnail( 'first_install' );
481 $this->get_message( 'first_install' );
482 }
483 break;
484 case 'update' :
485 do_action( 'wpdeveloper_update_notice_for_' . $this->plugin_name );
486 $this->get_thumbnail( 'update' );
487 $this->get_message( 'update' );
488 $this->dismiss_button_scripts();
489 break;
490 case 'review' :
491 do_action( 'wpdeveloper_review_notice_for_' . $this->plugin_name );
492 $this->get_thumbnail( 'review' );
493 $this->get_message( 'review' );
494 break;
495 }
496 }
497 /**
498 * Before Upsale Notice
499 * @return void
500 */
501 public function before_upsale(){
502 $classes = '';
503 if( $this->has_thumbnail('upsale') ) {
504 $classes = 'notice-has-thumbnail';
505 }
506 echo '<div class="error notice is-dismissible wpdeveloper-upsale-notice '. $classes .'">';
507 }
508 /**
509 * Upsale Notice
510 */
511 public function upsale_notice(){
512 do_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name );
513 do_action('wpdeveloper_upsale_notice_for_' . $this->plugin_name);
514 $this->get_thumbnail( 'upsale' );
515 $this->get_message( 'upsale' );
516 do_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name );
517 $this->upsale_button_script();
518 }
519 /**
520 * Get upsale arguments.
521 * @return array
522 */
523 private function get_upsale_args(){
524 return ( empty( $this->upsale_args ) ) ? array() : $this->upsale_args;
525 }
526 /**
527 * This function is responsible for making the button visible to the upsale notice.
528 */
529 private function upsale_button(){
530 $upsale_args = $this->get_upsale_args();
531 if( isset( $upsale_args['href'] ) ) {
532 echo '<a href="'. $upsale_args['href'] .'" target="_blank" class="button button-primary">'. $upsale_args['btn_text'] .'</a>';
533 return;
534 }
535 $plugin_slug = ( isset( $upsale_args['slug'] )) ? $upsale_args['slug'] : '' ;
536 if( empty( $plugin_slug ) ) {
537 return;
538 }
539 echo '<button data-slug="'. $plugin_slug .'" id="plugin-install-core-'. $this->plugin_name .'" class="button button-primary">'. __( 'Install Now!', 'embedpress' ) .'</button>';
540 }
541 /**
542 * This methods is responsible for get notice image.
543 *
544 * @param string $msg_for
545 * @return void
546 */
547 protected function get_thumbnail( $msg_for ){
548 $output = '';
549 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
550 $output = '<div class="wpdeveloper-notice-thumbnail">';
551 $output .= '<img src="'. $this->data['thumbnail'][ $msg_for ] .'" alt="NotificationX">';
552 $output .= '</div>';
553 }
554 echo $output;
555 }
556 /**
557 * Has Thumbnail Check
558 *
559 * @param string $msg_for
560 * @return boolean
561 */
562 protected function has_thumbnail( $msg_for = '' ){
563 if( empty( $msg_for ) ) {
564 return false;
565 }
566 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
567 return true;
568 }
569 return false;
570 }
571 private function has_notice( $notice ){
572 return isset( $this->data['message'], $this->data['message'][ $notice ] );
573 }
574 /**
575 * This method is responsible for get messages.
576 *
577 * @param string $msg_for
578 * @return void
579 */
580 protected function get_message( $msg_for ){
581 if( isset( $this->data['message'] ) && isset( $this->data['message'][ $msg_for ] ) ) {
582 echo '<div class="wpdeveloper-notice-message">';
583 echo $this->data['message'][ $msg_for ];
584 if( $msg_for === 'upsale' ) {
585 $this->upsale_button();
586 }
587 $this->dismissible_notice( $msg_for );
588 echo '</div>';
589 }
590 }
591 /**
592 * Detect which notice will show @ next.
593 * @return void
594 */
595 protected function next_notice(){
596 $options_data = $this->get_options_data();
597 if( ! $options_data ) {
598 $args = $this->get_args();
599 $return_notice = $args['notice_will_show'];
600 } else {
601 $return_notice = $options_data[ $this->plugin_name ]['notice_will_show'];
602 }
603
604 if( is_array( $return_notice ) ) {
605 $return_notice = array_flip( $return_notice );
606 ksort( $return_notice );
607 }
608
609 return $return_notice;
610 }
611 /**
612 * Which notice is deserve to show in next slot.
613 * @param string $notice
614 * @return boolean
615 */
616 private function deserve_notice( $notice ) {
617 $notices = $this->get_user_notices();
618 if( $notice === false ) {
619 return false;
620 }
621 if( empty( $notices ) ) {
622 return true;
623 } else {
624 if( isset( $notices[ $this->notice_id ] ) && isset( $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
625 if( in_array( $notice, $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
626 return false;
627 } else {
628 return true;
629 }
630 } else {
631 return true;
632 }
633 }
634 }
635 /**
636 * This is the main methods for generate the notice.
637 * @return void
638 */
639 public function admin_notices(){
640 $current_notice = current( $this->next_notice() );
641 if( get_user_meta( get_current_user_id(), $this->plugin_name . '_' . $current_notice, true ) ) {
642 return;
643 }
644 if( $current_notice == 'opt_in' ) {
645 do_action( $this->do_notice_action );
646 return;
647 }
648 do_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name );
649 do_action( $this->do_notice_action );
650 do_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name );
651 }
652 /**
653 * This method is responsible for all dismissible links generation.
654 * @param string $links_for
655 * @return void
656 */
657 public function dismissible_notice( $links_for = '' ){
658 if( empty( $links_for ) ) {
659 return;
660 }
661 $links = isset( $this->data['links'][ $links_for ] ) ? $this->data['links'][ $links_for ] : false;
662 if( $links ) :
663 $output = '<ul class="wpdeveloper-notice-link">';
664 foreach( $links as $key => $link_value ) {
665 if( ! empty( $link_value['label'] ) ) {
666 $output .= '<li>';
667 if( isset( $link_value['link'] ) ) {
668 $link = $link_value['link'];
669 $target = isset( $link_value['target'] ) ? 'target="'. $link_value['target'] .'"' : '';
670 if( isset( $link_value['data_args'] ) && is_array( $link_value['data_args'] ) ) {
671 $data_args = [];
672 foreach( $link_value['data_args'] as $key => $args_value ) {
673 $data_args[ $key ] = $args_value;
674 }
675 $data_args[ 'plugin' ] = $this->plugin_name;
676 $normal_link = add_query_arg( $data_args, $link );
677 $link = wp_nonce_url( $normal_link, 'wpdeveloper-nonce' );
678 }
679 $class = '';
680 if( isset( $link_value['link_class'] ) ) {
681 $class = 'class="' . implode( ' ', $link_value['link_class'] ) . '"';
682 }
683 $output .= '<a '. $class .' href="'. esc_url( $link ) .'" '. $target .'>';
684 }
685 if( isset( $link_value['icon_class'] ) ) {
686 $output .= '<span class="'. $link_value['icon_class'] .'"></span>';
687 }
688 if( isset( $link_value['icon_img'] ) ) {
689 $output .= '<img src="'. $link_value['icon_img'] .'" />';
690 }
691 $output .= $link_value['label'];
692 if( isset( $link_value['link'] ) ) {
693 $output .= '</a>';
694 }
695 $output .= '</li>';
696 }
697 }
698 $output .= '</ul>';
699 echo $output;
700 endif;
701 }
702 /**
703 * First Installation Track
704 * @return void
705 */
706 public function first_install_track( $args = array() ){
707 if( ! current_user_can( 'manage_options' ) ) {
708 return;
709 }
710 if( empty( $args ) ) {
711 $args = array(
712 'time' => $this->timestamp,
713 'version' => $this->version,
714 );
715 }
716 $options_data = $this->get_options_data();
717 $args = wp_parse_args( $args, $this->get_args() );
718 if( ! isset( $options_data[ $this->plugin_name ] )
719 || ( isset( $options_data[ $this->plugin_name ]['version'] ) && version_compare( $options_data[ $this->plugin_name ]['version'], $this->version, '!=' ) ) ) {
720 $this->update_options_data( $args );
721 }
722 }
723 /**
724 * First Installation Deactive Track
725 *
726 * @return void
727 */
728 public function first_install_end(){
729 $options_data = $this->get_options_data();
730 if( isset( $options_data[ $this->plugin_name ] ) ) {
731 unset( $options_data[ $this->plugin_name ] );
732 $this->update_options_data( $options_data, true );
733 }
734 }
735 /**
736 * Get all options from database!
737 * @return bool|array
738 */
739 protected function get_options_data( $key = ''){
740 $options_data = get_option( 'wpdeveloper_plugins_data' );
741 if( empty( $key ) ) {
742 return $options_data;
743 }
744
745 if( isset( $options_data[ $this->plugin_name ][ $key ] ) ) {
746 return $options_data[ $this->plugin_name ][ $key ];
747 }
748 return false;
749 }
750
751 /**
752 * This will update the options table for plugins.
753 *
754 * @param array $args
755 * @param bool $update
756 *
757 * @return void
758 */
759 protected function update_options_data( $args = array(), $update = false ){
760 if( $update ) {
761 $options_data = $args;
762 } else {
763 $options_data = $this->get_options_data();
764 $options_data[ $this->plugin_name ] = $args;
765 }
766 update_option( 'wpdeveloper_plugins_data', $options_data );
767 }
768 /**
769 * Set properties data, for some selected properties.
770 *
771 * @param string $name
772 * @param mixed $value
773 */
774 public function __set( $name, $value ){
775 if( in_array( $name, $this->properties ) ) {
776 $this->data[ $name ] = $value;
777 }
778 }
779 /**
780 * Invoked when some selected methods are called
781 *
782 * @param string $name
783 * @param array $values
784 * @return void
785 */
786 public function __call( $name, $values ){
787 if( in_array( $name, $this->methods ) ) {
788 $this->data[ $name ][ $values[0] ] = $values[1];
789 }
790 }
791 /**
792 * Get all option arguments.
793 * @param string $key
794 * @return array
795 */
796 private function get_args( $key = '' ){
797 if( empty( $key ) ) {
798 return $this->options_args;
799 }
800
801 if( isset( $this->options_args[ $key ] ) ) {
802 return $this->options_args[ $key ];
803 }
804
805 return false;
806 }
807 /**
808 * Resetting data on update.
809 * @return void
810 */
811 private function set_args_on_update(){
812 $args = $this->get_args();
813 $options_data = $this->get_options_data();
814 $set_data = $options_data[ $this->plugin_name ];
815 $args = wp_parse_args( $set_data, $args );
816 $this->update_options_data( $args );
817 }
818 /**
819 * When upgrade is complete. it will fired.
820 * @param WP_Upgrader $upgrader_object
821 * @param array $options
822 * @return void
823 */
824 public function upgrade_completed( $upgrader_object, $options ) {
825 // If an update has taken place and the updated type is plugins and the plugins element exists
826 if( isset( $options['action'] ) && $options['action'] == 'update' && $options['type'] == 'plugin' ) {
827 if( ! isset( $options['plugin'] ) && isset( $options['plugins'] ) ) {
828 foreach( $options['plugins'] as $plugin ) {
829 if( $plugin == $this->plugin_name ) {
830 $this->set_args_on_update();
831 }
832 }
833 }
834
835 if( isset( $options['plugin'] ) && $options['plugin'] == $this->plugin_name ) {
836 $this->set_args_on_update();
837 }
838 }
839 }
840 /**
841 * This function is responsible for get_user_notices
842 * @return void
843 */
844 private function get_user_notices() {
845 $notices = get_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, true );
846 return ! $notices ? array() : $notices;
847 }
848 /**
849 * This function is responsible for update meta information.
850 *
851 * @param string $notice
852 * @return void
853 */
854 private function update( $notice ){
855 if( empty( $notice ) ) {
856 return;
857 }
858 $options_data = $this->get_options_data();
859 $user_notices = $this->get_user_notices();
860 $user_notices[ $this->notice_id ][ $this->plugin_name ][] = $notice;
861 // Remove the upsale from notice_will_show field in options DB.
862 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] );
863 $this->update_options_data( $options_data[ $this->plugin_name ] );
864 // Set users meta, not to show again current_version notice.
865 update_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, $user_notices);
866 }
867
868 public function notice_dissmiss(){
869
870 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_notice_dissmiss' ) ) {
871 return;
872 }
873
874 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_notice_dissmiss_for_' . $this->plugin_name ) ) {
875 return;
876 }
877
878 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
879 $notice = isset( $_POST['notice'] ) ? $_POST['notice'] : false;
880 if( $dismiss ) {
881 $this->update( $notice );
882 echo 'success';
883 } else {
884 echo 'failed';
885 }
886 die();
887 }
888
889 /**
890 * This function is responsible for do action when
891 * the dismiss button clicked in upsale notice.
892 */
893 public function upsale_notice_dissmiss(){
894
895 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_upsale_notice_dissmiss' ) ) {
896 return;
897 }
898
899 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name ) ) {
900 return;
901 }
902
903 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
904 if( $dismiss ) {
905 $this->update( 'upsale' );
906 echo 'success';
907 } else {
908 echo 'failed';
909 }
910 die();
911 }
912
913 public function dismiss_button_scripts(){
914 ?>
915 <script type="text/javascript">
916 jQuery(document).ready( function($) {
917 if( $('.notice').length > 0 ) {
918 $('.notice').on('click', 'button.notice-dismiss', function (e) {
919 e.preventDefault();
920 $.ajax({
921 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
922 type: 'post',
923 data: {
924 action: 'wpdeveloper_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
925 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_notice_dissmiss'); ?>',
926 dismiss: true,
927 notice: $(this).parents('.notice').data('notice'),
928 },
929 success: function(response) {
930 $('.notice').hide();
931 console.log('Successfully saved!');
932 },
933 error: function(error) {
934 console.log('Something went wrong!');
935 },
936 complete: function() {
937 console.log('Its Complete.');
938 }
939 });
940 });
941 }
942 } );
943 </script>
944 <?php
945 }
946
947 /**
948 * Upsale Button Script.
949 * When install button is clicked, it will do its own things.
950 * also for dismiss button JS.
951 * @return void
952 */
953 public function upsale_button_script(){
954 $upsale_args = $this->get_upsale_args();
955
956 $plugin_slug = ( isset( $upsale_args['slug'] ) ) ? $upsale_args['slug'] : '';
957 $plugin_file = ( isset( $upsale_args['file'] ) ) ? $upsale_args['file'] : '';
958 $page_slug = ( isset( $upsale_args['page_slug'] ) ) ? $upsale_args['page_slug'] : '';
959
960 ?>
961 <script type="text/javascript">
962 jQuery(document).ready( function($) {
963 <?php if( ! empty( $plugin_slug ) && ! empty( $plugin_file ) ) : ?>
964 $('#plugin-install-core-<?php echo $this->plugin_name; ?>').on('click', function (e) {
965 var self = $(this);
966 e.preventDefault();
967 self.addClass('install-now updating-message');
968 self.text('<?php echo esc_js( 'Installing...' ); ?>');
969
970 $.ajax({
971 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
972 type: 'POST',
973 data: {
974 action: 'wpdeveloper_upsale_core_install_<?php echo $this->plugin_name; ?>',
975 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_core_install_' . $this->plugin_name); ?>',
976 slug : '<?php echo $plugin_slug; ?>',
977 file : '<?php echo $plugin_file; ?>'
978 },
979 success: function(response) {
980 self.text('<?php echo esc_js( 'Installed' ); ?>');
981 <?php if( ! empty( $page_slug ) ) : ?>
982 window.location.href = '<?php echo admin_url( "admin.php?page={$page_slug}" ); ?>';
983 <?php endif; ?>
984 },
985 error: function(error) {
986 self.removeClass('install-now updating-message');
987 alert( error );
988 },
989 complete: function() {
990 self.attr('disabled', 'disabled');
991 self.removeClass('install-now updating-message');
992 }
993 });
994 });
995
996 <?php endif; ?>
997
998 $('.wpdeveloper-upsale-notice').on('click', 'button.notice-dismiss', function (e) {
999 e.preventDefault();
1000 $.ajax({
1001 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
1002 type: 'post',
1003 data: {
1004 action: 'wpdeveloper_upsale_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
1005 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_notice_dissmiss'); ?>',
1006 dismiss: true
1007 },
1008 success: function(response) {
1009 console.log('Successfully saved!');
1010 },
1011 error: function(error) {
1012 console.log('Something went wrong!');
1013 },
1014 complete: function() {
1015 console.log('Its Complete.');
1016 }
1017 });
1018 });
1019 } );
1020 </script>
1021
1022 <?php
1023 }
1024 }
1025
1026