/var/www/html_uk/wp-content/plugins/automatewoo/includes/Workflows/Factory.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php

namespace AutomateWoo\Workflows;

use 
AutomateWoo\Clean;
use 
AutomateWoo\Cache;
use 
AutomateWoo\Entity\Workflow as WorkflowEntity;
use 
AutomateWoo\Exceptions\InvalidWorkflow;
use 
AutomateWoo\Workflow;
use 
WP_Error;

/**
 * @since 3.9
 */
class Factory {

    
/**
     * Get a Workflow object by its ID.
     *
     * @param int $id The workflow ID.
     *
     * @return Workflow|false The workflow object, or false when the ID is invalid or the object can't be retrieved.
     */
    
public static function get$id ) {
        
$id = (int) $id;
        if ( 
$id <= ) {
            return 
false;
        }

        
$id Clean::id$id );
        if ( ! 
$id ) {
            return 
false;
        }

        
$workflow = new Workflow$id );
        if ( ! 
$workflow->exists ) {
            return 
false;
        }

        return 
$workflow;
    }

    
/**
     * Create a workflow given an Entity.
     *
     * @since 5.1.0
     *
     * @param WorkflowEntity $entity The entity to use for Workflow creation.
     *
     * @return Workflow
     *
     * @throws InvalidWorkflow When the workflow already exists or there is an issue creating the workflow.
     */
    
public static function createWorkflowEntity $entity ) {
        
$workflow self::get$entity->get_id() );
        if ( 
$workflow ) {
            throw 
InvalidWorkflow::workflow_existsesc_html$workflow->get_id() ) );
        }

        return 
self::create_from_array$entity->to_array() );
    }

    
/**
     * Update a workflow given an Entity.
     *
     * @since 6.0.10
     *
     * @param WorkflowEntity $entity The entity to use for Workflow update.
     *
     * @return Workflow
     *
     * @throws InvalidWorkflow When the workflow already exists or there is an issue updating the workflow.
     */
    
public static function updateWorkflowEntity $entity ) {
        return 
self::update_from_array$entity->to_array() );
    }

    
/**
     * Create a workflow from an array of data.
     *
     * @since 5.1.0
     *
     * @param array $data The array of workflow data.
     *
     * @return Workflow
     * @throws InvalidWorkflow When there is an issue creating the workflow.
     */
    
public static function create_from_array$data = [] ) {
        
$data array_replace_recursive(
            [
                
'title'            => '',
                
'status'           => new StatusStatus::DISABLED ),
                
'type'             => 'automatic',
                
'is_transactional' => false,
                
'order'            => 0,
                
'origin'           => WorkflowEntity::ORIGIN_MANUALLY_CREATED,
                
'options'          => [
                    
'when_to_run' => 'immediately',
                ],
                
'trigger'          => [
                    
'name'    => '',
                    
'options' => [],
                ],
                
'rules'            => [],
                
'actions'          => [],
            ],
            
$data
        
);

        
$post_id  self::create_post$data );
        
$workflow self::create_workflow_object_from_data$post_id$data );

        
// Clear cached workflow data.
        
Cache::flush_group'workflows' );

        
do_action'automatewoo/workflow/created'$workflow->get_id() );

        return 
$workflow;
    }

    
/**
     * Update a workflow from an array of data.
     *
     * @since 6.0.10
     *
     * @param array $data The array of workflow data.
     *
     * @return Workflow
     * @throws InvalidWorkflow When there is an issue updating the workflow.
     */
    
public static function update_from_array$data = [] ) {
        
$post_id  self::update_post$data );
        
$workflow self::create_workflow_object_from_data$post_id$data );

        
// Clear cached workflow data.
        
Cache::flush_group'workflows' );

        
do_action'automatewoo/workflow/updated'$workflow->get_id() );

        return 
$workflow;
    }

    
/**
     * Create a post object from an array of workflow data.
     *
     * @param array $data The array of workflow data.
     *
     * @return int The created post ID.
     * @throws InvalidWorkflow When there is a problem creating the post.
     */
    
private static function create_post( array $data ): int {
        
$post_data self::prepare_post_data$data );

        
$post_id wp_insert_post$post_datatrue );
        if ( 
$post_id instanceof WP_Error ) {
            throw 
InvalidWorkflow::error_creating_workflowesc_html$post_id->get_error_message() ) );
        }

        return 
$post_id;
    }

    
/**
     * Update a post object from an array of workflow data.
     *
     * @param array $data The array of workflow data.
     *
     * @return int The updated post ID.
     * @throws InvalidWorkflow When there is a problem updating the post.
     */
    
private static function update_post( array $data ): int {
        
$post_data self::prepare_post_data$data );

        
$post_id wp_update_post$post_datatrue );
        if ( 
$post_id instanceof WP_Error ) {
            throw 
InvalidWorkflow::error_updating_workflowesc_html$post_id->get_error_message() ) );
        }

        return 
$post_id;
    }

    
/**
     * Create an array of post data from an array of workflow data.
     *
     * The 'status' property within the $data array should NOT be the post type equivalent. This
     * method will handle converting to the post type version of the status.
     *
     * @param array $data The array of workflow data.
     *
     * @return array
     */
    
private static function prepare_post_data$data ): array {
        if ( isset( 
$data['status'] ) ) {
            
$data['status'] = $data['status'] instanceof Status
                
$data['status']->get_post_status()
                : ( new 
Status$data['status'] ) )->get_post_status();
        }

        
$post_keys = [
            
'id'     => 'ID',
            
'title'  => 'post_title',
            
'status' => 'post_status',
            
'order'  => 'menu_order',
        ];

        
$post_data = [ 'post_type' => Workflow::POST_TYPE ];
        foreach ( 
array_intersect_key$data$post_keys ) as $key => $value ) {
            
$post_data$post_keys$key ] ] = $value;
        }

        return 
$post_data;
    }

    
/**
     * Create a workflow object from an array of data.
     *
     * @param int   $id   Workflow ID.
     * @param array $data Workflow data.
     *
     * @return Workflow
     */
    
private static function create_workflow_object_from_dataint $id, array $data ): Workflow {
        
$workflow = new Workflow$id );
        
$workflow->set_trigger_data$data['trigger']['name'], $data['trigger']['options'] );
        
$workflow->set_type$data['type'] );

        if ( ! empty( 
$data['rules'] ) ) {
            
$workflow->set_rule_data$data['rules'] );
        }

        if ( ! empty( 
$data['actions'] ) ) {
            
$workflow->set_actions_dataself::maybe_convert_to_legacy_action_data$data['actions'] ) );
        }

        
$workflow->update_meta'workflow_options'$data['options'] );
        
$workflow->update_meta'is_transactional'$data['is_transactional'] );
        
$workflow->update_meta'origin'$data['origin'] );

        return 
$workflow;
    }

    
/**
     * Convert action data structure to legacy data structure.
     *
     * @since 5.1.0
     *
     * @param array $actions
     *
     * @return array
     */
    
private static function maybe_convert_to_legacy_action_data( array $actions ): array {
        
$converted = [];
        foreach ( 
$actions as $action ) {
            if ( isset( 
$action['action_name'] ) ) {
                
$converted[] = $action;
                continue;
            }

            if ( isset( 
$action['name'], $action['options'] ) ) {
                
$converted[] = array_merge(
                    [ 
'action_name' => $action['name'] ],
                    
$action['options']
                );
            }
        }

        return 
$converted;
    }
}