From 9687ad74c2d909685f787e5ab8e357c691f0bd24 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Mon, 21 Nov 2022 11:04:58 +0000
Subject: [PATCH] Update cron runner to match upstream wp-cron code

---
 lib/cron.php | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/lib/cron.php b/lib/cron.php
index 0ab1423..b5a76ff 100644
--- a/lib/cron.php
+++ b/lib/cron.php
@@ -1,33 +1,32 @@
 <?php
 
-
 // Run cron jobs for the current blog.
 // (Use switch_to_blog() before calling this). 
 function noblogs_run_cron_for_current_blog() {
-  $crons = _get_cron_array();
-  if ($crons === false) {
-    return;
-  }
-
-  $local_time = time();
+  $crons = wp_get_ready_cron_jobs();
 
   foreach ($crons as $timestamp => $cronhooks) {
-    if ($timestamp > $local_time)
-      continue;
     foreach ($cronhooks as $hook => $keys) {
       foreach ($keys as $k => $v) {
         $args_str = implode(', ', $v['args']);
         echo "  {$k} -> {$hook} ({$args_str})\n";
         $schedule = $v['schedule'];
-        if ($schedule != false) {
-          $new_args = array($timestamp, $schedule, $hook, $v['args']);
-          call_user_func_array('wp_reschedule_event', $new_args);
+        if ($schedule) {
+            $result = wp_reschedule_event($timestamp, $schedule, $hook, $v['args'], true);
+            if (is_wp_error($result)) {
+                error_log('cron reschedule event error for hook: %1$s, code: %2$s, message: %3$s, data: %4%s',
+                          $hook, $result->get_error_code(), $result->get_error_message(), wp_json_encode($v));
+                do_action('cron_reschedule_event_error', $result, $hook, $v);
+            }
+        }
+        $result = wp_unschedule_event($timestamp, $hook, $v['args'], true);
+        if (is_wp_error($result)) {
+            error_log('cron unschedule event error for hook: %1$s, code: %2$s, message: %3$s, data: %4%s',
+                      $hook, $result->get_error_code(), $result->get_error_message(), wp_json_encode($v));
+            do_action('cron_unschedule_event_error', $result, $hook, $v);
         }
-        wp_unschedule_event($timestamp, $hook, $v['args']);
         do_action_ref_array($hook, $v['args']);
       }
     }
   }
 }
-
-
-- 
GitLab