| 1 |
<?php |
| 2 |
|
| 3 |
namespace TableBuilderScopedDeps\Wpmet\UtilityPackage\Rating; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
use DateTime; |
| 7 |
use TableBuilderScopedDeps\Wpmet\UtilityPackage\Notice\Notice as LibsNotice; |
| 8 |
use TableBuilderScopedDeps\Wpmet\UtilityPackage\Helper\Helper as UtilsHelper; |
| 9 |
/** |
| 10 |
* Asking client for rating and |
| 11 |
* other stuffs |
| 12 |
* Class Rating |
| 13 |
* @package Wpmet\UtilityPackage |
| 14 |
*/ |
| 15 |
class Rating |
| 16 |
{ |
| 17 |
private $plugin_name; |
| 18 |
private $priority = 10; |
| 19 |
private $days; |
| 20 |
private $rating_url; |
| 21 |
private $support_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 $rating_show_interval = 30; |
| 30 |
/** |
| 31 |
* scripts version |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
// protected $script_version = '2.0.0'; |
| 36 |
private static $instance; |
| 37 |
/** |
| 38 |
* Method: instance -> Return Notice module class instance |
| 39 |
* |
| 40 |
* @param string|null $text_domain |
| 41 |
* @param string|null $unique_id |
| 42 |
* @return mixed |
| 43 |
*/ |
| 44 |
public static function instance($text_domain = null, $unique_id = null) |
| 45 |
{ |
| 46 |
if ($text_domain == null) { |
| 47 |
return \false; |
| 48 |
} |
| 49 |
self::$instance = new self(); |
| 50 |
self::$instance->config($text_domain, is_null($unique_id) ? uniqid() : $unique_id); |
| 51 |
return self::$instance; |
| 52 |
} |
| 53 |
/** |
| 54 |
* Set Text domain |
| 55 |
* |
| 56 |
* @param string $text_domain |
| 57 |
* @param string $unique_id |
| 58 |
*/ |
| 59 |
public function config($text_domain, $unique_id) |
| 60 |
{ |
| 61 |
$this->text_domain = $text_domain; |
| 62 |
} |
| 63 |
/** |
| 64 |
* Get vesrion of $this |
| 65 |
* |
| 66 |
* @return \Wpmet\Rating\Rating |
| 67 |
*/ |
| 68 |
public function get_version() |
| 69 |
{ |
| 70 |
// return $this->script_version; |
| 71 |
return UtilsHelper::get_pac_version(); |
| 72 |
} |
| 73 |
/** |
| 74 |
* @return $this file location for debugging 🐛 purpose |
| 75 |
*/ |
| 76 |
public function get_script_location() |
| 77 |
{ |
| 78 |
return __FILE__; |
| 79 |
} |
| 80 |
/** |
| 81 |
* @param |
| 82 |
*/ |
| 83 |
public function set_plugin($plugin_name, $plugin_url) |
| 84 |
{ |
| 85 |
$this->plugin_name = $plugin_name; |
| 86 |
$this->rating_url = $plugin_url; |
| 87 |
return $this; |
| 88 |
} |
| 89 |
/** |
| 90 |
* @param |
| 91 |
*/ |
| 92 |
public function set_priority($priority) |
| 93 |
{ |
| 94 |
$this->priority = $priority; |
| 95 |
return $this; |
| 96 |
} |
| 97 |
public function set_first_appear_day($days = 7) |
| 98 |
{ |
| 99 |
$this->days = $days; |
| 100 |
return $this; |
| 101 |
} |
| 102 |
public function set_rating_url($url) |
| 103 |
{ |
| 104 |
$this->rating_url = $url; |
| 105 |
return $this; |
| 106 |
} |
| 107 |
public function set_support_url($url) |
| 108 |
{ |
| 109 |
$this->support_url = $url; |
| 110 |
return $this; |
| 111 |
} |
| 112 |
public function set_plugin_logo($logo_url) |
| 113 |
{ |
| 114 |
$this->plugin_logo = $logo_url; |
| 115 |
return $this; |
| 116 |
} |
| 117 |
public function set_allowed_screens($screen) |
| 118 |
{ |
| 119 |
$this->plugin_screens[] = $screen; |
| 120 |
return $this; |
| 121 |
} |
| 122 |
public function set_condition($result) |
| 123 |
{ |
| 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 |
return $this; |
| 135 |
} |
| 136 |
public static function init() |
| 137 |
{ |
| 138 |
add_action('wp_ajax_wpmet_rating_never_show_message', array(__CLASS__, 'never_show_message')); |
| 139 |
add_action('wp_ajax_wpmet_rating_ask_me_later_message', array(__CLASS__, 'ask_me_later_message')); |
| 140 |
} |
| 141 |
protected function is_current_screen_allowed($current_screen_id) |
| 142 |
{ |
| 143 |
if (in_array($current_screen_id, array_merge($this->plugin_screens, array('dashboard', 'plugins')))) { |
| 144 |
return \true; |
| 145 |
} |
| 146 |
return \false; |
| 147 |
} |
| 148 |
/** |
| 149 |
* ------------------------------------------ |
| 150 |
* 🚀 Rating class execution point |
| 151 |
* ------------------------------------------ |
| 152 |
*/ |
| 153 |
public function call() |
| 154 |
{ |
| 155 |
$this->init(); |
| 156 |
add_action('admin_head', array($this, 'fire'), $this->priority); |
| 157 |
} |
| 158 |
/** |
| 159 |
* ------------------------------------------- |
| 160 |
* 🔥 fire the rating functionality |
| 161 |
* ------------------------------------------- |
| 162 |
*/ |
| 163 |
public function fire() |
| 164 |
{ |
| 165 |
if (current_user_can('update_plugins')) { |
| 166 |
$current_screen = get_current_screen(); |
| 167 |
if (!$this->is_current_screen_allowed($current_screen->id)) { |
| 168 |
return; |
| 169 |
} |
| 170 |
if ($this->condition_status === \false) { |
| 171 |
return; |
| 172 |
} |
| 173 |
add_action('admin_footer', array($this, 'scripts'), 9999); |
| 174 |
if ($this->action_on_fire()) { |
| 175 |
if (!$this->is_installation_date_exists()) { |
| 176 |
$this->set_installation_date(); |
| 177 |
} |
| 178 |
if (get_option($this->text_domain . '_never_show') == 'yes') { |
| 179 |
return; |
| 180 |
} |
| 181 |
if (get_option($this->text_domain . '_ask_me_later') == 'yes') { |
| 182 |
$this->days = $this->rating_show_interval; |
| 183 |
$this->duplication = \true; |
| 184 |
$this->never_show_triggered = \true; |
| 185 |
if ($this->get_remaining_days() >= $this->days) { |
| 186 |
$this->duplication = \false; |
| 187 |
} |
| 188 |
} |
| 189 |
$this->display_message_box(); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
private function action_on_fire() |
| 194 |
{ |
| 195 |
return \true; |
| 196 |
} |
| 197 |
public function set_installation_date() |
| 198 |
{ |
| 199 |
add_option($this->text_domain . '_install_date', date('Y-m-d h:i:s')); |
| 200 |
} |
| 201 |
public function is_installation_date_exists() |
| 202 |
{ |
| 203 |
return get_option($this->text_domain . '_install_date') == \false ? \false : \true; |
| 204 |
} |
| 205 |
public function get_installation_date() |
| 206 |
{ |
| 207 |
return get_option($this->text_domain . '_install_date'); |
| 208 |
} |
| 209 |
public function set_first_action_date() |
| 210 |
{ |
| 211 |
add_option($this->text_domain . '_first_action_Date', date('Y-m-d h:i:s')); |
| 212 |
add_option($this->text_domain . '_first_action', 'yes'); |
| 213 |
} |
| 214 |
public function get_days($from_date, $to_date) |
| 215 |
{ |
| 216 |
return round(($to_date->format('U') - $from_date->format('U')) / (60 * 60 * 24)); |
| 217 |
} |
| 218 |
public function is_first_use($in_days) |
| 219 |
{ |
| 220 |
$install_date = get_option($this->text_domain . '_install_date'); |
| 221 |
$display_date = date('Y-m-d h:i:s'); |
| 222 |
$datetime1 = new DateTime($install_date); |
| 223 |
$datetime2 = new DateTime($display_date); |
| 224 |
$diff_interval = $this->get_days($datetime1, $datetime2); |
| 225 |
if (abs($diff_interval) >= $in_days && get_option($this->text_domain . '_first_action_Date') == 'yes') { |
| 226 |
// action implementation here |
| 227 |
} |
| 228 |
} |
| 229 |
/** |
| 230 |
* --------------------------------------------- |
| 231 |
* Change the status of Rating notification |
| 232 |
* not to show the message again |
| 233 |
* --------------------------------------------- |
| 234 |
*/ |
| 235 |
public static function never_show_message() |
| 236 |
{ |
| 237 |
if (empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'wpmet_rating')) { |
| 238 |
return \false; |
| 239 |
} |
| 240 |
if (!current_user_can('manage_options')) { |
| 241 |
return \false; |
| 242 |
} |
| 243 |
$plugin_name = isset($_POST['plugin_name']) ? sanitize_key(wp_unslash($_POST['plugin_name'])) : ''; |
| 244 |
if ('' === $plugin_name) { |
| 245 |
return \false; |
| 246 |
} |
| 247 |
add_option($plugin_name . '_never_show', 'yes'); |
| 248 |
} |
| 249 |
/** |
| 250 |
* set rating show interval if user set ask me later |
| 251 |
*/ |
| 252 |
public function rating_show_interval(int $rating_show_interval = 30) |
| 253 |
{ |
| 254 |
$this->rating_show_interval = $rating_show_interval; |
| 255 |
return $this; |
| 256 |
} |
| 257 |
public function get_remaining_days() |
| 258 |
{ |
| 259 |
$install_date = get_option($this->text_domain . '_install_date'); |
| 260 |
$display_date = date('Y-m-d h:i:s'); |
| 261 |
$datetime1 = new DateTime($install_date); |
| 262 |
$datetime2 = new DateTime($display_date); |
| 263 |
$diff_interval = $this->get_days($datetime1, $datetime2); |
| 264 |
return abs($diff_interval); |
| 265 |
} |
| 266 |
/** |
| 267 |
*---------------------------------- |
| 268 |
* Ask me later functionality |
| 269 |
*---------------------------------- |
| 270 |
*/ |
| 271 |
public function display_message_box() |
| 272 |
{ |
| 273 |
if (!$this->duplication) { |
| 274 |
global $wpmet_libs_execution_container; |
| 275 |
if (isset($wpmet_libs_execution_container['rating'])) { |
| 276 |
return; |
| 277 |
} |
| 278 |
} |
| 279 |
$wpmet_libs_execution_container['rating'] = __FILE__; |
| 280 |
$install_date = get_option($this->text_domain . '_install_date'); |
| 281 |
$display_date = date('Y-m-d h:i:s'); |
| 282 |
$datetime1 = new DateTime($install_date); |
| 283 |
$datetime2 = new DateTime($display_date); |
| 284 |
$diff_interval = $this->get_days($datetime1, $datetime2); |
| 285 |
if (abs($diff_interval) >= $this->days) { |
| 286 |
$not_good_enough_btn_id = $this->never_show_triggered ? '_btn_never_show' : '_btn_not_good'; |
| 287 |
$message = "Hello! Seems like you have used {$this->plugin_name} to build this website — Thanks a lot! <br>\n\t\t\t\t\t\tCould you please do us a <b>big favor</b> and give it a <b>5-star</b> rating on WordPress? \n\t\t\t\t\t\tThis would boost our motivation and help other users make a comfortable decision while choosing the {$this->plugin_name}"; |
| 288 |
LibsNotice::instance($this->text_domain, '_plugin_rating_msg_used_in_day')->set_message($message)->set_logo($this->plugin_logo, 'max-height: 100px !important')->set_button(array('url' => $this->rating_url, 'text' => 'Ok, you deserved it', 'class' => 'button-primary', 'id' => $this->text_domain . '_btn_deserved'))->set_button(array('url' => get_current_screen()->id == 'toplevel_page_getgenie' ? '#write-for-me' : '#', 'text' => 'I already did', 'class' => 'button-default', 'id' => $this->text_domain . '_btn_already_did', 'icon' => 'dashicons-before dashicons-smiley'))->set_button(array('url' => $this->support_url, 'text' => 'I need support', 'class' => 'button-default', 'id' => '#', 'icon' => 'dashicons-before dashicons-sos'))->set_button(['url' => '#', 'text' => 'Never ask again', 'class' => 'button-default', 'id' => $this->text_domain . '_btn_never_show', 'icon' => 'dashicons-before dashicons-welcome-comments'])->set_button(array('url' => get_current_screen()->id == 'toplevel_page_getgenie' ? '#write-for-me' : '#', 'text' => 'No, not good enough', 'class' => 'button-default', 'id' => $this->text_domain . $not_good_enough_btn_id, 'icon' => 'dashicons-before dashicons-thumbs-down'))->call(); |
| 289 |
} |
| 290 |
} |
| 291 |
/** |
| 292 |
*--------------------------------------------------------- |
| 293 |
* When user will click @notGoodEnough button |
| 294 |
* Then it will fire this function to change the status |
| 295 |
* for next asking time |
| 296 |
*--------------------------------------------------------- |
| 297 |
*/ |
| 298 |
public static function ask_me_later_message() |
| 299 |
{ |
| 300 |
if (empty($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'wpmet_rating')) { |
| 301 |
return \false; |
| 302 |
} |
| 303 |
if (!current_user_can('manage_options')) { |
| 304 |
return \false; |
| 305 |
} |
| 306 |
$plugin_name = isset($_POST['plugin_name']) ? sanitize_key(wp_unslash($_POST['plugin_name'])) : ''; |
| 307 |
if ('' === $plugin_name) { |
| 308 |
return \false; |
| 309 |
} |
| 310 |
if (get_option($plugin_name . '_ask_me_later') == \false) { |
| 311 |
add_option($plugin_name . '_ask_me_later', 'yes'); |
| 312 |
} else { |
| 313 |
add_option($plugin_name . '_never_show', 'yes'); |
| 314 |
} |
| 315 |
} |
| 316 |
/** |
| 317 |
*-------------------------------------- |
| 318 |
* Get current version of the plugin |
| 319 |
*-------------------------------------- |
| 320 |
*/ |
| 321 |
public function get_current_version() |
| 322 |
{ |
| 323 |
return $this->version; |
| 324 |
} |
| 325 |
/** |
| 326 |
*------------------------------------------- |
| 327 |
* Get previous version of the plugin |
| 328 |
* that have been stored in database |
| 329 |
*------------------------------------------- |
| 330 |
*/ |
| 331 |
public function get_previous_version() |
| 332 |
{ |
| 333 |
return get_option($this->text_domain . '_version'); |
| 334 |
} |
| 335 |
/** |
| 336 |
*---------------------------------------- |
| 337 |
* Set current version of the plugin |
| 338 |
*---------------------------------------- |
| 339 |
*/ |
| 340 |
public function set_version($version) |
| 341 |
{ |
| 342 |
if (!get_option($this->text_domain . '_version')) { |
| 343 |
add_option($this->text_domain . '_version'); |
| 344 |
} else { |
| 345 |
update_option($this->text_domain . '_version', $version); |
| 346 |
} |
| 347 |
} |
| 348 |
/** |
| 349 |
* |
| 350 |
* JS Ajax script for updating |
| 351 |
* rating status from users |
| 352 |
* |
| 353 |
*/ |
| 354 |
public function scripts() |
| 355 |
{ |
| 356 |
echo "\n\t\t\t<script>\n\t\t\tjQuery(document).ready(function (\$) {\n\t\t\t\t\n\t\t\t\t\$( '#" . esc_js($this->text_domain) . "_btn_already_did' ).on( 'click', function() {\n\n\t\t\t\t\t\$.ajax({\n\t\t\t\t\t\turl: ajaxurl,\n\t\t\t\t\t\ttype: 'POST',\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\taction \t: 'wpmet_rating_never_show_message',\n\t\t\t\t\t\t\tplugin_name : '" . esc_js($this->text_domain) . "',\n\t\t\t\t\t\t\tnonce : '" . esc_js(wp_create_nonce('wpmet_rating')) . "'\n\n\t\t\t\t\t\t},\n\t\t\t\t\t\tsuccess:function(response){\n\t\t\t\t\t\t\t\$('#" . esc_js($this->text_domain) . "-_plugin_rating_msg_used_in_day').remove();\n\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t});\n\n\t\t\t\t\$('#" . esc_js($this->text_domain) . "_btn_deserved').click(function(){\n\t\t\t\t\t\$.ajax({\n\t\t\t\t\t\turl: ajaxurl,\n\t\t\t\t\t\ttype: 'POST',\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\taction \t: 'wpmet_rating_never_show_message',\n\t\t\t\t\t\t\tplugin_name : '" . esc_js($this->text_domain) . "',\n\t\t\t\t\t\t\tnonce : '" . esc_js(wp_create_nonce('wpmet_rating')) . "'\n\t\t\t\t\t\t},\n\t\t\t\t\t\tsuccess:function(response){\n\t\t\t\t\t\t\t\$('#" . esc_js($this->text_domain) . "-_plugin_rating_msg_used_in_day').remove();\n\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t});\n\n\t\t\t\t\$('#" . esc_js($this->text_domain) . "_btn_not_good').click(function(){\n\t\t\t\t\t\$.ajax({\n\t\t\t\t\t\turl: ajaxurl,\n\t\t\t\t\t\ttype: 'POST',\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\taction \t: 'wpmet_rating_ask_me_later_message',\n\t\t\t\t\t\t\tplugin_name : '" . esc_js($this->text_domain) . "',\n\t\t\t\t\t\t\tnonce : '" . esc_js(wp_create_nonce('wpmet_rating')) . "'\n\t\t\t\t\t\t},\n\t\t\t\t\t\tsuccess:function(response){\n\t\t\t\t\t\t\t\$('#" . esc_js($this->text_domain) . "-_plugin_rating_msg_used_in_day').remove();\n\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t});\n\t\t\t\t\n\t\t\t\t\$('#" . esc_js($this->text_domain) . "_btn_never_show').click(function(){\n\t\t\t\t\t\$.ajax({\n\t\t\t\t\t\turl: ajaxurl,\n\t\t\t\t\t\ttype: 'POST',\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\taction \t: 'wpmet_rating_never_show_message',\n\t\t\t\t\t\t\tplugin_name : '" . esc_js($this->text_domain) . "',\n\t\t\t\t\t\t\tnonce : '" . esc_js(wp_create_nonce('wpmet_rating')) . "'\n\t\t\t\t\t\t},\n\t\t\t\t\t\tsuccess:function(response){\n\t\t\t\t\t\t\t\$('#" . esc_js($this->text_domain) . "-_plugin_rating_msg_used_in_day').remove();\n\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t});\n\n\t\t\t});\n\t\t\t</script>\n\t"; |
| 357 |
} |
| 358 |
} |
| 359 |
|