-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduled-posts-issue-fixer.php
68 lines (56 loc) · 2.02 KB
/
scheduled-posts-issue-fixer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Plugin Name: Scheduled Posts Issue Fixer
* Plugin URI: https://github.com/optimisthub/scheduled-posts-issue-fixer
* Description: This plugin does one thing and does it well: it fixes the missed schedule error and triggers your scheduled posts to publish on time. We’ve developed this post scheduler plugin with performance in mind, so it won’t affect the speed or performance of your website.
* Author: optimisthub
* Author URI: https://optimisthub.com
* Text Domain: scheduled-posts-issue-fixer
* Version: 1.0.10
* Requires at least: 5.0
* Tested up to: 6.1.1
* Requires PHP: 7.1
* License: GPLv2
*/
class ScheduledPostsIssueFixer
{
const CRON_NAME = 'scheduled_posts_issue_fixed';
const CRON_TIME = 'every_minute';
const SQL_ROW_LIMIT = 20;
public function __construct()
{
register_activation_hook(__FILE__,[$this, 'registerCron']);
register_deactivation_hook(__FILE__,[$this, 'deRegisterCron']);
add_action(self::CRON_NAME, [$this, 'publishPosts']);
}
public function registerCron()
{
if (! wp_next_scheduled(self::CRON_NAME ))
{
wp_schedule_event(time(), self::CRON_TIME, self::CRON_NAME);
}
}
public function deRegisterCron()
{
wp_clear_scheduled_hook(CRON_NAME);
}
public function publishPosts()
{
global $wpdb;
$gmt_offset = (int) get_option( 'gmt_offset', 0 );
$gmt_offset = apply_filters( 'scheduled_posts_issue_fixer_gmt_offset', $gmt_offset );
$currentTime = current_time( 'mysql', (int) $gmt_offset );
$postIds = $wpdb->get_col(
$wpdb->prepare
(
"SELECT ID FROM {$wpdb->posts} WHERE post_date <= %s AND post_status='future' LIMIT %d", $currentTime, self::SQL_ROW_LIMIT
)
);
if ( ! count( $postIds ) )
{
return;
}
array_map( 'wp_publish_post', $postIds );
}
}
new ScheduledPostsIssueFixer();