Created
February 5, 2023 06:39
-
-
Save mircobabini/5d58f963bf983f62cd58af8fbbcde86e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: SED Web - Scheduled Post Trigger | |
* Description: Publishes scheduled posts that were missed by cron at defined interval | |
* Plugin URI: | |
* Version: 1.4.0 | |
* Requires at least: 4.9 | |
* Requires PHP: 5.6 | |
* License: GPLv2 or later | |
* Author: SED Web | |
* Author URI: https://sedweb.com/ | |
* Forked from 1: https://wordpress.org/plugins/scheduled-post-trigger/ | |
* Forked from 2: https://gist.github.com/dmhendricks/b752f93470a3e7f186f8428665d79e19/ | |
*/ | |
namespace SEDWeb; | |
defined( 'ABSPATH' ) || die; | |
Scheduled_Post_Trigger::init(); | |
final class Scheduled_Post_Trigger { | |
protected static $instance; | |
protected static $interval = 5 * MINUTE_IN_SECONDS; // seconds | |
public static function init() { | |
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Scheduled_Post_Trigger ) ) { | |
self::$instance = new Scheduled_Post_Trigger; | |
// Set check interval (seconds) | |
if ( defined( 'TSP_CHECK_INTERVAL' ) && \TSP_CHECK_INTERVAL ) { | |
self::$interval = intval( \TSP_CHECK_INTERVAL ); | |
} | |
add_action( 'wp', array( self::$instance, 'on_front_page_view' ) ); | |
/** CRON JOB **/ | |
// add a schedule timer | |
add_filter( 'cron_schedules', array( self::$instance, 'add_extra_schedules' ) ); | |
// [03-Feb-2023 11:37:12 UTC] Errore nell'evento | |
// Cron di riprogrammazione per l'hook: tsp_publish_missed_posts, codice di | |
// errore: invalid_schedule, messaggio di errore: L'evento programmato non esiste., dati: {"schedule":"tsp_interval","args":[],"interval":300} | |
// create a scheduled events every 5 minutes | |
if ( ! wp_next_scheduled( 'tsp_publish_missed_posts' ) ) { | |
wp_schedule_event( time(), 'tsp_interval', 'tsp_publish_missed_posts' ); | |
} | |
// add the action for the scheduled event | |
add_action( 'tsp_publish_missed_posts', array( self::$instance, 'publish_missed_posts' ) ); | |
} | |
return self::$instance; | |
} | |
public static function add_extra_schedules( $schedules ) { | |
$schedules['tsp_interval'] = array( | |
'interval' => self::$interval, | |
'display' => "Every " . self::$interval . " seconds", | |
); | |
return $schedules; | |
} | |
/** | |
* Trigger on non-cached front page view | |
* | |
* @return void | |
*/ | |
public static function on_front_page_view() { | |
// For better performance: Limit to front page and singles for better performance | |
if ( ! ( is_front_page() || is_single() ) ) { | |
return; | |
} | |
self::publish_missed_posts(); | |
} | |
/** | |
* Publish missed scheduled posts | |
* | |
* @since 1.0.0 | |
*/ | |
public static function publish_missed_posts() { | |
$already_checked = false; | |
// Check cache key to see if it's time to check again | |
$cache_key = 'trigger-scheduled-posts-interval-' . ( defined( 'MULTISITE' ) && MULTISITE ? get_current_blog_id() : 'check' ); | |
wp_cache_get( $cache_key, null, false, $already_checked ); | |
if ( $already_checked ) { | |
return; | |
} | |
// Get schedule posts that were missed | |
global $wpdb; | |
$missed_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'future' AND post_date_gmt < UTC_TIMESTAMP()" ); | |
// If there are none, simply return | |
if ( ! empty( $missed_posts ) ) { | |
// Loop through each post ID and publish | |
foreach ( $missed_posts as $post_id ) { | |
wp_publish_post( $post_id ); | |
} | |
if ( function_exists( 'opcache_reset' ) && ini_get( 'opcache.enable' ) ) { | |
opcache_reset(); | |
} | |
if ( function_exists( 'wp_cache_flush' ) ) { | |
wp_cache_flush(); | |
} | |
} | |
// Add cache key to signify that we've already since interval | |
wp_cache_set( $cache_key, true, null, self::$interval ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment