PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.12.0
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.12.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.12.0, at includes/Admin.php

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