review.php
332 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 | global $pagenow; |
| 180 | |
| 181 | if ($pagenow == 'post.php' || $pagenow == 'profile.php') { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | $uid = get_current_user_id(); |
| 186 | if (!defined('IRB_H_CHECK_LOADED') && function_exists('get_current_user_id') && isset($uid) && intval($uid) > 0) { |
| 187 | |
| 188 | $since = intval($this->using_since[$this->slug]); |
| 189 | $diff = time() - $since; |
| 190 | |
| 191 | if ($diff > $this->minimum_time) { |
| 192 | |
| 193 | $seconds = $diff; |
| 194 | $minutes = intval($diff / 60); |
| 195 | $hours = intval($minutes / 60); |
| 196 | $days = intval($hours / 24); |
| 197 | |
| 198 | $this->days = $days; |
| 199 | $this->minutes = $minutes; |
| 200 | $data = $this->using_since; |
| 201 | |
| 202 | if (isset($data['users']) && isset($data['users'][$uid])) { |
| 203 | |
| 204 | if (isset($data['users'][$uid]['delay_between'])) { |
| 205 | if (time() <= intval($data['users'][$uid]['delay_between'])) { |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (isset($data['users'][$uid][$this->slug])) { |
| 211 | if (isset($data['users'][$uid][$this->slug]['remind'])) { |
| 212 | if (time() <= intval($data['users'][$uid][$this->slug]['remind'])) { |
| 213 | return false; |
| 214 | } |
| 215 | } |
| 216 | if (isset($data['users'][$uid][$this->slug]['dismiss'])) { |
| 217 | if ($data['users'][$uid][$this->slug]['dismiss'] == true || $data['users'][$uid][$this->slug]['dismiss'] == 'true') { |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | } |
| 224 | |
| 225 | define('IRB_H_CHECK_LOADED', true); |
| 226 | return true; |
| 227 | |
| 228 | } else return false; |
| 229 | |
| 230 | } else return false; |
| 231 | |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * add_assets - adds required assests by the banner |
| 236 | * |
| 237 | * @return void |
| 238 | */ |
| 239 | public function add_assets() { |
| 240 | |
| 241 | if (!defined('IRB_H_ASSETS_LOADED')) { |
| 242 | define('IRB_H_ASSETS_LOADED', true); |
| 243 | wp_enqueue_script('inisev-review-script', $this->__asset('js/script.js'), [], filemtime($this->__dir_asset('js/script.js')), true); |
| 244 | wp_enqueue_style('inisev-review-style', $this->__asset('css/style.css'), [], filemtime($this->__dir_asset('css/style.css'))); |
| 245 | wp_localize_script('inisev-review-script', 'inisev_review_dismiss', [ |
| 246 | 'nonce' => wp_create_nonce('inisev_review_dismiss'), |
| 247 | ], true); |
| 248 | } |
| 249 | |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * display_review - loads the HTML and prints it in the header only once |
| 254 | * |
| 255 | * @return void |
| 256 | */ |
| 257 | public function display_review() { |
| 258 | |
| 259 | if (!defined('IRB_H_HTML_LOADED')) { |
| 260 | define('IRB_H_HTML_LOADED', true); |
| 261 | include_once __DIR__ . '/views/banner.php'; |
| 262 | } |
| 263 | |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * handle_review_action - Handles all POST actions |
| 268 | * |
| 269 | * @param string $_POST['token'] - must be === 'irbh' |
| 270 | * @param string $_POST['slug'] - the unique slug |
| 271 | * @param string $_POST['mode'] - the unique action remind/dismiss |
| 272 | * |
| 273 | * @return json returns it to browser |
| 274 | */ |
| 275 | public function handle_review_action() { |
| 276 | |
| 277 | if (check_ajax_referer('inisev_review_dismiss', 'nonce', false) === false) { |
| 278 | return wp_send_json_error(); |
| 279 | } |
| 280 | |
| 281 | $slug = sanitize_text_field($_POST['slug']); |
| 282 | $mode = sanitize_text_field($_POST['mode']); |
| 283 | |
| 284 | if (!empty($_POST['slug']) && isset($mode) && in_array($mode, ['dismiss', 'remind'])) { |
| 285 | $option_name = $this->option_name; |
| 286 | $data = get_option($option_name, false); |
| 287 | if ($data != false) { |
| 288 | |
| 289 | $uid = get_current_user_id(); |
| 290 | |
| 291 | if (!array_key_exists('users', $data)) $data['users'] = []; |
| 292 | if (!array_key_exists($uid, $data['users'])) $data['users'][$uid] = []; |
| 293 | if (!array_key_exists($slug, $data['users'][$uid])) $data['users'][$uid][$slug] = []; |
| 294 | |
| 295 | $data['users'][$uid]['delay_between'] = strtotime($this->time_between); |
| 296 | |
| 297 | if ($mode == 'remind') { |
| 298 | $data['users'][$uid][$slug]['remind'] = strtotime($this->remind_time); |
| 299 | } |
| 300 | |
| 301 | if ($mode == 'dismiss') { |
| 302 | $data['users'][$uid][$slug]['dismiss'] = true; |
| 303 | } |
| 304 | |
| 305 | update_option($option_name, $data); |
| 306 | |
| 307 | wp_send_json_success(); |
| 308 | |
| 309 | } else wp_send_json_error(); |
| 310 | } else wp_send_json_error(); |
| 311 | |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * init_review - initialization when the user is authenticated already |
| 316 | * |
| 317 | * @return void |
| 318 | */ |
| 319 | public function init_review() { |
| 320 | |
| 321 | if ($this->can_be_displayed()) { |
| 322 | add_action('admin_enqueue_scripts', [&$this, 'add_assets']); |
| 323 | add_action('admin_notices', [&$this, 'display_review']); |
| 324 | } |
| 325 | |
| 326 | } |
| 327 | |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | } |
| 332 |