| @@ -1,0 +1,144 @@ | ||
| 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 | + * | |
| 36 | + * Callers that run before `init` (migrations and upgrade routines hooked to | |
| 37 | + * `plugins_loaded`, for instance) are deferred rather than refused: | |
| 38 | + * wp_schedule_event() -> wp_get_schedules() fires the `cron_schedules` | |
| 39 | + * filter, and its callbacks -- ours, NX Pro's, WooCommerce's -- translate | |
| 40 | + * their interval labels, which before `init` trips WordPress' | |
| 41 | + * _load_textdomain_just_in_time "called too early" notice (WP 6.7+). | |
| 42 | + * Guarding here rather than at each call site keeps the whole class of bug | |
| 43 | + * closed, whatever hooks a future caller happens to run on. | |
| 44 | + * | |
| 45 | + * @param int $post_id | |
| 46 | + * @param string $cache_key | |
| 47 | + */ | |
| 48 | + public function set_cron($post_id, $cache_key = 'nx_cache_interval') { | |
| 49 | + if (!$post_id || empty($post_id)) { | |
| 50 | + return; | |
| 51 | + } | |
| 52 | + | |
| 53 | + if (!did_action('init')) { | |
| 54 | + add_action('init', function () use ($post_id, $cache_key) { | |
| 55 | + $this->set_cron($post_id, $cache_key); | |
| 56 | + }, 20); | |
| 57 | + return; | |
| 58 | + } | |
| 59 | + | |
| 60 | + // First clear previously scheduled cron hook. | |
| 61 | + $this->clear_schedule((int) $post_id); | |
| 62 | + | |
| 63 | + // If there is no next event, start cron now. | |
| 64 | + if (!wp_next_scheduled($this->hook, array((int) $post_id))) { | |
| 65 | + wp_schedule_event(time(), $cache_key, $this->hook, array((int) $post_id)); | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Schedule cron jobs. | |
| 71 | + * @param int $post_id | |
| 72 | + * @param string $cache_key | |
| 73 | + */ | |
| 74 | + public function set_cron_single($post_id) { | |
| 75 | + if (!$post_id || empty($post_id)) { | |
| 76 | + return; | |
| 77 | + } | |
| 78 | + | |
| 79 | + // If there is no next event, start cron now. | |
| 80 | + if (!wp_next_scheduled($this->hook, array((int) $post_id))) { | |
| 81 | + wp_schedule_single_event(time() + 10, $this->hook, array((int) $post_id)); | |
| 82 | + } | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Clearing Schedule | |
| 87 | + * @param array $args | |
| 88 | + * @since 1.1.3 | |
| 89 | + */ | |
| 90 | + public function clear_schedule($post_id) { | |
| 91 | + if (empty($post_id)) { | |
| 92 | + return false; | |
| 93 | + } | |
| 94 | + return wp_clear_scheduled_hook($this->hook, array((int) $post_id)); | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * This method is responsible for cron schedules | |
| 99 | + * | |
| 100 | + * @param array $schedules | |
| 101 | + * @return array | |
| 102 | + * @since 1.1.3 | |
| 103 | + */ | |
| 104 | + public function cron_schedule($schedules) { | |
| 105 | + $download_stats_cache_duration = Settings::get_instance()->get('settings.download_stats_cache_duration', 3); | |
| 106 | + $reviews_cache_duration = Settings::get_instance()->get('settings.reviews_cache_duration', 3); | |
| 107 | + | |
| 108 | + $schedules['nx_wp_stats_interval'] = array( | |
| 109 | + 'interval' => MINUTE_IN_SECONDS * $download_stats_cache_duration, | |
| 110 | + // translators: %s: no of minutes | |
| 111 | + 'display' => sprintf(__('Every %s minutes', 'notificationx'), $download_stats_cache_duration) | |
| 112 | + ); | |
| 113 | + | |
| 114 | + $schedules['nx_wp_review_interval'] = array( | |
| 115 | + 'interval' => MINUTE_IN_SECONDS * $reviews_cache_duration, | |
| 116 | + // translators: %s: no of minutes | |
| 117 | + 'display' => sprintf(__('Every %s minutes', 'notificationx'), $reviews_cache_duration) | |
| 118 | + ); | |
| 119 | + | |
| 120 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 121 | + $schedules = apply_filters('nx_cron_schedules', $schedules); | |
| 122 | + | |
| 123 | + return $schedules; | |
| 124 | + } | |
| 125 | + | |
| 126 | + public function update_data($post_id) { | |
| 127 | + if (empty($post_id)) { | |
| 128 | + return; | |
| 129 | + } | |
| 130 | + $post = PostType::get_instance()->get_post($post_id); | |
| 131 | + | |
| 132 | + if(!empty($post['source']) && !empty($post['enabled'])){ | |
| 133 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 134 | + do_action("{$this->hook}_{$post['source']}", $post_id, $post); | |
| 135 | + } | |
| 136 | + else{ | |
| 137 | + $this->clear_schedule($post_id); | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | + public function delete_post($post_id) { | |
| 142 | + $this->clear_schedule($post_id); | |
| 143 | + } | |
| 144 | +} | |