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