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