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

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