| 1 |
<?php |
| 2 |
/** |
| 3 |
* Run Scheduled Automations |
| 4 |
* |
| 5 |
* @package AutomatorWP\Cron\Run_Scheduled_Automations |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Register schedule events |
| 14 |
* |
| 15 |
* @since 2.2.2 |
| 16 |
*/ |
| 17 |
function automatorwp_run_scheduled_automations_schedule_events() { |
| 18 |
|
| 19 |
if ( function_exists( 'as_schedule_recurring_action' ) ) { |
| 20 |
|
| 21 |
// Action scheduler support |
| 22 |
if ( ! as_next_scheduled_action( 'automatorwp_run_scheduled_automations_event' ) ) { |
| 23 |
as_schedule_recurring_action( time(), 60 * 5, 'automatorwp_run_scheduled_automations_event' ); |
| 24 |
} |
| 25 |
|
| 26 |
} else { |
| 27 |
|
| 28 |
// WP Cron |
| 29 |
if ( ! wp_next_scheduled( 'automatorwp_run_scheduled_automations_event' ) ) { |
| 30 |
wp_schedule_event( time(), 'five_minutes', 'automatorwp_run_scheduled_automations_event' ); |
| 31 |
} |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
} |
| 36 |
add_action( 'automatorwp_schedule_events', 'automatorwp_run_scheduled_automations_schedule_events' ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Clear scheduled events |
| 40 |
* |
| 41 |
* @since 2.2.2 |
| 42 |
*/ |
| 43 |
function automatorwp_run_scheduled_automations_clear_scheduled_events() { |
| 44 |
wp_clear_scheduled_hook( 'automatorwp_run_scheduled_automations_event' ); |
| 45 |
} |
| 46 |
add_action( 'automatorwp_clear_scheduled_events', 'automatorwp_run_scheduled_automations_clear_scheduled_events' ); |
| 47 |
|
| 48 |
/** |
| 49 |
* Process the run scheduled automations process |
| 50 |
* |
| 51 |
* @since 2.2.2 |
| 52 |
*/ |
| 53 |
function automatorwp_run_scheduled_automations() { |
| 54 |
|
| 55 |
global $wpdb; |
| 56 |
|
| 57 |
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ); |
| 58 |
|
| 59 |
$automations = AutomatorWP()->db->automations; |
| 60 |
$automations_meta = AutomatorWP()->db->automations_meta; |
| 61 |
|
| 62 |
$loop_types = automatorwp_get_automation_loop_types(); |
| 63 |
|
| 64 |
$patterns = implode( ', ', array_fill( 0, count( $loop_types ), '%s' ) ); |
| 65 |
|
| 66 |
// Get scheduled automations |
| 67 |
$results = $wpdb->get_results( $wpdb->prepare( " |
| 68 |
SELECT a.id |
| 69 |
FROM {$automations} AS a |
| 70 |
LEFT JOIN {$automations_meta} AS am1 ON ( am1.id = a.id AND am1.meta_key = 'schedule_run' ) |
| 71 |
LEFT JOIN {$automations_meta} AS am2 ON ( am2.id = a.id AND am2.meta_key = 'recurring_run' ) |
| 72 |
LEFT JOIN {$automations_meta} AS am3 ON ( am3.id = a.id AND am3.meta_key = 'next_run_date' ) |
| 73 |
WHERE a.type IN ( {$patterns} ) |
| 74 |
AND a.status = 'active' |
| 75 |
AND ( am1.meta_value = 'on' OR am2.meta_value = 'on' ) |
| 76 |
AND am3.meta_value <= '{$datetime}'", |
| 77 |
$loop_types |
| 78 |
) ); |
| 79 |
|
| 80 |
|
| 81 |
if( is_array( $results ) ) { |
| 82 |
foreach ( $results as $automation ) { |
| 83 |
$schedule_run = (bool) automatorwp_get_automation_meta( $automation->id, 'schedule_run', true ); |
| 84 |
|
| 85 |
$last_log = automatorwp_get_object_last_log( $automation->id, 'automation' ); |
| 86 |
|
| 87 |
$automation_schedule_id = automatorwp_check_scheduled_automations_time( $automation->id, $last_log ); |
| 88 |
|
| 89 |
// Schedule automations |
| 90 |
if ( ! empty( $schedule_run ) && ( empty( $last_log ) || ! empty( $automation_schedule_id ) ) ){ |
| 91 |
// Run automations |
| 92 |
automatorwp_run_automation( $automation->id ); |
| 93 |
} |
| 94 |
|
| 95 |
// Recurring Automations |
| 96 |
if ( empty( $schedule_run ) ) { |
| 97 |
// Run automations |
| 98 |
automatorwp_run_automation( $automation->id ); |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
add_action( 'automatorwp_run_scheduled_automations_event', 'automatorwp_run_scheduled_automations' ); |
| 106 |
|
| 107 |
/** |
| 108 |
* Process to check scheduled automations time |
| 109 |
* |
| 110 |
* @since 2.2.2 |
| 111 |
*/ |
| 112 |
function automatorwp_check_scheduled_automations_time( $automation_id, $last_log ) { |
| 113 |
|
| 114 |
global $wpdb; |
| 115 |
|
| 116 |
$automations = AutomatorWP()->db->automations; |
| 117 |
$automations_meta = AutomatorWP()->db->automations_meta; |
| 118 |
|
| 119 |
if ( ! isset( $last_log->date ) ) |
| 120 |
return false; |
| 121 |
|
| 122 |
$timestamp = strtotime( $last_log->date ); |
| 123 |
|
| 124 |
// Get scheduled automations |
| 125 |
$results = $wpdb->get_results( " |
| 126 |
SELECT id |
| 127 |
FROM {$automations_meta} |
| 128 |
WHERE meta_key = 'schedule_run' |
| 129 |
AND meta_value = 'on' |
| 130 |
AND id IN ( |
| 131 |
SELECT id |
| 132 |
FROM {$automations_meta} |
| 133 |
WHERE meta_key = 'schedule_run_datetime' |
| 134 |
AND meta_value > {$timestamp} |
| 135 |
) "); |
| 136 |
|
| 137 |
return $results; |
| 138 |
|
| 139 |
} |
| 140 |
|