<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[44693] trunk: Cron: Move logic for finding the next event's timestamp.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/44693">44693</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/44693","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-01-23 23:07:39 +0000 (Wed, 23 Jan 2019)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Cron: Move logic for finding the next event's timestamp.
Moves the logic for determining when an event will next run from `wp_next_scheduled()` to `wp_get_scheduled_event()`.
This improves the performance of `wp_get_scheduled_event()` by avoiding duplicate function calls and object cache hits.
Props peterwilsoncc.
Fixes <a href="https://core.trac.wordpress.org/ticket/45976">#45976</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescronphp">trunk/src/wp-includes/cron.php</a></li>
<li><a href="#trunktestsphpunittestscronphp">trunk/tests/phpunit/tests/cron.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescronphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/cron.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/cron.php 2019-01-23 03:38:19 UTC (rev 44692)
+++ trunk/src/wp-includes/cron.php 2019-01-23 23:07:39 UTC (rev 44693)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -481,7 +481,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieve a scheduled event.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Retrieve the full event object for a given event.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieve the full event object for a given event, if no timestamp is specified the next
+ * scheduled event is returned.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.1.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -493,11 +494,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool|object The event object. False if the event does not exist.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $timestamp ) {
- // Get the next scheduled event.
- $timestamp = wp_next_scheduled( $hook, $args );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filter to preflight or hijack retrieving a scheduled event.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -514,7 +510,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $args Array containing each separate argument to pass to the hook's callback function.
</span><span class="cx" style="display: block; padding: 0 10px"> * Although not passed to a callback, these arguments are used to uniquely identify the
</span><span class="cx" style="display: block; padding: 0 10px"> * event.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param int $timestamp Unix timestamp (UTC) of the event.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int|null $timestamp Unix timestamp (UTC) of the event. Null to retrieve next scheduled event.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $pre = apply_filters( 'pre_get_scheduled_event', null, $hook, $args, $timestamp );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( null !== $pre ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -521,15 +517,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $pre;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( null !== $timestamp && ! is_numeric( $timestamp ) ) {
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $crons = _get_cron_array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $key = md5( serialize( $args ) );
-
- if ( ! $timestamp || ! isset( $crons[ $timestamp ] ) ) {
- // No such event.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $crons ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! isset( $crons[ $timestamp ][ $hook ] ) || ! isset( $crons[ $timestamp ][ $hook ][ $key ] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $key = md5( serialize( $args ) );
+
+ if ( ! $timestamp ) {
+ // Get next event.
+ $next = false;
+ foreach ( $crons as $timestamp => $cron ) {
+ if ( isset( $cron[ $hook ][ $key ] ) ) {
+ $next = $timestamp;
+ break;
+ }
+ }
+ if ( ! $next ) {
+ return false;
+ }
+
+ $timestamp = $next;
+ } elseif ( ! isset( $crons[ $timestamp ][ $hook ][ $key ] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -551,7 +564,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieve the next timestamp for an event.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.1.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.1.0 {@see 'pre_next_scheduled'} and {@see 'next_scheduled'} filters added.
</del><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $hook Action hook of the event.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -560,48 +572,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return false|int The Unix timestamp of the next time the event will occur. False if the event doesn't exist.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_next_scheduled( $hook, $args = array() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Filter to preflight or hijack retrieving the next scheduled event timestamp.
- *
- * Returning a non-null value will short-circuit the normal retrieval
- * process, causing the function to return the filtered value instead.
- *
- * Pass the timestamp of the next event if it exists, false if not.
- *
- * @since 5.1.0
- *
- * @param null|bool $pre Value to return instead. Default null to continue unscheduling the event.
- * @param string $hook Action hook of the event.
- * @param array $args Arguments to pass to the hook's callback function.
- */
- $pre = apply_filters( 'pre_next_scheduled', null, $hook, $args );
- if ( null !== $pre ) {
- return $pre;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $next_event = wp_get_scheduled_event( $hook, $args );
+ if ( ! $next_event ) {
+ return false;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $crons = _get_cron_array();
- $key = md5( serialize( $args ) );
- $next = false;
-
- if ( ! empty( $crons ) ) {
- foreach ( $crons as $timestamp => $cron ) {
- if ( isset( $cron[ $hook ][ $key ] ) ) {
- $next = $timestamp;
- break;
- }
- }
- }
-
- /**
- * Filter the next scheduled event timestamp.
- *
- * @since 5.1.0
- *
- * @param int|bool $next The UNIX timestamp when the scheduled event will next occur, or false if not found.
- * @param string $hook Action hook to execute when cron is run.
- * @param array $args Arguments to be passed to the callback function. Used for deduplicating events.
- */
- return apply_filters( 'next_scheduled', $next, $hook, $args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $next_event->timestamp;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestscronphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/cron.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/cron.php 2019-01-23 03:38:19 UTC (rev 44692)
+++ trunk/tests/phpunit/tests/cron.php 2019-01-23 23:07:39 UTC (rev 44693)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -458,7 +458,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_pre_scheduled_event_hooks() {
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'pre_get_scheduled_event', array( $this, 'filter_pre_scheduled_event_hooks' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- add_filter( 'pre_next_scheduled', array( $this, 'filter_pre_scheduled_event_hooks' ) );
</del><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $actual = wp_get_scheduled_event( 'preflight_event', array(), $this->plus_thirty_minutes );
</span><span class="cx" style="display: block; padding: 0 10px"> $actual2 = wp_next_scheduled( 'preflight_event', array() );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -471,7 +470,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $expected, $actual );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( $expected, $actual2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( $expected->timestamp, $actual2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> function filter_pre_scheduled_event_hooks() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -482,4 +481,119 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'args' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Ensure wp_get_scheduled_event() returns the expected one off events.
+ *
+ * When no timestamp is specified, the next event should be returned.
+ * When a timestamp is specified, a particular event should be returned.
+ *
+ * @ticket 45976.
+ */
+ function test_get_scheduled_event_singles() {
+ $hook = __FUNCTION__;
+ $args = array( 'arg1' );
+ $ts_late = strtotime( '+30 minutes' );
+ $ts_next = strtotime( '+3 minutes' );
+
+ $expected1 = (object) array(
+ 'hook' => $hook,
+ 'timestamp' => $ts_late,
+ 'schedule' => false,
+ 'args' => $args,
+ );
+
+ $expected2 = (object) array(
+ 'hook' => $hook,
+ 'timestamp' => $ts_next,
+ 'schedule' => false,
+ 'args' => $args,
+ );
+
+ // Schedule late running event.
+ wp_schedule_single_event( $ts_late, $hook, $args );
+ // Schedule next running event.
+ wp_schedule_single_event( $ts_next, $hook, $args );
+
+ // Late running, timestamp specified.
+ $this->assertEquals( $expected1, wp_get_scheduled_event( $hook, $args, $ts_late ) );
+
+ // Next running, timestamp specified.
+ $this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args, $ts_next ) );
+
+ // Next running, no timestamp specified.
+ $this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args ) );
+ }
+
+ /**
+ * Ensure wp_get_scheduled_event() returns the expected recurring events.
+ *
+ * When no timestamp is specified, the next event should be returned.
+ * When a timestamp is specified, a particular event should be returned.
+ *
+ * @ticket 45976.
+ */
+ function test_get_scheduled_event_recurring() {
+ $hook = __FUNCTION__;
+ $args = array( 'arg1' );
+ $ts_late = strtotime( '+30 minutes' );
+ $ts_next = strtotime( '+3 minutes' );
+ $schedule = 'hourly';
+ $interval = HOUR_IN_SECONDS;
+
+ $expected1 = (object) array(
+ 'hook' => $hook,
+ 'timestamp' => $ts_late,
+ 'schedule' => $schedule,
+ 'args' => $args,
+ 'interval' => $interval,
+ );
+
+ $expected2 = (object) array(
+ 'hook' => $hook,
+ 'timestamp' => $ts_next,
+ 'schedule' => $schedule,
+ 'args' => $args,
+ 'interval' => $interval,
+ );
+
+ // Schedule late running event.
+ wp_schedule_event( $ts_late, $schedule, $hook, $args );
+ // Schedule next running event.
+ wp_schedule_event( $ts_next, $schedule, $hook, $args );
+
+ // Late running, timestamp specified.
+ $this->assertEquals( $expected1, wp_get_scheduled_event( $hook, $args, $ts_late ) );
+
+ // Next running, timestamp specified.
+ $this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args, $ts_next ) );
+
+ // Next running, no timestamp specified.
+ $this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args ) );
+ }
+
+ /**
+ * Ensure wp_get_scheduled_event() returns false when expected.
+ *
+ * @ticket 45976.
+ */
+ function test_get_scheduled_event_false() {
+ $hook = __FUNCTION__;
+ $args = array( 'arg1' );
+ $ts = strtotime( '+3 minutes' );
+
+ // No scheduled events.
+ // - With timestamp
+ $this->assertFalse( wp_get_scheduled_event( $hook, $args, $ts ) );
+ // - Get next, none scheduled.
+ $this->assertFalse( wp_get_scheduled_event( $hook, $args ) );
+
+ // Schedule an event.
+ wp_schedule_event( $ts, $hook, $args );
+ // - unregistered timestamp
+ $this->assertFalse( wp_get_scheduled_event( $hook, $args, strtotime( '+30 minutes' ) ) );
+ // - invalid timestamp.
+ $this->assertFalse( wp_get_scheduled_event( $hook, $args, 'Words Fail!' ) );
+
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>