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