/var/www/html_uk/wp-content/plugins/automatewoo/includes/Workflows.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

namespace AutomateWoo;

use 
AutomateWoo\Workflows\Factory;

/**
 * Workflow management class
 *
 * @class Workflows
 */
class Workflows {

    
/**
     * Add hooks
     */
    
public static function init() {
        
$self 'AutomateWoo\Workflows'/** @var $self Workflows (for IDE) */

        
add_action'save_post', [ $self'do_workflow_updated_action' ], 20 );
        
add_action'save_post', [ $self'do_workflow_created_action' ], 20 );
        
add_action'delete_post', [ $self'do_workflow_deleted_action' ], 20 );
        
add_action'delete_post', [ $self'maybe_cleanup_workflow_data' ] );

        
// update cron events after workflow is updated
        
add_action'automatewoo/workflow/updated', [ $self'maybe_schedule_custom_time_of_day_event' ] );

        
// schedule events at the start of each day
        
add_action'automatewoo_midnight', [ $self'schedule_all_custom_time_of_day_events' ] );

        
// reschedule after gmt offset change
        
add_action'automatewoo/gmt_offset_changed', [ $self'schedule_all_custom_time_of_day_events' ], 20 );
    }

    
/**
     * Get workflow types.
     *
     * @since 5.0.0
     * @return array
     */
    
public static function get_types() {
        return 
apply_filters(
            
'automatewoo/workflow/types',
            [
                
'automatic' => __'Automatic''automatewoo' ),
                
'manual'    => __'Manual''automatewoo' ),
            ]
        );
    }

    
/**
     * @param int $post_id
     */
    
public static function do_workflow_updated_action$post_id ) {
        if ( 
get_post_type$post_id ) === 'aw_workflow' && get_post_status$post_id ) !== 'auto-draft' ) {
            
do_action'automatewoo/workflow/updated', (int) $post_id );
        }
    }

    
/**
     * Trigger the workflow created action.
     *
     * @since 4.9.0
     *
     * @param int $post_id The post ID for the Workflow.
     */
    
public static function do_workflow_created_action$post_id ) {
        
// phpcs:disable WordPress.Security.NonceVerification.Missing
        
if (
            
get_post_type$post_id ) === 'aw_workflow' &&
            isset( 
$_POST['original_post_status'] ) &&
            
'auto-draft' === $_POST['original_post_status']
        ) {
            
do_action'automatewoo/workflow/created', (int) $post_id );
        }
        
// phpcs:enable
    
}

    
/**
     * @param int $post_id
     */
    
public static function do_workflow_deleted_action$post_id ) {
        if ( 
get_post_type$post_id ) === 'aw_workflow' ) {
            
do_action'automatewoo/workflow/deleted', (int) $post_id );
        }
    }

    
/**
     * @param int $post_id
     */
    
public static function maybe_cleanup_workflow_data$post_id ) {
        if ( 
get_post_type$post_id ) === 'aw_workflow' ) {
            
self::delete_related_data$post_id );
        }
    }

    
/**
     * Delete logs, unsubscribes, queue related to a workflow
     *
     * @param int $workflow_id
     */
    
public static function delete_related_data$workflow_id ) {
        
$logs_query  = ( new Log_Query() )->where_workflow$workflow_id );
        
$queue_query = ( new Queue_Query() )->where_workflow$workflow_id );

        
$data array_merge$logs_query->get_results(), $queue_query->get_results() );

        foreach ( 
$data as $item ) {
            
/** @var Model $item */
            
$item->delete();
        }
    }

    
/**
     * Updates custom time of day cron hook if needed.
     *
     * @since 3.8
     * @param int $workflow_id
     */
    
public static function maybe_schedule_custom_time_of_day_event$workflow_id ) {
        
$workflow Factory::get$workflow_id );

        if ( ! 
$workflow || ! $workflow->is_active() ) {
            return;
        }

        
$trigger $workflow->get_trigger();

        if ( ! 
$trigger || ! $trigger::SUPPORTS_CUSTOM_TIME_OF_DAY ) {
            return;
        }

        
// update the cron event but delete the event if the time was set in the past
        // midnight cron worker will add the events again tomorrow
        
self::schedule_custom_time_of_day_event$workflowtrue );
    }

    
/**
     * @since 3.8
     *
     * @param Workflow $workflow
     * @param bool     $clear_if_time_has_passed_for_today
     */
    
public static function schedule_custom_time_of_day_event$workflow$clear_if_time_has_passed_for_today false ) {
        
$hook 'automatewoo/custom_time_of_day_workflow';

        
$time array_map'absint', (array) $workflow->get_trigger_option'time' ) );

        
// calculate time of day in site's timezone
        
$datetime = new DateTime'now' );
        
$datetime->convert_to_site_time();
        
$datetime->setTime( isset( $time[0] ) ? $time[0] : 0, isset( $time[1] ) ? $time[1] : 0);
        
$datetime->convert_to_utc_time();

        
$passed_for_today $datetime->getTimestamp() < time();

        
AW()->action_scheduler()->cancel$hook, [ $workflow->get_id() ] );

        if ( 
$passed_for_today && $clear_if_time_has_passed_for_today ) {
            return;
        }

        
AW()->action_scheduler()->schedule_single$datetime->getTimestamp(), $hook, [ $workflow->get_id() ] );
    }

    
/**
     * Resets all cron events for custom time of day workflows.
     *
     * @since 3.8
     */
    
public static function schedule_all_custom_time_of_day_events() {
        
$query = new Workflow_Query();
        
$query->set_triggerTriggers::get_custom_time_of_day_triggers() );
        
$workflows $query->get_results();
        foreach ( 
$workflows as $workflow ) {
            
self::schedule_custom_time_of_day_event$workflow );
        }
    }

    
/**
     * Get number of manual workflows.
     *
     * @since 5.0.0
     *
     * @return int
     */
    
public static function get_manual_workflows_count() {
        global 
$wpdb;

        
$count Cache::get'manual_workflows_count''workflows' );
        if ( 
false === $count ) {
            
$count $wpdb->get_var(
                
"SELECT COUNT(post.ID) FROM {$wpdb->posts} as post
                LEFT JOIN 
{$wpdb->postmeta} AS meta ON post.ID = meta.post_id
                WHERE post_type = 'aw_workflow'
                AND post_status != 'trash'
                AND meta.meta_key = 'type'
                AND meta.meta_value = 'manual'"
            
);

            
Cache::set'manual_workflows_count', (int) $count'workflows' );
        }

        return (int) 
$count;
    }
}