PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.10.0
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.10.0
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / Admin.php

Admin.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.10.0, at includes/Admin.php

801 lines 28.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 use DateTime;
6 use Hurrytimer\Utils\Helpers;
7
8 class Admin {
9 /**
10 * The ID of this plugin.
11 *
12 * @since 1.0.0
13 * @access private
14 * @var string $pluginName The ID of this plugin.
15 */
16 private $pluginName;
17
18 /**
19 * The version of this plugin.
20 *
21 * @since 1.0.0
22 * @access private
23 * @var string $version The current version of this plugin.
24 */
25 private $version;
26
27 /**
28 * Initialize the class and set its properties.
29 *
30 * @param string $pluginName The name of this plugin.
31 * @param string $version The version of this plugin.
32 *
33 * @since 1.0.0
34 *
35 */
36 public function __construct($pluginName, $version) {
37 $this->pluginName = $pluginName;
38 $this->version = $version;
39 add_action('wp_insert_post', [$this, 'regenerate_css'], 10, 3);
40 add_action('wp_ajax_wcSearchProducts', [$this, 'ajaxWcSearchProducts']);
41 add_action('wp_ajax_hurrytimer/search_wc_coupon', [$this, 'search_wc_coupon']);
42 add_action('wp_ajax_add_wc_condition_group', [$this, 'ajaxAddWcConditionGroup']);
43 add_action('wp_ajax_add_wc_condition', [$this, 'ajaxAddWcCondition']);
44 add_action('wp_ajax_load_wc_condition', [$this, 'ajaxLoadWcCondition']);
45 add_action('wp_ajax_hurryt_dismiss_leave_review_notice', [$this, 'dismiss_leave_review_ajax']);
46 add_action('wp_ajax_hurryt_remind_leave_review_notice', [$this, 'remind_leave_review_ajax']);
47 add_action('admin_init', [$this, 'activateCampaign']);
48 add_action('admin_init', [$this, 'deactivateCampaign']);
49 add_action('admin_init', [$this, 'resetEvergreenCampaign']);
50 add_action('admin_init', [$this, 'resetAllEvergreenCampaigns']);
51 add_action('admin_init', [$this, 'pluginSettings']);
52 add_filter('post_row_actions', [$this, 'campaignsListRowActions']);
53 add_action('admin_menu', [$this, 'menu']);
54 add_filter(
55 'bulk_actions-edit-' . HURRYT_POST_TYPE,
56 [$this, 'campaignsListBulkActions',]
57 );
58
59 add_filter(
60 'manage_' . HURRYT_POST_TYPE . '_posts_columns',
61 [$this, 'campaignsListColumns',]
62 );
63
64
65 add_action(
66 'manage_' . HURRYT_POST_TYPE . '_posts_custom_column',
67 [$this, 'countdownListColumnsContent'],
68 10,
69 2
70 );
71
72 add_filter(
73 'bulk_post_updated_messages',
74 [$this, 'customBulkPostUpdatedMessages'],
75 10,
76 2
77 );
78
79 add_action('admin_menu', [$this, 'helpTabs']);
80 add_action('admin_notices', [$this, 'resetEvergreenCampaignNotice']);
81 add_action('admin_notices', [$this, 'resetAllEvergreenCampaignNotice']);
82
83 add_action('admin_notices', [$this, 'countdown_activation_notice']);
84 add_filter('admin_footer_text', [$this, 'admin_footer_text']);
85 add_filter('wp_insert_post_data', [$this, 'mark_post_being_saved']);
86
87 add_filter('plugin_action_links_' . HURRYT_BASENAME, [$this, 'set_plugin_action_links']);
88
89 //removeIf(pro)
90 add_action('admin_notices', [$this, 'leave_review_notice']);
91 //endRemoveIf(pro)
92
93 add_action('wp_ajax_hurryt_dismiss_headline_moved_notice', [$this, 'dismiss_headline_moved_notice']);
94
95 add_action('admin_enqueue_scripts', [$this, 'enqueue_review_scripts']);
96 }
97
98 function enqueue_review_scripts(){
99 wp_enqueue_script(
100 'hurryt-review',
101 HURRYT_URL . 'assets/js/review.js',
102 ['jquery'],
103 HURRYT_VERSION,
104 true
105 );
106
107 wp_localize_script(
108 'hurryt-review',
109 'hurrytimer_ajax_review',
110 [
111 'url' => admin_url('admin-ajax.php'),
112 'nonce' => wp_create_nonce('hurryt-review-nonce'),
113 ]
114 );
115
116 }
117 public function search_wc_coupon() {
118 check_ajax_referer('hurryt-admin', 'nonce');
119 $search = isset($_GET['search']) ? sanitize_text_field($_GET['search']) : '';
120 $exclude = isset($_GET['exclude']) ? array_map('intval', (array)$_GET['exclude']) : [];
121
122 $items = get_posts([
123 'post_type' => 'shop_coupon',
124 's' => $search,
125 'post__not_in' => $exclude,
126 'post_status' => 'any',
127 'numberposts' => 30
128 ]);
129
130 $results = array_map(function ($item) {
131 return [
132 "id" => $item->post_title,
133 "text" => $item->post_title,
134 ];
135 }, $items);
136 exit(json_encode(["results" => $results, "pagination" => true]));
137 }
138 public function dismiss_leave_review_ajax() {
139 check_ajax_referer('hurryt-review-nonce', 'nonce');
140 add_option('hurryt_leave_review_dismissed', '1');
141 }
142 public function remind_leave_review_ajax() {
143 check_ajax_referer('hurryt-review-nonce', 'nonce');
144 set_transient('hurryt_remind_review_notice', '1', 86400);
145 }
146
147 public function dismiss_headline_moved_notice() {
148 check_ajax_referer('hurryt-admin', 'nonce');
149 add_option('hurryt_headline_moved_notice_dismissed', '1');
150 }
151
152 private function should_ask_for_review() {
153
154 if (!apply_filters('hurryt_show_plugin_review_notice', true)) {
155 return false;
156 }
157
158 if (get_transient('hurryt_remind_review_notice')) {
159 return false;
160 }
161 if (get_option('hurryt_leave_review_dismissed')) {
162 return false;
163 }
164 if (isset($_COOKIE['hurryt_leave_review_remind'])) {
165 return false;
166 }
167 if (isset($_COOKIE['hurryt_leave_review_dismissed'])) {
168 return false;
169 }
170
171 $campaigns = get_posts([
172 'post_type' => HURRYT_POST_TYPE,
173 'post_status' => 'publish',
174 'posts_per_page' => 1,
175 'orderby' => 'post_date',
176 'order' => 'ASC'
177 ]);
178
179 if (is_wp_error($campaigns) || empty($campaigns) || !is_array($campaigns)) {
180 return false;
181 }
182
183 $post_date = new DateTime($campaigns[0]->post_date);
184
185 $now = new DateTime(current_time('mysql'));
186
187 $diff = $post_date->diff($now);
188
189 if (!$diff) {
190 return false;
191 }
192
193 return $diff->d >= 1;
194 }
195
196 public function leave_review_notice() {
197 if(! $this->should_ask_for_review()){
198 return;
199 }
200 ?>
201 <div class="notice notice-info is-dismissible">
202 <h3>Support the development of HurryTimer plugin!</h3>
203 <p>Thank you for choosing <b>HurryTimer</b>. If you liked the plugin, kindly leave us a 5-star review on <a href="https://wordpress.org/support/plugin/hurrytimer/reviews/?filter=5" target="_blank">WordPress.org</a>. We really appreciate your support!</p>
204
205 <p>
206 <a href="https://wordpress.org/support/plugin/hurrytimer/reviews/?filter=5" class="button-primary">Leave a review</a>
207 &nbsp;<button type="button" class="button button-default" id="hurryt-remind-review-notice">Remind me later</button>
208 &nbsp;<button type="button" class="button-link" id="hurryt-dismiss-review-notice" style="margin-left:10px">Already done!</button>
209 </p>
210 </div>
211 <?php
212 }
213
214 function mark_post_being_saved($data) {
215
216 global $hurryt_saving_post;
217
218 $hurryt_saving_post = 1;
219
220 return $data;
221 }
222
223 /**
224 * Generate published campaigns CSS file.
225 *
226 * @param $post_id
227 * @param $post
228 * @param $update
229 */
230 function regenerate_css($post_id, $post, $update) {
231 if (wp_is_post_revision($post_id)) {
232 return;
233 }
234
235 if ($post->post_type !== HURRYT_POST_TYPE) {
236 return;
237 }
238
239 if ($update) {
240 CSS_Builder::get_instance()->generate_css();
241 }
242 }
243
244 public function admin_footer_text() {
245 $screen = get_current_screen();
246 if ($screen->post_type === HURRYT_POST_TYPE) {
247 include HURRYT_DIR . 'admin/templates/footer-rate-plugin.php';
248 }
249 }
250
251 public function set_plugin_action_links($links) {
252 $settings_url = admin_url('admin.php?page=hurrytimer_settings');
253 $settings_anchor = '<a href="' . $settings_url . '">' . __('Settings') . '</a>';
254 $manage_url = admin_url('edit.php?post_type=' . HURRYT_POST_TYPE);
255 $manage_anchor = '<a href="' . $manage_url . '">' . __('Campaigns', 'hurrytimer') . '</a>';
256 $links[] = $manage_anchor;
257 $links[] = $settings_anchor;
258
259 //removeIf(pro)
260 $links[]
261 = '<a href="https://hurrytimer.com/?utm_source=plugin&utm_medium=installed_plugins&utm_campaign=go_pro" target="_blank" style="font-weight:bold;color:#F37335">Go Pro</a>';
262
263 //endRemoveIf(pro)
264
265 return $links;
266 }
267
268
269 public function countdown_activation_notice() {
270 $post_id = filter_input(INPUT_GET, 'post', FILTER_VALIDATE_INT);
271 $status = filter_input(INPUT_GET, 'hurrytimer_action');
272
273 if (!$post_id || !$status || !($post = get_post($post_id))) {
274 return;
275 }
276 if (
277 $status === "activate-compaign"
278 &&
279 !Utils\Helpers::isPostActive($post_id)
280 ) { ?>
281 <div class="notice notice-success is-dismissible">
282 <p><?php _e(
283 'Countdown timer deactivated.',
284 "hurrytimer"
285 ); ?></p>
286 </div>
287 <?php } else { ?>
288 <div class="notice notice-success is-dismissible">
289 <p><?php _e('Countdown timer activated.', "hurrytimer"); ?></p>
290 </div>
291 <?php }
292 }
293
294 public function helpTabs() {
295 add_action('load-edit.php', [$this, 'editHelpTabs']);
296 add_action('load-post-new.php', [$this, 'editHelpTabs']);
297 add_action('load-post.php', [$this, 'editHelpTabs']);
298 }
299
300 public function editHelpTabs() {
301 $screen = get_current_screen();
302 if (strpos($screen->id, 'hurrytimer_countdown') === false) {
303 return;
304 }
305 $screen->remove_help_tabs();
306 }
307
308 public function customBulkPostUpdatedMessages($bulk_messages, $bulk_counts) {
309 $bulk_messages[HURRYT_POST_TYPE] = [
310 'updated' => _n(
311 '%s countdown timer updated.',
312 '%s countdown timers updated.',
313 $bulk_counts['updated']
314 ),
315 'locked' => _n(
316 '%s countdown timer not updated, somebody is editing it.',
317 '%s countdown timers not updated, somebody is editing them.',
318 $bulk_counts['locked']
319 ),
320 'deleted' => _n(
321 '%s countdown timer permanently deleted.',
322 '%s countdown timers permanently deleted.',
323 $bulk_counts['deleted']
324 ),
325 'trashed' => _n(
326 '%s countdown timer deleted.',
327 '%s countdown timers deleted.',
328 $bulk_counts['trashed']
329 ),
330 'untrashed' => _n(
331 '%s countdown timer restored from the Trash.',
332 '%s countdown timers restored from the Trash.',
333 $bulk_counts['untrashed']
334 ),
335 ];
336
337 return $bulk_messages;
338 }
339
340 public function countdownListColumnsContent($column, $post_id) {
341 $campaign = new Campaign($post_id);
342 switch ($column) {
343 case 'status':
344 echo $campaign->is_published()
345 ? __('Active', 'hurrytimer')
346 : __('Inactive', 'hurrytimer');
347 break;
348 case 'mode':
349 if ($campaign->is_evergreen()) {
350 echo __('Evergreen', 'hurrytimer');
351 }
352 if ($campaign->is_one_time()) {
353 echo __('One-Time', 'hurrytimer');
354 }
355 if ($campaign->is_recurring()) {
356 echo __('Recurring', 'hurrytimer');
357 }
358 break;
359 case 'shortcode':
360 ?>
361 <input style="min-width:100%" type="text" readonly onfocus="this.select()" value="[hurrytimer id=&quot;<?php echo $post_id ?>&quot;]">
362 <?php
363
364 break;
365 }
366 }
367
368 public function campaignsListBulkActions($actions) {
369 unset($actions['edit']);
370 return $actions;
371 }
372
373 public function campaignsListColumns($columns) {
374 $allowed_columns = [];
375 foreach ($columns as $column_name => $value) {
376 if (in_array($column_name, ['cb', 'title', 'date'])) {
377 $allowed_columns[$column_name] = $value;
378 }
379 }
380
381 $columns = $allowed_columns;
382 $date = $columns['date'];
383 unset($columns['date']);
384 $columns = array_merge($columns, [
385 'status' => __('Status', "hurrytimer"),
386 'mode' => __('Mode', "hurrytimer"),
387 'shortcode' => __('Shortcode', "hurrytimer"),
388 ]);
389 $columns['date'] = $date;
390
391 return $columns;
392 }
393
394 /**
395 * Register the stylesheets for the admin area.
396 *
397 * @since 1.0.0
398 */
399
400 public function enqueueStyles() {
401
402 if (!$this->should_enqueue_scripts()) {
403 return;
404 }
405
406 $version = defined('WP_DEBUG') && WP_DEBUG ? time() : $this->version;
407
408 wp_enqueue_style('wp-color-picker');
409 wp_dequeue_style('select2');
410
411 wp_enqueue_style(
412 "codemirror",
413 HURRYT_URL . 'assets/css/codemirror.css',
414 [],
415 "5.42.2",
416 'all'
417 );
418 wp_enqueue_style(
419 'hurryt-select2',
420 HURRYT_URL . 'assets/css/select2.css',
421 array(),
422 '4.0.5',
423 'all'
424 );
425
426 wp_enqueue_style(
427 'hurryt-base-css',
428 HURRYT_URL . 'assets/css/main.css',
429 array(),
430 $version,
431 'all'
432 );
433
434 wp_enqueue_style(
435 $this->pluginName,
436 HURRYT_URL . 'assets/css/admin.css',
437 ['wp-color-picker', 'hurryt-base-css', 'hurryt-select2'],
438 $version,
439 'all'
440 );
441 }
442
443 public function campaignsListRowActions($actions) {
444 global $post;
445 if ($post->post_type === HURRYT_POST_TYPE) {
446 unset($actions['inline hide-if-no-js']);
447 }
448
449 return $actions;
450 }
451
452 /**
453 * Register the JavaScript for the admin area.
454 *
455 * @since 1.0.0
456 */
457
458 private function should_enqueue_scripts() {
459 $screen = get_current_screen();
460 $screen_id = $screen ? $screen->id : '';
461
462 $screens = [
463 'edit-hurrytimer_countdown',
464 'hurrytimer_countdown',
465 'hurrytimer_page_hurrytimer_settings',
466 'hurrytimer_page_hurrytimer_license'
467 ];
468
469 return apply_filters('hurrytimer/enqueue_scripts', in_array($screen_id, $screens, true));
470 }
471 public function enqueueScripts() {
472 if (!$this->should_enqueue_scripts()) {
473 return;
474 }
475 wp_enqueue_editor();
476 wp_enqueue_script('jquery-ui-core');
477 wp_enqueue_script('jquery-ui-tooltip');
478 wp_enqueue_script('wp-color-picker');
479 wp_enqueue_script('jquery-ui-slider');
480 wp_dequeue_script('select2');
481
482 wp_enqueue_script(
483 'hurryt-cookie',
484 HURRYT_URL . 'assets/js/cookie.min.js',
485 [],
486 '2.2.0',
487 true
488 );
489
490 wp_enqueue_script(
491 'jq-date-picker-addon',
492 HURRYT_URL . 'assets/js/timepicker-addon.js',
493 ['jquery-ui-datepicker'],
494 '1.6.3',
495 true
496 );
497
498 wp_enqueue_script(
499 'hurryt-select2',
500 HURRYT_URL . 'assets/js/select2.min.js',
501 ['jquery'],
502 '4.0.5',
503 true
504 );
505
506 $deps = ['wp-color-picker', 'hurryt-cookie', 'jq-date-picker-addon', 'hurryt-select2'];
507
508 $version = defined('WP_DEBUG') && WP_DEBUG ? time() : $this->version;
509
510 wp_enqueue_script(
511 $this->pluginName,
512 HURRYT_URL . 'assets/js/admin.js',
513 $deps,
514 $version,
515 true
516 );
517
518 wp_localize_script($this->pluginName, 'hurrytimer_ajax_object', array(
519 'ajax_nonce' => wp_create_nonce('hurryt-admin'),
520 'ajax_url' => admin_url('admin-ajax.php'),
521 'headline_pos' => [
522 'above_timer' => C::HEADLINE_POSITION_ABOVE_TIMER,
523 'below_timer' => C::HEADLINE_POSITION_BELOW_TIMER,
524 ],
525 'mode' => [
526 'evergreen' => C::MODE_EVERGREEN,
527 'regular' => C::MODE_REGULAR,
528 'recurring' => C::MODE_RECURRING,
529 ],
530 'COOKIEPATH' => defined('COOKIEPATH') ? COOKIEPATH : '',
531 'COOKIE_DOMAIN' => defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '',
532 ));
533 }
534
535 public function menu() {
536
537 add_menu_page(
538 'HurryTimer',
539 'HurryTimer',
540 apply_filters('hurryt_admin_capability', 'edit_posts', 'campaigns_list'),
541 'hurrytimer',
542 null,
543 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2aWV3Qm94PSIwIDAgNjkuNTYgNzAuNjIiPiAgPGRlZnM+ICAgIDxsaW5lYXJHcmFkaWVudCBpZD0iYzNiZGI3MTYtYmI3Ny00MzUxLTllOGYtYTUwY2Y3ODEzNDNlIiB5MT0iMzUuMzEiIHgyPSI2NS4wNyIgeTI9IjM1LjMxIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSI+ICAgICAgPHN0b3Agb2Zmc2V0PSIwIiBzdG9wLWNvbG9yPS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3ZnPg=='
544 );
545 add_submenu_page(
546 'hurrytimer',
547 __('Add Campaign', 'hurrytimer'),
548 __('Add Campaign', 'hurrytimer'),
549 apply_filters('hurryt_admin_capability', 'edit_posts', 'add_campaign'),
550 'post-new.php?post_type=hurrytimer_countdown'
551 );
552
553 add_submenu_page(
554 'hurrytimer',
555 __('Settings', 'hurrytimer'),
556 __('Settings', 'hurrytimer'),
557 apply_filters('hurryt_admin_capability', 'manage_options', 'settings'),
558 'hurrytimer_settings',
559 [$this, 'settings']
560 );
561
562 }
563
564 public function settings() {
565 include_once HURRYT_DIR . 'admin/templates/settings.php';
566 }
567
568 public function activateCampaign() {
569 if (
570 isset($_GET['hurrytimer_action'])
571 && $_GET['hurrytimer_action'] === "activate-compaign"
572 && isset($_GET['_wpnonce'])
573 && wp_verify_nonce($_GET['_wpnonce'], 'activate-compaign')
574 ) {
575 $postId = intval($_GET['post']);
576 wp_update_post(['ID' => $postId, 'post_status' => 'publish']);
577 }
578 }
579
580 function resetEvergreenCampaignNotice() {
581 if (
582 isset($_GET['hurryt-action'])
583 && isset($_GET['hurryt-scope'])
584 && isset($_GET['post'])
585 && $_GET['hurryt-action'] === "reset-evergreen-compaign"
586 && isset($_GET['_wpnonce'])
587 && wp_verify_nonce($_GET['_wpnonce'], 'reset-evergreen-compaign')
588 ) {
589 $scopeText
590 = $_GET['hurryt-scope'] === 'admin'
591 ? ' you '
592 : ' all visitors '; ?>
593 <div class="notice notice-success is-dismissible">
594 <p><?php echo sprintf(__('Campaign has been reset for %1$s successfully.', 'hurrytimer'), $scopeText); ?>
595
596 </p>
597 </div>
598 <?php
599 }
600 }
601
602 function resetAllEvergreenCampaignNotice() {
603 if (
604 isset($_GET['hurryt-page'])
605 && isset($_GET['hurryt-scope'])
606 && isset($_GET['hurryt-action'])
607 && $_GET['hurryt-page'] === "hurrytimer_settings"
608 && $_GET['hurryt-action'] === "reset-all-evergreen-compaigns"
609 && isset($_GET['_wpnonce'])
610 && wp_verify_nonce($_GET['_wpnonce'], 'reset-all-evergreen-compaigns')
611 ) {
612 $scopeText
613 = $_GET['hurryt-scope'] === 'admin'
614 ? ' you '
615 : ' all visitors '; ?>
616 <div class="notice notice-success is-dismissible">
617 <p><?php _e(
618 "All evergreen campaigns have been reset for $scopeText successfully.",
619 'hurrytimer'
620 ); ?></p>
621 </div>
622 <?php
623 }
624 }
625
626 public function resetEvergreenCampaign() {
627 if (
628 isset($_GET['hurryt-action'])
629 && isset($_GET['hurryt-scope'])
630 && isset($_GET['post'])
631 && $_GET['hurryt-action'] === "reset-evergreen-compaign"
632 && isset($_GET['_wpnonce'])
633 && wp_verify_nonce($_GET['_wpnonce'], 'reset-evergreen-compaign')
634 ) {
635 $postId = intval($_GET['post']);
636
637 $campaign = new EvergreenCampaign($postId);
638 $campaign->reset(sanitize_text_field($_GET['hurryt-scope']));
639 }
640 }
641
642 public function resetAllEvergreenCampaigns() {
643
644 if (
645 isset($_GET['hurryt-scope'])
646 && isset($_GET['hurryt-action'])
647 && $_GET['hurryt-action'] === "reset-all-evergreen-compaigns"
648 && isset($_GET['_wpnonce'])
649 && wp_verify_nonce($_GET['_wpnonce'], 'reset-all-evergreen-compaigns')
650 ) {
651 $evergreenCampaign = new EvergreenCampaign();
652 $evergreenCampaign->resetAll(sanitize_text_field($_GET['hurryt-scope']));
653 }
654 }
655
656 public function deactivateCampaign() {
657 if (
658 isset($_GET['hurrytimer_action'])
659 && $_GET['hurrytimer_action'] === "deactivate-compaign"
660 && isset($_GET['_wpnonce'])
661 && wp_verify_nonce($_GET['_wpnonce'], 'deactivate-compaign')
662 ) {
663 $postId = intval($_GET['post']);
664 wp_update_post(['ID' => $postId, 'post_status' => 'draft']);
665 }
666 }
667
668 public function settingsBox() {
669 require plugin_dir_path(dirname(__FILE__)) .
670 'admin/CampaignSettings.php';
671 new CampaignSettings();
672 }
673
674 //removeIf(pro)
675 public function upgradeBanner() {
676 add_meta_box(
677 'hurrytimer-upgrade-banner',
678 __('Get the Most of HurryTimer', 'hurrytimer'),
679 function () {
680 include HURRYT_DIR . 'admin/templates/upgrade-banner.php';
681 },
682 HURRYT_POST_TYPE,
683 'side',
684 'low'
685 );
686 }
687 //endRemoveIf(pro)
688
689 /**
690 * Search products
691 *
692 * @return void
693 */
694 public function ajaxWcSearchProducts() {
695 $search = isset($_GET['search']) ? sanitize_text_field($_GET['search']) : '';
696 $selection = isset($_GET['productsSelection']) ? sanitize_text_field($_GET['productsSelection']) : -1;
697 $exclude = isset($_GET['exclude']) ? array_map('intval', (array)$_GET['exclude']) : [];
698
699 if (
700 in_array($selection, [
701 C::WC_PS_TYPE_INCLUDE_CATEGORIES,
702 C::WC_PS_TYPE_EXCLUDE_CATEGORIES,
703 ])
704 ) {
705 $args = [
706 "hide_empty" => false,
707 "taxonomy" => "product_cat",
708 "name__like" => $search,
709 'exclude' => $exclude,
710 ];
711
712 $items = get_terms($args);
713 $results = array_map(function ($item) {
714 return [
715 "id" => $item->term_id,
716 "text" => $item->name,
717 ];
718 }, $items);
719 } else {
720 $args = [
721 's' => $search,
722 'post_type' => 'product',
723 'post__not_in' => $exclude,
724 'post_status' => 'any',
725 'numberposts' => 50 // TODO: apply paginating in select2
726 ];
727 $items = get_posts($args);
728 $results = array_map(function ($item) {
729 return [
730 "id" => $item->ID,
731 "text" => $item->post_title,
732 ];
733 }, $items);
734 }
735 exit(json_encode(["results" => $results, "pagination" => true]));
736 }
737
738 public function sanitize( $input ) {
739 $sanitized_input = Helpers::sanitize_array($input);
740 return $sanitized_input;
741 }
742 public function pluginSettings() {
743 register_setting('hurrytimer_settings', 'hurryt_settings', [$this, 'sanitize']);
744 add_settings_section(
745 'hurryt_settings_general',
746 __('General', 'hurrytimer'),
747 null,
748 'hurrytimer_settings'
749 );
750 add_settings_field(
751 'hurryt_disable_actions_edit_mode',
752 'Disable actions in the admin area',
753 function ($args) {
754 $options = get_option('hurryt_settings'); ?>
755 <label for="">
756 <input type="checkbox" name="hurryt_settings[disable_actions]" <?php checked(
757 isset($options['disable_actions'])
758 && $options['disable_actions'],
759 1
760 ); ?> id="hurryt-disable-actions" value="1" />
761 </label>
762 <p class="description">
763 Don't run actions when editing or previewing a page in the admin area.
764 </p>
765 <?php
766 },
767 'hurrytimer_settings',
768 'hurryt_settings_general'
769 );
770 }
771
772 function ajaxAddWcConditionGroup() {
773 check_ajax_referer('hurryt-admin', 'nonce');
774
775 ob_start();
776 include HURRYT_DIR . 'admin/templates/wc-condition-group.php';
777 echo ob_get_clean();
778 wp_die();
779 }
780
781 function ajaxAddWcCondition() {
782 check_ajax_referer('hurryt-admin', 'nonce');
783
784 ob_start();
785 $groupId = sanitize_key($_GET['group_id']);
786 include HURRYT_DIR . 'admin/templates/wc-condition.php';
787 echo ob_get_clean();
788 wp_die();
789 }
790
791 function ajaxLoadWcCondition() {
792 check_ajax_referer('hurryt-admin', 'nonce');
793 ob_start();
794 $groupId = sanitize_key($_GET['group_id']);
795 $selected = hurryt_wc_conditions()[sanitize_key($_GET['condition_key'])];
796 include HURRYT_DIR . 'admin/templates/wc-condition.php';
797 echo ob_get_clean();
798 wp_die();
799 }
800 }
801