| 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 |
|
| 9 |
/** |
| 10 |
* Class that takes care of adding plugin review notice after X days |
| 11 |
*/ |
| 12 |
|
| 13 |
class ReviewNotice { |
| 14 |
|
| 15 |
/** |
| 16 |
* Interval in days between reminder notice |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
* @access public |
| 20 |
* @const int days_interval |
| 21 |
*/ |
| 22 |
public const days_interval = 7; |
| 23 |
|
| 24 |
/** |
| 25 |
* Max days before stopping the reminder notice |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @access public |
| 29 |
* @const int max_days |
| 30 |
*/ |
| 31 |
public const max_days = 120; |
| 32 |
|
| 33 |
/** |
| 34 |
* Check if a notice is currently active |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @access public |
| 38 |
* @var bool $active_notice |
| 39 |
*/ |
| 40 |
public static $active_notice = false; |
| 41 |
|
| 42 |
/** |
| 43 |
* Review url |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* @access public |
| 47 |
* @var string review_url |
| 48 |
*/ |
| 49 |
public static $review_url = ''; |
| 50 |
|
| 51 |
public static function init() { |
| 52 |
|
| 53 |
self::$review_url = 'https://wordpress.org/support/plugin/'. SLUG .'/reviews/?rate=5&filter=5/#new-post'; |
| 54 |
|
| 55 |
// Add Ajax Events |
| 56 |
add_action("wp_ajax_".SLUG."_plugin_rate_action", [__CLASS__, 'ajax_plugin_rate_action']); |
| 57 |
add_action('wpide_inline_scripts', [__CLASS__, 'append_inline_script'], 1); |
| 58 |
|
| 59 |
if(!self::enabled()) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
self::$active_notice = true; |
| 64 |
|
| 65 |
// Init Review Notice |
| 66 |
add_action( 'admin_notices', [ __CLASS__, 'add_review_notice' ] ); |
| 67 |
} |
| 68 |
|
| 69 |
public static function enabled(): bool |
| 70 |
{ |
| 71 |
|
| 72 |
$min_days_trigger = self::get_min_days_trigger(); |
| 73 |
$days_passed = self::get_days_passed(); |
| 74 |
|
| 75 |
return $min_days_trigger !== -1 && $days_passed >= $min_days_trigger && !self::$active_notice; |
| 76 |
} |
| 77 |
|
| 78 |
public static function get_min_days_trigger(): int |
| 79 |
{ |
| 80 |
|
| 81 |
$option_key = App::instance()->prefix('review_notice_min_days'); |
| 82 |
return intval(get_option($option_key, self::days_interval)); |
| 83 |
} |
| 84 |
|
| 85 |
public static function set_min_days_trigger($value) { |
| 86 |
|
| 87 |
$option_key = App::instance()->prefix('review_notice_min_days'); |
| 88 |
return update_option($option_key, $value); |
| 89 |
} |
| 90 |
|
| 91 |
public static function get_days_passed(): int |
| 92 |
{ |
| 93 |
|
| 94 |
$install_timestamp = Migrations::$installed_time; |
| 95 |
$current_time = time(); |
| 96 |
|
| 97 |
$date_diff = $current_time - $install_timestamp; |
| 98 |
|
| 99 |
return intval(round($date_diff / (60 * 60 * 24))); |
| 100 |
} |
| 101 |
|
| 102 |
public static function get_readable_days_passed() { |
| 103 |
|
| 104 |
$install_timestamp = Migrations::$installed_time; |
| 105 |
$current_time = time(); |
| 106 |
|
| 107 |
return human_time_diff($install_timestamp, $current_time); |
| 108 |
} |
| 109 |
|
| 110 |
public static function ajax_plugin_rate_action() { |
| 111 |
|
| 112 |
// Continue only if the nonce is correct |
| 113 |
$nonce = sanitize_text_field($_REQUEST['_nonce']); |
| 114 |
|
| 115 |
if ( ! wp_verify_nonce( $nonce, App::instance()->prefix('wp_rate_action_nonce') ) ) { |
| 116 |
wp_send_json_error(); |
| 117 |
} |
| 118 |
|
| 119 |
$rate_action = sanitize_text_field($_POST['rate_action']); |
| 120 |
|
| 121 |
self::rate_plugin($rate_action); |
| 122 |
|
| 123 |
wp_send_json_success(); |
| 124 |
} |
| 125 |
|
| 126 |
public static function rate_plugin($rate_action, $days_interval = null) { |
| 127 |
|
| 128 |
$min_days_trigger = self::get_min_days_trigger(); |
| 129 |
$days_passed = self::get_days_passed(); |
| 130 |
$days_interval = !empty($days_interval) ? $days_interval : self::days_interval; |
| 131 |
|
| 132 |
if ( -1 === $min_days_trigger ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
if ('done-rating' === $rate_action) { |
| 137 |
|
| 138 |
$min_days_trigger = -1; |
| 139 |
|
| 140 |
} else { |
| 141 |
|
| 142 |
if($min_days_trigger > self::max_days) { |
| 143 |
$min_days_trigger = -1; |
| 144 |
}else { |
| 145 |
$min_days_trigger = $days_passed + $days_interval; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
self::set_min_days_trigger($min_days_trigger); |
| 150 |
} |
| 151 |
|
| 152 |
public static function add_review_notice() { |
| 153 |
|
| 154 |
$current_user = wp_get_current_user(); |
| 155 |
$first_name = ucfirst(strtolower(!empty($current_user->user_firstname) ? $current_user->user_firstname : $current_user->display_name)); |
| 156 |
|
| 157 |
$time_passed = self::get_readable_days_passed(); |
| 158 |
$action = SLUG."_plugin_rate_action"; |
| 159 |
?> |
| 160 |
<div class="notice notice-info" data-slug="<?php echo esc_attr(SLUG); ?>"> |
| 161 |
<?php |
| 162 |
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>'); |
| 163 |
echo ' '; |
| 164 |
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>"); |
| 165 |
?> |
| 166 |
<br><br><em>~ Georges H</em> |
| 167 |
<br><br> |
| 168 |
<ul class="wpide-wp-rate-notice" data-action="<?php echo esc_attr($action);?>" data-nonce="<?php echo wp_create_nonce( App::instance()->prefix('wp_rate_action_nonce') ) ?>"> |
| 169 |
<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> |
| 170 |
<li><span class="dashicons dashicons-thumbs-down"></span> <a data-rate-action="not-enough" href="#"><?php echo esc_html__( 'Nope, maybe later', 'wpide' ) ?></a></li> |
| 171 |
<li><span class="dashicons dashicons-yes"></span> <a data-rate-action="done-rating" href="#"><?php echo esc_html__( 'I already did', 'wpide' ) ?></a></li> |
| 172 |
<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> |
| 173 |
</ul> |
| 174 |
</div> |
| 175 |
<?php |
| 176 |
} |
| 177 |
|
| 178 |
public static function append_inline_script($scripts): string |
| 179 |
{ |
| 180 |
|
| 181 |
$scripts .= ' |
| 182 |
(function( $ ) { |
| 183 |
$(function() { |
| 184 |
|
| 185 |
$(document).on("click", ".wpide-wp-rate-notice a[data-rate-action]", function(evt) { |
| 186 |
|
| 187 |
var notice = $(this).closest(".wpide-wp-rate-notice"); |
| 188 |
var rateAction = $(this).attr("data-rate-action"); |
| 189 |
var container = $( this ).closest( ".nk-notification-item" ); |
| 190 |
var index = container.index(); |
| 191 |
|
| 192 |
var event = new CustomEvent("wpide_notice_removed", { detail: index }); |
| 193 |
document.body.dispatchEvent(event); |
| 194 |
|
| 195 |
evt.preventDefault(); |
| 196 |
|
| 197 |
$.post(ajaxurl, { |
| 198 |
action: notice.attr("data-action"), |
| 199 |
rate_action: rateAction, |
| 200 |
_nonce: notice.attr("data-nonce") |
| 201 |
}).fail(function(error) { |
| 202 |
console.log(error); |
| 203 |
}); |
| 204 |
}); |
| 205 |
|
| 206 |
}); |
| 207 |
})( jQuery ); |
| 208 |
'; |
| 209 |
|
| 210 |
return $scripts; |
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
|