Skip to content

Commit

Permalink
Optimize force_publish_missed_schedules query
Browse files Browse the repository at this point in the history
  • Loading branch information
rbcorrales committed Dec 14, 2024
1 parent 643cec3 commit bab2d13
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
69 changes: 69 additions & 0 deletions __tests__/unit-tests/test-internal-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,73 @@ function test_prune_duplicate_events() {
$this->assertEquals( $duplicate_recurring_1->get_status(), Cron_Control\Events_Store::STATUS_COMPLETED, 'duplicate recurring event 1 was marked as completed' );
$this->assertEquals( $duplicate_recurring_2->get_status(), Cron_Control\Events_Store::STATUS_COMPLETED, 'duplicate recurring event 2 was marked as completed' );
}

function test_force_publish_missed_schedules() {
// Define the filter callback to override post status.
$future_insert_filter = function ( $data ) {
if ( 'publish' === $data['post_status'] ) {
$data['post_status'] = 'future'; // Ensure it remains future even if the date is in the past.
}
return $data;
};

// Add the filter to ensure 'future' posts with past dates are not auto-published.
add_filter( 'wp_insert_post_data', $future_insert_filter );

// Create two posts with a 'future' status.
$this->factory()->post->create(
array(
'post_title' => 'Future post that should be published',
'post_status' => 'future',
'post_type' => 'post',
'post_date' => gmdate( 'Y-m-d H:i:s', time() - 1000 ),
)
);

$this->factory()->post->create(
array(
'post_title' => 'Future post that should not be published',
'post_status' => 'future',
'post_type' => 'post',
'post_date' => gmdate( 'Y-m-d H:i:s', time() + 1000 ),
)
);

// Remove the filter after creating the test posts.
remove_filter( 'wp_insert_post_data', $future_insert_filter );

// Count posts with 'future' status before running the method.
$future_posts_before = get_posts(
array(
'post_status' => 'future',
'numberposts' => -1,
)
);

$this->assertCount( 2, $future_posts_before, 'Two posts should be scheduled initially.' );

// Run the function to publish missed schedules.
Cron_Control\Internal_Events::instance()->force_publish_missed_schedules();

// Query posts again after running the function.
$future_posts_after = get_posts(
array(
'post_status' => 'future',
'post_type' => 'post',
'numberposts' => -1,
)
);

$published_posts = get_posts(
array(
'post_status' => 'publish',
'post_type' => 'post',
'numberposts' => -1,
)
);

// Assert counts after the function runs.
$this->assertCount( 1, $future_posts_after, 'One post should still be scheduled.' );
$this->assertCount( 1, $published_posts, 'One post should be published.' );
}
}
2 changes: 1 addition & 1 deletion includes/class-internal-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function is_internal_event( $action ): bool {
public function force_publish_missed_schedules() {
global $wpdb;

$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 ) ) );
$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 ) ) );

foreach ( $missed_posts as $missed_post ) {
$missed_post = absint( $missed_post );
Expand Down

0 comments on commit bab2d13

Please sign in to comment.