| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Classes; |
| 4 |
|
| 5 |
use WPIDE\App\App; |
| 6 |
use const WPIDE\Constants\NAME; |
| 7 |
use const WPIDE\Constants\SLUG; |
| 8 |
use const WPIDE\Constants\VERSION; |
| 9 |
use const WPIDE\Constants\ASSETS_URL; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class that takes care of adding plugin review notice after X days |
| 13 |
*/ |
| 14 |
|
| 15 |
class ReviewNotice { |
| 16 |
|
| 17 |
/** |
| 18 |
* Interval in days between reminder notice |
| 19 |
*/ |
| 20 |
public const days_interval = 7; |
| 21 |
|
| 22 |
/** |
| 23 |
* Max days before stopping the reminder notice |
| 24 |
*/ |
| 25 |
public const max_days = 120; |
| 26 |
|
| 27 |
/** |
| 28 |
* Check if a notice is currently active |
| 29 |
*/ |
| 30 |
public static $active_notice = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* Review url |
| 34 |
*/ |
| 35 |
public static $review_url = ''; |
| 36 |
|
| 37 |
/** |
| 38 |
* @throws \Exception |
| 39 |
*/ |
| 40 |
public static function init() { |
| 41 |
|
| 42 |
self::$review_url = 'https://wordpress.org/support/plugin/'. SLUG .'/reviews/?rate=5&filter=5/#new-post'; |
| 43 |
|
| 44 |
// Add Ajax Events |
| 45 |
add_action("wp_ajax_".SLUG."_plugin_rate_action", [__CLASS__, 'ajaxPluginRateAction']); |
| 46 |
add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue_admin_assets'], 1); |
| 47 |
|
| 48 |
if(!self::enabled()) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
self::$active_notice = true; |
| 53 |
|
| 54 |
// Init Review Notice |
| 55 |
add_action( 'admin_notices', [ __CLASS__, 'add_review_notice' ] ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @throws \Exception |
| 60 |
*/ |
| 61 |
public static function enabled(): bool |
| 62 |
{ |
| 63 |
|
| 64 |
$min_days_trigger = self::getMinDaysTrigger(); |
| 65 |
$days_passed = self::getDaysPassed(); |
| 66 |
|
| 67 |
return $min_days_trigger !== -1 && $days_passed >= $min_days_trigger && !self::$active_notice; |
| 68 |
} |
| 69 |
|
| 70 |
public static function getMinDaysTrigger(): int |
| 71 |
{ |
| 72 |
|
| 73 |
$option_key = App::instance()->prefix('review_notice_min_days'); |
| 74 |
return intval(get_option($option_key, self::days_interval)); |
| 75 |
} |
| 76 |
|
| 77 |
public static function setMinDaysTrigger($value): bool |
| 78 |
{ |
| 79 |
|
| 80 |
$option_key = App::instance()->prefix('review_notice_min_days'); |
| 81 |
return update_option($option_key, $value); |
| 82 |
} |
| 83 |
|
| 84 |
public static function getDaysPassed(): int |
| 85 |
{ |
| 86 |
|
| 87 |
$install_timestamp = Migrations::$installed_time; |
| 88 |
$current_time = time(); |
| 89 |
|
| 90 |
$date_diff = $current_time - $install_timestamp; |
| 91 |
|
| 92 |
return intval(round($date_diff / (60 * 60 * 24))); |
| 93 |
} |
| 94 |
|
| 95 |
public static function getReadableDaysPassed(): string |
| 96 |
{ |
| 97 |
|
| 98 |
$install_timestamp = Migrations::$installed_time; |
| 99 |
$current_time = time(); |
| 100 |
|
| 101 |
return human_time_diff($install_timestamp, $current_time); |
| 102 |
} |
| 103 |
|
| 104 |
public static function ajaxPluginRateAction() { |
| 105 |
|
| 106 |
// Continue only if the nonce is correct |
| 107 |
$nonce = sanitize_text_field($_REQUEST['_nonce']); |
| 108 |
|
| 109 |
if ( ! wp_verify_nonce( $nonce, App::instance()->prefix('wp_notice_action_nonce') ) ) { |
| 110 |
wp_send_json_error(); |
| 111 |
} |
| 112 |
|
| 113 |
$notice_action = sanitize_text_field($_POST['notice_action']); |
| 114 |
|
| 115 |
self::ratePlugin($notice_action); |
| 116 |
|
| 117 |
wp_send_json_success(); |
| 118 |
} |
| 119 |
|
| 120 |
public static function ratePlugin($notice_action, $days_interval = null) { |
| 121 |
|
| 122 |
$min_days_trigger = self::getMinDaysTrigger(); |
| 123 |
$days_passed = self::getDaysPassed(); |
| 124 |
$days_interval = !empty($days_interval) ? $days_interval : self::days_interval; |
| 125 |
|
| 126 |
if ( -1 === $min_days_trigger ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
if ('done-rating' === $notice_action) { |
| 131 |
|
| 132 |
$min_days_trigger = -1; |
| 133 |
|
| 134 |
} else { |
| 135 |
|
| 136 |
if($min_days_trigger > self::max_days) { |
| 137 |
$min_days_trigger = -1; |
| 138 |
}else { |
| 139 |
$min_days_trigger = $days_passed + $days_interval; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
self::setMinDaysTrigger($min_days_trigger); |
| 144 |
} |
| 145 |
|
| 146 |
public static function add_review_notice() { |
| 147 |
|
| 148 |
$current_user = wp_get_current_user(); |
| 149 |
$first_name = ucfirst(strtolower(!empty($current_user->user_firstname) ? $current_user->user_firstname : $current_user->display_name)); |
| 150 |
|
| 151 |
$time_passed = self::getReadableDaysPassed(); |
| 152 |
$action = SLUG."_plugin_rate_action"; |
| 153 |
?> |
| 154 |
<div class="notice notice-info wpide-notice" data-slug="<?php echo esc_attr(SLUG); ?>"> |
| 155 |
<p> |
| 156 |
<?php |
| 157 |
echo sprintf(esc_html__("Hey %s, I noticed you've been using %s for the past %s – that’s awesome!", "wpide"), $first_name, '<strong>' . NAME . '</strong>', '<strong>'.$time_passed.'</strong>'); |
| 158 |
echo ' '; |
| 159 |
echo sprintf(esc_html__('Could you please do me a %1$sBIG favor%2$s and give it a %1$s5-star rating%2$s on WordPress? Just to %1$s help us%2$s spread the word and boost our motivation. Many thanks!', "wpide"), "<strong>", "</strong>"); |
| 160 |
?> |
| 161 |
<br><br> |
| 162 |
<em>~ Georges H</em> |
| 163 |
</p> |
| 164 |
<ul class="wpide-notice-action" data-action="<?php echo esc_attr($action);?>" data-nonce="<?php echo wp_create_nonce( App::instance()->prefix('wp_notice_action_nonce') ) ?>"> |
| 165 |
<li><span class="dashicons dashicons-thumbs-up"></span> <a target="_blank" href="<?php echo esc_url(self::$review_url);?>"><?php echo esc_html__( 'Ok, you deserve it', 'wpide' ) ?></a></li> |
| 166 |
<li><span class="dashicons dashicons-thumbs-down"></span> <a data-action="not-enough" href="#"><?php echo esc_html__( 'Nope, maybe later', 'wpide' ) ?></a></li> |
| 167 |
<li><span class="dashicons dashicons-yes"></span> <a data-action="done-rating" href="#"><?php echo esc_html__( 'I already did', 'wpide' ) ?></a></li> |
| 168 |
<li><span class="dashicons dashicons-sos"></span> <a target="_blank" href="https://xplodedthemes.com/support/"><?php echo esc_html__( 'Help me first', 'wpide' ) ?></a></li> |
| 169 |
</ul> |
| 170 |
</div> |
| 171 |
<?php |
| 172 |
} |
| 173 |
|
| 174 |
public static function enqueue_admin_assets() |
| 175 |
{ |
| 176 |
|
| 177 |
wp_enqueue_script(SLUG.'-notice', ASSETS_URL.'global/js/notice-min.js', [], VERSION); |
| 178 |
wp_enqueue_style(SLUG.'-notice', ASSETS_URL.'global/css/notice.css', [], VERSION); |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|