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