PluginProbe ʕ •ᴥ•ʔ
Essential Addons for Elementor – Popular Elementor Templates & Widgets / 5.8.7
Essential Addons for Elementor – Popular Elementor Templates & Widgets v5.8.7
6.6.7 6.6.6 6.6.5 6.6.4 6.6.3 5.7.3 5.7.4 5.8.0 5.8.1 5.8.10 5.8.11 5.8.12 5.8.13 5.8.14 5.8.15 5.8.16 5.8.18 5.8.2 5.8.3 5.8.4 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.10 5.9.11 5.9.12 5.9.13 5.9.14 5.9.15 5.9.16 5.9.17 5.9.18 5.9.19 5.9.2 5.9.20 5.9.21 5.9.22 5.9.23 5.9.24 5.9.25 5.9.26 5.9.27 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 6.0.1 6.0.10 6.0.11 6.0.12 6.0.13 6.0.14 6.0.15 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 6.0.8 6.0.9 6.1.0 6.1.1 6.1.10 6.1.11 trunk 6.1.12 1.0.0 6.1.13 1.0.1 6.1.14 1.1.0 6.1.15 2.0 6.1.17 2.1 6.1.18 2.10.0 6.1.19 2.10.1 6.1.2 2.10.2 6.1.20 2.10.3 6.1.3 2.10.4 6.1.4 2.10.5 6.1.5 2.2.0 6.1.6 2.2.1 6.1.7 2.2.2 6.1.8 2.2.3 6.1.9 2.2.4 6.2.0 2.2.5 6.2.1 2.3.0 6.2.2 2.3.1 6.2.3 2.4.0 6.2.4 2.4.1 6.3.0 2.4.2 6.3.1 2.4.3 6.3.2 2.5.0 6.3.3 2.6.0 6.4.0 2.7.0 6.5.0 2.7.1 6.5.1 2.7.10 6.5.10 2.7.11 6.5.11 2.7.2 6.5.12 2.7.3 6.5.13 2.7.4 6.5.2 2.7.5 6.5.3 2.7.6 6.5.4 2.7.7 6.5.5 2.7.8 6.5.6 2.7.9 6.5.7 2.8.0 6.5.8 2.8.1 6.5.9 2.8.2 6.6.0 2.8.3 6.6.1 2.8.4 6.6.2 2.8.5 2.8.6 2.8.7 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.4.1 3.1.5 3.2.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 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.2 3.9.3 3.9.4 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 5.0.0 5.0.1 5.0.10 5.0.11 5.0.12 5.0.13 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.4.6 5.5.5 5.6.5 5.7.2
essential-addons-for-elementor-lite / includes / Classes / WPDeveloper_Notice.php
essential-addons-for-elementor-lite / includes / Classes Last commit date
Asset_Builder.php 2 years ago Bootstrap.php 2 years ago Elements_Manager.php 2 years ago Helper.php 2 years ago Migration.php 2 years ago Plugin_Usage_Tracker.php 2 years ago WPDeveloper_Core_Installer.php 4 years ago WPDeveloper_Notice.php 2 years ago WPDeveloper_Plugin_Installer.php 4 years ago WPDeveloper_Setup_Wizard.php 2 years ago index.php 3 years ago
WPDeveloper_Notice.php
935 lines
1 <?php
2 namespace Essential_Addons_Elementor\Classes;
3
4 if (!defined('ABSPATH')) {
5 exit;
6 } // Exit if accessed directly.
7
8 use Essential_Addons_Elementor\Classes\Helper;
9 use Essential_Addons_Elementor\Classes\WPDeveloper_Core_Installer;
10
11 class WPDeveloper_Notice {
12 /**
13 * Admin Notice Key
14 *
15 * @var array
16 */
17 const ADMIN_UPDATE_NOTICE_KEY = 'wpdeveloper_notices_seen';
18
19 /**
20 * All Data
21 * @var array
22 */
23 private $data = array();
24 private $properties = array(
25 'links', 'message', 'thumbnail',
26 );
27 private $methods = array(
28 'message', 'thumbnail', 'classes'
29 );
30 /**
31 * cne_day == current_notice_end_day
32 *
33 * @var integer
34 */
35 public $cne_time = '2 day';
36 public $maybe_later_time = '7 day';
37 public $finish_time = [];
38 /**
39 * Plugin Name
40 *
41 * @var string
42 */
43 private $plugin_name;
44 /**
45 * Plugin File Name
46 * @var string
47 */
48 private $plugin_file;
49 /**
50 * First Install Version Of The Plugin
51 *
52 * @var string
53 */
54 private $version;
55 /**
56 * Saved Data in DB
57 * @var array
58 */
59 private $options_data;
60 /**
61 * Current Timestamp
62 * @var integer
63 */
64 public $timestamp;
65 /**
66 * Primary Notice Action
67 *
68 * @var string
69 */
70 private $do_notice_action;
71 /**
72 * Default Options Set
73 *
74 * @var array
75 */
76 public $options_args = array(
77
78 );
79 /**
80 * Notice ID for users.
81 * @var string
82 */
83 private $notice_id;
84 /**
85 * Upsale Notice Arguments
86 * @var array
87 */
88 public $upsale_args;
89 /**
90 * Revoke this function when the object is created.
91 *
92 * @param string $plugin_name
93 * @param string $version
94 */
95 public function __construct( $plugin_file = '', $version = '' ) {
96 $this->plugin_file = $plugin_file;
97 $this->plugin_name = basename( $plugin_file, '.php' );
98 $this->version = $version;
99 $this->timestamp = intval( current_time( 'timestamp' ) );
100 $this->notice_id = 'wpdeveloper_notice_' . str_replace( '.', '_', $this->version );
101
102 $this->do_notice_action = 'wpdeveloper_notices_for_' . $this->plugin_name;
103
104 new WPDeveloper_Core_Installer( $this->plugin_name );
105 }
106 /**
107 * Initiate The Plugin
108 * @return void
109 */
110 public function init(){
111 $this->migration();
112 add_action( 'init', array( $this, 'first_install_track') );
113 add_action( 'deactivate_' . $this->plugin_file, array( $this, 'first_install_end' ) );
114 add_action( 'init', array( $this, 'hooks' ) );
115 }
116 public function migration(){
117 $user_notices = $this->get_user_notices();
118 if( \version_compare( get_option( 'eael_version', false ), '3.7.2', '==' ) && ! get_option( 'eael_notice_migration', false ) ) {
119 if( is_array( $user_notices ) ) {
120 array_walk( $user_notices, function( $value, $key ){
121 array_walk( $value, function( $v, $k ){
122 array_walk( $v, function( $vv, $kk ){
123 update_user_meta( get_current_user_id(), $this->plugin_name . '_' . $vv, true );
124 } );
125 } );
126 } );
127 }
128 update_option( 'eael_notice_migration', true );
129 }
130 }
131 /**
132 * All Hooks
133 * @return void
134 */
135 public function hooks(){
136 add_action( 'wpdeveloper_notice_clicked_for_' . $this->plugin_name, array( $this, 'clicked' ) );
137 add_action( 'wp_ajax_wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name, array( $this, 'upsale_notice_dissmiss' ) );
138 add_action( 'wp_ajax_wpdeveloper_notice_dissmiss_for_' . $this->plugin_name, array( $this, 'notice_dissmiss' ) );
139 add_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name, array( $this, 'before' ) );
140 add_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name, array( $this, 'after' ) );
141 add_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name, array( $this, 'before_upsale' ) );
142 add_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name, array( $this, 'after' ) );
143 add_action( $this->do_notice_action, array( $this, 'content' ) );
144 // if( current_user_can( 'install_plugins' ) ) {
145 if( isset( $_GET['plugin'] ) && $_GET['plugin'] == $this->plugin_name ) {
146 do_action( 'wpdeveloper_notice_clicked_for_' . $this->plugin_name );
147 /**
148 * Redirect User To the Current URL, but without set query arguments.
149 */
150 wp_safe_redirect( $this->redirect_to() );
151 }
152 $return_notice = $this->next_notice();
153 $current_notice = current( $return_notice );
154 $next_notice = next( $return_notice );
155
156 $deserve_notice = $this->deserve_notice( $current_notice );
157 $options_data = $this->get_options_data();
158 $user_notices = $this->get_user_notices();
159
160 $notice_time = isset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] )
161 ? $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] : $this->timestamp;
162 $next_notice_time = $next_notice ? $options_data[ $this->plugin_name ]['notice_will_show'][ $next_notice ] : $this->timestamp;
163 $current_notice_end = $this->makeTime( $notice_time, $this->cne_time );
164
165 if( ! $deserve_notice ) {
166 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
167 $this->update_options_data( $options_data[ $this->plugin_name ] );
168 }
169
170 if( $deserve_notice ) {
171 /**
172 * TODO: automatic maybe later setup with time.
173 */
174 if( ( $this->timestamp >= $current_notice_end ) || ( $this->timestamp > $next_notice_time ) ) {
175 $this->maybe_later( $current_notice );
176 $notice_time = false;
177 }
178
179 if( isset( $this->finish_time[ $current_notice ] ) ) {
180 if( $this->timestamp >= strtotime( $this->finish_time[ $current_notice ] ) ) {
181 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
182 $this->update_options_data( $options_data[ $this->plugin_name ] );
183 $notice_time = false;
184 }
185 }
186
187 if( $notice_time != false ) {
188 if( $notice_time <= $this->timestamp ) {
189 if( $current_notice === 'upsale' ) {
190 $upsale_args = $this->get_upsale_args();
191 if( empty( $upsale_args ) ) {
192 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
193 $this->update_options_data( $options_data[ $this->plugin_name ] );
194 } else {
195 /**
196 * For Upsale Remove
197 * if the plugin is activated.
198 */
199 if( isset( $upsale_args['condition'], $upsale_args['condition']['by'] ) ) {
200 switch( $upsale_args['condition']['by'] ) {
201 case 'class' :
202 if( isset( $upsale_args['condition']['class'] ) && class_exists( $upsale_args['condition']['class'] ) ) {
203 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
204 $this->update_options_data( $options_data[ $this->plugin_name ] );
205 return;
206 }
207 break;
208 case 'function' :
209 if( isset( $upsale_args['condition']['function'] ) && function_exists( $upsale_args['condition']['function'] ) ) {
210 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $current_notice ] );
211 $this->update_options_data( $options_data[ $this->plugin_name ] );
212 return;
213 }
214 break;
215 }
216 }
217 if ( ! function_exists( 'get_plugins' ) ) {
218 include ABSPATH . '/wp-admin/includes/plugin.php';
219 }
220 $plugins = get_plugins();
221 $pkey = $upsale_args['slug'] . '/' . $upsale_args['file'];
222 if( isset( $plugins[ $pkey ] ) ) {
223 $this->update( $current_notice );
224 return;
225 }
226 add_action( 'admin_notices', array( $this, 'upsale_notice' ) );
227 add_action( 'eael_admin_notices', array( $this, 'upsale_notice' ) );
228 }
229 } else {
230 if( $this->is_ok( 'message', $current_notice ) || $current_notice === 'opt_in' ) {
231 add_action( 'admin_notices', array( $this, 'admin_notices' ) );
232 add_action( 'eael_admin_notices', array( $this, 'admin_notices' ) );
233 }
234 }
235 }
236 }
237 }
238 // }
239 }
240 /**
241 * Make time using timestamp and a string like 2 Hour, 2 Day, 30 Minutes, 1 Week, 1 year
242 * @param integer $current
243 * @param string $time
244 * @return integer
245 */
246 public function makeTime( $current, $time ) {
247 return intval( strtotime( date( 'Y-m-d h:i:s', intval( $current ) ) . " +$time" ) );
248 }
249 /**
250 * Automatice Maybe Later.
251 * @param string $notice
252 * @return void
253 */
254 private function maybe_later( $notice ){
255 if( empty( $notice ) ) {
256 return;
257 }
258 $options_data = $this->get_options_data();
259 $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] = $this->makeTime( $this->timestamp, $this->maybe_later_time );
260 $this->update_options_data( $options_data[ $this->plugin_name ] );
261 }
262 /**
263 * When links are clicked, this function will invoked.
264 * @return void
265 */
266 public function clicked(){
267 if( isset( $_GET['plugin'] ) ) {
268 $plugin = sanitize_text_field( $_GET['plugin'] );
269 if( $plugin === $this->plugin_name ) {
270 $options_data = $this->get_options_data();
271 $clicked_from = current( $this->next_notice() );
272 if( isset( $_GET['plugin_action'] ) ) {
273 $plugin_action = sanitize_text_field( $_GET['plugin_action'] );
274 }
275 if( isset( $_GET['dismiss'] ) ) {
276 $dismiss = sanitize_text_field( $_GET['dismiss'] );
277 }
278 if( isset( $_GET['later'] ) ) {
279 $later = sanitize_text_field( $_GET['later'] );
280 }
281
282 $later_time = '';
283
284 switch( $clicked_from ) {
285
286 case 'opt_in' :
287 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
288 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
289 break;
290
291 case 'first_install' :
292 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
293 break;
294
295 case 'update' :
296 $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
297 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
298 break;
299 // case 'update_400k' :
300 // $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ;
301 // $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
302 // break;
303 case 'review' :
304 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
305 break;
306 case 'upsale' :
307 $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time );
308 break;
309 }
310
311 if( isset( $later ) && $later == true ) {
312 $options_data[ $this->plugin_name ]['notice_will_show'][ $clicked_from ] = $later_time;
313 }
314 if( isset( $dismiss ) && $dismiss == true ) {
315 update_user_meta( get_current_user_id(), $this->plugin_name . '_' . $clicked_from, true );
316 $this->update( $clicked_from );
317 }
318 $this->update_options_data( $options_data[ $this->plugin_name ] );
319 }
320 }
321 }
322 /**
323 * For Redirecting Current Page without Arguments!
324 *
325 * @return void
326 */
327 private function redirect_to(){
328 $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
329 $query_string = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
330 wp_parse_str( $query_string, $current_url );
331
332 $unset_array = array( 'dismiss', 'plugin', '_wpnonce', 'later', 'plugin_action', 'marketing_optin' );
333
334 foreach( $unset_array as $value ) {
335 if( isset( $current_url[ $value ] ) ) {
336 unset( $current_url[ $value ] );
337 }
338 }
339
340 $current_url = http_build_query($current_url);
341 $redirect_url = $request_uri . '?' . $current_url;
342 return $redirect_url;
343 }
344 /**
345 * Before Notice
346 * @return void
347 */
348 public function before(){
349 $current_notice = current( $this->next_notice() );
350 $classes = 'notice notice-info put-dismiss-notice';
351 if( isset( $this->data['classes'] ) ) {
352 if( isset( $this->data['classes'][ $current_notice ] ) ) {
353 $classes = $this->data['classes'][ $current_notice ];
354 }
355 }
356
357 if( $this->has_thumbnail( $current_notice ) ) {
358 $classes .= 'notice-has-thumbnail';
359 }
360
361 echo '<div class="'. $classes .' wpdeveloper-'. $current_notice .'-notice" data-notice="'. $current_notice .'">';
362 }
363 /**
364 * After Notice
365 * @return void
366 */
367 public function after(){
368 echo '</div>';
369 }
370 /**
371 * Content generation & Hooks Funciton.
372 * @return void
373 */
374 public function content(){
375 $options_data = $this->get_options_data();
376 $notice = current( $this->next_notice() );
377
378 switch( $notice ) {
379 case 'opt_in' :
380 do_action('wpdeveloper_optin_notice_for_' . $this->plugin_name );
381 break;
382 case 'first_install' :
383 if( $options_data[ $this->plugin_name ]['first_install'] !== 'deactivated' ) {
384 do_action( 'wpdeveloper_first_install_notice_for_' . $this->plugin_name );
385 $this->get_thumbnail( 'first_install' );
386 $this->get_message( 'first_install' );
387 }
388 break;
389 case 'update' :
390 do_action( 'wpdeveloper_update_notice_for_' . $this->plugin_name );
391 $this->dismiss_button_scripts();
392 $this->get_thumbnail( 'update' );
393 $this->get_message( 'update' );
394 break;
395 // case 'update_400k' :
396 // do_action( 'wpdeveloper_update_notice_for_' . $this->plugin_name );
397 // $this->dismiss_button_scripts();
398 // $this->get_thumbnail( 'update_400k' );
399 // $this->get_message( 'update_400k' );
400 // break;
401 case 'review' :
402 do_action( 'wpdeveloper_review_notice_for_' . $this->plugin_name );
403 $this->get_thumbnail( 'review' );
404 $this->get_message( 'review' );
405 break;
406 }
407 }
408 /**
409 * Before Upsale Notice
410 * @return void
411 */
412 public function before_upsale(){
413 $classes = '';
414 if( $this->has_thumbnail('upsale') ) {
415 $classes = 'notice-has-thumbnail';
416 }
417 echo '<div class="error notice is-dismissible wpdeveloper-upsale-notice '. $classes .'">';
418 }
419 /**
420 * Upsale Notice
421 */
422 public function upsale_notice(){
423 do_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name );
424 do_action('wpdeveloper_upsale_notice_for_' . $this->plugin_name);
425 $this->get_thumbnail( 'upsale' );
426 $this->get_message( 'upsale' );
427 do_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name );
428 $this->upsale_button_script();
429 }
430 /**
431 * Get upsale arguments.
432 * @return void
433 */
434 private function get_upsale_args(){
435 return ( empty( $this->upsale_args ) ) ? array() : $this->upsale_args;
436 }
437 /**
438 * This function is responsible for making the button visible to the upsale notice.
439 */
440 private function upsale_button(){
441 $upsale_args = $this->get_upsale_args();
442 $plugin_slug = ( isset( $upsale_args['slug'] )) ? $upsale_args['slug'] : '' ;
443 $btn_text = ( isset( $upsale_args['btn_text'] )) ? $upsale_args['btn_text'] : __( 'Install Now!', 'essential-addons-for-elementor-lite' ) ;
444 if( empty( $plugin_slug ) ) {
445 return;
446 }
447 echo '<button data-slug="'. esc_attr( $plugin_slug ) .'" id="plugin-install-core-'. $this->plugin_name .'" class="button button-primary">'. Helper::eael_wp_kses( $btn_text ) .'</button>';
448 }
449 /**
450 * This methods is responsible for get notice image.
451 *
452 * @param string $msg_for
453 * @return void
454 */
455 protected function get_thumbnail( $msg_for ){
456 $output = '';
457 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
458 $output = '<div class="wpdeveloper-notice-thumbnail">';
459 $output .= '<img src="'. esc_url( $this->data['thumbnail'][ $msg_for ] ) .'" alt="">';
460 $output .= '</div>';
461 }
462 echo wp_kses_post( $output );
463
464 }
465 /**
466 * Has Thumbnail Check
467 *
468 * @param string $msg_for
469 * @return boolean
470 */
471 protected function has_thumbnail( $msg_for = '' ){
472 if( empty( $msg_for ) ) {
473 return false;
474 }
475 if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) {
476 return true;
477 }
478 return false;
479 }
480 /**
481 * This method is responsible for get messages.
482 *
483 * @param string $msg_for
484 * @return void
485 */
486 protected function get_message( $msg_for ){
487 if( isset( $this->data['message'] ) && isset( $this->data['message'][ $msg_for ] ) ) {
488 echo '<div class="wpdeveloper-notice-message">';
489 echo Helper::eael_wp_kses( $this->data['message'][ $msg_for ] );
490 if( $msg_for === 'upsale' ) {
491 $this->upsale_button();
492 }
493 $this->dismissible_notice( $msg_for );
494 echo '</div>';
495 }
496 }
497 /**
498 * Detect which notice will show @ next.
499 * @return array
500 */
501 protected function next_notice() {
502 $options_data = $this->get_options_data();
503 if ( ! $options_data ) {
504 $args = $this->get_args();
505 $return_notice = $args['notice_will_show'];
506 } else {
507 $return_notice = $options_data[ $this->plugin_name ]['notice_will_show'];
508 }
509
510 if ( is_array( $return_notice ) ) {
511 $return_notice = array_flip( $return_notice );
512 ksort( $return_notice );
513 }
514
515 return (array) $return_notice;
516 }
517 /**
518 * Which notice is deserve to show in next slot.
519 * @param string $notice
520 * @return boolean
521 */
522 private function deserve_notice( $notice ) {
523 $notices = $this->get_user_notices();
524 if( $notice === false ) {
525 return false;
526 }
527 if( empty( $notices ) ) {
528 return true;
529 } else {
530 if( isset( $notices[ $this->notice_id ] ) && isset( $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
531 if( in_array( $notice, $notices[ $this->notice_id ][ $this->plugin_name ] ) ) {
532 return false;
533 } else {
534 return true;
535 }
536 } else {
537 return true;
538 }
539 }
540 }
541 /**
542 * This is the main methods for generate the notice.
543 * @return void
544 */
545 public function admin_notices(){
546 $current_notice = current( $this->next_notice() );
547 if( get_user_meta( get_current_user_id(), $this->plugin_name . '_' . $current_notice, true ) ) {
548 return;
549 }
550 if( $current_notice == 'opt_in' ) {
551 do_action( $this->do_notice_action );
552 return;
553 }
554 do_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name );
555 do_action( $this->do_notice_action );
556 do_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name );
557 }
558 /**
559 * This method is responsible for all dismissible links generation.
560 * @param string $links_for
561 * @return void
562 */
563 public function dismissible_notice( $links_for = '' ){
564 if( empty( $links_for ) ) {
565 return;
566 }
567 $links = isset( $this->data['links'][ $links_for ] ) ? $this->data['links'][ $links_for ] : false;
568 if( $links ) :
569 $output = '<ul class="wpdeveloper-notice-link">';
570 foreach( $links as $key => $link_value ) {
571 if( ! empty( $link_value['label'] ) ) {
572 $output .= '<li>';
573 if( isset( $link_value['link'] ) ) {
574 $link = $link_value['link'];
575 $target = isset( $link_value['target'] ) ? 'target="'. esc_attr( $link_value['target'] ) .'"' : '';
576 if( isset( $link_value['data_args'] ) && is_array( $link_value['data_args'] ) ) {
577 $data_args = [];
578 foreach( $link_value['data_args'] as $key => $args_value ) {
579 $data_args[ $key ] = $args_value;
580 }
581 $data_args[ 'plugin' ] = $this->plugin_name;
582 $normal_link = add_query_arg( $data_args, $link );
583 $link = wp_nonce_url( $normal_link, 'wpdeveloper-nonce' );
584 }
585 $class = '';
586 if( isset( $link_value['link_class'] ) ) {
587 $class = 'class="' . sanitize_html_class( implode( ' ', $link_value['link_class'] ) ) . '"';
588 }
589 $output .= '<a '. $class .' href="'. esc_url( $link ) .'" '. $target .'>';
590 }
591 if( isset( $link_value['icon_class'] ) ) {
592 $output .= '<span class="'. esc_attr( $link_value['icon_class'] ) .'"></span>';
593 }
594 if( isset( $link_value['icon_img'] ) ) {
595 $output .= '<img src="'. esc_url( $link_value['icon_img'] ) .'" alt="" />';
596 }
597 $output .= $link_value['label'];
598 if( isset( $link_value['link'] ) ) {
599 $output .= '</a>';
600 }
601 $output .= '</li>';
602 }
603 }
604 $output .= '</ul>';
605
606 printf( '%1$s', $output );
607 endif;
608 }
609 /**
610 * First Installation Track
611 * @return void
612 */
613 public function first_install_track( $args = array() ){
614 if( ! current_user_can( 'manage_options' ) ) {
615 return;
616 }
617 if( empty( $args ) ) {
618 $args = array(
619 'time' => $this->timestamp,
620 'version' => $this->version,
621 );
622 }
623 $options_data = $this->get_options_data();
624 $args = wp_parse_args( $args, $this->get_args() );
625 if( ! isset( $options_data[ $this->plugin_name ] )
626 || ( isset( $options_data[ $this->plugin_name ]['version'] ) && version_compare( $options_data[ $this->plugin_name ]['version'], $this->version, '!=' ) ) ) {
627 $this->update_options_data( $args );
628 }
629 }
630 /**
631 * First Installation Deactive Track
632 *
633 * @return void
634 */
635 public function first_install_end(){
636 // $args = array(
637 // 'first_install' => 'deactivated'
638 // );
639 // $options_data = $this->get_options_data();
640 // if( isset( $options_data[ $this->plugin_name ] ) ) {
641 // $args = wp_parse_args( $args, $options_data[ $this->plugin_name ] );
642 // $this->update_options_data( $args );
643 // }
644 delete_option( 'wpdeveloper_plugins_data' );
645 }
646 /**
647 * Get all options from database!
648 * @return void
649 */
650 protected function get_options_data( $key = '' ) {
651 $options_data = get_option( 'wpdeveloper_plugins_data', [] );
652 if ( empty( $key ) ) {
653 return $options_data;
654 }
655
656 if ( isset( $options_data[ $this->plugin_name ][ $key ] ) ) {
657 return $options_data[ $this->plugin_name ][ $key ];
658 }
659
660 return [];
661 }
662 /**
663 * This will update the options table for plugins.
664 *
665 * @param mixed $new_data
666 * @param array $args
667 * @return void
668 */
669 protected function update_options_data( $args = array() ){
670 $options_data = $this->get_options_data();
671 $options_data[ $this->plugin_name ] = $args;
672 update_option( 'wpdeveloper_plugins_data', $options_data );
673 }
674 /**
675 * Set properties data, for some selected properties.
676 *
677 * @param string $name
678 * @param mixed $value
679 */
680 public function __set( $name, $value ){
681 if( in_array( $name, $this->properties ) ) {
682 $this->data[ $name ] = $value;
683 }
684 }
685 /**
686 * Invoked when some selected methods are called
687 *
688 * @param string $name
689 * @param array $values
690 * @return void
691 */
692 public function __call( $name, $values ){
693 if( in_array( $name, $this->methods ) ) {
694 $this->data[ $name ][ $values[0] ] = $values[1];
695 }
696 }
697 protected function is_ok( $name, $notice ){
698 if( isset( $this->data[ $name ], $this->data[ $name ][ $notice ] ) ) {
699 return true;
700 }
701 return false;
702 }
703 /**
704 * Get all option arguments.
705 * @param string $key
706 * @return array
707 */
708 private function get_args( $key = '' ){
709 if( empty( $key ) ) {
710 return $this->options_args;
711 }
712
713 if( isset( $this->options_args[ $key ] ) ) {
714 return $this->options_args[ $key ];
715 }
716
717 return false;
718 }
719 /**
720 * Resetting data on update.
721 * @return void
722 */
723 private function set_args_on_update(){
724 $args = $this->get_args();
725 $options_data = $this->get_options_data();
726 $set_data = $options_data[ $this->plugin_name ];
727 $args = wp_parse_args( $set_data, $args );
728 $this->update_options_data( $args );
729 }
730 /**
731 * When upgrade is complete. it will fired.
732 * @param WP_Upgrader $upgrader_object
733 * @param array $options
734 * @return void
735 */
736 public function upgrade_completed( $upgrader_object, $options ) {
737 // If an update has taken place and the updated type is plugins and the plugins element exists
738 if( isset( $options['action'] ) && $options['action'] == 'update' && $options['type'] == 'plugin' ) {
739 if( ! isset( $options['plugin'] ) && isset( $options['plugins'] ) ) {
740 foreach( $options['plugins'] as $plugin ) {
741 if( $plugin == $this->plugin_name ) {
742 $this->set_args_on_update();
743 }
744 }
745 }
746
747 if( isset( $options['plugin'] ) && $options['plugin'] == $this->plugin_name ) {
748 $this->set_args_on_update();
749 }
750 }
751 }
752 /**
753 * This function is responsible for get_user_notices
754 * @return void
755 */
756 private function get_user_notices() {
757 $notices = get_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, true );
758 return ! $notices ? array() : $notices;
759 }
760 /**
761 * This function is responsible for update meta information.
762 *
763 * @param string $notice
764 * @return void
765 */
766 private function update( $notice ){
767 if( empty( $notice ) ) {
768 return;
769 }
770 $options_data = $this->get_options_data();
771 $user_notices = $this->get_user_notices();
772 $user_notices[ $this->notice_id ][ $this->plugin_name ][] = $notice;
773 // Remove the upsale from notice_will_show field in options DB.
774 unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] );
775 $this->update_options_data( $options_data[ $this->plugin_name ] );
776 // Set users meta, not to show again current_version notice.
777 update_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, $user_notices);
778 }
779
780 public function notice_dissmiss(){
781 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_notice_dissmiss' ) ) {
782 return;
783 }
784
785 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_notice_dissmiss_for_' . $this->plugin_name ) ) {
786 return;
787 }
788
789 $dismiss = isset( $_POST['dismiss'] ) ? sanitize_text_field( $_POST['dismiss'] ) : false;
790 $notice = isset( $_POST['notice'] ) ? sanitize_text_field( $_POST['notice'] ) : false;
791 if( $dismiss ) {
792 $this->update( $notice );
793 update_user_meta( get_current_user_id(), $this->plugin_name . '_' . $notice, true );
794 echo 'success';
795 } else {
796 echo 'failed';
797 }
798 die();
799 }
800
801 /**
802 * This function is responsible for do action when
803 * the dismiss button clicked in upsale notice.
804 */
805 public function upsale_notice_dissmiss(){
806
807 if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_upsale_notice_dissmiss' ) ) {
808 return;
809 }
810
811 if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name ) ) {
812 return;
813 }
814
815 $dismiss = isset( $_POST['dismiss'] ) ? sanitize_text_field( $_POST['dismiss'] ) : false;
816 if( $dismiss ) {
817 $this->update( 'upsale' );
818 echo 'success';
819 } else {
820 echo 'failed';
821 }
822 die();
823 }
824
825 public function dismiss_button_scripts(){
826 ?>
827 <script type="text/javascript">
828 jQuery(document).ready( function($) {
829 var wpdevNotice = $('.notice.is-dismissible');
830 if( wpdevNotice.length > 0 ) {
831 $('body').on('click', 'button.notice-dismiss', function (e) {
832 e.preventDefault();
833 $.ajax({
834 url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>',
835 type: 'post',
836 data: {
837 action: 'wpdeveloper_notice_dissmiss_for_<?php echo esc_html( $this->plugin_name ); ?>',
838 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_notice_dissmiss'); ?>',
839 dismiss: true,
840 notice: wpdevNotice.data('notice'),
841 },
842 success: function(response) {
843 $('.notice').hide();
844 console.log('Successfully saved!');
845 },
846 error: function(error) {
847 console.log('Something went wrong!');
848 },
849 complete: function() {
850 console.log('Its Complete.');
851 }
852 });
853 });
854 }
855 } );
856 </script>
857 <?php
858 }
859
860 /**
861 * Upsale Button Script.
862 * When install button is clicked, it will do its own things.
863 * also for dismiss button JS.
864 * @return void
865 */
866 public function upsale_button_script(){
867 $upsale_args = $this->get_upsale_args();
868
869 $plugin_slug = ( isset( $upsale_args['slug'] ) ) ? $upsale_args['slug'] : '';
870 $plugin_file = ( isset( $upsale_args['file'] ) ) ? $upsale_args['file'] : '';
871 $page_slug = ( isset( $upsale_args['page_slug'] ) ) ? $upsale_args['page_slug'] : '';
872
873 ?>
874 <script type="text/javascript">
875 jQuery(document).ready( function($) {
876 <?php if( ! empty( $plugin_slug ) && ! empty( $plugin_file ) ) : ?>
877 $('#plugin-install-core-<?php echo esc_html( $this->plugin_name ); ?>').on('click', function (e) {
878 var self = $(this);
879 e.preventDefault();
880 self.addClass('install-now updating-message');
881 self.text('<?php echo esc_js( 'Installing...' ); ?>');
882
883 $.ajax({
884 url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>',
885 type: 'POST',
886 data: {
887 action: 'wpdeveloper_upsale_core_install_<?php echo esc_html( $this->plugin_name ); ?>',
888 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_core_install_' . esc_html( $this->plugin_name )); ?>',
889 slug : '<?php echo esc_html( $plugin_slug ); ?>',
890 file : '<?php echo esc_html( $plugin_file ); ?>'
891 },
892 success: function(response) {
893 self.text('<?php echo esc_js( 'Installed' ); ?>');
894 <?php if( ! empty( $page_slug ) ) : ?>
895 window.location.href = '<?php echo esc_url( admin_url( "admin.php?page={$page_slug}" ) ); ?>';
896 <?php endif; ?>
897 },
898 error: function(error) {
899 self.removeClass('install-now updating-message');
900 },
901 complete: function() {
902 self.attr('disabled', 'disabled');
903 self.removeClass('install-now updating-message');
904 }
905 });
906 });
907 <?php endif; ?>
908 $('.wpdeveloper-upsale-notice').on('click', 'button.notice-dismiss', function (e) {
909 e.preventDefault();
910 $.ajax({
911 url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>',
912 type: 'post',
913 data: {
914 action: 'wpdeveloper_upsale_notice_dissmiss_for_<?php echo esc_html( $this->plugin_name ); ?>',
915 _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_notice_dissmiss'); ?>',
916 dismiss: true
917 },
918 success: function(response) {
919 console.log('Successfully saved!');
920 },
921 error: function(error) {
922 console.log('Something went wrong!');
923 },
924 complete: function() {
925 console.log('Its Complete.');
926 }
927 });
928 });
929 } );
930 </script>
931
932 <?php
933 }
934 }
935