| 1 |
<?php |
| 2 |
namespace Wpmet\UtilityPackage\Rating; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
use DateTime; |
| 7 |
use Wpmet\UtilityPackage\Notice\Notice as LibsNotice; |
| 8 |
use Wpmet\UtilityPackage\Helper\Helper as UtilsHelper; |
| 9 |
|
| 10 |
/** |
| 11 |
* Asking client for rating and |
| 12 |
* other stuffs |
| 13 |
* Class Rating |
| 14 |
* @package Wpmet\UtilityPackage |
| 15 |
*/ |
| 16 |
class Rating { |
| 17 |
|
| 18 |
private $plugin_name; |
| 19 |
private $message; |
| 20 |
private $priority = 10; |
| 21 |
private $days; |
| 22 |
private $rating_url; |
| 23 |
private $support_url; |
| 24 |
|
| 25 |
private $version; |
| 26 |
private $condition_status = true; |
| 27 |
private $text_domain; |
| 28 |
private $plugin_logo; |
| 29 |
private $plugin_screens; |
| 30 |
private $duplication = false; |
| 31 |
private $never_show_triggered = false; |
| 32 |
private $rating_show_interval = 30; |
| 33 |
|
| 34 |
/** |
| 35 |
* scripts version |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
// protected $script_version = '2.0.0'; |
| 40 |
|
| 41 |
private static $instance; |
| 42 |
|
| 43 |
/** |
| 44 |
* Method: instance -> Return Notice module class instance |
| 45 |
* |
| 46 |
* @param string|null $text_domain |
| 47 |
* @param string|null $unique_id |
| 48 |
* @return mixed |
| 49 |
*/ |
| 50 |
public static function instance( $text_domain = null, $unique_id = null ) { |
| 51 |
if ( $text_domain == null ) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
self::$instance = new self(); |
| 56 |
self::$instance->config( $text_domain, ( is_null( $unique_id ) ? uniqid() : $unique_id ) ); |
| 57 |
return self::$instance; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Set Text domain |
| 62 |
* |
| 63 |
* @param string $text_domain |
| 64 |
* @param string $unique_id |
| 65 |
*/ |
| 66 |
public function config( $text_domain, $unique_id ) { |
| 67 |
$this->text_domain = $text_domain; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get vesrion of $this |
| 72 |
* |
| 73 |
* @return \Wpmet\Rating\Rating |
| 74 |
*/ |
| 75 |
public function get_version() { |
| 76 |
// return $this->script_version; |
| 77 |
return UtilsHelper::get_pac_version(); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @return $this file location for debugging 🐛 purpose |
| 83 |
*/ |
| 84 |
public function get_script_location() { |
| 85 |
return __FILE__; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @param |
| 90 |
*/ |
| 91 |
public function set_plugin( $plugin_name, $plugin_url ) { |
| 92 |
$this->plugin_name = $plugin_name; |
| 93 |
$this->rating_url = $plugin_url; |
| 94 |
return $this; |
| 95 |
} |
| 96 |
/** |
| 97 |
* Set message for review |
| 98 |
* @param $message string type |
| 99 |
*/ |
| 100 |
public function set_message( $message ) { |
| 101 |
$this->message = $message; |
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @param |
| 107 |
*/ |
| 108 |
public function set_priority( $priority ) { |
| 109 |
$this->priority = $priority; |
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
public function set_first_appear_day( $days = 7 ) { |
| 114 |
$this->days = $days; |
| 115 |
return $this; |
| 116 |
} |
| 117 |
|
| 118 |
public function set_rating_url( $url ) { |
| 119 |
$this->rating_url = $url; |
| 120 |
return $this; |
| 121 |
} |
| 122 |
|
| 123 |
public function set_support_url( $url ) { |
| 124 |
$this->support_url = $url; |
| 125 |
return $this; |
| 126 |
} |
| 127 |
|
| 128 |
public function set_plugin_logo( $logo_url ) { |
| 129 |
$this->plugin_logo = $logo_url; |
| 130 |
return $this; |
| 131 |
} |
| 132 |
|
| 133 |
public function set_allowed_screens( $screen ) { |
| 134 |
|
| 135 |
$this->plugin_screens[] = $screen; |
| 136 |
|
| 137 |
return $this; |
| 138 |
} |
| 139 |
|
| 140 |
public function set_condition( $result ) { |
| 141 |
switch ( gettype( $result ) ) { |
| 142 |
case 'boolean': |
| 143 |
$this->condition_status = $result; |
| 144 |
break; |
| 145 |
case 'object': |
| 146 |
$this->condition_status = $result(); |
| 147 |
break; |
| 148 |
default: |
| 149 |
$this->condition_status = false; |
| 150 |
} |
| 151 |
|
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
public static function init() { |
| 156 |
add_action( 'wp_ajax_wpmet_rating_never_show_message', array( __CLASS__, 'never_show_message' ) ); |
| 157 |
add_action( 'wp_ajax_wpmet_rating_ask_me_later_message', array( __CLASS__, 'ask_me_later_message' ) ); |
| 158 |
} |
| 159 |
|
| 160 |
protected function is_current_screen_allowed( $current_screen_id ) { |
| 161 |
if ( in_array( $current_screen_id, array_merge( $this->plugin_screens, array( 'dashboard', 'plugins' ) ) ) ) { |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* ------------------------------------------ |
| 170 |
* 🚀 Rating class execution point |
| 171 |
* ------------------------------------------ |
| 172 |
*/ |
| 173 |
public function call() { |
| 174 |
|
| 175 |
$this->init(); |
| 176 |
add_action( 'admin_head', array( $this, 'fire' ), $this->priority ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* ------------------------------------------- |
| 181 |
* 🔥 fire the rating functionality |
| 182 |
* ------------------------------------------- |
| 183 |
*/ |
| 184 |
public function fire() { |
| 185 |
|
| 186 |
if ( current_user_can( 'update_plugins' ) ) { |
| 187 |
|
| 188 |
$current_screen = get_current_screen(); |
| 189 |
|
| 190 |
if ( ! $this->is_current_screen_allowed( $current_screen->id ) ) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
if ( $this->condition_status === false) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
add_action( 'admin_footer', array( $this, 'scripts' ), 9999 ); |
| 201 |
|
| 202 |
if ( $this->action_on_fire() ) { |
| 203 |
if ( ! $this->is_installation_date_exists() ) { |
| 204 |
$this->set_installation_date(); |
| 205 |
} |
| 206 |
|
| 207 |
if ( get_option( $this->text_domain . '_never_show' ) == 'yes' ) { |
| 208 |
|
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
if ( get_option( $this->text_domain . '_ask_me_later' ) == 'yes' ) { |
| 213 |
|
| 214 |
$this->days = $this->rating_show_interval; |
| 215 |
$this->duplication = true; |
| 216 |
$this->never_show_triggered = true; |
| 217 |
if ( $this->get_remaining_days() >= $this->days ) { |
| 218 |
$this->duplication = false; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
$this->display_message_box(); |
| 223 |
|
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
private function action_on_fire() { |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
public function set_installation_date() { |
| 236 |
add_option( $this->text_domain . '_install_date', date( 'Y-m-d h:i:s' ) ); |
| 237 |
} |
| 238 |
|
| 239 |
public function is_installation_date_exists() { |
| 240 |
return ( get_option( $this->text_domain . '_install_date' ) == false ) ? false : true; |
| 241 |
} |
| 242 |
|
| 243 |
public function get_installation_date() { |
| 244 |
return get_option( $this->text_domain . '_install_date' ); |
| 245 |
} |
| 246 |
|
| 247 |
public function set_first_action_date() { |
| 248 |
add_option( $this->text_domain . '_first_action_Date', date( 'Y-m-d h:i:s' ) ); |
| 249 |
add_option( $this->text_domain . '_first_action', 'yes' ); |
| 250 |
} |
| 251 |
|
| 252 |
public function get_days( $from_date, $to_date ) { |
| 253 |
return round( ( $to_date->format( 'U' ) - $from_date->format( 'U' ) ) / ( 60 * 60 * 24 ) ); |
| 254 |
} |
| 255 |
|
| 256 |
public function is_first_use( $in_days ) { |
| 257 |
$install_date = get_option( $this->text_domain . '_install_date' ); |
| 258 |
$display_date = date( 'Y-m-d h:i:s' ); |
| 259 |
$datetime1 = new DateTime( $install_date ); |
| 260 |
$datetime2 = new DateTime( $display_date ); |
| 261 |
$diff_interval = $this->get_days( $datetime1, $datetime2 ); |
| 262 |
|
| 263 |
if ( abs( $diff_interval ) >= $in_days && get_option( $this->text_domain . '_first_action_Date' ) == 'yes' ) { |
| 264 |
|
| 265 |
// action implementation here |
| 266 |
|
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* --------------------------------------------- |
| 272 |
* Change the status of Rating notification |
| 273 |
* not to show the message again |
| 274 |
* --------------------------------------------- |
| 275 |
*/ |
| 276 |
public static function never_show_message() { |
| 277 |
|
| 278 |
if( empty( $_POST['nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wpmet_rating' ) ){ |
| 279 |
//return false; |
| 280 |
} |
| 281 |
|
| 282 |
$plugin_name = isset($_POST['plugin_name']) ? sanitize_key( $_POST['plugin_name'] ) : ''; |
| 283 |
add_option( $plugin_name . '_never_show', 'yes' ); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
/** |
| 288 |
* set rating show interval if user set ask me later |
| 289 |
*/ |
| 290 |
public function rating_show_interval( int $rating_show_interval = 30 ) { |
| 291 |
|
| 292 |
$this->rating_show_interval = $rating_show_interval; |
| 293 |
|
| 294 |
return $this; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
public function get_remaining_days() { |
| 299 |
$install_date = get_option( $this->text_domain . '_install_date' ); |
| 300 |
$display_date = date( 'Y-m-d h:i:s' ); |
| 301 |
$datetime1 = new DateTime( $install_date ); |
| 302 |
$datetime2 = new DateTime( $display_date ); |
| 303 |
$diff_interval = $this->get_days( $datetime1, $datetime2 ); |
| 304 |
return abs( $diff_interval ); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
*---------------------------------- |
| 309 |
* Ask me later functionality |
| 310 |
*---------------------------------- |
| 311 |
*/ |
| 312 |
public function display_message_box() { |
| 313 |
|
| 314 |
if ( ! $this->duplication ) { |
| 315 |
global $wpmet_libs_execution_container; |
| 316 |
|
| 317 |
if ( isset( $wpmet_libs_execution_container['rating'] ) ) { |
| 318 |
return; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
$wpmet_libs_execution_container['rating'] = __FILE__; |
| 323 |
|
| 324 |
$install_date = get_option( $this->text_domain . '_install_date' ); |
| 325 |
$display_date = date( 'Y-m-d h:i:s' ); |
| 326 |
$datetime1 = new DateTime( $install_date ); |
| 327 |
$datetime2 = new DateTime( $display_date ); |
| 328 |
$diff_interval = $this->get_days( $datetime1, $datetime2 ); |
| 329 |
if ( abs( $diff_interval ) >= $this->days ) { |
| 330 |
|
| 331 |
$not_good_enough_btn_id = ( $this->never_show_triggered ) ? '_btn_never_show' : '_btn_not_good'; |
| 332 |
|
| 333 |
$message = "Hello! Seems like you have used {$this->plugin_name} to build this website — Thanks a lot! <br> |
| 334 |
Could you please do us a <b>big favor</b> and give it a <b>5-star</b> rating on WordPress? |
| 335 |
This would boost our motivation and help other users make a comfortable decision while choosing the {$this->plugin_name}"; |
| 336 |
|
| 337 |
$message = $this->message ?? $message; |
| 338 |
LibsNotice::instance( $this->text_domain, '_plugin_rating_msg_used_in_day' ) |
| 339 |
->set_message( $message ) |
| 340 |
->set_logo( $this->plugin_logo, 'max-height: 100px !important' ) |
| 341 |
->set_button( |
| 342 |
array( |
| 343 |
'url' => $this->rating_url, |
| 344 |
'text' => 'Ok, you deserved it', |
| 345 |
'class' => 'button-primary', |
| 346 |
'id' => $this->text_domain . '_btn_deserved', |
| 347 |
) |
| 348 |
) |
| 349 |
->set_button( |
| 350 |
array( |
| 351 |
'url' => get_current_screen()->id == 'toplevel_page_getgenie' ? '#write-for-me' : '#', |
| 352 |
'text' => 'I already did', |
| 353 |
'class' => 'button-default', |
| 354 |
'id' => $this->text_domain . '_btn_already_did', |
| 355 |
'icon' => 'dashicons-before dashicons-smiley', |
| 356 |
) |
| 357 |
) |
| 358 |
->set_button( |
| 359 |
array( |
| 360 |
'url' => $this->support_url, |
| 361 |
'text' => 'I need support', |
| 362 |
'class' => 'button-default', |
| 363 |
'id' => '#', |
| 364 |
'icon' => 'dashicons-before dashicons-sos', |
| 365 |
) |
| 366 |
) |
| 367 |
// ->set_button([ |
| 368 |
// 'url' => '#', |
| 369 |
// 'text' => 'Never ask again', |
| 370 |
// 'class' => 'button-default', |
| 371 |
// 'id' => $this->text_domain . '_btn_never_show', |
| 372 |
// 'icon' => 'dashicons-before dashicons-welcome-comments', |
| 373 |
// ]) |
| 374 |
|
| 375 |
->set_button( |
| 376 |
array( |
| 377 |
'url' => get_current_screen()->id == 'toplevel_page_getgenie' ? '#write-for-me' : '#', |
| 378 |
'text' => 'No, not good enough', |
| 379 |
'class' => 'button-default', |
| 380 |
'id' => $this->text_domain . '_btn_never_show', |
| 381 |
'icon' => 'dashicons-before dashicons-thumbs-down', |
| 382 |
) |
| 383 |
) |
| 384 |
->call(); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
/** |
| 390 |
*--------------------------------------------------------- |
| 391 |
* When user will click @notGoodEnough button |
| 392 |
* Then it will fire this function to change the status |
| 393 |
* for next asking time |
| 394 |
*--------------------------------------------------------- |
| 395 |
*/ |
| 396 |
|
| 397 |
|
| 398 |
public static function ask_me_later_message() { |
| 399 |
|
| 400 |
if( empty( $_POST['nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wpmet_rating' ) ){ |
| 401 |
return false; |
| 402 |
} |
| 403 |
|
| 404 |
$plugin_name = isset($_POST['plugin_name']) ? sanitize_key( $_POST['plugin_name'] ) : ''; |
| 405 |
if ( get_option( $plugin_name . '_ask_me_later' ) == false ) { |
| 406 |
add_option( $plugin_name . '_ask_me_later', 'yes' ); |
| 407 |
} else { |
| 408 |
add_option( $plugin_name . '_never_show', 'yes' ); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
*-------------------------------------- |
| 414 |
* Get current version of the plugin |
| 415 |
*-------------------------------------- |
| 416 |
*/ |
| 417 |
|
| 418 |
public function get_current_version() { |
| 419 |
|
| 420 |
return $this->version; |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
*------------------------------------------- |
| 425 |
* Get previous version of the plugin |
| 426 |
* that have been stored in database |
| 427 |
*------------------------------------------- |
| 428 |
*/ |
| 429 |
|
| 430 |
|
| 431 |
public function get_previous_version() { |
| 432 |
|
| 433 |
return get_option( $this->text_domain . '_version' ); |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
*---------------------------------------- |
| 438 |
* Set current version of the plugin |
| 439 |
*---------------------------------------- |
| 440 |
*/ |
| 441 |
|
| 442 |
public function set_version( $version ) { |
| 443 |
if ( ! get_option( $this->text_domain . '_version' ) ) { |
| 444 |
add_option( $this->text_domain . '_version' ); |
| 445 |
} else { |
| 446 |
update_option( $this->text_domain . '_version', $version ); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* |
| 452 |
* JS Ajax script for updating |
| 453 |
* rating status from users |
| 454 |
* |
| 455 |
*/ |
| 456 |
|
| 457 |
public function scripts() { |
| 458 |
|
| 459 |
echo " |
| 460 |
<script> |
| 461 |
jQuery(document).ready(function ($) { |
| 462 |
|
| 463 |
$( '#" . esc_js( $this->text_domain ) . "_btn_already_did' ).on( 'click', function() { |
| 464 |
|
| 465 |
$.ajax({ |
| 466 |
url: ajaxurl, |
| 467 |
type: 'POST', |
| 468 |
data: { |
| 469 |
action : 'wpmet_rating_never_show_message', |
| 470 |
plugin_name : '" . esc_js( $this->text_domain ) . "', |
| 471 |
nonce : '" . esc_js( wp_create_nonce( 'wpmet_rating' ) ) . "' |
| 472 |
|
| 473 |
}, |
| 474 |
success:function(response){ |
| 475 |
$('#" . esc_js( $this->text_domain ) . "-_plugin_rating_msg_used_in_day').remove(); |
| 476 |
|
| 477 |
} |
| 478 |
}); |
| 479 |
|
| 480 |
}); |
| 481 |
|
| 482 |
$('#" . esc_js( $this->text_domain ) . "_btn_deserved').click(function(){ |
| 483 |
$.ajax({ |
| 484 |
url: ajaxurl, |
| 485 |
type: 'POST', |
| 486 |
data: { |
| 487 |
action : 'wpmet_rating_never_show_message', |
| 488 |
plugin_name : '" . esc_js( $this->text_domain ) . "', |
| 489 |
nonce : '" . esc_js( wp_create_nonce( 'wpmet_rating' ) ) . "' |
| 490 |
}, |
| 491 |
success:function(response){ |
| 492 |
$('#" . esc_js( $this->text_domain ) . "-_plugin_rating_msg_used_in_day').remove(); |
| 493 |
|
| 494 |
} |
| 495 |
}); |
| 496 |
}); |
| 497 |
|
| 498 |
$('#" . esc_js( $this->text_domain ) . "_btn_not_good').click(function(){ |
| 499 |
$.ajax({ |
| 500 |
url: ajaxurl, |
| 501 |
type: 'POST', |
| 502 |
data: { |
| 503 |
action : 'wpmet_rating_never_show_message', |
| 504 |
plugin_name : '" . esc_js( $this->text_domain ) . "', |
| 505 |
nonce : '" . esc_js( wp_create_nonce( 'wpmet_rating' ) ) . "' |
| 506 |
}, |
| 507 |
success:function(response){ |
| 508 |
$('#" . esc_js( $this->text_domain ) . "-_plugin_rating_msg_used_in_day').remove(); |
| 509 |
|
| 510 |
} |
| 511 |
}); |
| 512 |
}); |
| 513 |
|
| 514 |
$('#" . esc_js( $this->text_domain ) . "_btn_never_show').click(function(){ |
| 515 |
$.ajax({ |
| 516 |
url: ajaxurl, |
| 517 |
type: 'POST', |
| 518 |
data: { |
| 519 |
action : 'wpmet_rating_never_show_message', |
| 520 |
plugin_name : '" . esc_js( $this->text_domain ) . "', |
| 521 |
nonce : '" . esc_js( wp_create_nonce( 'wpmet_rating' ) ) . "' |
| 522 |
}, |
| 523 |
success:function(response){ |
| 524 |
$('#" . esc_js( $this->text_domain ) . "-_plugin_rating_msg_used_in_day').remove(); |
| 525 |
|
| 526 |
} |
| 527 |
}); |
| 528 |
}); |
| 529 |
|
| 530 |
}); |
| 531 |
</script> |
| 532 |
"; |
| 533 |
} |
| 534 |
} |
| 535 |
|