PluginProbe
WPIDE – File Manager & Code Editor / 3.4.1
WPIDE – File Manager & Code Editor v3.4.1
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / App / Classes / ReviewNotice.php

ReviewNotice.php in WPIDE – File Manager & Code Editor 3.4.1, at App/Classes/ReviewNotice.php

188 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @since 1.0.0
21 * @access public
22 * @const int days_interval
23 */
24 public const days_interval = 7;
25
26 /**
27 * Max days before stopping the reminder notice
28 *
29 * @since 1.0.0
30 * @access public
31 * @const int max_days
32 */
33 public const max_days = 120;
34
35 /**
36 * Check if a notice is currently active
37 *
38 * @since 1.0.0
39 * @access public
40 * @var bool $active_notice
41 */
42 public static $active_notice = false;
43
44 /**
45 * Review url
46 *
47 * @since 1.0.0
48 * @access public
49 * @var string review_url
50 */
51 public static $review_url = '';
52
53 public static function init() {
54
55 self::$review_url = 'https://wordpress.org/support/plugin/'. SLUG .'/reviews/?rate=5&filter=5/#new-post';
56
57 // Add Ajax Events
58 add_action("wp_ajax_".SLUG."_plugin_rate_action", [__CLASS__, 'ajax_plugin_rate_action']);
59 add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue_admin_assets'], 1);
60
61 if(!self::enabled()) {
62 return;
63 }
64
65 self::$active_notice = true;
66
67 // Init Review Notice
68 add_action( 'admin_notices', [ __CLASS__, 'add_review_notice' ] );
69 }
70
71 public static function enabled(): bool
72 {
73
74 $min_days_trigger = self::get_min_days_trigger();
75 $days_passed = self::get_days_passed();
76
77 return $min_days_trigger !== -1 && $days_passed >= $min_days_trigger && !self::$active_notice;
78 }
79
80 public static function get_min_days_trigger(): int
81 {
82
83 $option_key = App::instance()->prefix('review_notice_min_days');
84 return intval(get_option($option_key, self::days_interval));
85 }
86
87 public static function set_min_days_trigger($value) {
88
89 $option_key = App::instance()->prefix('review_notice_min_days');
90 return update_option($option_key, $value);
91 }
92
93 public static function get_days_passed(): int
94 {
95
96 $install_timestamp = Migrations::$installed_time;
97 $current_time = time();
98
99 $date_diff = $current_time - $install_timestamp;
100
101 return intval(round($date_diff / (60 * 60 * 24)));
102 }
103
104 public static function get_readable_days_passed() {
105
106 $install_timestamp = Migrations::$installed_time;
107 $current_time = time();
108
109 return human_time_diff($install_timestamp, $current_time);
110 }
111
112 public static function ajax_plugin_rate_action() {
113
114 // Continue only if the nonce is correct
115 $nonce = sanitize_text_field($_REQUEST['_nonce']);
116
117 if ( ! wp_verify_nonce( $nonce, App::instance()->prefix('wp_rate_action_nonce') ) ) {
118 wp_send_json_error();
119 }
120
121 $rate_action = sanitize_text_field($_POST['rate_action']);
122
123 self::rate_plugin($rate_action);
124
125 wp_send_json_success();
126 }
127
128 public static function rate_plugin($rate_action, $days_interval = null) {
129
130 $min_days_trigger = self::get_min_days_trigger();
131 $days_passed = self::get_days_passed();
132 $days_interval = !empty($days_interval) ? $days_interval : self::days_interval;
133
134 if ( -1 === $min_days_trigger ) {
135 return;
136 }
137
138 if ('done-rating' === $rate_action) {
139
140 $min_days_trigger = -1;
141
142 } else {
143
144 if($min_days_trigger > self::max_days) {
145 $min_days_trigger = -1;
146 }else {
147 $min_days_trigger = $days_passed + $days_interval;
148 }
149 }
150
151 self::set_min_days_trigger($min_days_trigger);
152 }
153
154 public static function add_review_notice() {
155
156 $current_user = wp_get_current_user();
157 $first_name = ucfirst(strtolower(!empty($current_user->user_firstname) ? $current_user->user_firstname : $current_user->display_name));
158
159 $time_passed = self::get_readable_days_passed();
160 $action = SLUG."_plugin_rate_action";
161 ?>
162 <div class="notice notice-info wpide-rating-notice" data-slug="<?php echo esc_attr(SLUG); ?>">
163 <?php
164 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>');
165 echo '&nbsp;';
166 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>");
167 ?>
168 <br><br><em>~ Georges H</em>
169 <br><br>
170 <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') ) ?>">
171 <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>
172 <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>
173 <li><span class="dashicons dashicons-yes"></span> <a data-rate-action="done-rating" href="#"><?php echo esc_html__( 'I already did', 'wpide' ) ?></a></li>
174 <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>
175 </ul>
176 </div>
177 <?php
178 }
179
180 public static function enqueue_admin_assets()
181 {
182
183 wp_enqueue_script(SLUG.'-rating-notice', ASSETS_URL.'global/js/rating-notice-min.js', [], VERSION);
184 wp_enqueue_style(SLUG.'-rating-notice', ASSETS_URL.'global/css/rating-notice.css', [], VERSION);
185 }
186
187 }
188