| @@ -31,8 +31,18 @@ | ||
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 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 | + * | |
| 35 | 45 | * @param int $post_id |
| 36 | 46 | * @param string $cache_key |
| 37 | 47 | */ |
| 38 | 48 | public function set_cron($post_id, $cache_key = 'nx_cache_interval') { |
| @@ -38,8 +48,16 @@ | ||
| 38 | 48 | public function set_cron($post_id, $cache_key = 'nx_cache_interval') { |
| 39 | 49 | if (!$post_id || empty($post_id)) { |
| 40 | 50 | return; |
| 41 | 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 | + | |
| 42 | 60 | // First clear previously scheduled cron hook. |
| 43 | 61 | $this->clear_schedule((int) $post_id); |
| 44 | 62 | |
| 45 | 63 | // If there is no next event, start cron now. |
| @@ -98,8 +116,9 @@ | ||
| 98 | 116 | // translators: %s: no of minutes |
| 99 | 117 | 'display' => sprintf(__('Every %s minutes', 'notificationx'), $reviews_cache_duration) |
| 100 | 118 | ); |
| 101 | 119 | |
| 120 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 102 | 121 | $schedules = apply_filters('nx_cron_schedules', $schedules); |
| 103 | 122 | |
| 104 | 123 | return $schedules; |
| 105 | 124 | } |
| @@ -110,8 +129,9 @@ | ||
| 110 | 129 | } |
| 111 | 130 | $post = PostType::get_instance()->get_post($post_id); |
| 112 | 131 | |
| 113 | 132 | if(!empty($post['source']) && !empty($post['enabled'])){ |
| 133 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 114 | 134 | do_action("{$this->hook}_{$post['source']}", $post_id, $post); |
| 115 | 135 | } |
| 116 | 136 | else{ |
| 117 | 137 | $this->clear_schedule($post_id); |