Commit 19283be4 authored by Erick Hitter's avatar Erick Hitter

Fix internal consistency in what we call these things

parent 12c12329
......@@ -20,7 +20,7 @@ class Internal_Events extends Singleton {
*
* @var array
*/
private $internal_jobs = array();
private $internal_events = array();
/**
* Schedules for internal events
......@@ -29,7 +29,7 @@ class Internal_Events extends Singleton {
*
* @var array
*/
private $internal_jobs_schedules = array();
private $internal_events_schedules = array();
/**
* Register hooks
......@@ -44,8 +44,8 @@ class Internal_Events extends Singleton {
add_action( 'rest_api_init', array( $this, 'schedule_internal_events' ) );
add_filter( 'cron_schedules', array( $this, 'register_internal_events_schedules' ) );
foreach ( $this->internal_jobs as $internal_job ) {
add_action( $internal_job['action'], $internal_job['callback'] );
foreach ( $this->internal_events as $internal_event ) {
add_action( $internal_event['action'], $internal_event['callback'] );
}
}
......@@ -53,7 +53,7 @@ class Internal_Events extends Singleton {
* Populate internal events, allowing for additions
*/
private function prepare_internal_events() {
$internal_jobs = array(
$internal_events = array(
array(
'schedule' => 'a8c_cron_control_minute',
'action' => 'a8c_cron_control_force_publish_missed_schedules',
......@@ -78,7 +78,7 @@ class Internal_Events extends Singleton {
// Allow additional internal events to be specified, ensuring the above cannot be overwritten.
if ( defined( 'CRON_CONTROL_ADDITIONAL_INTERNAL_EVENTS' ) && is_array( \CRON_CONTROL_ADDITIONAL_INTERNAL_EVENTS ) ) {
$internal_actions = wp_list_pluck( $internal_jobs, 'action' );
$internal_actions = wp_list_pluck( $internal_events, 'action' );
foreach ( \CRON_CONTROL_ADDITIONAL_INTERNAL_EVENTS as $additional ) {
if ( in_array( $additional['action'], $internal_actions, true ) ) {
......@@ -89,18 +89,18 @@ class Internal_Events extends Singleton {
continue;
}
$internal_jobs[] = $additional;
$internal_events[] = $additional;
}
}
$this->internal_jobs = $internal_jobs;
$this->internal_events = $internal_events;
}
/**
* Allow custom internal events to provide their own schedules
*/
private function prepare_internal_events_schedules() {
$internal_jobs_schedules = array(
$internal_events_schedules = array(
'a8c_cron_control_minute' => array(
'interval' => 1 * MINUTE_IN_SECONDS,
'display' => __( 'Cron Control internal job - every minute', 'automattic-cron-control' ),
......@@ -114,7 +114,7 @@ class Internal_Events extends Singleton {
// Allow additional schedules for custom events, ensuring the above cannot be overwritten.
if ( defined( 'CRON_CONTROL_ADDITIONAL_INTERNAL_EVENTS_SCHEDULES' ) && is_array( \CRON_CONTROL_ADDITIONAL_INTERNAL_EVENTS_SCHEDULES ) ) {
foreach ( \CRON_CONTROL_ADDITIONAL_INTERNAL_EVENTS_SCHEDULES as $name => $attrs ) {
if ( array_key_exists( $name, $internal_jobs_schedules ) ) {
if ( array_key_exists( $name, $internal_events_schedules ) ) {
continue;
}
......@@ -122,42 +122,42 @@ class Internal_Events extends Singleton {
continue;
}
$internal_jobs_schedules[ $name ] = $attrs;
$internal_events_schedules[ $name ] = $attrs;
}
}
$this->internal_jobs_schedules = $internal_jobs_schedules;
$this->internal_events_schedules = $internal_events_schedules;
}
/**
* Include custom schedules used for internal jobs
* Include custom schedules used for internal events
*
* @param array $schedules List of registered event intervals.
* @return array
*/
public function register_internal_events_schedules( $schedules ) {
return array_merge( $schedules, $this->internal_jobs_schedules );
return array_merge( $schedules, $this->internal_events_schedules );
}
/**
* Schedule internal jobs
* Schedule internal events
*/
public function schedule_internal_events() {
$when = strtotime( sprintf( '+%d seconds', JOB_QUEUE_WINDOW_IN_SECONDS ) );
$schedules = wp_get_schedules();
foreach ( $this->internal_jobs as $job_args ) {
if ( ! wp_next_scheduled( $job_args['action'] ) ) {
$interval = array_key_exists( $job_args['schedule'], $schedules ) ? $schedules[ $job_args['schedule'] ]['interval'] : 0;
foreach ( $this->internal_events as $event_args ) {
if ( ! wp_next_scheduled( $event_args['action'] ) ) {
$interval = array_key_exists( $event_args['schedule'], $schedules ) ? $schedules[ $event_args['schedule'] ]['interval'] : 0;
$args = array(
'schedule' => $job_args['schedule'],
'schedule' => $event_args['schedule'],
'args' => array(),
'interval' => $interval,
);
schedule_event( $when, $job_args['action'], $args );
schedule_event( $when, $event_args['action'], $args );
}
}
}
......@@ -173,7 +173,7 @@ class Internal_Events extends Singleton {
* @return bool
*/
public function is_internal_event( $action ) {
return in_array( $action, wp_list_pluck( $this->internal_jobs, 'action' ), true );
return in_array( $action, wp_list_pluck( $this->internal_events, 'action' ), true );
}
/**
......@@ -256,32 +256,32 @@ class Internal_Events extends Singleton {
// Confirm internal events are scheduled for when they're expected.
$schedules = wp_get_schedules();
foreach ( $this->internal_jobs as $internal_job ) {
$timestamp = wp_next_scheduled( $internal_job['action'] );
foreach ( $this->internal_events as $internal_event ) {
$timestamp = wp_next_scheduled( $internal_event['action'] );
// Will reschedule on its own.
if ( false === $timestamp ) {
continue;
}
$job_details = get_event_by_attributes( array(
$event_details = get_event_by_attributes( array(
'timestamp' => $timestamp,
'action' => $internal_job['action'],
'action' => $internal_event['action'],
'instance' => md5( maybe_serialize( array() ) ),
) );
if ( $job_details->schedule !== $internal_job['schedule'] ) {
if ( $event_details->schedule !== $internal_event['schedule'] ) {
if ( $timestamp <= time() ) {
$timestamp = time() + ( 1 * \MINUTE_IN_SECONDS );
}
$args = array(
'schedule' => $internal_job['schedule'],
'args' => $job_details->args,
'interval' => $schedules[ $internal_job['schedule'] ]['interval'],
'schedule' => $internal_event['schedule'],
'args' => $event_details->args,
'interval' => $schedules[ $internal_event['schedule'] ]['interval'],
);
schedule_event( $timestamp, $job_details->action, $args, $job_details->ID );
schedule_event( $timestamp, $event_details->action, $args, $event_details->ID );
}
}
}
......
Markdown is supported
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