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