| @@ -4,8 +4,10 @@ | ||
| 4 | 4 | |
| 5 | 5 | use WPIDE\App\App; |
| 6 | 6 | use const WPIDE\Constants\NAME; |
| 7 | 7 | use const WPIDE\Constants\SLUG; |
| 8 | +use const WPIDE\Constants\VERSION; | |
| 9 | +use const WPIDE\Constants\ASSETS_URL; | |
| 8 | 10 | |
| 9 | 11 | /** |
| 10 | 12 | * Class that takes care of adding plugin review notice after X days |
| 11 | 13 | */ |
| @@ -13,49 +15,36 @@ | ||
| 13 | 15 | class ReviewNotice { |
| 14 | 16 | |
| 15 | 17 | /** |
| 16 | 18 | * Interval in days between reminder notice |
| 17 | - * | |
| 18 | - * @since 1.0.0 | |
| 19 | - * @access public | |
| 20 | - * @const int days_interval | |
| 21 | 19 | */ |
| 22 | 20 | public const days_interval = 7; |
| 23 | 21 | |
| 24 | 22 | /** |
| 25 | 23 | * Max days before stopping the reminder notice |
| 26 | - * | |
| 27 | - * @since 1.0.0 | |
| 28 | - * @access public | |
| 29 | - * @const int max_days | |
| 30 | 24 | */ |
| 31 | 25 | public const max_days = 120; |
| 32 | 26 | |
| 33 | 27 | /** |
| 34 | 28 | * Check if a notice is currently active |
| 35 | - * | |
| 36 | - * @since 1.0.0 | |
| 37 | - * @access public | |
| 38 | - * @var bool $active_notice | |
| 39 | 29 | */ |
| 40 | 30 | public static $active_notice = false; |
| 41 | 31 | |
| 42 | 32 | /** |
| 43 | 33 | * Review url |
| 44 | - * | |
| 45 | - * @since 1.0.0 | |
| 46 | - * @access public | |
| 47 | - * @var string review_url | |
| 48 | 34 | */ |
| 49 | 35 | public static $review_url = ''; |
| 50 | 36 | |
| 37 | + /** | |
| 38 | + * @throws \Exception | |
| 39 | + */ | |
| 51 | 40 | public static function init() { |
| 52 | 41 | |
| 53 | 42 | self::$review_url = 'https://wordpress.org/support/plugin/'. SLUG .'/reviews/?rate=5&filter=5/#new-post'; |
| 54 | 43 | |
| 55 | 44 | // 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); | |
| 45 | + add_action("wp_ajax_".SLUG."_plugin_rate_action", [__CLASS__, 'ajaxPluginRateAction']); | |
| 46 | + add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue_admin_assets'], 1); | |
| 58 | 47 | |
| 59 | 48 | if(!self::enabled()) { |
| 60 | 49 | return; |
| 61 | 50 | } |
| @@ -65,18 +54,21 @@ | ||
| 65 | 54 | // Init Review Notice |
| 66 | 55 | add_action( 'admin_notices', [ __CLASS__, 'add_review_notice' ] ); |
| 67 | 56 | } |
| 68 | 57 | |
| 58 | + /** | |
| 59 | + * @throws \Exception | |
| 60 | + */ | |
| 69 | 61 | public static function enabled(): bool |
| 70 | 62 | { |
| 71 | 63 | |
| 72 | - $min_days_trigger = self::get_min_days_trigger(); | |
| 73 | - $days_passed = self::get_days_passed(); | |
| 64 | + $min_days_trigger = self::getMinDaysTrigger(); | |
| 65 | + $days_passed = self::getDaysPassed(); | |
| 74 | 66 | |
| 75 | 67 | return $min_days_trigger !== -1 && $days_passed >= $min_days_trigger && !self::$active_notice; |
| 76 | 68 | } |
| 77 | 69 | |
| 78 | - public static function get_min_days_trigger(): int | |
| 70 | + public static function getMinDaysTrigger(): int | |
| 79 | 71 | { |
| 80 | 72 | |
| 81 | 73 | $option_key = App::instance()->prefix('review_notice_min_days'); |
| 82 | 74 | return intval(get_option($option_key, self::days_interval)); |
| @@ -81,15 +73,16 @@ | ||
| 81 | 73 | $option_key = App::instance()->prefix('review_notice_min_days'); |
| 82 | 74 | return intval(get_option($option_key, self::days_interval)); |
| 83 | 75 | } |
| 84 | 76 | |
| 85 | - public static function set_min_days_trigger($value) { | |
| 77 | + public static function setMinDaysTrigger($value): bool | |
| 78 | + { | |
| 86 | 79 | |
| 87 | 80 | $option_key = App::instance()->prefix('review_notice_min_days'); |
| 88 | 81 | return update_option($option_key, $value); |
| 89 | 82 | } |
| 90 | 83 | |
| 91 | - public static function get_days_passed(): int | |
| 84 | + public static function getDaysPassed(): int | |
| 92 | 85 | { |
| 93 | 86 | |
| 94 | 87 | $install_timestamp = Migrations::$installed_time; |
| 95 | 88 | $current_time = time(); |
| @@ -98,9 +91,10 @@ | ||
| 98 | 91 | |
| 99 | 92 | return intval(round($date_diff / (60 * 60 * 24))); |
| 100 | 93 | } |
| 101 | 94 | |
| 102 | - public static function get_readable_days_passed() { | |
| 95 | + public static function getReadableDaysPassed(): string | |
| 96 | + { | |
| 103 | 97 | |
| 104 | 98 | $install_timestamp = Migrations::$installed_time; |
| 105 | 99 | $current_time = time(); |
| 106 | 100 | |
| @@ -106,28 +100,28 @@ | ||
| 106 | 100 | |
| 107 | 101 | return human_time_diff($install_timestamp, $current_time); |
| 108 | 102 | } |
| 109 | 103 | |
| 110 | - public static function ajax_plugin_rate_action() { | |
| 104 | + public static function ajaxPluginRateAction() { | |
| 111 | 105 | |
| 112 | 106 | // Continue only if the nonce is correct |
| 113 | 107 | $nonce = sanitize_text_field($_REQUEST['_nonce']); |
| 114 | 108 | |
| 115 | - if ( ! wp_verify_nonce( $nonce, App::instance()->prefix('wp_rate_action_nonce') ) ) { | |
| 109 | + if ( ! wp_verify_nonce( $nonce, App::instance()->prefix('wp_notice_action_nonce') ) ) { | |
| 116 | 110 | wp_send_json_error(); |
| 117 | 111 | } |
| 118 | 112 | |
| 119 | - $rate_action = sanitize_text_field($_POST['rate_action']); | |
| 113 | + $notice_action = sanitize_text_field($_POST['notice_action']); | |
| 120 | 114 | |
| 121 | - self::rate_plugin($rate_action); | |
| 115 | + self::ratePlugin($notice_action); | |
| 122 | 116 | |
| 123 | 117 | wp_send_json_success(); |
| 124 | 118 | } |
| 125 | 119 | |
| 126 | - public static function rate_plugin($rate_action, $days_interval = null) { | |
| 120 | + public static function ratePlugin($notice_action, $days_interval = null) { | |
| 127 | 121 | |
| 128 | - $min_days_trigger = self::get_min_days_trigger(); | |
| 129 | - $days_passed = self::get_days_passed(); | |
| 122 | + $min_days_trigger = self::getMinDaysTrigger(); | |
| 123 | + $days_passed = self::getDaysPassed(); | |
| 130 | 124 | $days_interval = !empty($days_interval) ? $days_interval : self::days_interval; |
| 131 | 125 | |
| 132 | 126 | if ( -1 === $min_days_trigger ) { |
| 133 | 127 | return; |
| @@ -132,9 +126,9 @@ | ||
| 132 | 126 | if ( -1 === $min_days_trigger ) { |
| 133 | 127 | return; |
| 134 | 128 | } |
| 135 | 129 | |
| 136 | - if ('done-rating' === $rate_action) { | |
| 130 | + if ('done-rating' === $notice_action) { | |
| 137 | 131 | |
| 138 | 132 | $min_days_trigger = -1; |
| 139 | 133 | |
| 140 | 134 | } else { |
| @@ -145,9 +139,9 @@ | ||
| 145 | 139 | $min_days_trigger = $days_passed + $days_interval; |
| 146 | 140 | } |
| 147 | 141 | } |
| 148 | 142 | |
| 149 | - self::set_min_days_trigger($min_days_trigger); | |
| 143 | + self::setMinDaysTrigger($min_days_trigger); | |
| 150 | 144 | } |
| 151 | 145 | |
| 152 | 146 | public static function add_review_notice() { |
| 153 | 147 | |
| @@ -153,23 +147,25 @@ | ||
| 153 | 147 | |
| 154 | 148 | $current_user = wp_get_current_user(); |
| 155 | 149 | $first_name = ucfirst(strtolower(!empty($current_user->user_firstname) ? $current_user->user_firstname : $current_user->display_name)); |
| 156 | 150 | |
| 157 | - $time_passed = self::get_readable_days_passed(); | |
| 151 | + $time_passed = self::getReadableDaysPassed(); | |
| 158 | 152 | $action = SLUG."_plugin_rate_action"; |
| 159 | 153 | ?> |
| 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') ) ?>"> | |
| 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') ) ?>"> | |
| 169 | 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> |
| 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> | |
| 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> | |
| 172 | 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> |
| 173 | 169 | </ul> |
| 174 | 170 | </div> |
| 175 | 171 | <?php |
| @@ -174,40 +170,12 @@ | ||
| 174 | 170 | </div> |
| 175 | 171 | <?php |
| 176 | 172 | } |
| 177 | 173 | |
| 178 | - public static function append_inline_script($scripts): string | |
| 174 | + public static function enqueue_admin_assets() | |
| 179 | 175 | { |
| 180 | 176 | |
| 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; | |
| 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); | |
| 211 | 179 | } |
| 212 | 180 | |
| 213 | 181 | } |