Verified Commit 77f67c32 authored by Erick Hitter's avatar Erick Hitter
Browse files

Add locking to event creation

When `wp_next_scheduled()` is called incorrectly, it can lead to an endless loop of event creation. Locking will, at least, slow it down.
parent 3aef3144
......@@ -10,6 +10,8 @@ class Cron_Options_CPT extends Singleton {
/**
* Class properties
*/
const LOCK = 'create-jobs';
private $post_type = 'wpccr_events';
private $post_status = 'inherit';
......@@ -24,6 +26,9 @@ class Cron_Options_CPT extends Singleton {
// Data storage
add_action( 'init', array( $this, 'register_post_type' ) );
// Lock for post insertion, to guard against endless event creation when `wp_next_scheduled()` is misused
Lock::prime_lock( self::LOCK );
// Option interception
add_filter( 'pre_option_cron', array( $this, 'get_option' ) );
add_filter( 'pre_update_option_cron', array( $this, 'update_option' ), 10, 2 );
......@@ -221,6 +226,11 @@ class Cron_Options_CPT extends Singleton {
* Create a job post, respecting whether or not Core is ready for CPTs
*/
private function create_job( $job_post ) {
// Limit how many events to insert at once
if ( ! Lock::check_lock( self::LOCK, 5 ) ) {
return false;
}
// If called before `init`, we need to insert directly because post types aren't registered earlier
if ( did_action( 'init' ) ) {
wp_insert_post( $job_post );
......@@ -261,6 +271,9 @@ class Cron_Options_CPT extends Singleton {
$this->posts_to_clean[] = $wpdb->insert_id;
}
}
// Allow more events to be created
Lock::free_lock( self::LOCK );
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment