PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 3.3.3
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v3.3.3
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
920 lines
1 <?php
2 namespace EmbedPress\Includes\Classes;
3
4 if (!defined('ABSPATH')) {
5 exit;
6 } // Exit if accessed directly.
7
8 use EmbedPress\Includes\Classes\EmbedPress_Core_Installer;
9
10 class EmbedPress_Notice {
11 /**
12 * Admin Notice Key
13 *
14 * @var array
15 */
16 const ADMIN_UPDATE_NOTICE_KEY = 'wpdeveloper_notices_seen';
17 public $text_domain = 'embedpress';
18 /**
19 * All Data
20 * @var array
21 */
22 private $data = array();
23 private $properties = array(
24 'links', 'message', 'thumbnail',
25 );
26 private $methods = array(
27 'message', 'thumbnail', 'classes'
28 );
29 /**
30 * cne_day == current_notice_end_day
31 *
32 * @var integer
33 */
34 public $cne_time = '2 day';
35 public $maybe_later_time = '7 day';
36 public $finish_time = [];
37 /**
38 * Plugin Name
39 *
40 * @var string
41 */
42 private $plugin_name;
43 /**
44 * Plugin File Name
45 * @var string
46 */
47 private $plugin_file;
48 /**
49 * First Install Version Of The Plugin
50 *
51 * @var string
52 */
53 private $version;
54 /**
55 * Saved Data in DB
56 * @var array
57 */
58 private $options_data;
59 /**
60 * Current Timestamp
61 * @var integer
62 */
63 public $timestamp;
64 /**
65 * Primary Notice Action
66 *
67 * @var string
68 */
69 private $do_notice_action;
70 /**
71 * Default Options Set
72 *
73 * @var array
74 */
75 public $options_args = array(
76 // 'first_install' => true,
77 // 'notice_will_show' => [
78 // 'opt_in' => true,
79 // 'first_install' => false,
80 // 'update' => true,
81 // 'review' => true,
82 // 'upsale' => true,
83 // ]
84 );
85 /**
86 * Notice ID for users.
87 * @var string
88 */
89 private $notice_id;
90 /**
91 * Upsale Notice Arguments
92 * @var array
93 */
94 public $upsale_args;
95
96 /**
97 * Revoke this function when the object is created.
98 *
99 * @param string $plugin_file
100 * @param string $version
101 */
102 public function __construct( $plugin_file = '', $version = '' ) {
103 $this->plugin_file = $plugin_file;
104 $this->plugin_name = basename( $plugin_file, '.php' );
105 $this->version = $version;
106 $this->timestamp = intval( current_time( 'timestamp' ) );
107 $this->notice_id = 'wpdeveloper_notice_' . str_replace( '.', '_', $this->version );
108
109 $this->do_notice_action = 'wpdeveloper_notices_for_' . $this->plugin_name;
110 new EmbedPress_Core_Installer( $this->plugin_name );
111
112 }
113 /**
114 * Initiate The Plugin
115 * @return void
116 */
117 public function init(){
118 add_action( 'init', array( $this, 'first_install_track') );
119 add_action( 'deactivate_' . $this->plugin_file, array( $this, 'first_install_end' ) );
120 add_action( 'init', array( $this, 'hooks' ) );
121 }
122 /**
123 * All Hooks
124 * @return void
125 */
126 public function hooks(){
127 add_action( 'wpdeveloper_notice_clicked_for_' . $this->plugin_name, array( $this, 'clicked' ) );
128 add_action( 'wp_ajax_wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name, array( $this, 'upsale_notice_dissmiss' ) );
129 add_action( 'wp_ajax_wpdeveloper_notice_dissmiss_for_' . $this->plugin_name, array( $this, 'notice_dissmiss' ) );
130 add_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name, array( $this, 'before' ) );
131 add_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name, array( $this, 'after' ) );
132 add_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name, array( $this, 'before_upsale' ) );
133 add_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name, array( $this, 'after' ) );
134 add_action( $this->do_notice_action, array( $this, 'content' ) );
135 if( current_user_can( 'install_plugins' ) ) {
136 if( isset( $_GET['plugin'] ) && $_GET['plugin'] == $this->plugin_name ) {
137 if( isset( $_GET['tab'] ) && $_GET['tab'] === 'plugin-information' ) {
138 return;
139 }
140 do_action( 'wpdeveloper_notice_clicked_for_' . $this->plugin_name );
141 /**
142 * Redirect User To the Current URL, but without set query arguments.
143 */
144 wp_safe_redirect( $this->redirect_to() );
145 }
146 $return_notice = $this->next_notice();
147 $current_notice = current( $return_notice );
148 $next_notice = next( $return_notice );
149
150 $deserve_notice = $this->deserve_notice( $current_notice );
151 $options_data = $this->get_options_data();
152 $user_notices = $this->get_user_notices();
153
154 $notice_time = isset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] )
155 ? $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] : $this->timestamp;
156 $next_notice_time = $next_notice ? $options_data[ $this->plugin_name ]['notice_will_show'][ $next_notice ] : $this->timestamp;
157 $current_notice_end = $this->makeTime( $notice_time, $this->cne_time );
158
159 if( ! $deserve_notice ) {
160 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
161 $this->update_options_data( $options_data[ $this->plugin_name ] );
162 }
163
164 if( $deserve_notice ) {
165 /**
166 * TODO: automatic maybe later setup with time.
167 */
168 if( ( $this->timestamp >= $current_notice_end ) || ( $this->timestamp > $next_notice_time ) ) {
169 $this->maybe_later( $current_notice );
170 $notice_time = false;
171 }
172
173 if( isset( $this->finish_time[ $current_notice ] ) ) {
174 if( $this->timestamp >= strtotime( $this->finish_time[ $current_notice ] ) ) {
175 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
176 $this->update_options_data( $options_data[ $this->plugin_name ] );
177 $notice_time = false;
178 }
179 }
180
181 if( $notice_time != false ) {
182 if( $notice_time <= $this->timestamp ) {
183 if( $current_notice === 'upsale' ) {
184 $upsale_args = $this->get_upsale_args();
185 if( empty( $upsale_args ) ) {
186 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
187 $this->update_options_data( $options_data[ $this->plugin_name ] );
188 } else {
189 if( isset( $upsale_args['condition'], $upsale_args['condition']['by'] ) ) {
190 switch( $upsale_args['condition']['by'] ) {
191 case 'class' :
192 if( isset( $upsale_args['condition']['class'] ) && class_exists( $upsale_args['condition']['class'] ) ) {
193 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
194 $this->update_options_data( $options_data[ $this->plugin_name ] );
195 return;
196 }
197 break;
198 case 'function' :
199 if( isset( $upsale_args['condition']['function'] ) && function_exists( $upsale_args['condition']['function'] ) ) {
200 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
201 $this->update_options_data( $options_data[ $this->plugin_name ] );
202 return;
203 }
204 break;
205 }
206 }
207 if( isset( $upsale_args['slug'], $upsale_args['file'] ) ) {
208 if ( ! function_exists( 'get_plugins' ) ) {
209 include ABSPATH . '/wp-admin/includes/plugin.php';
210 }
211 $plugins = get_plugins();
212 $pkey = $upsale_args['slug'] . '/' . $upsale_args['file'];
213 if( isset( $plugins[ $pkey ] ) ) {
214 $this->update( $current_notice );
215 return;
216 }
217 }
218 add_action( 'admin_notices', array( $this, 'upsale_notice' ) );
219 }
220 } else {
221 add_action( 'admin_notices', array( $this, 'admin_notices' ) );
222 }
223 }
224 }
225 }
226 }
227 }
228 /**
229 * Make time using timestamp and a string like 2 Hour, 2 Day, 30 Minutes, 1 Week, 1 year
230 * @param integer $current
231 * @param string $time
232 * @return integer
233 */
234 public function makeTime( $current, $time ) {
235 return intval( strtotime( date('r', $current) . " +$time" ) );
236 }
237 /**
238 * Automatice Maybe Later.
239 * @param string $notice
240 * @return void
241 */
242 private function maybe_later( $notice ){
243 if( empty( $notice ) ) {
244 return;
245 }
246 $options_data = $this->get_options_data();
247 $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] = $this->makeTime( $this->timestamp, $this->maybe_later_time );
248 $this->update_options_data( $options_data[ $this->plugin_name ] );
249 }
250 /**
251 * When links are clicked, this function will invoked.
252 * @return void
253 */
254 public function clicked(){
255 if( isset( $_GET['plugin'] ) ) {
256 $plugin = sanitize_text_field( $_GET['plugin'] );
257 if( $plugin === $this->plugin_name ) {
258 $options_data = $this->get_options_data();
259 $clicked_from = current( $this->next_notice() );
260 if( isset( $_GET['plugin_action'] ) ) {
261 $plugin_action = sanitize_text_field( $_GET['plugin_action'] );
262 }
263 if( isset( $_GET['dismiss'] ) ) {
264 $dismiss = sanitize_text_field( $_GET['dismiss'] );
265 }
266 if( isset( $_GET['later'] ) ) {
267 $later = sanitize_text_field( $_GET['later'] );
268 }
269
270 $later_time = '';
271
272 switch( $clicked_from ) {
273
274 case 'opt_in' :
275 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
276 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
277 break;
278
279 case 'first_install' :
280 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
281 break;
282
283 case 'update' :
284 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
285 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
286 break;
287
288 case 'review' :
289 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
290 break;
291
292 case 'upsale' :
293 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
294 break;
295 }
296
297 if( isset( $later ) && $later == true ) {
298 $options_data[ $this->plugin_name ]['notice_will_show'][ $clicked_from ] = $later_time;
299 }
300 if( isset( $dismiss ) && $dismiss == true ) {
301 update_user_meta( get_current_user_id(), $this->plugin_name . '_' . $clicked_from, true );
302 $this->update( $clicked_from );
303 }
304 $this->update_options_data( $options_data[ $this->plugin_name ] );
305 }
306 }
307 }
308 /**
309 * For Redirecting Current Page without Arguments!
310 *
311 * @return void
312 */
313 private function redirect_to(){
314 $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
315 $query_string = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
316 parse_str( $query_string, $current_url );
317
318 $unset_array = array( 'dismiss', 'plugin', '_wpnonce', 'later', 'plugin_action', 'marketing_optin' );
319
320 foreach( $unset_array as $value ) {
321 if( isset( $current_url[ $value ] ) ) {
322 unset( $current_url[ $value ] );
323 }
324 }
325
326 $current_url = http_build_query($current_url);
327 $redirect_url = $request_uri . '?' . $current_url;
328 return $redirect_url;
329 }
330 /**
331 * Before Notice
332 * @return void
333 */
334 public function before(){
335 $current_notice = current( $this->next_notice() );
336 $classes = 'notice notice-info put-dismiss-notice';
337 if( isset( $this->data['classes'] ) ) {
338 if( isset( $this->data['classes'][ $current_notice ] ) ) {
339 $classes = $this->data['classes'][ $current_notice ];
340 }
341 }
342
343 if( $this->has_thumbnail( $current_notice ) ) {
344 $classes .= 'notice-has-thumbnail';
345 }
346
347 if( ! $this->has_notice( $current_notice ) ) {
348 return;
349 }
350
351 echo '<div data-notice="'. $current_notice .'" class="'. $classes .' wpdeveloper-'. $current_notice .'-notice">';
352 }
353 /**
354 * After Notice
355 * @return void
356 */
357 public function after(){
358 $current_notice = current( $this->next_notice() );
359 if( ! $this->has_notice( $current_notice ) ) {
360 return;
361 }
362 echo '</div>';
363 }
364 /**
365 * Content generation & Hooks Funciton.
366 * @return void
367 */
368 public function content(){
369 $options_data = $this->get_options_data();
370 $notice = current( $this->next_notice() );
371 switch( $notice ) {
372 case 'opt_in' :
373 do_action('wpdeveloper_optin_notice_for_' . $this->plugin_name );
374 break;
375 case 'first_install' :
376 if( $options_data[ $this->plugin_name ]['first_install'] !== 'deactivated' ) {
377 do_action( 'wpdeveloper_first_install_notice_for_' . $this->plugin_name );
378 $this->get_thumbnail( 'first_install' );
379 $this->get_message( 'first_install' );
380 }
381 break;
382 case 'update' :
383 do_action( 'wpdeveloper_update_notice_for_' . $this->plugin_name );
384 $this->get_thumbnail( 'update' );
385 $this->get_message( 'update' );
386 $this->dismiss_button_scripts();
387 break;
388 case 'review' :
389 do_action( 'wpdeveloper_review_notice_for_' . $this->plugin_name );
390 $this->get_thumbnail( 'review' );
391 $this->get_message( 'review' );
392 break;
393 }
394 }
395 /**
396 * Before Upsale Notice
397 * @return void
398 */
399 public function before_upsale(){
400 $classes = '';
401 if( $this->has_thumbnail('upsale') ) {
402 $classes = 'notice-has-thumbnail';
403 }
404 echo '<div class="error notice is-dismissible wpdeveloper-upsale-notice '. $classes .'">';
405 }
406 /**
407 * Upsale Notice
408 */
409 public function upsale_notice(){
410 do_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name );
411 do_action('wpdeveloper_upsale_notice_for_' . $this->plugin_name);
412 $this->get_thumbnail( 'upsale' );
413 $this->get_message( 'upsale' );
414 do_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name );
415 $this->upsale_button_script();
416 }
417 /**
418 * Get upsale arguments.
419 * @return array
420 */
421 private function get_upsale_args(){
422 return ( empty( $this->upsale_args ) ) ? array() : $this->upsale_args;
423 }
424 /**
425 * This function is responsible for making the button visible to the upsale notice.
426 */
427 private function upsale_button(){
428 $upsale_args = $this->get_upsale_args();
429 if( isset( $upsale_args['href'] ) ) {
430 echo '<a href="'. $upsale_args['href'] .'" target="_blank" class="button button-primary">'. $upsale_args['btn_text'] .'</a>';
431 return;
432 }
433 $plugin_slug = ( isset( $upsale_args['slug'] )) ? $upsale_args['slug'] : '' ;
434 if( empty( $plugin_slug ) ) {
435 return;
436 }
437 echo '<button data-slug="'. $plugin_slug .'" id="plugin-install-core-'. $this->plugin_name .'" class="button button-primary">'. __( 'Install Now!', 'embedpress' ) .'</button>';
438 }
439 /**
440 * This methods is responsible for get notice image.
441 *
442 * @param string $msg_for
443 * @return void
444 */
445 protected function get_thumbnail( $msg_for ){
446 $output = '';
447 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
448 $output = '<div class="wpdeveloper-notice-thumbnail">';
449 $output .= '<img src="'. $this->data['thumbnail'][ $msg_for ] .'" alt="NotificationX">';
450 $output .= '</div>';
451 }
452 echo $output;
453 }
454 /**
455 * Has Thumbnail Check
456 *
457 * @param string $msg_for
458 * @return boolean
459 */
460 protected function has_thumbnail( $msg_for = '' ){
461 if( empty( $msg_for ) ) {
462 return false;
463 }
464 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
465 return true;
466 }
467 return false;
468 }
469 private function has_notice( $notice ){
470 return isset( $this->data['message'], $this->data['message'][ $notice ] );
471 }
472 /**
473 * This method is responsible for get messages.
474 *
475 * @param string $msg_for
476 * @return void
477 */
478 protected function get_message( $msg_for ){
479 if( isset( $this->data['message'] ) && isset( $this->data['message'][ $msg_for ] ) ) {
480 echo '<div class="wpdeveloper-notice-message">';
481 echo $this->data['message'][ $msg_for ];
482 if( $msg_for === 'upsale' ) {
483 $this->upsale_button();
484 }
485 $this->dismissible_notice( $msg_for );
486 echo '</div>';
487 }
488 }
489 /**
490 * Detect which notice will show @ next.
491 * @return void
492 */
493 protected function next_notice(){
494 $options_data = $this->get_options_data();
495 if( ! $options_data ) {
496 $args = $this->get_args();
497 $return_notice = $args['notice_will_show'];
498 } else {
499 $return_notice = $options_data[ $this->plugin_name ]['notice_will_show'];
500 }
501
502 if( is_array( $return_notice ) ) {
503 $return_notice = array_flip( $return_notice );
504 ksort( $return_notice );
505 }
506
507 return $return_notice;
508 }
509 /**
510 * Which notice is deserve to show in next slot.
511 * @param string $notice
512 * @return boolean
513 */
514 private function deserve_notice( $notice ) {
515 $notices = $this->get_user_notices();
516 if( $notice === false ) {
517 return false;
518 }
519 if( empty( $notices ) ) {
520 return true;
521 } else {
522 if( isset( $notices[ $this->notice_id ] ) && isset( $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
523 if( in_array( $notice, $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
524 return false;
525 } else {
526 return true;
527 }
528 } else {
529 return true;
530 }
531 }
532 }
533 /**
534 * This is the main methods for generate the notice.
535 * @return void
536 */
537 public function admin_notices(){
538 $current_notice = current( $this->next_notice() );
539 if( get_user_meta( get_current_user_id(), $this->plugin_name . '_' . $current_notice, true ) ) {
540 return;
541 }
542 if( $current_notice == 'opt_in' ) {
543 do_action( $this->do_notice_action );
544 return;
545 }
546 do_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name );
547 do_action( $this->do_notice_action );
548 do_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name );
549 }
550 /**
551 * This method is responsible for all dismissible links generation.
552 * @param string $links_for
553 * @return void
554 */
555 public function dismissible_notice( $links_for = '' ){
556 if( empty( $links_for ) ) {
557 return;
558 }
559 $links = isset( $this->data['links'][ $links_for ] ) ? $this->data['links'][ $links_for ] : false;
560 if( $links ) :
561 $output = '<ul class="wpdeveloper-notice-link">';
562 foreach( $links as $key => $link_value ) {
563 if( ! empty( $link_value['label'] ) ) {
564 $output .= '<li>';
565 if( isset( $link_value['link'] ) ) {
566 $link = $link_value['link'];
567 $target = isset( $link_value['target'] ) ? 'target="'. $link_value['target'] .'"' : '';
568 if( isset( $link_value['data_args'] ) && is_array( $link_value['data_args'] ) ) {
569 $data_args = [];
570 foreach( $link_value['data_args'] as $key => $args_value ) {
571 $data_args[ $key ] = $args_value;
572 }
573 $data_args[ 'plugin' ] = $this->plugin_name;
574 $normal_link = add_query_arg( $data_args, $link );
575 $link = wp_nonce_url( $normal_link, 'wpdeveloper-nonce' );
576 }
577 $class = '';
578 if( isset( $link_value['link_class'] ) ) {
579 $class = 'class="' . implode( ' ', $link_value['link_class'] ) . '"';
580 }
581 $output .= '<a '. $class .' href="'. esc_url( $link ) .'" '. $target .'>';
582 }
583 if( isset( $link_value['icon_class'] ) ) {
584 $output .= '<span class="'. $link_value['icon_class'] .'"></span>';
585 }
586 if( isset( $link_value['icon_img'] ) ) {
587 $output .= '<img src="'. $link_value['icon_img'] .'" />';
588 }
589 $output .= $link_value['label'];
590 if( isset( $link_value['link'] ) ) {
591 $output .= '</a>';
592 }
593 $output .= '</li>';
594 }
595 }
596 $output .= '</ul>';
597 echo $output;
598 endif;
599 }
600 /**
601 * First Installation Track
602 * @return void
603 */
604 public function first_install_track( $args = array() ){
605 if( ! current_user_can( 'manage_options' ) ) {
606 return;
607 }
608 if( empty( $args ) ) {
609 $args = array(
610 'time' => $this->timestamp,
611 'version' => $this->version,
612 );
613 }
614 $options_data = $this->get_options_data();
615 $args = wp_parse_args( $args, $this->get_args() );
616 if( ! isset( $options_data[ $this->plugin_name ] )
617 || ( isset( $options_data[ $this->plugin_name ]['version'] ) && version_compare( $options_data[ $this->plugin_name ]['version'], $this->version, '!=' ) ) ) {
618 $this->update_options_data( $args );
619 }
620 }
621 /**
622 * First Installation Deactive Track
623 *
624 * @return void
625 */
626 public function first_install_end(){
627 $options_data = $this->get_options_data();
628 if( isset( $options_data[ $this->plugin_name ] ) ) {
629 unset( $options_data[ $this->plugin_name ] );
630 $this->update_options_data( $options_data, true );
631 }
632 }
633 /**
634 * Get all options from database!
635 * @return void
636 */
637 protected function get_options_data( $key = ''){
638 $options_data = get_option( 'wpdeveloper_plugins_data' );
639 if( empty( $key ) ) {
640 return $options_data;
641 }
642
643 if( isset( $options_data[ $this->plugin_name ][ $key ] ) ) {
644 return $options_data[ $this->plugin_name ][ $key ];
645 }
646 return false;
647 }
648 /**
649 * This will update the options table for plugins.
650 *
651 * @param mixed $new_data
652 * @param array $args
653 * @return void
654 */
655 protected function update_options_data( $args = array(), $update = false ){
656 if( $update ) {
657 $options_data = $args;
658 } else {
659 $options_data = $this->get_options_data();
660 $options_data[ $this->plugin_name ] = $args;
661 }
662 update_option( 'wpdeveloper_plugins_data', $options_data );
663 }
664 /**
665 * Set properties data, for some selected properties.
666 *
667 * @param string $name
668 * @param mixed $value
669 */
670 public function __set( $name, $value ){
671 if( in_array( $name, $this->properties ) ) {
672 $this->data[ $name ] = $value;
673 }
674 }
675 /**
676 * Invoked when some selected methods are called
677 *
678 * @param string $name
679 * @param array $values
680 * @return void
681 */
682 public function __call( $name, $values ){
683 if( in_array( $name, $this->methods ) ) {
684 $this->data[ $name ][ $values[0] ] = $values[1];
685 }
686 }
687 /**
688 * Get all option arguments.
689 * @param string $key
690 * @return array
691 */
692 private function get_args( $key = '' ){
693 if( empty( $key ) ) {
694 return $this->options_args;
695 }
696
697 if( isset( $this->options_args[ $key ] ) ) {
698 return $this->options_args[ $key ];
699 }
700
701 return false;
702 }
703 /**
704 * Resetting data on update.
705 * @return void
706 */
707 private function set_args_on_update(){
708 $args = $this->get_args();
709 $options_data = $this->get_options_data();
710 $set_data = $options_data[ $this->plugin_name ];
711 $args = wp_parse_args( $set_data, $args );
712 $this->update_options_data( $args );
713 }
714 /**
715 * When upgrade is complete. it will fired.
716 * @param WP_Upgrader $upgrader_object
717 * @param array $options
718 * @return void
719 */
720 public function upgrade_completed( $upgrader_object, $options ) {
721 // If an update has taken place and the updated type is plugins and the plugins element exists
722 if( isset( $options['action'] ) && $options['action'] == 'update' && $options['type'] == 'plugin' ) {
723 if( ! isset( $options['plugin'] ) && isset( $options['plugins'] ) ) {
724 foreach( $options['plugins'] as $plugin ) {
725 if( $plugin == $this->plugin_name ) {
726 $this->set_args_on_update();
727 }
728 }
729 }
730
731 if( isset( $options['plugin'] ) && $options['plugin'] == $this->plugin_name ) {
732 $this->set_args_on_update();
733 }
734 }
735 }
736 /**
737 * This function is responsible for get_user_notices
738 * @return void
739 */
740 private function get_user_notices() {
741 $notices = get_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, true );
742 return ! $notices ? array() : $notices;
743 }
744 /**
745 * This function is responsible for update meta information.
746 *
747 * @param string $notice
748 * @return void
749 */
750 private function update( $notice ){
751 if( empty( $notice ) ) {
752 return;
753 }
754 $options_data = $this->get_options_data();
755 $user_notices = $this->get_user_notices();
756 $user_notices[ $this->notice_id ][ $this->plugin_name ][] = $notice;
757 // Remove the upsale from notice_will_show field in options DB.
758 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] );
759 $this->update_options_data( $options_data[ $this->plugin_name ] );
760 // Set users meta, not to show again current_version notice.
761 update_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, $user_notices);
762 }
763
764 public function notice_dissmiss(){
765
766 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_notice_dissmiss' ) ) {
767 return;
768 }
769
770 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_notice_dissmiss_for_' . $this->plugin_name ) ) {
771 return;
772 }
773
774 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
775 $notice = isset( $_POST['notice'] ) ? $_POST['notice'] : false;
776 if( $dismiss ) {
777 $this->update( $notice );
778 echo 'success';
779 } else {
780 echo 'failed';
781 }
782 die();
783 }
784
785 /**
786 * This function is responsible for do action when
787 * the dismiss button clicked in upsale notice.
788 */
789 public function upsale_notice_dissmiss(){
790
791 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_upsale_notice_dissmiss' ) ) {
792 return;
793 }
794
795 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name ) ) {
796 return;
797 }
798
799 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
800 if( $dismiss ) {
801 $this->update( 'upsale' );
802 echo 'success';
803 } else {
804 echo 'failed';
805 }
806 die();
807 }
808
809 public function dismiss_button_scripts(){
810 ?>
811 <script type="text/javascript">
812 jQuery(document).ready( function($) {
813 if( $('.notice').length > 0 ) {
814 $('.notice').on('click', 'button.notice-dismiss', function (e) {
815 e.preventDefault();
816 $.ajax({
817 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
818 type: 'post',
819 data: {
820 action: 'wpdeveloper_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
821 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_notice_dissmiss'); ?>',
822 dismiss: true,
823 notice: $(this).parents('.notice').data('notice'),
824 },
825 success: function(response) {
826 $('.notice').hide();
827 console.log('Successfully saved!');
828 },
829 error: function(error) {
830 console.log('Something went wrong!');
831 },
832 complete: function() {
833 console.log('Its Complete.');
834 }
835 });
836 });
837 }
838 } );
839 </script>
840 <?php
841 }
842
843 /**
844 * Upsale Button Script.
845 * When install button is clicked, it will do its own things.
846 * also for dismiss button JS.
847 * @return void
848 */
849 public function upsale_button_script(){
850 $upsale_args = $this->get_upsale_args();
851
852 $plugin_slug = ( isset( $upsale_args['slug'] ) ) ? $upsale_args['slug'] : '';
853 $plugin_file = ( isset( $upsale_args['file'] ) ) ? $upsale_args['file'] : '';
854 $page_slug = ( isset( $upsale_args['page_slug'] ) ) ? $upsale_args['page_slug'] : '';
855
856 ?>
857 <script type="text/javascript">
858 jQuery(document).ready( function($) {
859 <?php if( ! empty( $plugin_slug ) && ! empty( $plugin_file ) ) : ?>
860 $('#plugin-install-core-<?php echo $this->plugin_name; ?>').on('click', function (e) {
861 var self = $(this);
862 e.preventDefault();
863 self.addClass('install-now updating-message');
864 self.text('<?php echo esc_js( 'Installing...' ); ?>');
865
866 $.ajax({
867 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
868 type: 'POST',
869 data: {
870 action: 'wpdeveloper_upsale_core_install_<?php echo $this->plugin_name; ?>',
871 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_core_install_' . $this->plugin_name); ?>',
872 slug : '<?php echo $plugin_slug; ?>',
873 file : '<?php echo $plugin_file; ?>'
874 },
875 success: function(response) {
876 self.text('<?php echo esc_js( 'Installed' ); ?>');
877 <?php if( ! empty( $page_slug ) ) : ?>
878 window.location.href = '<?php echo admin_url( "admin.php?page={$page_slug}" ); ?>';
879 <?php endif; ?>
880 },
881 error: function(error) {
882 self.removeClass('install-now updating-message');
883 alert( error );
884 },
885 complete: function() {
886 self.attr('disabled', 'disabled');
887 self.removeClass('install-now updating-message');
888 }
889 });
890 });
891
892 <?php endif; ?>
893
894 $('.wpdeveloper-upsale-notice').on('click', 'button.notice-dismiss', function (e) {
895 e.preventDefault();
896 $.ajax({
897 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
898 type: 'post',
899 data: {
900 action: 'wpdeveloper_upsale_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
901 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_notice_dissmiss'); ?>',
902 dismiss: true
903 },
904 success: function(response) {
905 console.log('Successfully saved!');
906 },
907 error: function(error) {
908 console.log('Something went wrong!');
909 },
910 complete: function() {
911 console.log('Its Complete.');
912 }
913 });
914 });
915 } );
916 </script>
917
918 <?php
919 }
920 }