Skip to content

Commit bab2d13

Browse files
committed
Optimize force_publish_missed_schedules query
1 parent 643cec3 commit bab2d13

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

__tests__/unit-tests/test-internal-events.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,73 @@ function test_prune_duplicate_events() {
9292
$this->assertEquals( $duplicate_recurring_1->get_status(), Cron_Control\Events_Store::STATUS_COMPLETED, 'duplicate recurring event 1 was marked as completed' );
9393
$this->assertEquals( $duplicate_recurring_2->get_status(), Cron_Control\Events_Store::STATUS_COMPLETED, 'duplicate recurring event 2 was marked as completed' );
9494
}
95+
96+
function test_force_publish_missed_schedules() {
97+
// Define the filter callback to override post status.
98+
$future_insert_filter = function ( $data ) {
99+
if ( 'publish' === $data['post_status'] ) {
100+
$data['post_status'] = 'future'; // Ensure it remains future even if the date is in the past.
101+
}
102+
return $data;
103+
};
104+
105+
// Add the filter to ensure 'future' posts with past dates are not auto-published.
106+
add_filter( 'wp_insert_post_data', $future_insert_filter );
107+
108+
// Create two posts with a 'future' status.
109+
$this->factory()->post->create(
110+
array(
111+
'post_title' => 'Future post that should be published',
112+
'post_status' => 'future',
113+
'post_type' => 'post',
114+
'post_date' => gmdate( 'Y-m-d H:i:s', time() - 1000 ),
115+
)
116+
);
117+
118+
$this->factory()->post->create(
119+
array(
120+
'post_title' => 'Future post that should not be published',
121+
'post_status' => 'future',
122+
'post_type' => 'post',
123+
'post_date' => gmdate( 'Y-m-d H:i:s', time() + 1000 ),
124+
)
125+
);
126+
127+
// Remove the filter after creating the test posts.
128+
remove_filter( 'wp_insert_post_data', $future_insert_filter );
129+
130+
// Count posts with 'future' status before running the method.
131+
$future_posts_before = get_posts(
132+
array(
133+
'post_status' => 'future',
134+
'numberposts' => -1,
135+
)
136+
);
137+
138+
$this->assertCount( 2, $future_posts_before, 'Two posts should be scheduled initially.' );
139+
140+
// Run the function to publish missed schedules.
141+
Cron_Control\Internal_Events::instance()->force_publish_missed_schedules();
142+
143+
// Query posts again after running the function.
144+
$future_posts_after = get_posts(
145+
array(
146+
'post_status' => 'future',
147+
'post_type' => 'post',
148+
'numberposts' => -1,
149+
)
150+
);
151+
152+
$published_posts = get_posts(
153+
array(
154+
'post_status' => 'publish',
155+
'post_type' => 'post',
156+
'numberposts' => -1,
157+
)
158+
);
159+
160+
// Assert counts after the function runs.
161+
$this->assertCount( 1, $future_posts_after, 'One post should still be scheduled.' );
162+
$this->assertCount( 1, $published_posts, 'One post should be published.' );
163+
}
95164
}

includes/class-internal-events.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function is_internal_event( $action ): bool {
146146
public function force_publish_missed_schedules() {
147147
global $wpdb;
148148

149-
$missed_posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_status = 'future' AND post_date <= %s LIMIT 0,100;", current_time( 'mysql', false ) ) );
149+
$missed_posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} p JOIN (SELECT DISTINCT post_type FROM {$wpdb->posts}) AS t ON p.post_type = t.post_type WHERE post_status = 'future' AND post_date <= %s LIMIT 0,100;", current_time( 'mysql', false ) ) );
150150

151151
foreach ( $missed_posts as $missed_post ) {
152152
$missed_post = absint( $missed_post );

0 commit comments

Comments
 (0)