PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.4
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.4
6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 All 35 releases
← All changes | includes/cron/run-scheduled-automations.php +143 -0 5.7.3 → 6.0.4 View file →
@@ -1,0 +1,143 @@
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 + // Bail if tables are not yet created
63 + if( ! automatorwp_database_table_exists( $automations ) ) return;
64 +
65 + $loop_types = automatorwp_get_automation_loop_types();
66 +
67 + $patterns = implode( ', ', array_fill( 0, count( $loop_types ), '%s' ) );
68 +
69 +
70 + // Get scheduled automations
71 + $results = $wpdb->get_results( $wpdb->prepare( "
72 + SELECT a.id
73 + FROM {$automations} AS a
74 + LEFT JOIN {$automations_meta} AS am1 ON ( am1.id = a.id AND am1.meta_key = 'schedule_run' )
75 + LEFT JOIN {$automations_meta} AS am2 ON ( am2.id = a.id AND am2.meta_key = 'recurring_run' )
76 + LEFT JOIN {$automations_meta} AS am3 ON ( am3.id = a.id AND am3.meta_key = 'next_run_date' )
77 + WHERE a.type IN ( {$patterns} )
78 + AND a.status = 'active'
79 + AND ( am1.meta_value = 'on' OR am2.meta_value = 'on' )
80 + AND am3.meta_value <= '{$datetime}'",
81 + $loop_types
82 + ) );
83 +
84 +
85 + if( is_array( $results ) ) {
86 + foreach ( $results as $automation ) {
87 + $schedule_run = (bool) automatorwp_get_automation_meta( $automation->id, 'schedule_run', true );
88 +
89 + $last_log = automatorwp_get_object_last_log( $automation->id, 'automation' );
90 +
91 + $automation_schedule_id = automatorwp_check_scheduled_automations_time( $automation->id, $last_log );
92 +
93 + // Schedule automations
94 + if ( ! empty( $schedule_run ) && ( empty( $last_log ) || ! empty( $automation_schedule_id ) ) ){
95 + // Run automations
96 + automatorwp_run_automation( $automation->id );
97 + }
98 +
99 + // Recurring Automations
100 + if ( empty( $schedule_run ) ) {
101 + // Run automations
102 + automatorwp_run_automation( $automation->id );
103 + }
104 +
105 + }
106 + }
107 +
108 +}
109 +add_action( 'automatorwp_run_scheduled_automations_event', 'automatorwp_run_scheduled_automations' );
110 +
111 +/**
112 + * Process to check scheduled automations time
113 + *
114 + * @since 2.2.2
115 + */
116 +function automatorwp_check_scheduled_automations_time( $automation_id, $last_log ) {
117 +
118 + global $wpdb;
119 +
120 + $automations = AutomatorWP()->db->automations;
121 + $automations_meta = AutomatorWP()->db->automations_meta;
122 +
123 + if ( ! isset( $last_log->date ) )
124 + return false;
125 +
126 + $timestamp = strtotime( $last_log->date );
127 +
128 + // Get scheduled automations
129 + $results = $wpdb->get_results( "
130 + SELECT id
131 + FROM {$automations_meta}
132 + WHERE meta_key = 'schedule_run'
133 + AND meta_value = 'on'
134 + AND id IN (
135 + SELECT id
136 + FROM {$automations_meta}
137 + WHERE meta_key = 'schedule_run_datetime'
138 + AND meta_value > {$timestamp}
139 + ) ");
140 +
141 + return $results;
142 +
143 +}