/var/www/html_uk/wp-content/plugins/automatewoo/includes/Rest_Api/Controllers/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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php

namespace AutomateWoo\Rest_Api\Controllers;

use 
AutomateWoo\Format;
use 
AutomateWoo\Permissions;
use 
AutomateWoo\Rest_Api\Schema\WorkflowSchema;
use 
AutomateWoo\Rest_Api\Utilities\CreateUpdateWorkflow;
use 
AutomateWoo\Rest_Api\Utilities\DeleteWorkflow;
use 
AutomateWoo\Rest_Api\Utilities\GetWorkflow;
use 
AutomateWoo\Rest_Api\Utilities\Pagination;
use 
AutomateWoo\Rest_Api\Utilities\RestException;
use 
AutomateWoo\Workflow;
use 
AutomateWoo\Workflows as WorkflowsHelper;
use 
AutomateWoo\Workflow_Query;
use 
WP_Error;
use 
WP_REST_Request;
use 
WP_REST_Response;
use 
WP_REST_Server;

defined'ABSPATH' ) || exit;

/**
 * Workflows Rest API controller.
 *
 * @since 4.9.0
 */
class Workflows extends AbstractController {

    use 
WorkflowSchema;
    use 
CreateUpdateWorkflow;
    use 
DeleteWorkflow;
    use 
GetWorkflow;

    
/**
     * Route base.
     *
     * @var string
     */
    
protected $rest_base 'workflows';

    
/**
     * Register the routes.
     */
    
public function register_routes() {
        
register_rest_route(
            
$this->namespace,
            
"/{$this->rest_base}",
            [
                [
                    
'methods'             => WP_REST_Server::READABLE,
                    
'callback'            => [ $this'get_items' ],
                    
'permission_callback' => [ Permissions::class, 'can_manage' ],
                    
'args'                => $this->get_collection_params(),
                ],
                [
                    
'methods'             => WP_REST_Server::CREATABLE,
                    
'callback'            => [ $this'create_item' ],
                    
'permission_callback' => [ Permissions::class, 'can_manage' ],
                    
'args'                => $this->get_properties_schema(),
                ],
                
'schema' => [ $this'get_public_item_schema' ],
            ]
        );

        
register_rest_route(
            
$this->namespace,
            
"/{$this->rest_base}/(?P<id>[\d]+)",
            [
                
'args'   => [
                    
'id' => [
                        
'description' => __'Unique identifier for the workflow.''automatewoo' ),
                        
'type'        => 'integer',
                    ],
                ],
                [
                    
'methods'             => WP_REST_Server::READABLE,
                    
'callback'            => [ $this'get_item' ],
                    
'permission_callback' => [ Permissions::class, 'can_manage' ],
                ],
                [
                    
'methods'             => WP_REST_Server::EDITABLE,
                    
'callback'            => [ $this'update_item' ],
                    
'permission_callback' => [ Permissions::class, 'can_manage' ],
                    
'args'                => $this->get_update_parameters_schema(),
                ],
                [
                    
'methods'             => WP_REST_Server::DELETABLE,
                    
'callback'            => [ $this'delete_item' ],
                    
'permission_callback' => [ Permissions::class, 'can_manage' ],
                    
'args'                => $this->get_delete_parameters_schema(),
                ],
                
'schema' => [ $this'get_public_item_schema' ],
            ]
        );
    }

    
/**
     * Retrieves a collection workflows.
     *
     * @param WP_REST_Request $request
     *
     * @return WP_Error|WP_REST_Response
     */
    
public function get_items$request ) {
        
$query = new Workflow_Query();
        
$query->set_no_found_rowsfalse );
        
$query->set_limit$request->get_param'per_page' ) );
        
$query->set_page$request->get_param'page' ) );
        
$query->set_status$request->get_param'status' ) ?: 'any' );
        
$query->set_trigger$request->get_param'trigger' ) );

        if ( 
$request->has_param'type' ) ) {
            
$query->set_type$request->get_param'type' ) );
        }

        if ( 
$request->has_param'search' ) ) {
            
$query->set_search$request->get_param'search' ) );
        }
        if ( 
$request->has_param'include' ) ) {
            
$query->set_include$request->get_param'include' ) );
        }

        
$items $query->get_results();
        
$data  = [];

        foreach ( 
$items as $item ) {
            
$item_data $this->prepare_item_for_response$item$request );
            
$data[]    = $this->prepare_response_for_collection$item_data );
        }

        
$response = new WP_REST_Response$data );
        
$response = ( new Pagination$request$response$query->get_found_rows() ) )->add_headers();

        return 
$response;
    }

    
/**
     * Retrieves a single Workflow item.
     *
     * @param WP_REST_Request $request Full request object.
     *
     * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function get_item$request ) {
        try {
            
$workflow $this->get_workflow$request['id'] );
        } catch ( 
RestException $e ) {
            return 
$e->get_wp_error();
        }

        return 
rest_ensure_response$this->prepare_item_for_response$workflow$request ) );
    }

    
/**
     * Retrieves the query params for the collections.
     *
     * @return array
     */
    
public function get_collection_params() {
        
$params parent::get_collection_params();

        
$params['context']['default'] = 'view';

        
$params['status'] = [
            
'description'       => __'Limit results by status.''automatewoo' ),
            
'type'              => 'string',
            
'validate_callback' => 'rest_validate_request_arg',
            
'enum'              => [ 'active''disabled' ],
        ];

        
$params['type'] = [
            
'description'       => __'Limit results by type.''automatewoo' ),
            
'type'              => 'string',
            
'validate_callback' => 'rest_validate_request_arg',
            
'enum'              => array_keysWorkflowsHelper::get_types() ),
        ];

        
$params['trigger'] = [
            
'description'       => __'Limit results by one or more triggers.''automatewoo' ),
            
'type'              => 'array',
            
'items'             => [
                
'type' => 'string',
            ],
            
'default'           => [],
            
'sanitize_callback' => 'wp_parse_slug_list',
        ];

        return 
$params;
    }

    
/**
     * Creates a single Workflow item.
     *
     * @since 6.0.10
     *
     * @param WP_REST_Request $request Full request object.
     *
     * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function create_item$request ) {
        try {
            
$workflow $this->create_workflow$request );
        } catch ( 
RestException $e ) {
            return 
$e->get_wp_error();
        }

        return 
rest_ensure_response$this->prepare_item_for_response$workflow$request ) );
    }

    
/**
     * Updates a single Workflow item.
     *
     * @since 6.0.10
     *
     * @param WP_REST_Request $request Full request object.
     *
     * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function update_item$request ) {
        try {
            
$workflow $this->update_workflow$request );
        } catch ( 
RestException $e ) {
            return 
$e->get_wp_error();
        }

        return 
rest_ensure_response$this->prepare_item_for_response$workflow$request ) );
    }

    
/**
     * Deletes a single Workflow item (moves to trash if available).
     *
     * @since 6.0.10
     *
     * @param WP_REST_Request $request Full request object.
     *
     * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
     */
    
public function delete_item$request ) {
        try {
            
$id    = (int) $request['id'];
            
$force boolval$request['force'] );

            
$workflow $this->delete_workflow$id$force );
        } catch ( 
RestException $e ) {
            return 
$e->get_wp_error();
        }

        
// If force deleted return a simple response as the full workflow data is no longer available.
        
if ( 'deleted' === $workflow->get_status() ) {
            return 
rest_ensure_response(
                [
                    
'id'     => $workflow->get_id(),
                    
'status' => $workflow->get_status(),
                ]
            );
        }

        return 
rest_ensure_response$this->prepare_item_for_response$workflow$request ) );
    }

    
/**
     * Prepare the workflow for the REST response.
     *
     * @param Workflow        $workflow
     * @param WP_REST_Request $request
     *
     * @return WP_REST_Response|mixed
     */
    
public function prepare_item_for_response$workflow$request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
        
$data = [
            
'id'                             => $workflow->get_id(),
            
'title'                          => $workflow->get_title(),
            
'status'                         => $workflow->get_status(),
            
'type'                           => $workflow->get_type(),
            
'trigger'                        => [
                
'name'    => $workflow->get_trigger_name(),
                
'options' => $workflow->get_trigger_options(),
            ],
            
'rules'                          => $workflow->get_rule_data(),
            
'actions'                        => array_values$workflow->get_actions_data() ),
            
'timing'                         => $this->prepare_timing_for_response$workflow ),
            
'is_transactional'               => $workflow->is_transactional(),
            
'is_tracking_enabled'            => $workflow->is_tracking_enabled(),
            
'is_conversion_tracking_enabled' => $workflow->is_conversion_tracking_enabled(),
            
'google_analytics_link_tracking' => $workflow->get_ga_tracking_params(),
            
'workflow_order'                 => $workflow->get_order(),
        ];

        return 
rest_ensure_response$data );
    }

    
/**
     * Prepare the workflow timing for the REST response.
     *
     * @param Workflow $workflow
     *
     * @return array
     */
    
protected function prepare_timing_for_response$workflow ) {
        
$data = [
            
'type' => $workflow->get_timing_type(),
        ];

        switch ( 
$workflow->get_timing_type() ) {
            case 
'delayed':
                
$data['delay'] = [
                    
'unit'  => $workflow->get_timing_delay_unit(),
                    
'value' => $workflow->get_timing_delay_number(),
                ];
                break;
            case 
'scheduled':
                
$data['scheduled'] = [
                    
'time_of_day' => $workflow->get_scheduled_time(),
                    
'days'        => array_mapFormat::class . '::api_weekday'$workflow->get_scheduled_days() ),
                ];

                if ( 
$workflow->get_timing_delay_number() ) {
                    
$data['delay'] = [
                        
'unit'  => $workflow->get_timing_delay_unit(),
                        
'value' => $workflow->get_timing_delay_number(),
                    ];
                }
                break;
            case 
'fixed':
                
$data['datetime'] = Format::api_datetime$workflow->get_fixed_time() );
                break;
            case 
'datetime':
                
$data['variable'] = $workflow->get_option'queue_datetime'false );
                break;
        }

        return 
$data;
    }
}