| @@ -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. |