/var/www/html_uk/wp-content/plugins/automatewoo/includes/Action.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php

namespace AutomateWoo;

use 
AutomateWoo\Actions\ActionInterface;
use 
AutomateWoo\Actions\PreviewableInterface;
use 
AutomateWoo\Fields\Field;

/**
 * Abstract Class Action
 *
 * All workflow actions extend this class.
 */
abstract class Action implements ActionInterface {

    
/**
     * The action's unique name/slug.
     *
     * @var string
     */
    
public $name;

    
/**
     * The data items required by the action.
     *
     * @var array
     */
    
public $required_data_items = [];

    
/**
     * The action's title.
     *
     * @var string
     */
    
public $title;

    
/**
     * The action's description.
     *
     * @var string
     */
    
public $description;

    
/**
     * The action's group.
     *
     * @var string
     */
    
public $group;

    
/**
     * The action's fields objects.
     *
     * @var Field[]
     */
    
public $fields;

    
/**
     * Array containing the action's option values.
     *
     * @var array
     */
    
public $options;

    
/**
     * The workflow the action belongs to.
     *
     * This prop may not be set depending on the context.
     *
     * @var Workflow
     */
    
public $workflow;

    
/**
     * Knows if admin details have been loaded.
     *
     * @var bool
     */
    
protected $has_loaded_admin_details false;

    
/**
     * This method no longer has an explicit purpose and is deprecated.
     *
     * @deprecated
     */
    
public function init() {
        
wc_deprecated_function__METHOD__'5.2.0' );
    }

    
/**
     * Method to load the action's fields.
     *
     * TODO make protected method
     */
    
public function load_fields() {}

    
/**
     * Method to set the action's admin props.
     *
     * Admin props include: title, group and description.
     */
    
protected function load_admin_details() {}

    
/**
     * Loads the action's admin props.
     */
    
protected function maybe_load_admin_details() {
        if ( ! 
$this->has_loaded_admin_details ) {
            
$this->load_admin_details();
            
$this->has_loaded_admin_details true;
        }
    }

    
/**
     * Get the action's title.
     *
     * @param bool $prepend_group
     * @return string
     */
    
public function get_title$prepend_group false ) {
        
$this->maybe_load_admin_details();
        
$group $this->get_group();
        
$title $this->title ?: '';

        if ( 
$prepend_group && $group !== __'Other''automatewoo' ) ) {
            return 
$group ' - ' $title;
        }

        return 
$title;
    }

    
/**
     * Get the action's group.
     *
     * @return string
     */
    
public function get_group() {
        
$this->maybe_load_admin_details();
        return 
$this->group $this->group __'Other''automatewoo' );
    }

    
/**
     * Get the action's description.
     *
     * @return string
     */
    
public function get_description() {
        
$this->maybe_load_admin_details();
        return 
$this->description ?: '';
    }

    
/**
     * Get the action's name.
     *
     * @return string
     */
    
public function get_name() {
        return 
$this->name ?: '';
    }

    
/**
     * Set the action's name.
     *
     * @param string $name
     */
    
public function set_name$name ) {
        
$this->name $name;
    }

    
/**
     * Get the action's description HTML.
     *
     * @return string
     */
    
public function get_description_html() {
        if ( ! 
$this->get_description() ) {
            return 
'';
        }

        return 
'<p class="aw-field-description">' $this->get_description() . '</p>';
    }

    
/**
     * Adds a field to the action.
     *
     * Should only be called in the load_fields() method.
     *
     * @param Field $field
     */
    
protected function add_field$field ) {
        
$field->set_name_base'aw_workflow_data[actions]' );
        
$this->fields$field->get_name() ] = $field;
    }

    
/**
     * Removes a field from the action.
     *
     * Should only be called in the load_fields() method.
     *
     * @param string $field_name
     */
    
protected function remove_field$field_name ) {
        unset( 
$this->fields$field_name ] );
    }

    
/**
     * Gets specific field belonging to the action.
     *
     * @param string $name
     *
     * @return Field|false
     */
    
public function get_field$name ) {
        
$this->get_fields();

        if ( ! isset( 
$this->fields$name ] ) ) {
            return 
false;
        }

        return 
$this->fields$name ];
    }

    
/**
     * Gets the action's fields.
     *
     * @return Field[]
     */
    
public function get_fields() {
        if ( ! isset( 
$this->fields ) ) {
            
$this->fields = [];
            
$this->load_fields();
        }

        return 
$this->fields;
    }

    
/**
     * Get a list of required field names.
     *
     * @since 6.0.10
     *
     * @return Field[]
     */
    
public function get_required_fields(): array {
        
$required_fields = [];
        foreach ( 
$this->get_fields() as $name => $field ) {
            if ( 
$field->get_required() ) {
                
$required_fields$name ] = $field;
            }
        }

        return 
$required_fields;
    }

    
/**
     * Set the action's options.
     *
     * @param array $options
     */
    
public function set_options$options ) {
        
$this->options $options;
    }

    
/**
     * Returns an option for use when running the action.
     *
     * Option value will already have been sanitized by it's field ::sanitize_value() method.
     *
     * @param string $field_name
     * @param bool   $process_variables
     * @param bool   $allow_html
     *
     * @return mixed Will vary depending on the field type specified in the action's fields.
     */
    
public function get_option$field_name$process_variables false$allow_html false ) {
        
$value $this->get_option_raw$field_name );

        if ( 
is_string$value ) ) {
            
$value $this->process_option_string_value$value$process_variables$allow_html );
        } elseif ( 
is_array$value ) ) {
            foreach ( 
$value as &$array_value ) {
                if ( 
is_string$array_value ) ) {
                    
$array_value $this->process_option_string_value$array_value$process_variables$allow_html );
                }
            }
        }

        return 
apply_filters'automatewoo_action_option'$value$field_name$process_variables$this );
    }

    
/**
     * Process an option string value converting any variables.
     *
     * @since 5.2.0
     *
     * @param string $value
     * @param bool   $process_variables
     * @param bool   $allow_html
     *
     * @return string
     */
    
protected function process_option_string_valuestring $valuebool $process_variablesbool $allow_html ) {
        if ( 
$process_variables ) {
            return 
$this->workflow->variable_processor()->process_field$value$allow_html );
        } elseif ( ! 
$allow_html ) {
            return 
html_entity_decodewp_strip_all_tags$value ) );
        }
        return 
$value;
    }

    
/**
     * Get an option for use when editing the action.
     *
     * The value will be already sanitized by the field object.
     * This is used to displaying an option value for editing.
     *
     * @since 4.4.0
     *
     * @param string $field_name
     *
     * @return mixed
     */
    
public function get_option_raw$field_name ) {
        if ( isset( 
$this->options$field_name ] ) ) {
            return 
$this->options$field_name ];
        }

        return 
false;
    }

    
/**
     * Used to dynamically load option values for an action field.
     *
     * TODO move to HasDynamicFieldOptions interface
     *
     * @param string       $field_name
     * @param string|false $reference_field_value
     *
     * @return array
     */
    
public function get_dynamic_field_options$field_name$reference_field_value false ) {
        return [];
    }

    
/**
     * Check requirements for the action.
     *
     * TODO move to HasRequirements interface
     * TODO Ideally change behaviour to "get_requirements" rather than actually performing check
     *
     * Runs before an action is loaded in the admin area.
     */
    
public function check_requirements() {}

    
/**
     * Display a warning in the admin area.
     *
     * TODO move into admin/UI related code
     *
     * @param string $message
     */
    
public function warning$message ) {
        if ( ! 
is_admin() ) {
            return;
        }
        
?>
        <script type="text/javascript">
            alert('ERROR: <?php echo esc_html$message ); ?>');
        </script>
        <?php
    
}

    
/**
     * Get text for action deprecation warning.
     *
     * @return string
     */
    
protected function get_deprecation_warning() {
        return 
__'THIS ACTION IS DEPRECATED AND SHOULD NOT BE USED.''automatewoo' );
    }

    
/**
     * Does this action have a preview ability?
     *
     * @deprecated in 5.2.0 Use Previewable interface instead
     * @see PreviewableInterface
     *
     * @return bool
     */
    
public function can_be_previewed() {
        
wc_deprecated_function__METHOD__'5.2.0' );
        return 
$this instanceof PreviewableInterface;
    }

    
/**
     * Returns preview content.
     *
     * @deprecated in 5.2.0 Use Previewable interface instead
     * @see PreviewableInterface
     *
     * @return string|\WP_Error
     */
    
public function preview() {
        
wc_deprecated_function__METHOD__'5.2.0'PreviewableInterface::class );
        if ( 
$this instanceof PreviewableInterface ) {
            return 
$this->get_preview();
        }
    }

    
/**
     * Add a message to the actions logs in case the response is not successful.
     *
     * @param Remote_Request $request the request to log
     * @throws \Exception When the request is not successful.
     */
    
protected function maybe_log_action$request ) {
        if ( ! 
$request->is_successful() ) {
            
$body $request->get_body();
            
$body $body['detail'] ?? $request->get_body_raw();
            throw new 
\Exceptionesc_html$body ) );
        }
    }


    
/**
     * Validates the required fields in the action.
     *
     * @throws \Exception When there are required fields not present.
     */
    
protected function validate_required_fields() {
        
$errors array_filter(
            
$this->get_fields(),
            function ( 
$field ) {
                return 
$field->get_required() && ! $this->get_option$field->get_name() );
            }
        );

        if ( ! empty( 
$errors ) ) {
            
$message sprintf(
                
/* translators: Comma separated list of required options. */
                
__'The following required option(s) were not provided: %s.''automatewoo' ),
                
implode(
                    
', ',
                    
array_map(
                        function ( 
$error ) {
                            return 
$error->get_title();
                        },
                        
$errors
                    
)
                )
            );
            throw new 
\Exceptionesc_html$message ) );
        }
    }
}