Bootstrap.php
4 years ago
Helper.php
4 years ago
Migration.php
4 years ago
Plugin_Usage_Tracker.php
4 years ago
WPDeveloper_Core_Installer.php
4 years ago
WPDeveloper_Notice.php
4 years ago
WPDeveloper_Plugin_Installer.php
4 years ago
WPDeveloper_Setup_Wizard.php
4 years ago
WPDeveloper_Notice.php
932 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 | } |
| 228 | } else { |
| 229 | if( $this->is_ok( 'message', $current_notice ) || $current_notice === 'opt_in' ) { |
| 230 | add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | // } |
| 237 | } |
| 238 | /** |
| 239 | * Make time using timestamp and a string like 2 Hour, 2 Day, 30 Minutes, 1 Week, 1 year |
| 240 | * @param integer $current |
| 241 | * @param string $time |
| 242 | * @return integer |
| 243 | */ |
| 244 | public function makeTime( $current, $time ) { |
| 245 | return intval( strtotime( date( 'Y-m-d h:i:s', intval( $current ) ) . " +$time" ) ); |
| 246 | } |
| 247 | /** |
| 248 | * Automatice Maybe Later. |
| 249 | * @param string $notice |
| 250 | * @return void |
| 251 | */ |
| 252 | private function maybe_later( $notice ){ |
| 253 | if( empty( $notice ) ) { |
| 254 | return; |
| 255 | } |
| 256 | $options_data = $this->get_options_data(); |
| 257 | $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] = $this->makeTime( $this->timestamp, $this->maybe_later_time ); |
| 258 | $this->update_options_data( $options_data[ $this->plugin_name ] ); |
| 259 | } |
| 260 | /** |
| 261 | * When links are clicked, this function will invoked. |
| 262 | * @return void |
| 263 | */ |
| 264 | public function clicked(){ |
| 265 | if( isset( $_GET['plugin'] ) ) { |
| 266 | $plugin = sanitize_text_field( $_GET['plugin'] ); |
| 267 | if( $plugin === $this->plugin_name ) { |
| 268 | $options_data = $this->get_options_data(); |
| 269 | $clicked_from = current( $this->next_notice() ); |
| 270 | if( isset( $_GET['plugin_action'] ) ) { |
| 271 | $plugin_action = sanitize_text_field( $_GET['plugin_action'] ); |
| 272 | } |
| 273 | if( isset( $_GET['dismiss'] ) ) { |
| 274 | $dismiss = sanitize_text_field( $_GET['dismiss'] ); |
| 275 | } |
| 276 | if( isset( $_GET['later'] ) ) { |
| 277 | $later = sanitize_text_field( $_GET['later'] ); |
| 278 | } |
| 279 | |
| 280 | $later_time = ''; |
| 281 | |
| 282 | switch( $clicked_from ) { |
| 283 | |
| 284 | case 'opt_in' : |
| 285 | $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ; |
| 286 | $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time ); |
| 287 | break; |
| 288 | |
| 289 | case 'first_install' : |
| 290 | $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time ); |
| 291 | break; |
| 292 | |
| 293 | case 'update' : |
| 294 | $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ; |
| 295 | $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time ); |
| 296 | break; |
| 297 | // case 'update_400k' : |
| 298 | // $dismiss = ( isset( $plugin_action ) ) ? $plugin_action : false ; |
| 299 | // $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time ); |
| 300 | // break; |
| 301 | case 'review' : |
| 302 | $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time ); |
| 303 | break; |
| 304 | case 'upsale' : |
| 305 | $later_time = $this->makeTime( $this->timestamp, $this->maybe_later_time ); |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | if( isset( $later ) && $later == true ) { |
| 310 | $options_data[ $this->plugin_name ]['notice_will_show'][ $clicked_from ] = $later_time; |
| 311 | } |
| 312 | if( isset( $dismiss ) && $dismiss == true ) { |
| 313 | update_user_meta( get_current_user_id(), $this->plugin_name . '_' . $clicked_from, true ); |
| 314 | $this->update( $clicked_from ); |
| 315 | } |
| 316 | $this->update_options_data( $options_data[ $this->plugin_name ] ); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | /** |
| 321 | * For Redirecting Current Page without Arguments! |
| 322 | * |
| 323 | * @return void |
| 324 | */ |
| 325 | private function redirect_to(){ |
| 326 | $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); |
| 327 | $query_string = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY ); |
| 328 | wp_parse_str( $query_string, $current_url ); |
| 329 | |
| 330 | $unset_array = array( 'dismiss', 'plugin', '_wpnonce', 'later', 'plugin_action', 'marketing_optin' ); |
| 331 | |
| 332 | foreach( $unset_array as $value ) { |
| 333 | if( isset( $current_url[ $value ] ) ) { |
| 334 | unset( $current_url[ $value ] ); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | $current_url = http_build_query($current_url); |
| 339 | $redirect_url = $request_uri . '?' . $current_url; |
| 340 | return $redirect_url; |
| 341 | } |
| 342 | /** |
| 343 | * Before Notice |
| 344 | * @return void |
| 345 | */ |
| 346 | public function before(){ |
| 347 | $current_notice = current( $this->next_notice() ); |
| 348 | $classes = 'notice notice-info put-dismiss-notice'; |
| 349 | if( isset( $this->data['classes'] ) ) { |
| 350 | if( isset( $this->data['classes'][ $current_notice ] ) ) { |
| 351 | $classes = $this->data['classes'][ $current_notice ]; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if( $this->has_thumbnail( $current_notice ) ) { |
| 356 | $classes .= 'notice-has-thumbnail'; |
| 357 | } |
| 358 | |
| 359 | echo '<div class="'. $classes .' wpdeveloper-'. $current_notice .'-notice" data-notice="'. $current_notice .'">'; |
| 360 | } |
| 361 | /** |
| 362 | * After Notice |
| 363 | * @return void |
| 364 | */ |
| 365 | public function after(){ |
| 366 | echo '</div>'; |
| 367 | } |
| 368 | /** |
| 369 | * Content generation & Hooks Funciton. |
| 370 | * @return void |
| 371 | */ |
| 372 | public function content(){ |
| 373 | $options_data = $this->get_options_data(); |
| 374 | $notice = current( $this->next_notice() ); |
| 375 | |
| 376 | switch( $notice ) { |
| 377 | case 'opt_in' : |
| 378 | do_action('wpdeveloper_optin_notice_for_' . $this->plugin_name ); |
| 379 | break; |
| 380 | case 'first_install' : |
| 381 | if( $options_data[ $this->plugin_name ]['first_install'] !== 'deactivated' ) { |
| 382 | do_action( 'wpdeveloper_first_install_notice_for_' . $this->plugin_name ); |
| 383 | $this->get_thumbnail( 'first_install' ); |
| 384 | $this->get_message( 'first_install' ); |
| 385 | } |
| 386 | break; |
| 387 | case 'update' : |
| 388 | do_action( 'wpdeveloper_update_notice_for_' . $this->plugin_name ); |
| 389 | $this->dismiss_button_scripts(); |
| 390 | $this->get_thumbnail( 'update' ); |
| 391 | $this->get_message( 'update' ); |
| 392 | break; |
| 393 | // case 'update_400k' : |
| 394 | // do_action( 'wpdeveloper_update_notice_for_' . $this->plugin_name ); |
| 395 | // $this->dismiss_button_scripts(); |
| 396 | // $this->get_thumbnail( 'update_400k' ); |
| 397 | // $this->get_message( 'update_400k' ); |
| 398 | // break; |
| 399 | case 'review' : |
| 400 | do_action( 'wpdeveloper_review_notice_for_' . $this->plugin_name ); |
| 401 | $this->get_thumbnail( 'review' ); |
| 402 | $this->get_message( 'review' ); |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | /** |
| 407 | * Before Upsale Notice |
| 408 | * @return void |
| 409 | */ |
| 410 | public function before_upsale(){ |
| 411 | $classes = ''; |
| 412 | if( $this->has_thumbnail('upsale') ) { |
| 413 | $classes = 'notice-has-thumbnail'; |
| 414 | } |
| 415 | echo '<div class="error notice is-dismissible wpdeveloper-upsale-notice '. $classes .'">'; |
| 416 | } |
| 417 | /** |
| 418 | * Upsale Notice |
| 419 | */ |
| 420 | public function upsale_notice(){ |
| 421 | do_action( 'wpdeveloper_before_upsale_notice_for_' . $this->plugin_name ); |
| 422 | do_action('wpdeveloper_upsale_notice_for_' . $this->plugin_name); |
| 423 | $this->get_thumbnail( 'upsale' ); |
| 424 | $this->get_message( 'upsale' ); |
| 425 | do_action( 'wpdeveloper_after_upsale_notice_for_' . $this->plugin_name ); |
| 426 | $this->upsale_button_script(); |
| 427 | } |
| 428 | /** |
| 429 | * Get upsale arguments. |
| 430 | * @return void |
| 431 | */ |
| 432 | private function get_upsale_args(){ |
| 433 | return ( empty( $this->upsale_args ) ) ? array() : $this->upsale_args; |
| 434 | } |
| 435 | /** |
| 436 | * This function is responsible for making the button visible to the upsale notice. |
| 437 | */ |
| 438 | private function upsale_button(){ |
| 439 | $upsale_args = $this->get_upsale_args(); |
| 440 | $plugin_slug = ( isset( $upsale_args['slug'] )) ? $upsale_args['slug'] : '' ; |
| 441 | $btn_text = ( isset( $upsale_args['btn_text'] )) ? $upsale_args['btn_text'] : __( 'Install Now!', 'essential-addons-for-elementor-lite' ) ; |
| 442 | if( empty( $plugin_slug ) ) { |
| 443 | return; |
| 444 | } |
| 445 | 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>'; |
| 446 | } |
| 447 | /** |
| 448 | * This methods is responsible for get notice image. |
| 449 | * |
| 450 | * @param string $msg_for |
| 451 | * @return void |
| 452 | */ |
| 453 | protected function get_thumbnail( $msg_for ){ |
| 454 | $output = ''; |
| 455 | if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) { |
| 456 | $output = '<div class="wpdeveloper-notice-thumbnail">'; |
| 457 | $output .= '<img src="'. esc_url( $this->data['thumbnail'][ $msg_for ] ) .'" alt="">'; |
| 458 | $output .= '</div>'; |
| 459 | } |
| 460 | echo wp_kses_post( $output ); |
| 461 | |
| 462 | } |
| 463 | /** |
| 464 | * Has Thumbnail Check |
| 465 | * |
| 466 | * @param string $msg_for |
| 467 | * @return boolean |
| 468 | */ |
| 469 | protected function has_thumbnail( $msg_for = '' ){ |
| 470 | if( empty( $msg_for ) ) { |
| 471 | return false; |
| 472 | } |
| 473 | if( isset( $this->data['thumbnail'] ) && isset( $this->data['thumbnail'][ $msg_for ] ) ) { |
| 474 | return true; |
| 475 | } |
| 476 | return false; |
| 477 | } |
| 478 | /** |
| 479 | * This method is responsible for get messages. |
| 480 | * |
| 481 | * @param string $msg_for |
| 482 | * @return void |
| 483 | */ |
| 484 | protected function get_message( $msg_for ){ |
| 485 | if( isset( $this->data['message'] ) && isset( $this->data['message'][ $msg_for ] ) ) { |
| 486 | echo '<div class="wpdeveloper-notice-message">'; |
| 487 | echo Helper::eael_wp_kses( $this->data['message'][ $msg_for ] ); |
| 488 | if( $msg_for === 'upsale' ) { |
| 489 | $this->upsale_button(); |
| 490 | } |
| 491 | $this->dismissible_notice( $msg_for ); |
| 492 | echo '</div>'; |
| 493 | } |
| 494 | } |
| 495 | /** |
| 496 | * Detect which notice will show @ next. |
| 497 | * @return void |
| 498 | */ |
| 499 | protected function next_notice(){ |
| 500 | $options_data = $this->get_options_data(); |
| 501 | if( ! $options_data ) { |
| 502 | $args = $this->get_args(); |
| 503 | $return_notice = $args['notice_will_show']; |
| 504 | } else { |
| 505 | $return_notice = $options_data[ $this->plugin_name ]['notice_will_show']; |
| 506 | } |
| 507 | |
| 508 | if( is_array( $return_notice ) ) { |
| 509 | $return_notice = array_flip( $return_notice ); |
| 510 | ksort( $return_notice ); |
| 511 | } |
| 512 | |
| 513 | return $return_notice; |
| 514 | } |
| 515 | /** |
| 516 | * Which notice is deserve to show in next slot. |
| 517 | * @param string $notice |
| 518 | * @return boolean |
| 519 | */ |
| 520 | private function deserve_notice( $notice ) { |
| 521 | $notices = $this->get_user_notices(); |
| 522 | if( $notice === false ) { |
| 523 | return false; |
| 524 | } |
| 525 | if( empty( $notices ) ) { |
| 526 | return true; |
| 527 | } else { |
| 528 | if( isset( $notices[ $this->notice_id ] ) && isset( $notices[ $this->notice_id ][ $this->plugin_name ] ) ) { |
| 529 | if( in_array( $notice, $notices[ $this->notice_id ][ $this->plugin_name ] ) ) { |
| 530 | return false; |
| 531 | } else { |
| 532 | return true; |
| 533 | } |
| 534 | } else { |
| 535 | return true; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | /** |
| 540 | * This is the main methods for generate the notice. |
| 541 | * @return void |
| 542 | */ |
| 543 | public function admin_notices(){ |
| 544 | $current_notice = current( $this->next_notice() ); |
| 545 | if( get_user_meta( get_current_user_id(), $this->plugin_name . '_' . $current_notice, true ) ) { |
| 546 | return; |
| 547 | } |
| 548 | if( $current_notice == 'opt_in' ) { |
| 549 | do_action( $this->do_notice_action ); |
| 550 | return; |
| 551 | } |
| 552 | do_action( 'wpdeveloper_before_notice_for_' . $this->plugin_name ); |
| 553 | do_action( $this->do_notice_action ); |
| 554 | do_action( 'wpdeveloper_after_notice_for_' . $this->plugin_name ); |
| 555 | } |
| 556 | /** |
| 557 | * This method is responsible for all dismissible links generation. |
| 558 | * @param string $links_for |
| 559 | * @return void |
| 560 | */ |
| 561 | public function dismissible_notice( $links_for = '' ){ |
| 562 | if( empty( $links_for ) ) { |
| 563 | return; |
| 564 | } |
| 565 | $links = isset( $this->data['links'][ $links_for ] ) ? $this->data['links'][ $links_for ] : false; |
| 566 | if( $links ) : |
| 567 | $output = '<ul class="wpdeveloper-notice-link">'; |
| 568 | foreach( $links as $key => $link_value ) { |
| 569 | if( ! empty( $link_value['label'] ) ) { |
| 570 | $output .= '<li>'; |
| 571 | if( isset( $link_value['link'] ) ) { |
| 572 | $link = $link_value['link']; |
| 573 | $target = isset( $link_value['target'] ) ? 'target="'. esc_attr( $link_value['target'] ) .'"' : ''; |
| 574 | if( isset( $link_value['data_args'] ) && is_array( $link_value['data_args'] ) ) { |
| 575 | $data_args = []; |
| 576 | foreach( $link_value['data_args'] as $key => $args_value ) { |
| 577 | $data_args[ $key ] = $args_value; |
| 578 | } |
| 579 | $data_args[ 'plugin' ] = $this->plugin_name; |
| 580 | $normal_link = add_query_arg( $data_args, $link ); |
| 581 | $link = wp_nonce_url( $normal_link, 'wpdeveloper-nonce' ); |
| 582 | } |
| 583 | $class = ''; |
| 584 | if( isset( $link_value['link_class'] ) ) { |
| 585 | $class = 'class="' . sanitize_html_class( implode( ' ', $link_value['link_class'] ) ) . '"'; |
| 586 | } |
| 587 | $output .= '<a '. $class .' href="'. esc_url( $link ) .'" '. $target .'>'; |
| 588 | } |
| 589 | if( isset( $link_value['icon_class'] ) ) { |
| 590 | $output .= '<span class="'. esc_attr( $link_value['icon_class'] ) .'"></span>'; |
| 591 | } |
| 592 | if( isset( $link_value['icon_img'] ) ) { |
| 593 | $output .= '<img src="'. esc_url( $link_value['icon_img'] ) .'" alt="" />'; |
| 594 | } |
| 595 | $output .= $link_value['label']; |
| 596 | if( isset( $link_value['link'] ) ) { |
| 597 | $output .= '</a>'; |
| 598 | } |
| 599 | $output .= '</li>'; |
| 600 | } |
| 601 | } |
| 602 | $output .= '</ul>'; |
| 603 | |
| 604 | printf( '%1$s', $output ); |
| 605 | endif; |
| 606 | } |
| 607 | /** |
| 608 | * First Installation Track |
| 609 | * @return void |
| 610 | */ |
| 611 | public function first_install_track( $args = array() ){ |
| 612 | if( ! current_user_can( 'manage_options' ) ) { |
| 613 | return; |
| 614 | } |
| 615 | if( empty( $args ) ) { |
| 616 | $args = array( |
| 617 | 'time' => $this->timestamp, |
| 618 | 'version' => $this->version, |
| 619 | ); |
| 620 | } |
| 621 | $options_data = $this->get_options_data(); |
| 622 | $args = wp_parse_args( $args, $this->get_args() ); |
| 623 | if( ! isset( $options_data[ $this->plugin_name ] ) |
| 624 | || ( isset( $options_data[ $this->plugin_name ]['version'] ) && version_compare( $options_data[ $this->plugin_name ]['version'], $this->version, '!=' ) ) ) { |
| 625 | $this->update_options_data( $args ); |
| 626 | } |
| 627 | } |
| 628 | /** |
| 629 | * First Installation Deactive Track |
| 630 | * |
| 631 | * @return void |
| 632 | */ |
| 633 | public function first_install_end(){ |
| 634 | // $args = array( |
| 635 | // 'first_install' => 'deactivated' |
| 636 | // ); |
| 637 | // $options_data = $this->get_options_data(); |
| 638 | // if( isset( $options_data[ $this->plugin_name ] ) ) { |
| 639 | // $args = wp_parse_args( $args, $options_data[ $this->plugin_name ] ); |
| 640 | // $this->update_options_data( $args ); |
| 641 | // } |
| 642 | delete_option( 'wpdeveloper_plugins_data' ); |
| 643 | } |
| 644 | /** |
| 645 | * Get all options from database! |
| 646 | * @return void |
| 647 | */ |
| 648 | protected function get_options_data( $key = ''){ |
| 649 | $options_data = get_option( 'wpdeveloper_plugins_data' ); |
| 650 | if( empty( $key ) ) { |
| 651 | return $options_data; |
| 652 | } |
| 653 | |
| 654 | if( isset( $options_data[ $this->plugin_name ][ $key ] ) ) { |
| 655 | return $options_data[ $this->plugin_name ][ $key ]; |
| 656 | } |
| 657 | return false; |
| 658 | } |
| 659 | /** |
| 660 | * This will update the options table for plugins. |
| 661 | * |
| 662 | * @param mixed $new_data |
| 663 | * @param array $args |
| 664 | * @return void |
| 665 | */ |
| 666 | protected function update_options_data( $args = array() ){ |
| 667 | $options_data = $this->get_options_data(); |
| 668 | $options_data[ $this->plugin_name ] = $args; |
| 669 | update_option( 'wpdeveloper_plugins_data', $options_data ); |
| 670 | } |
| 671 | /** |
| 672 | * Set properties data, for some selected properties. |
| 673 | * |
| 674 | * @param string $name |
| 675 | * @param mixed $value |
| 676 | */ |
| 677 | public function __set( $name, $value ){ |
| 678 | if( in_array( $name, $this->properties ) ) { |
| 679 | $this->data[ $name ] = $value; |
| 680 | } |
| 681 | } |
| 682 | /** |
| 683 | * Invoked when some selected methods are called |
| 684 | * |
| 685 | * @param string $name |
| 686 | * @param array $values |
| 687 | * @return void |
| 688 | */ |
| 689 | public function __call( $name, $values ){ |
| 690 | if( in_array( $name, $this->methods ) ) { |
| 691 | $this->data[ $name ][ $values[0] ] = $values[1]; |
| 692 | } |
| 693 | } |
| 694 | protected function is_ok( $name, $notice ){ |
| 695 | if( isset( $this->data[ $name ], $this->data[ $name ][ $notice ] ) ) { |
| 696 | return true; |
| 697 | } |
| 698 | return false; |
| 699 | } |
| 700 | /** |
| 701 | * Get all option arguments. |
| 702 | * @param string $key |
| 703 | * @return array |
| 704 | */ |
| 705 | private function get_args( $key = '' ){ |
| 706 | if( empty( $key ) ) { |
| 707 | return $this->options_args; |
| 708 | } |
| 709 | |
| 710 | if( isset( $this->options_args[ $key ] ) ) { |
| 711 | return $this->options_args[ $key ]; |
| 712 | } |
| 713 | |
| 714 | return false; |
| 715 | } |
| 716 | /** |
| 717 | * Resetting data on update. |
| 718 | * @return void |
| 719 | */ |
| 720 | private function set_args_on_update(){ |
| 721 | $args = $this->get_args(); |
| 722 | $options_data = $this->get_options_data(); |
| 723 | $set_data = $options_data[ $this->plugin_name ]; |
| 724 | $args = wp_parse_args( $set_data, $args ); |
| 725 | $this->update_options_data( $args ); |
| 726 | } |
| 727 | /** |
| 728 | * When upgrade is complete. it will fired. |
| 729 | * @param WP_Upgrader $upgrader_object |
| 730 | * @param array $options |
| 731 | * @return void |
| 732 | */ |
| 733 | public function upgrade_completed( $upgrader_object, $options ) { |
| 734 | // If an update has taken place and the updated type is plugins and the plugins element exists |
| 735 | if( isset( $options['action'] ) && $options['action'] == 'update' && $options['type'] == 'plugin' ) { |
| 736 | if( ! isset( $options['plugin'] ) && isset( $options['plugins'] ) ) { |
| 737 | foreach( $options['plugins'] as $plugin ) { |
| 738 | if( $plugin == $this->plugin_name ) { |
| 739 | $this->set_args_on_update(); |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | if( isset( $options['plugin'] ) && $options['plugin'] == $this->plugin_name ) { |
| 745 | $this->set_args_on_update(); |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | /** |
| 750 | * This function is responsible for get_user_notices |
| 751 | * @return void |
| 752 | */ |
| 753 | private function get_user_notices() { |
| 754 | $notices = get_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, true ); |
| 755 | return ! $notices ? array() : $notices; |
| 756 | } |
| 757 | /** |
| 758 | * This function is responsible for update meta information. |
| 759 | * |
| 760 | * @param string $notice |
| 761 | * @return void |
| 762 | */ |
| 763 | private function update( $notice ){ |
| 764 | if( empty( $notice ) ) { |
| 765 | return; |
| 766 | } |
| 767 | $options_data = $this->get_options_data(); |
| 768 | $user_notices = $this->get_user_notices(); |
| 769 | $user_notices[ $this->notice_id ][ $this->plugin_name ][] = $notice; |
| 770 | // Remove the upsale from notice_will_show field in options DB. |
| 771 | unset( $options_data[ $this->plugin_name ]['notice_will_show'][ $notice ] ); |
| 772 | $this->update_options_data( $options_data[ $this->plugin_name ] ); |
| 773 | // Set users meta, not to show again current_version notice. |
| 774 | update_user_meta( get_current_user_id(), self::ADMIN_UPDATE_NOTICE_KEY, $user_notices); |
| 775 | } |
| 776 | |
| 777 | public function notice_dissmiss(){ |
| 778 | if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_notice_dissmiss' ) ) { |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_notice_dissmiss_for_' . $this->plugin_name ) ) { |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | $dismiss = isset( $_POST['dismiss'] ) ? sanitize_text_field( $_POST['dismiss'] ) : false; |
| 787 | $notice = isset( $_POST['notice'] ) ? sanitize_text_field( $_POST['notice'] ) : false; |
| 788 | if( $dismiss ) { |
| 789 | $this->update( $notice ); |
| 790 | update_user_meta( get_current_user_id(), $this->plugin_name . '_' . $notice, true ); |
| 791 | echo 'success'; |
| 792 | } else { |
| 793 | echo 'failed'; |
| 794 | } |
| 795 | die(); |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * This function is responsible for do action when |
| 800 | * the dismiss button clicked in upsale notice. |
| 801 | */ |
| 802 | public function upsale_notice_dissmiss(){ |
| 803 | |
| 804 | if( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wpdeveloper_upsale_notice_dissmiss' ) ) { |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | if( ! isset( $_POST['action'] ) || ( $_POST['action'] !== 'wpdeveloper_upsale_notice_dissmiss_for_' . $this->plugin_name ) ) { |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | $dismiss = isset( $_POST['dismiss'] ) ? sanitize_text_field( $_POST['dismiss'] ) : false; |
| 813 | if( $dismiss ) { |
| 814 | $this->update( 'upsale' ); |
| 815 | echo 'success'; |
| 816 | } else { |
| 817 | echo 'failed'; |
| 818 | } |
| 819 | die(); |
| 820 | } |
| 821 | |
| 822 | public function dismiss_button_scripts(){ |
| 823 | ?> |
| 824 | <script type="text/javascript"> |
| 825 | jQuery(document).ready( function($) { |
| 826 | var wpdevNotice = $('.notice.is-dismissible'); |
| 827 | if( wpdevNotice.length > 0 ) { |
| 828 | $('body').on('click', 'button.notice-dismiss', function (e) { |
| 829 | e.preventDefault(); |
| 830 | $.ajax({ |
| 831 | url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', |
| 832 | type: 'post', |
| 833 | data: { |
| 834 | action: 'wpdeveloper_notice_dissmiss_for_<?php echo esc_html( $this->plugin_name ); ?>', |
| 835 | _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_notice_dissmiss'); ?>', |
| 836 | dismiss: true, |
| 837 | notice: wpdevNotice.data('notice'), |
| 838 | }, |
| 839 | success: function(response) { |
| 840 | $('.notice').hide(); |
| 841 | console.log('Successfully saved!'); |
| 842 | }, |
| 843 | error: function(error) { |
| 844 | console.log('Something went wrong!'); |
| 845 | }, |
| 846 | complete: function() { |
| 847 | console.log('Its Complete.'); |
| 848 | } |
| 849 | }); |
| 850 | }); |
| 851 | } |
| 852 | } ); |
| 853 | </script> |
| 854 | <?php |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * Upsale Button Script. |
| 859 | * When install button is clicked, it will do its own things. |
| 860 | * also for dismiss button JS. |
| 861 | * @return void |
| 862 | */ |
| 863 | public function upsale_button_script(){ |
| 864 | $upsale_args = $this->get_upsale_args(); |
| 865 | |
| 866 | $plugin_slug = ( isset( $upsale_args['slug'] ) ) ? $upsale_args['slug'] : ''; |
| 867 | $plugin_file = ( isset( $upsale_args['file'] ) ) ? $upsale_args['file'] : ''; |
| 868 | $page_slug = ( isset( $upsale_args['page_slug'] ) ) ? $upsale_args['page_slug'] : ''; |
| 869 | |
| 870 | ?> |
| 871 | <script type="text/javascript"> |
| 872 | jQuery(document).ready( function($) { |
| 873 | <?php if( ! empty( $plugin_slug ) && ! empty( $plugin_file ) ) : ?> |
| 874 | $('#plugin-install-core-<?php echo esc_html( $this->plugin_name ); ?>').on('click', function (e) { |
| 875 | var self = $(this); |
| 876 | e.preventDefault(); |
| 877 | self.addClass('install-now updating-message'); |
| 878 | self.text('<?php echo esc_js( 'Installing...' ); ?>'); |
| 879 | |
| 880 | $.ajax({ |
| 881 | url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', |
| 882 | type: 'POST', |
| 883 | data: { |
| 884 | action: 'wpdeveloper_upsale_core_install_<?php echo esc_html( $this->plugin_name ); ?>', |
| 885 | _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_core_install_' . esc_html( $this->plugin_name )); ?>', |
| 886 | slug : '<?php echo esc_html( $plugin_slug ); ?>', |
| 887 | file : '<?php echo esc_html( $plugin_file ); ?>' |
| 888 | }, |
| 889 | success: function(response) { |
| 890 | self.text('<?php echo esc_js( 'Installed' ); ?>'); |
| 891 | <?php if( ! empty( $page_slug ) ) : ?> |
| 892 | window.location.href = '<?php echo esc_url( admin_url( "admin.php?page={$page_slug}" ) ); ?>'; |
| 893 | <?php endif; ?> |
| 894 | }, |
| 895 | error: function(error) { |
| 896 | self.removeClass('install-now updating-message'); |
| 897 | }, |
| 898 | complete: function() { |
| 899 | self.attr('disabled', 'disabled'); |
| 900 | self.removeClass('install-now updating-message'); |
| 901 | } |
| 902 | }); |
| 903 | }); |
| 904 | <?php endif; ?> |
| 905 | $('.wpdeveloper-upsale-notice').on('click', 'button.notice-dismiss', function (e) { |
| 906 | e.preventDefault(); |
| 907 | $.ajax({ |
| 908 | url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', |
| 909 | type: 'post', |
| 910 | data: { |
| 911 | action: 'wpdeveloper_upsale_notice_dissmiss_for_<?php echo esc_html( $this->plugin_name ); ?>', |
| 912 | _wpnonce: '<?php echo wp_create_nonce('wpdeveloper_upsale_notice_dissmiss'); ?>', |
| 913 | dismiss: true |
| 914 | }, |
| 915 | success: function(response) { |
| 916 | console.log('Successfully saved!'); |
| 917 | }, |
| 918 | error: function(error) { |
| 919 | console.log('Something went wrong!'); |
| 920 | }, |
| 921 | complete: function() { |
| 922 | console.log('Its Complete.'); |
| 923 | } |
| 924 | }); |
| 925 | }); |
| 926 | } ); |
| 927 | </script> |
| 928 | |
| 929 | <?php |
| 930 | } |
| 931 | } |
| 932 |