PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.13
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.13
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Admin / Cron.php

Cron.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.13, at includes/Admin/Cron.php

127 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace NotificationX\Admin;
3
4 use NotificationX\Core\PostType;
5 use NotificationX\GetInstance;
6
7 /**
8 * This class is responsible for Cron Jobs
9 * for NotificationX & NotificationX Pro
10 * @method static Cron get_instance($args = null)
11 */
12 class Cron {
13 /**
14 * Instance of Cron
15 *
16 * @var Cron
17 */
18 use GetInstance;
19
20 /**
21 * Cron hook.
22 * @var string $hook
23 */
24 public $hook = 'nx_cron_update_data';
25
26 public function __construct(){
27 add_filter('cron_schedules', [$this, 'cron_schedule'], 10, 1);
28 add_action($this->hook, array($this, 'update_data'), 10, 1);
29 add_action('nx_delete_post', array($this, 'delete_post'), 10, 1);
30
31 }
32
33 /**
34 * Schedule cron jobs.
35 * @param int $post_id
36 * @param string $cache_key
37 */
38 public function set_cron($post_id, $cache_key = 'nx_cache_interval') {
39 if (!$post_id || empty($post_id)) {
40 return;
41 }
42 // First clear previously scheduled cron hook.
43 $this->clear_schedule((int) $post_id);
44
45 // If there is no next event, start cron now.
46 if (!wp_next_scheduled($this->hook, array((int) $post_id))) {
47 wp_schedule_event(time(), $cache_key, $this->hook, array((int) $post_id));
48 }
49 }
50
51 /**
52 * Schedule cron jobs.
53 * @param int $post_id
54 * @param string $cache_key
55 */
56 public function set_cron_single($post_id) {
57 if (!$post_id || empty($post_id)) {
58 return;
59 }
60
61 // If there is no next event, start cron now.
62 if (!wp_next_scheduled($this->hook, array((int) $post_id))) {
63 wp_schedule_single_event(time() + 10, $this->hook, array((int) $post_id));
64 }
65 }
66
67 /**
68 * Clearing Schedule
69 * @param array $args
70 * @since 1.1.3
71 */
72 public function clear_schedule($post_id) {
73 if (empty($post_id)) {
74 return false;
75 }
76 return wp_clear_scheduled_hook($this->hook, array((int) $post_id));
77 }
78
79 /**
80 * This method is responsible for cron schedules
81 *
82 * @param array $schedules
83 * @return array
84 * @since 1.1.3
85 */
86 public function cron_schedule($schedules) {
87 $download_stats_cache_duration = Settings::get_instance()->get('settings.download_stats_cache_duration', 3);
88 $reviews_cache_duration = Settings::get_instance()->get('settings.reviews_cache_duration', 3);
89
90 $schedules['nx_wp_stats_interval'] = array(
91 'interval' => MINUTE_IN_SECONDS * $download_stats_cache_duration,
92 // translators: %s: no of minutes
93 'display' => sprintf(__('Every %s minutes', 'notificationx'), $download_stats_cache_duration)
94 );
95
96 $schedules['nx_wp_review_interval'] = array(
97 'interval' => MINUTE_IN_SECONDS * $reviews_cache_duration,
98 // translators: %s: no of minutes
99 'display' => sprintf(__('Every %s minutes', 'notificationx'), $reviews_cache_duration)
100 );
101
102 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context.
103 $schedules = apply_filters('nx_cron_schedules', $schedules);
104
105 return $schedules;
106 }
107
108 public function update_data($post_id) {
109 if (empty($post_id)) {
110 return;
111 }
112 $post = PostType::get_instance()->get_post($post_id);
113
114 if(!empty($post['source']) && !empty($post['enabled'])){
115 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context.
116 do_action("{$this->hook}_{$post['source']}", $post_id, $post);
117 }
118 else{
119 $this->clear_schedule($post_id);
120 }
121 }
122
123 public function delete_post($post_id) {
124 $this->clear_schedule($post_id);
125 }
126 }
127