/var/www/html_uk/wp-content/plugins/automatewoo/includes/Rules/Rule.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
// phpcs:ignoreFile

namespace AutomateWoo\Rules;

use 
AutomateWoo\Clean;
use 
UnexpectedValueException;

/**
 * @class Rule
 */
abstract class Rule {

    
/** @var string */
    
public $name;

    
/** @var string */
    
public $title;

    
/** @var string */
    
public $group;

    
/** @var string string|number|object|select  */
    
public $type;

    
/**
     * Define the data type used by the rule.
     * This is a required property.
     *
     * @var string
     */
    
public $data_item;

    
/** @var array  */
    
public $compare_types = [];

    
/** @var \AutomateWoo\Workflow */
    
private $workflow;

    
/** @var bool - e.g meta rules have 2 value fields so their value data is an stored as an array */
    
public $has_multiple_value_fields false;


    
/**
     * Constructor
     */
    
function __construct() {
        
$this->init();
        
$this->determine_rule_group();
    }


    
/**
     * Init the rule.
     */
    
abstract public function init();


    
/**
     * Validates that a given workflow data item passed the rule validation
     * based on the supplied $compare_type and $value.
     *
     * @param mixed  $data_item    A valid workflow data item e.g. an instance of `\WC_Order` for an order based rule.
     * @param string $compare_type The user selected compare type for the rule.
     * @param mixed  $value        The user entered value for the rule. This value is validated by the validate_value() method beforehand.
     *
     * @return bool
     */
    
abstract public function validate$data_item$compare_type$value );


    
/**
     * @param $workflow
     */
    
function set_workflow$workflow ) {
        
$this->workflow $workflow;
    }


    
/**
     * @return \AutomateWoo\Workflow
     */
    
function get_workflow() {
        return 
$this->workflow;
    }


    
/**
     * @since 4.2
     * @return \AutomateWoo\Data_Layer
     */
    
function data_layer() {
        return 
$this->get_workflow()->data_layer();
    }


    
/**
     * Get is/is not compare types.
     *
     * @since 4.6
     *
     * @return array
     */
    
public function get_is_or_not_compare_types() {
        return [
            
'is'     => __'is''automatewoo' ),
            
'is_not' => __'is not''automatewoo' ),
        ];
    }


    
/**
     * @return array
     */
    
function get_string_compare_types() {
        return [
            
'contains' => __'contains''automatewoo' ),
            
'not_contains' => __'does not contain''automatewoo' ),
            
'is' => __'is''automatewoo' ),
            
'is_not' => __'is not''automatewoo' ),
            
'starts_with' => __'starts with''automatewoo' ),
            
'ends_with' => __'ends with''automatewoo' ),
            
'blank' => __'is blank''automatewoo' ),
            
'not_blank' => __'is not blank''automatewoo' ),
            
'regex' => __'matches regex''automatewoo' ),
        ];
    }


    
/**
     * @return array
     */
    
function get_multi_string_compare_types() {
        return [
            
'contains' => __'any contains''automatewoo' ),
            
'is' => __'any matches exactly''automatewoo' ),
            
'starts_with' => __'any starts with''automatewoo' ),
            
'ends_with' => __'any ends with''automatewoo' ),
        ];
    }


    
/**
     * @return array
     */
    
function get_float_compare_types() {
        return 
$this->get_is_or_not_compare_types() + [
            
'greater_than' => __'is greater than''automatewoo' ),
            
'less_than' => __'is less than''automatewoo' ),
        ];
    }


    
/**
     * @return array
     */
    
function get_integer_compare_types() {
        return 
$this->get_float_compare_types() + [
            
'multiple_of' => __'is a multiple of''automatewoo' ),
            
'not_multiple_of' => __'is not a multiple of''automatewoo' )
        ];
    }

    
/**
     * Get multi-select match compare types.
     *
     * @since 4.6
     *
     * @return array
     */
    
public function get_multi_select_compare_types() {
        return [
            
'matches_any'  => __'matches any''automatewoo' ),
            
'matches_all'  => __'matches all''automatewoo' ),
            
'matches_none' => __'matches none''automatewoo' ),
        ];
    }

    
/**
     * Get includes or not includes compare types.
     *
     * @since 4.6
     *
     * @return array
     */
    
public function get_includes_or_not_compare_types() {
        return [
            
'includes'     => __'includes''automatewoo' ),
            
'not_includes' => __'does not include''automatewoo' ),
        ];
    }

    
/**
     * @param $compare_type
     * @return bool
     */
    
function is_string_compare_type$compare_type ) {
        return 
array_key_exists$compare_type$this->get_string_compare_types() );
    }


    
/**
     * @param $compare_type
     * @return bool
     */
    
function is_integer_compare_type$compare_type ) {
        return 
array_key_exists$compare_type$this->get_integer_compare_types() );
    }


    
/**
     * @param $compare_type
     * @return bool
     */
    
function is_float_compare_type$compare_type ) {
        return 
array_key_exists$compare_type$this->get_float_compare_types() );
    }


    
/**
     * @param $compare_type
     * @return bool
     */
    
function is_is_or_is_not_compare_type$compare_type ) {
        return 
array_key_exists$compare_type$this->get_is_or_not_compare_types() );
    }


    
/**
     * Validate a string based rule value.
     *
     * @param string $actual_value
     * @param string $compare_type
     * @param string $expected_value
     *
     * @return bool
     */
    
function validate_string$actual_value$compare_type$expected_value ) {

        
$actual_value = (string) $actual_value;
        
$expected_value = (string) $expected_value;

        
// most comparisons are case insensitive
        
$actual_value_lowercase   strtolower$actual_value );
        
$expected_value_lowercase strtolower$expected_value );

        switch ( 
$compare_type ) {

            case 
'is':
                return 
$actual_value_lowercase == $expected_value_lowercase;
                break;

            case 
'is_not':
                return 
$actual_value_lowercase != $expected_value_lowercase;
                break;

            case 
'contains':
                return 
strstr$actual_value_lowercase$expected_value_lowercase ) !== false;
                break;

            case 
'not_contains':
                return 
strstr$actual_value_lowercase$expected_value_lowercase ) === false;
                break;

            case 
'starts_with':
                return 
aw_str_starts_with$actual_value_lowercase$expected_value_lowercase );

            case 
'ends_with':
                return 
aw_str_ends_with$actual_value_lowercase$expected_value_lowercase );

            case 
'blank':
                return empty( 
$actual_value );
                break;

            case 
'not_blank':
                return ! empty( 
$actual_value );
                break;

            case 
'regex':
                
// Regex validation must not use case insensitive values
                
return $this->validate_string_regex$actual_value$expected_value );
        }

        return 
false;
    }


    
/**
     * Only supports 'contains', 'is', 'starts_with', 'ends_with'
     *
     * @param array $actual_values
     * @param string $compare_type
     * @param string $expected_value
     * @return bool
     */
    
function validate_string_multi$actual_values$compare_type$expected_value ) {

        if ( empty( 
$expected_value ) ) {
            return 
false;
        }

        
// look for at least one item that validates the text match
        
foreach ( $actual_values as $coupon_code ) {
            if ( 
$this->validate_string$coupon_code$compare_type$expected_value ) ) {
                return 
true;
            }
        }

        return 
false;
    }


    
/**
     * @param $actual_value
     * @param $compare_type
     * @param $expected_value
     * @return bool
     */
    
function validate_number$actual_value$compare_type$expected_value ) {

        
$actual_value = (float) $actual_value;
        
$expected_value = (float) $expected_value;

        switch ( 
$compare_type ) {

            case 
'is':
                return 
$actual_value == $expected_value;
                break;

            case 
'is_not':
                return 
$actual_value != $expected_value;
                break;

            case 
'greater_than':
                return 
$actual_value $expected_value;
                break;

            case 
'less_than':
                return 
$actual_value $expected_value;
                break;

        }


        
// validate 'multiple of' compares, only accept integers
        
if ( ! $this->is_whole_number$actual_value ) || ! $this->is_whole_number$expected_value ) ) {
            return 
false;
        }

        
$actual_value = (int) $actual_value;
        
$expected_value = (int) $expected_value;

        switch ( 
$compare_type ) {

            case 
'multiple_of':
                return 
$actual_value $expected_value == 0;
                break;

            case 
'not_multiple_of':
                return 
$actual_value $expected_value != 0;
                break;
        }

        return 
false;
    }


    
/**
     * @param $number
     * @return bool
     */
    
function is_whole_number$number ) {
        
$number = (float) $number;
        return 
floor$number ) == $number;
    }


    
/**
     * Determine the rule group based on it's title.
     *
     * If the group prop is already set that will be used.
     *
     * @since 4.5.0
     *
     * @return string
     */
    
public function determine_rule_group() {
        if ( isset( 
$this->group ) ) {
            return;
        }

        
// extract the hyphenated part of the title and use as group
        
if ( isset( $this->title ) && strstr$this->title'-' ) ) {
            list( 
$this->group ) = explode' - '$this->title);
        }

        if ( empty( 
$this->group ) ) {
            
$this->group __'Other''automatewoo' );
        }
    }

    
/**
     * Validates string regex rule.
     *
     * @since 4.6.0
     *
     * @param string $string
     * @param string $regex
     *
     * @return bool
     */
    
protected function validate_string_regex$string$regex ) {
        
$regex $this->remove_global_regex_modifiertrim$regex ) );

        
// Add '/' delimiters if none are provided in the regex.
        
if ( ! preg_match'#^/(.+)/[gi]*$#'$regex ) ) {

            
// Escape any unescaped delimiters in the regex first.
            
if ( preg_match'#[^\\\\]/#'$regex ) ) {
                
$regex str_replace'/''\\/'$regex );
            }

            
$regex '/' $regex '/';
        }

        return (bool) @
preg_match$regex$string );
    }

    
/**
     * Remove the global regex modifier as it is not supported by PHP.
     *
     * @since 4.6.0
     *
     * @param string $regex
     *
     * @return string
     */
    
protected function remove_global_regex_modifier$regex ) {
        return 
preg_replace_callback'/(\/[a-z]+)$/', function( $modifiers ){
            return 
str_replace'g'''$modifiers[0] );
        }, 
$regex );
    }

    
/**
     * Sanitizes the rule's value.
     *
     * This method runs before WRITING a value to the DB but doesn't run before READING.
     *
     * @since 4.6.0
     *
     * @param mixed $value
     *
     * @return mixed
     */
    
public function sanitize_value$value ) {
        return 
Clean::recursive$value );
    }

    
/**
     * Formats a rule's value for display in the rules UI.
     *
     * @since 4.6.0
     *
     * @param mixed $value
     *
     * @return mixed
     */
    
public function format_value$value ) {
        return 
$value;
    }

    
/**
     * Validate the rule's user entered value.
     *
     * @since 5.1.0
     *
     * @param mixed $value
     *
     * @throws UnexpectedValueException When the value is not valid.
     */
    
public function validate_value$value ) {
        
// Override this method in child classes.
    
}

}