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