PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents / 3.0.0
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents v3.0.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
EmbedPress_Core_Installer.php 6 years ago EmbedPress_Notice.php 5 years ago EmbedPress_Plugin_Usage_Tracker.php 5 years ago
EmbedPress_Notice.php
909 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 echo '<div data-notice="'. $current_notice .'" class="'. $classes .' wpdeveloper-'. $current_notice .'-notice">';
348 }
349 /**
350 * After Notice
351 * @return void
352 */
353 public function after(){
354 echo '</div>';
355 }
356 /**
357 * Content generation & Hooks Funciton.
358 * @return void
359 */
360 public function content(){
361 $options_data = $this->get_options_data();
362 $notice = current( $this->next_notice() );
363 switch( $notice ) {
364 case 'opt_in' :
365 do_action('wpdeveloper_optin_notice_for_' . $this->plugin_name );
366 break;
367 case 'first_install' :
368 if( $options_data[ $this->plugin_name ]['first_install'] !== 'deactivated' ) {
369 do_action( 'wpdeveloper_first_install_notice_for_' . $this->plugin_name );
370 $this->get_thumbnail( 'first_install' );
371 $this->get_message( 'first_install' );
372 }
373 break;
374 case 'update' :
375 do_action( 'wpdeveloper_update_notice_for_' . $this->plugin_name );
376 $this->get_thumbnail( 'update' );
377 $this->get_message( 'update' );
378 $this->dismiss_button_scripts();
379 break;
380 case 'review' :
381 do_action( 'wpdeveloper_review_notice_for_' . $this->plugin_name );
382 $this->get_thumbnail( 'review' );
383 $this->get_message( 'review' );
384 break;
385 }
386 }
387 /**
388 * Before Upsale Notice
389 * @return void
390 */
391 public function before_upsale(){
392 $classes = '';
393 if( $this->has_thumbnail('upsale') ) {
394 $classes = 'notice-has-thumbnail';
395 }
396 echo '<div class="error notice is-dismissible wpdeveloper-upsale-notice '. $classes .'">';
397 }
398 /**
399 * Upsale Notice
400 */
401 public function upsale_notice(){
402 do_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name );
403 do_action('wpdeveloper_upsale_notice_for_' . $this->plugin_name);
404 $this->get_thumbnail( 'upsale' );
405 $this->get_message( 'upsale' );
406 do_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name );
407 $this->upsale_button_script();
408 }
409 /**
410 * Get upsale arguments.
411 * @return void
412 */
413 private function get_upsale_args(){
414 return ( empty( $this->upsale_args ) ) ? array() : $this->upsale_args;
415 }
416 /**
417 * This function is responsible for making the button visible to the upsale notice.
418 */
419 private function upsale_button(){
420 $upsale_args = $this->get_upsale_args();
421 if( isset( $upsale_args['href'] ) ) {
422 echo '<a href="'. $upsale_args['href'] .'" target="_blank" class="button button-primary">'. $upsale_args['btn_text'] .'</a>';
423 return;
424 }
425 $plugin_slug = ( isset( $upsale_args['slug'] )) ? $upsale_args['slug'] : '' ;
426 if( empty( $plugin_slug ) ) {
427 return;
428 }
429 echo '<button data-slug="'. $plugin_slug .'" id="plugin-install-core-'. $this->plugin_name .'" class="button button-primary">'. __( 'Install Now!', $this->text_domain, 'notificationx' ) .'</button>';
430 }
431 /**
432 * This methods is responsible for get notice image.
433 *
434 * @param string $msg_for
435 * @return void
436 */
437 protected function get_thumbnail( $msg_for ){
438 $output = '';
439 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
440 $output = '<div class="wpdeveloper-notice-thumbnail">';
441 $output .= '<img src="'. $this->data['thumbnail'][ $msg_for ] .'" alt="NotificationX">';
442 $output .= '</div>';
443 }
444 echo $output;
445 }
446 /**
447 * Has Thumbnail Check
448 *
449 * @param string $msg_for
450 * @return boolean
451 */
452 protected function has_thumbnail( $msg_for = '' ){
453 if( empty( $msg_for ) ) {
454 return false;
455 }
456 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
457 return true;
458 }
459 return false;
460 }
461 /**
462 * This method is responsible for get messages.
463 *
464 * @param string $msg_for
465 * @return void
466 */
467 protected function get_message( $msg_for ){
468 if( isset( $this->data['message'] ) && isset( $this->data['message'][ $msg_for ] ) ) {
469 echo '<div class="wpdeveloper-notice-message">';
470 echo $this->data['message'][ $msg_for ];
471 if( $msg_for === 'upsale' ) {
472 $this->upsale_button();
473 }
474 $this->dismissible_notice( $msg_for );
475 echo '</div>';
476 }
477 }
478 /**
479 * Detect which notice will show @ next.
480 * @return void
481 */
482 protected function next_notice(){
483 $options_data = $this->get_options_data();
484 if( ! $options_data ) {
485 $args = $this->get_args();
486 $return_notice = $args['notice_will_show'];
487 } else {
488 $return_notice = $options_data[ $this->plugin_name ]['notice_will_show'];
489 }
490
491 if( is_array( $return_notice ) ) {
492 $return_notice = array_flip( $return_notice );
493 ksort( $return_notice );
494 }
495
496 return $return_notice;
497 }
498 /**
499 * Which notice is deserve to show in next slot.
500 * @param string $notice
501 * @return boolean
502 */
503 private function deserve_notice( $notice ) {
504 $notices = $this->get_user_notices();
505 if( $notice === false ) {
506 return false;
507 }
508 if( empty( $notices ) ) {
509 return true;
510 } else {
511 if( isset( $notices[ $this->notice_id ] ) && isset( $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
512 if( in_array( $notice, $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
513 return false;
514 } else {
515 return true;
516 }
517 } else {
518 return true;
519 }
520 }
521 }
522 /**
523 * This is the main methods for generate the notice.
524 * @return void
525 */
526 public function admin_notices(){
527 $current_notice = current( $this->next_notice() );
528 if( get_user_meta( get_current_user_id(), $this->plugin_name . '_' . $current_notice, true ) ) {
529 return;
530 }
531 if( $current_notice == 'opt_in' ) {
532 do_action( $this->do_notice_action );
533 return;
534 }
535 do_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name );
536 do_action( $this->do_notice_action );
537 do_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name );
538 }
539 /**
540 * This method is responsible for all dismissible links generation.
541 * @param string $links_for
542 * @return void
543 */
544 public function dismissible_notice( $links_for = '' ){
545 if( empty( $links_for ) ) {
546 return;
547 }
548 $links = isset( $this->data['links'][ $links_for ] ) ? $this->data['links'][ $links_for ] : false;
549 if( $links ) :
550 $output = '<ul class="wpdeveloper-notice-link">';
551 foreach( $links as $key => $link_value ) {
552 if( ! empty( $link_value['label'] ) ) {
553 $output .= '<li>';
554 if( isset( $link_value['link'] ) ) {
555 $link = $link_value['link'];
556 $target = isset( $link_value['target'] ) ? 'target="'. $link_value['target'] .'"' : '';
557 if( isset( $link_value['data_args'] ) && is_array( $link_value['data_args'] ) ) {
558 $data_args = [];
559 foreach( $link_value['data_args'] as $key => $args_value ) {
560 $data_args[ $key ] = $args_value;
561 }
562 $data_args[ 'plugin' ] = $this->plugin_name;
563 $normal_link = add_query_arg( $data_args, $link );
564 $link = wp_nonce_url( $normal_link, 'wpdeveloper-nonce' );
565 }
566 $class = '';
567 if( isset( $link_value['link_class'] ) ) {
568 $class = 'class="' . implode( ' ', $link_value['link_class'] ) . '"';
569 }
570 $output .= '<a '. $class .' href="'. esc_url( $link ) .'" '. $target .'>';
571 }
572 if( isset( $link_value['icon_class'] ) ) {
573 $output .= '<span class="'. $link_value['icon_class'] .'"></span>';
574 }
575 if( isset( $link_value['icon_img'] ) ) {
576 $output .= '<img src="'. $link_value['icon_img'] .'" />';
577 }
578 $output .= $link_value['label'];
579 if( isset( $link_value['link'] ) ) {
580 $output .= '</a>';
581 }
582 $output .= '</li>';
583 }
584 }
585 $output .= '</ul>';
586 echo $output;
587 endif;
588 }
589 /**
590 * First Installation Track
591 * @return void
592 */
593 public function first_install_track( $args = array() ){
594 if( ! current_user_can( 'manage_options' ) ) {
595 return;
596 }
597 if( empty( $args ) ) {
598 $args = array(
599 'time' => $this->timestamp,
600 'version' => $this->version,
601 );
602 }
603 $options_data = $this->get_options_data();
604 $args = wp_parse_args( $args, $this->get_args() );
605 if( ! isset( $options_data[ $this->plugin_name ] )
606 || ( isset( $options_data[ $this->plugin_name ]['version'] ) && version_compare( $options_data[ $this->plugin_name ]['version'], $this->version, '!=' ) ) ) {
607 $this->update_options_data( $args );
608 }
609 }
610 /**
611 * First Installation Deactive Track
612 *
613 * @return void
614 */
615 public function first_install_end(){
616 // $args = array(
617 // 'first_install' => 'deactivated'
618 // );
619 // $options_data = $this->get_options_data();
620 // if( isset( $options_data[ $this->plugin_name ] ) ) {
621 // $args = wp_parse_args( $args, $options_data[ $this->plugin_name ] );
622 // $this->update_options_data( $args );
623 // }
624 delete_option( 'wpdeveloper_plugins_data' );
625 }
626 /**
627 * Get all options from database!
628 * @return void
629 */
630 protected function get_options_data( $key = ''){
631 $options_data = get_option( 'wpdeveloper_plugins_data' );
632 if( empty( $key ) ) {
633 return $options_data;
634 }
635
636 if( isset( $options_data[ $this->plugin_name ][ $key ] ) ) {
637 return $options_data[ $this->plugin_name ][ $key ];
638 }
639 return false;
640 }
641 /**
642 * This will update the options table for plugins.
643 *
644 * @param mixed $new_data
645 * @param array $args
646 * @return void
647 */
648 protected function update_options_data( $args = array() ){
649 $options_data = $this->get_options_data();
650 $options_data[ $this->plugin_name ] = $args;
651 update_option( 'wpdeveloper_plugins_data', $options_data );
652 }
653 /**
654 * Set properties data, for some selected properties.
655 *
656 * @param string $name
657 * @param mixed $value
658 */
659 public function __set( $name, $value ){
660 if( in_array( $name, $this->properties ) ) {
661 $this->data[ $name ] = $value;
662 }
663 }
664 /**
665 * Invoked when some selected methods are called
666 *
667 * @param string $name
668 * @param array $values
669 * @return void
670 */
671 public function __call( $name, $values ){
672 if( in_array( $name, $this->methods ) ) {
673 $this->data[ $name ][ $values[0] ] = $values[1];
674 }
675 }
676 /**
677 * Get all option arguments.
678 * @param string $key
679 * @return array
680 */
681 private function get_args( $key = '' ){
682 if( empty( $key ) ) {
683 return $this->options_args;
684 }
685
686 if( isset( $this->options_args[ $key ] ) ) {
687 return $this->options_args[ $key ];
688 }
689
690 return false;
691 }
692 /**
693 * Resetting data on update.
694 * @return void
695 */
696 private function set_args_on_update(){
697 $args = $this->get_args();
698 $options_data = $this->get_options_data();
699 $set_data = $options_data[ $this->plugin_name ];
700 $args = wp_parse_args( $set_data, $args );
701 $this->update_options_data( $args );
702 }
703 /**
704 * When upgrade is complete. it will fired.
705 * @param WP_Upgrader $upgrader_object
706 * @param array $options
707 * @return void
708 */
709 public function upgrade_completed( $upgrader_object, $options ) {
710 // If an update has taken place and the updated type is plugins and the plugins element exists
711 if( isset( $options['action'] ) && $options['action'] == 'update' && $options['type'] == 'plugin' ) {
712 if( ! isset( $options['plugin'] ) && isset( $options['plugins'] ) ) {
713 foreach( $options['plugins'] as $plugin ) {
714 if( $plugin == $this->plugin_name ) {
715 $this->set_args_on_update();
716 }
717 }
718 }
719
720 if( isset( $options['plugin'] ) && $options['plugin'] == $this->plugin_name ) {
721 $this->set_args_on_update();
722 }
723 }
724 }
725 /**
726 * This function is responsible for get_user_notices
727 * @return void
728 */
729 private function get_user_notices() {
730 $notices = get_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, true );
731 return ! $notices ? array() : $notices;
732 }
733 /**
734 * This function is responsible for update meta information.
735 *
736 * @param string $notice
737 * @return void
738 */
739 private function update( $notice ){
740 if( empty( $notice ) ) {
741 return;
742 }
743 $options_data = $this->get_options_data();
744 $user_notices = $this->get_user_notices();
745 $user_notices[ $this->notice_id ][ $this->plugin_name ][] = $notice;
746 // Remove the upsale from notice_will_show field in options DB.
747 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] );
748 $this->update_options_data( $options_data[ $this->plugin_name ] );
749 // Set users meta, not to show again current_version notice.
750 update_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, $user_notices);
751 }
752
753 public function notice_dissmiss(){
754
755 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_notice_dissmiss' ) ) {
756 return;
757 }
758
759 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_notice_dissmiss_for_' . $this->plugin_name ) ) {
760 return;
761 }
762
763 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
764 $notice = isset( $_POST['notice'] ) ? $_POST['notice'] : false;
765 if( $dismiss ) {
766 $this->update( $notice );
767 echo 'success';
768 } else {
769 echo 'failed';
770 }
771 die();
772 }
773
774 /**
775 * This function is responsible for do action when
776 * the dismiss button clicked in upsale notice.
777 */
778 public function upsale_notice_dissmiss(){
779
780 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_upsale_notice_dissmiss' ) ) {
781 return;
782 }
783
784 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name ) ) {
785 return;
786 }
787
788 $dismiss = isset( $_POST['dismiss'] ) ? $_POST['dismiss'] : false;
789 if( $dismiss ) {
790 $this->update( 'upsale' );
791 echo 'success';
792 } else {
793 echo 'failed';
794 }
795 die();
796 }
797
798 public function dismiss_button_scripts(){
799 ?>
800 <script type="text/javascript">
801 jQuery(document).ready( function($) {
802 if( $('.notice').length > 0 ) {
803 $('.notice').on('click', 'button.notice-dismiss', function (e) {
804 e.preventDefault();
805 $.ajax({
806 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
807 type: 'post',
808 data: {
809 action: 'wpdeveloper_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
810 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_notice_dissmiss'); ?>',
811 dismiss: true,
812 notice: $(this).parents('.notice').data('notice'),
813 },
814 success: function(response) {
815 $('.notice').hide();
816 console.log('Successfully saved!');
817 },
818 error: function(error) {
819 console.log('Something went wrong!');
820 },
821 complete: function() {
822 console.log('Its Complete.');
823 }
824 });
825 });
826 }
827 } );
828 </script>
829 <?php
830 }
831
832 /**
833 * Upsale Button Script.
834 * When install button is clicked, it will do its own things.
835 * also for dismiss button JS.
836 * @return void
837 */
838 public function upsale_button_script(){
839 $upsale_args = $this->get_upsale_args();
840
841 $plugin_slug = ( isset( $upsale_args['slug'] ) ) ? $upsale_args['slug'] : '';
842 $plugin_file = ( isset( $upsale_args['file'] ) ) ? $upsale_args['file'] : '';
843 $page_slug = ( isset( $upsale_args['page_slug'] ) ) ? $upsale_args['page_slug'] : '';
844
845 ?>
846 <script type="text/javascript">
847 jQuery(document).ready( function($) {
848 <?php if( ! empty( $plugin_slug ) && ! empty( $plugin_file ) ) : ?>
849 $('#plugin-install-core-<?php echo $this->plugin_name; ?>').on('click', function (e) {
850 var self = $(this);
851 e.preventDefault();
852 self.addClass('install-now updating-message');
853 self.text('<?php echo esc_js( 'Installing...' ); ?>');
854
855 $.ajax({
856 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
857 type: 'POST',
858 data: {
859 action: 'wpdeveloper_upsale_core_install_<?php echo $this->plugin_name; ?>',
860 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_core_install_' . $this->plugin_name); ?>',
861 slug : '<?php echo $plugin_slug; ?>',
862 file : '<?php echo $plugin_file; ?>'
863 },
864 success: function(response) {
865 self.text('<?php echo esc_js( 'Installed' ); ?>');
866 <?php if( ! empty( $page_slug ) ) : ?>
867 window.location.href = '<?php echo admin_url( "admin.php?page={$page_slug}" ); ?>';
868 <?php endif; ?>
869 },
870 error: function(error) {
871 self.removeClass('install-now updating-message');
872 alert( error );
873 },
874 complete: function() {
875 self.attr('disabled', 'disabled');
876 self.removeClass('install-now updating-message');
877 }
878 });
879 });
880
881 <?php endif; ?>
882
883 $('.wpdeveloper-upsale-notice').on('click', 'button.notice-dismiss', function (e) {
884 e.preventDefault();
885 $.ajax({
886 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
887 type: 'post',
888 data: {
889 action: 'wpdeveloper_upsale_notice_dissmiss_for_<?php echo $this->plugin_name; ?>',
890 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_notice_dissmiss'); ?>',
891 dismiss: true
892 },
893 success: function(response) {
894 console.log('Successfully saved!');
895 },
896 error: function(error) {
897 console.log('Something went wrong!');
898 },
899 complete: function() {
900 console.log('Its Complete.');
901 }
902 });
903 });
904 } );
905 </script>
906
907 <?php
908 }
909 }