rating.php
483 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 | |
| 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 | |
| 162 | public function fire() |
| 163 | { |
| 164 | |
| 165 | if (current_user_can('update_plugins')) { |
| 166 | |
| 167 | $current_screen = get_current_screen(); |
| 168 | |
| 169 | if (!$this->is_current_screen_allowed($current_screen->id) || $this->condition_status === false || get_option($this->text_domain . '_never_show') == 'yes') { |
| 170 | return; |
| 171 | } |
| 172 | add_action('admin_footer', array($this, 'scripts'), 9999); |
| 173 | |
| 174 | if($this->action_on_fire()) { |
| 175 | if (!$this->is_installation_date_exists()) { |
| 176 | $this->set_installation_date(); |
| 177 | } |
| 178 | |
| 179 | if (get_option($this->text_domain . '_ask_me_later') == 'yes') { |
| 180 | |
| 181 | $this->days = '30'; |
| 182 | $this->duplication = true; |
| 183 | $this->never_show_triggered = true; |
| 184 | if ($this->get_remaining_days() >= $this->days) { |
| 185 | $this->duplication = false; |
| 186 | } |
| 187 | } |
| 188 | $this->display_message_box(); |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | } |
| 193 | private function action_on_fire() { |
| 194 | return true; |
| 195 | } |
| 196 | public function set_installation_date() |
| 197 | { |
| 198 | add_option($this->text_domain . '_install_date', date('Y-m-d h:i:s')); |
| 199 | } |
| 200 | |
| 201 | public function is_installation_date_exists() |
| 202 | { |
| 203 | return (get_option($this->text_domain . '_install_date') == false) ? false : true; |
| 204 | } |
| 205 | |
| 206 | public function get_installation_date() |
| 207 | { |
| 208 | return get_option($this->text_domain . '_install_date'); |
| 209 | } |
| 210 | |
| 211 | public function set_first_action_date() |
| 212 | { |
| 213 | add_option($this->text_domain . '_first_action_Date', date('Y-m-d h:i:s')); |
| 214 | add_option($this->text_domain . '_first_action', 'yes'); |
| 215 | } |
| 216 | |
| 217 | public function get_days($from_date, $to_date) |
| 218 | { |
| 219 | return round(($to_date->format('U') - $from_date->format('U')) / (60 * 60 * 24)); |
| 220 | } |
| 221 | |
| 222 | public function is_first_use($in_days) |
| 223 | { |
| 224 | $install_date = get_option($this->text_domain . '_install_date'); |
| 225 | $display_date = date('Y-m-d h:i:s'); |
| 226 | $datetime1 = new DateTime($install_date); |
| 227 | $datetime2 = new DateTime($display_date); |
| 228 | $diff_interval = $this->get_days($datetime1, $datetime2); |
| 229 | |
| 230 | if (abs($diff_interval) >= $in_days && get_option($this->text_domain . '_first_action_Date') == 'yes') { |
| 231 | |
| 232 | // action implementation here |
| 233 | |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * --------------------------------------------- |
| 239 | * Change the status of Rating notification |
| 240 | * not to show the message again |
| 241 | * --------------------------------------------- |
| 242 | */ |
| 243 | public static function never_show_message() |
| 244 | { |
| 245 | if (empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'shopengine_rating')) { |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | $plugin_name = isset($_POST['plugin_name']) ? sanitize_key($_POST['plugin_name']) : ''; |
| 250 | add_option($plugin_name . '_never_show', 'yes'); |
| 251 | } |
| 252 | |
| 253 | public function get_remaining_days() |
| 254 | { |
| 255 | $install_date = get_option($this->text_domain . '_install_date'); |
| 256 | $display_date = date('Y-m-d h:i:s'); |
| 257 | $datetime1 = new DateTime($install_date); |
| 258 | $datetime2 = new DateTime($display_date); |
| 259 | $diff_interval = $this->get_days($datetime1, $datetime2); |
| 260 | return abs($diff_interval); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | *---------------------------------- |
| 265 | * Ask me later functionality |
| 266 | *---------------------------------- |
| 267 | */ |
| 268 | |
| 269 | public function display_message_box() |
| 270 | { |
| 271 | if (!$this->duplication) { |
| 272 | global $wpmet_libs_execution_container; |
| 273 | |
| 274 | if (isset($wpmet_libs_execution_container['rating'])) { |
| 275 | return; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | $wpmet_libs_execution_container['rating'] = __FILE__; |
| 280 | |
| 281 | $install_date = get_option($this->text_domain . '_install_date'); |
| 282 | $display_date = date('Y-m-d h:i:s'); |
| 283 | $datetime1 = new DateTime($install_date); |
| 284 | $datetime2 = new DateTime($display_date); |
| 285 | $diff_interval = $this->get_days($datetime1, $datetime2); |
| 286 | if (abs($diff_interval) >= $this->days) { |
| 287 | |
| 288 | $not_good_enough_btn_id = ($this->never_show_triggered) ? '_btn_never_show' : '_btn_not_good'; |
| 289 | |
| 290 | $message = "Designing high-converting WooCommerce pages with <strong>ShopEngine?</strong> 🛒<br> |
| 291 | Your support means everything! Help us grow by dropping a <strong>5-star</strong> review today. ✨"; |
| 292 | |
| 293 | Notice::instance()->set_config($this->text_domain, '_plugin_rating_msg_used_in_day') |
| 294 | ->set_message($message) |
| 295 | ->set_logo($this->plugin_logo, 'max-height: 100px !important') |
| 296 | ->set_button( |
| 297 | array( |
| 298 | 'url' => $this->rating_url, |
| 299 | 'text' => 'Ok, you deserved it', |
| 300 | 'class' => 'button-primary', |
| 301 | 'id' => $this->text_domain . '_btn_deserved', |
| 302 | 'target_blank' => true, |
| 303 | ) |
| 304 | ) |
| 305 | ->set_button( |
| 306 | array( |
| 307 | 'url' => '#', |
| 308 | 'text' => 'Already did', |
| 309 | 'class' => 'button-default', |
| 310 | 'id' => $this->text_domain . '_btn_already_did', |
| 311 | 'icon' => 'dashicons-before dashicons-smiley', |
| 312 | ) |
| 313 | ) |
| 314 | ->set_button( |
| 315 | array( |
| 316 | 'url' => 'https://wpmet.com/support-ticket', |
| 317 | 'text' => 'Need support', |
| 318 | 'class' => 'button-default', |
| 319 | 'id' => '#', |
| 320 | 'icon' => 'dashicons-before dashicons-sos', |
| 321 | 'target_blank' => true, |
| 322 | ) |
| 323 | ) |
| 324 | ->set_button( |
| 325 | array( |
| 326 | 'url' => '#', |
| 327 | 'text' => 'Not good', |
| 328 | 'class' => 'button-default', |
| 329 | 'id' => $this->text_domain . $not_good_enough_btn_id, |
| 330 | 'icon' => 'dashicons-before dashicons-thumbs-down', |
| 331 | ) |
| 332 | ) |
| 333 | ->call(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | *--------------------------------------------------------- |
| 339 | * When user will click @notGoodEnough button |
| 340 | * Then it will fire this function to change the status |
| 341 | * for next asking time |
| 342 | *--------------------------------------------------------- |
| 343 | */ |
| 344 | |
| 345 | |
| 346 | public static function ask_me_later_message() |
| 347 | { |
| 348 | if (empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'shopengine_rating')) { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | $plugin_name = isset($_POST['plugin_name']) ? sanitize_key($_POST['plugin_name']) : ''; |
| 353 | if (get_option($plugin_name . '_ask_me_later') == false) { |
| 354 | add_option($plugin_name . '_ask_me_later', 'yes'); |
| 355 | } else { |
| 356 | add_option($plugin_name . '_never_show', 'yes'); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | *-------------------------------------- |
| 362 | * Get current version of the plugin |
| 363 | *-------------------------------------- |
| 364 | */ |
| 365 | |
| 366 | public function get_current_version() |
| 367 | { |
| 368 | return $this->version; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | *------------------------------------------- |
| 373 | * Get previous version of the plugin |
| 374 | * that have been stored in database |
| 375 | *------------------------------------------- |
| 376 | */ |
| 377 | |
| 378 | |
| 379 | public function get_previous_version() |
| 380 | { |
| 381 | return get_option($this->text_domain . '_version'); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | *---------------------------------------- |
| 386 | * Set current version of the plugin |
| 387 | *---------------------------------------- |
| 388 | */ |
| 389 | |
| 390 | public function set_version($version) |
| 391 | { |
| 392 | if (!get_option($this->text_domain . '_version')) { |
| 393 | add_option($this->text_domain . '_version'); |
| 394 | } else { |
| 395 | update_option($this->text_domain . '_version', $version); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * |
| 401 | * JS Ajax script for updating |
| 402 | * rating status from users |
| 403 | * |
| 404 | */ |
| 405 | |
| 406 | public function scripts() |
| 407 | { |
| 408 | echo " |
| 409 | <script> |
| 410 | jQuery(document).ready(function ($) { |
| 411 | |
| 412 | $( '#" . esc_js($this->text_domain) . "_btn_already_did' ).on( 'click', function() { |
| 413 | |
| 414 | $.ajax({ |
| 415 | url: ajaxurl, |
| 416 | type: 'POST', |
| 417 | data: { |
| 418 | action : 'shopengine_rating_never_show_message', |
| 419 | plugin_name : '" . esc_js($this->text_domain) . "', |
| 420 | nonce : '" . esc_js(wp_create_nonce('shopengine_rating')) . "' |
| 421 | |
| 422 | }, |
| 423 | success:function(response){ |
| 424 | $('#" . esc_js($this->text_domain) . "-_plugin_rating_msg_used_in_day').remove(); |
| 425 | |
| 426 | } |
| 427 | }); |
| 428 | |
| 429 | }); |
| 430 | |
| 431 | $('#" . esc_js($this->text_domain) . "_btn_deserved').click(function(){ |
| 432 | $.ajax({ |
| 433 | url: ajaxurl, |
| 434 | type: 'POST', |
| 435 | data: { |
| 436 | action : 'shopengine_rating_never_show_message', |
| 437 | plugin_name : '" . esc_js($this->text_domain) . "', |
| 438 | nonce : '" . esc_js(wp_create_nonce('shopengine_rating')) . "' |
| 439 | }, |
| 440 | success:function(response){ |
| 441 | $('#" . esc_js($this->text_domain) . "-_plugin_rating_msg_used_in_day').remove(); |
| 442 | |
| 443 | } |
| 444 | }); |
| 445 | }); |
| 446 | |
| 447 | $('#" . esc_js($this->text_domain) . "_btn_not_good').click(function(){ |
| 448 | $.ajax({ |
| 449 | url: ajaxurl, |
| 450 | type: 'POST', |
| 451 | data: { |
| 452 | action : 'shopengine_rating_ask_me_later_message', |
| 453 | plugin_name : '" . esc_js($this->text_domain) . "', |
| 454 | nonce : '" . esc_js(wp_create_nonce('shopengine_rating')) . "' |
| 455 | }, |
| 456 | success:function(response){ |
| 457 | $('#" . esc_js($this->text_domain) . "-_plugin_rating_msg_used_in_day').remove(); |
| 458 | |
| 459 | } |
| 460 | }); |
| 461 | }); |
| 462 | |
| 463 | $('#" . esc_js($this->text_domain) . "_btn_never_show').click(function(){ |
| 464 | $.ajax({ |
| 465 | url: ajaxurl, |
| 466 | type: 'POST', |
| 467 | data: { |
| 468 | action : 'shopengine_rating_never_show_message', |
| 469 | plugin_name : '" . esc_js($this->text_domain) . "', |
| 470 | nonce : '" . esc_js(wp_create_nonce('shopengine_rating')) . "' |
| 471 | }, |
| 472 | success:function(response){ |
| 473 | $('#" . esc_js($this->text_domain) . "-_plugin_rating_msg_used_in_day').remove(); |
| 474 | |
| 475 | } |
| 476 | }); |
| 477 | }); |
| 478 | |
| 479 | }); |
| 480 | </script>"; |
| 481 | } |
| 482 | } |
| 483 |