PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 1.3.7
Backup Migration v1.3.7
2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / modules / review / review.php
backup-backup / modules / review Last commit date
assets 2 years ago views 2 years ago review.php 2 years ago
review.php
326 lines
1 <?php
2
3 /**
4 * File for our cool review ask in the header
5 *
6 * @category Child Plugin
7 * @version v0.1.0
8 * @since v0.1.0
9 * @author iClyde <kontakt@iclyde.pl>
10 */
11
12 // Namespace
13 namespace Inisev\Subs;
14
15 // Disallow direct access
16 if (defined('ABSPATH')) {
17
18 /**
19 * Main class for handling the Review
20 */
21 if (!class_exists('Inisev\Subs\Inisev_Review')) {
22 class Inisev_Review {
23
24 /**
25 * Local variables
26 */
27 private $root; // __ROOT__ of plugin's root
28 private $file; // __FILE__ of plugin's root
29 private $slug; // Plugin's slug
30 private $name; // Name displayed in banner
31 private $days; // Days displayed in banner
32 private $minutes; // Minutes displayed in banner
33 private $debug = false; // If true it will display minutes
34 private $minimum_time = (30 * 24 * 60 * 60); // Minimum time required to display (in seconds)
35 // private $minimum_time = (3 * 60); // Minimum time required to display (in seconds)
36 private $remind_time = '+14 days'; // Time when banner will be reminded
37 private $time_between = '+2 days'; // Time when new banner can be displayed
38
39 /**
40 * Local URLs
41 */
42 private $root_url; // Root URL for plugin's dir
43 private $assets_url; // Root URL for review assets
44 private $review_url; // Review URL
45 private $plugin_menu_url; // Plugin's settings menu
46 public $option_name = '_irb_h_bn_review'; // Option name for this module
47 public $using_since; // Check since user uses this plugin
48
49 /**
50 * __construct:
51 * Compile some variables for "future use"
52 * Such as slug of current plugin, root dir of plugin
53 *
54 * @param string $root_file __FILE__ of plugin's main file
55 * @param string $root_dir __DIR__ of plugin's main file
56 * @param string $individual_slug Individual slug - mostly plugin's slug
57 * @param string $display_name The name that will be displayed in the banner
58 * @param string $review_url The URL that will be served as review one
59 * @param string $plugin_menu_url Plugin menu slug example.com/wp-admin/admin.php?page=<this slug here>
60 * @return Inisev_Review The review object
61 */
62 function __construct($root_file, $root_dir, $individual_slug, $display_name, $review_url, $plugin_menu_url) {
63
64 $this->file = $root_file;
65 $this->root = $root_dir;
66 $this->slug = $individual_slug;
67 $this->name = $display_name;
68
69 $this->review_url = $review_url;
70 $this->plugin_menu_url = admin_url('admin.php?page=' . $plugin_menu_url);
71
72 $this->root_url = plugin_dir_url($this->file);
73 $this->assets_url = $this->root_url . 'modules/review/assets/';
74 $option_name = $this->option_name;
75 $empty = ['users' => []];
76 $empty[$individual_slug] = time();
77
78 $data = get_option($option_name, false);
79
80 if ($data != false && isset($data) && is_array($data)) {
81
82 if (!array_key_exists($individual_slug, $data)) {
83
84 $data[$individual_slug] = time();
85 update_option($option_name, $data);
86
87 }
88
89 $this->using_since = $data;
90
91 } else {
92
93 if ($individual_slug == 'copy-delete-posts' && get_option('_cdp_review', false) != false) {
94 if (isset(get_option('_cdp_review', false)['installed'])) {
95 $empty[$individual_slug] = get_option('_cdp_review', false)['installed'];
96 }
97 }
98
99 update_option($option_name, $empty);
100 $this->using_since = $empty;
101
102 }
103
104 // Add handler for Ajax request
105 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
106
107 // Check if slug is defined
108 if (isset($_POST['slug']) && !empty($_POST['slug'])) {
109
110 // Handle the request
111 add_action('wp_ajax_inisev_review', [&$this, 'handle_review_action']);
112
113 }
114
115 // Stop for POST
116 return;
117
118 }
119
120 add_action('wp_loaded', [&$this, 'init_review']);
121
122 }
123
124 /**
125 * __asset - Loads assets
126 *
127 * @param string $file path relative
128 * @return string file URL
129 */
130 private function __asset($file) {
131
132 return $this->assets_url . $file;
133
134 }
135
136 /**
137 * __dir_asset - Loads assets
138 *
139 * @param string $file path relative
140 * @return string absolute path
141 */
142 private function __dir_asset($file) {
143
144 return __DIR__ . '/assets' . '/' . $file;
145
146 }
147
148 /**
149 * _asset - Loads assets and automatically echo
150 *
151 * @param string $file path relative
152 * @echo string file URL
153 */
154 private function _asset($file) {
155
156 echo $this->assets_url . $file;
157
158 }
159
160 /**
161 * _dir_asset - Loads assets and automatically echo
162 *
163 * @param string $file path relative
164 * @echo string absolute path
165 */
166 private function _dir_asset($file) {
167
168 echo __DIR__ . '/assets' . '/' . $file;
169
170 }
171
172 /**
173 * can_be_displayed - check if the banner should be displayed
174 *
175 * @return bool true if banner can be displayed
176 */
177 private function can_be_displayed() {
178
179 $uid = get_current_user_id();
180 if (!defined('IRB_H_CHECK_LOADED') && function_exists('get_current_user_id') && isset($uid) && intval($uid) > 0) {
181
182 $since = intval($this->using_since[$this->slug]);
183 $diff = time() - $since;
184
185 if ($diff > $this->minimum_time) {
186
187 $seconds = $diff;
188 $minutes = intval($diff / 60);
189 $hours = intval($minutes / 60);
190 $days = intval($hours / 24);
191
192 $this->days = $days;
193 $this->minutes = $minutes;
194 $data = $this->using_since;
195
196 if (isset($data['users']) && isset($data['users'][$uid])) {
197
198 if (isset($data['users'][$uid]['delay_between'])) {
199 if (time() <= intval($data['users'][$uid]['delay_between'])) {
200 return false;
201 }
202 }
203
204 if (isset($data['users'][$uid][$this->slug])) {
205 if (isset($data['users'][$uid][$this->slug]['remind'])) {
206 if (time() <= intval($data['users'][$uid][$this->slug]['remind'])) {
207 return false;
208 }
209 }
210 if (isset($data['users'][$uid][$this->slug]['dismiss'])) {
211 if ($data['users'][$uid][$this->slug]['dismiss'] == true || $data['users'][$uid][$this->slug]['dismiss'] == 'true') {
212 return false;
213 }
214 }
215 }
216
217 }
218
219 define('IRB_H_CHECK_LOADED', true);
220 return true;
221
222 } else return false;
223
224 } else return false;
225
226 }
227
228 /**
229 * add_assets - adds required assests by the banner
230 *
231 * @return void
232 */
233 public function add_assets() {
234
235 if (!defined('IRB_H_ASSETS_LOADED')) {
236 define('IRB_H_ASSETS_LOADED', true);
237 wp_enqueue_script('inisev-review-script', $this->__asset('js/script.js'), [], filemtime($this->__dir_asset('js/script.js')), true);
238 wp_enqueue_style('inisev-review-style', $this->__asset('css/style.css'), [], filemtime($this->__dir_asset('css/style.css')));
239 wp_localize_script('inisev-review-script', 'inisev_review_dismiss', [
240 'nonce' => wp_create_nonce('inisev_review_dismiss'),
241 ], true);
242 }
243
244 }
245
246 /**
247 * display_review - loads the HTML and prints it in the header only once
248 *
249 * @return void
250 */
251 public function display_review() {
252
253 if (!defined('IRB_H_HTML_LOADED')) {
254 define('IRB_H_HTML_LOADED', true);
255 include_once __DIR__ . '/views/banner.php';
256 }
257
258 }
259
260 /**
261 * handle_review_action - Handles all POST actions
262 *
263 * @param string $_POST['token'] - must be === 'irbh'
264 * @param string $_POST['slug'] - the unique slug
265 * @param string $_POST['mode'] - the unique action remind/dismiss
266 *
267 * @return json returns it to browser
268 */
269 public function handle_review_action() {
270
271 if (check_ajax_referer('inisev_review_dismiss', 'nonce', false) === false) {
272 return wp_send_json_error();
273 }
274
275 $slug = sanitize_text_field($_POST['slug']);
276 $mode = sanitize_text_field($_POST['mode']);
277
278 if (!empty($_POST['slug']) && isset($mode) && in_array($mode, ['dismiss', 'remind'])) {
279 $option_name = $this->option_name;
280 $data = get_option($option_name, false);
281 if ($data != false) {
282
283 $uid = get_current_user_id();
284
285 if (!array_key_exists('users', $data)) $data['users'] = [];
286 if (!array_key_exists($uid, $data['users'])) $data['users'][$uid] = [];
287 if (!array_key_exists($slug, $data['users'][$uid])) $data['users'][$uid][$slug] = [];
288
289 $data['users'][$uid]['delay_between'] = strtotime($this->time_between);
290
291 if ($mode == 'remind') {
292 $data['users'][$uid][$slug]['remind'] = strtotime($this->remind_time);
293 }
294
295 if ($mode == 'dismiss') {
296 $data['users'][$uid][$slug]['dismiss'] = true;
297 }
298
299 update_option($option_name, $data);
300
301 wp_send_json_success();
302
303 } else wp_send_json_error();
304 } else wp_send_json_error();
305
306 }
307
308 /**
309 * init_review - initialization when the user is authenticated already
310 *
311 * @return void
312 */
313 public function init_review() {
314
315 if ($this->can_be_displayed()) {
316 add_action('admin_enqueue_scripts', [&$this, 'add_assets']);
317 add_action('admin_notices', [&$this, 'display_review']);
318 }
319
320 }
321
322 }
323 }
324
325 }
326