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