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