/var/www/html_uk/wp-content/plugins/automatewoo/includes/Rules/Order_Meta.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
<?php

namespace AutomateWoo\Rules;

use 
AutomateWoo\RuleQuickFilters\Clauses\ClauseInterface;
use 
AutomateWoo\Rules\Interfaces\QuickFilterable;
use 
AutomateWoo\Rules\Utilities\NumericQuickFilter;
use 
AutomateWoo\Rules\Utilities\StringQuickFilter;
use 
InvalidArgumentException;

defined'ABSPATH' ) || exit;

/**
 * @class Order_Meta
 */
class Order_Meta extends Abstract_Meta implements QuickFilterable {

    use 
StringQuickFilter;
    use 
NumericQuickFilter;

    
/** @var string */
    
public $data_item 'order';

    
/** @var string the prefix of the property name for Quick Filter clauses */
    
public static $property_prefix 'meta.';

    
/**
     * Init the rule
     */
    
public function init() {
        
$this->title __'Order - Custom Field''automatewoo' );
    }

    
/**
     * Check if the key is an internal meta key, has a getter method, and the
     * getter has no required parameters.
     *
     * Referring to the implementation of the order method `is_internal_meta_key`
     * in WooCommerce Core, it's crucial to understand why it might inadvertently
     * trigger PHP notices due to the call of `wc_doing_it_wrong`, Consequently,
     * relying solely on that method to check if a meta key is internal can be
     * problematic.
     *
     * Ref: https://github.com/woocommerce/woocommerce/blob/9.8.4/plugins/woocommerce/includes/abstracts/abstract-wc-data.php#L324-L345
     *
     * @param \WC_Order $order Order to check.
     * @param string    $key Key to check.
     * @return string|bool The getter method name if all conditions are met, otherwise false.
     */
    
protected function get_parameterless_internal_meta_key_getter$order$key ) {
        
$data_store        $order->get_data_store();
        
$internal_meta_key = ! empty( $key ) && $data_store && in_array$key$data_store->get_internal_meta_keys(), true );

        if ( ! 
$internal_meta_key ) {
            return 
false;
        }

        
$method     'get_' ltrim$key'_' );
        
$has_getter is_callable( array( $order$method ) );

        if ( 
$has_getter ) {
            
$reflection_method     = new \ReflectionMethod$order$method );
            
$required_params_count $reflection_method->getNumberOfRequiredParameters();

            if ( 
$required_params_count === ) {
                return 
$method;
            }
        }

        return 
false;
    }

    
/**
     * Validate the rule based on options set by a workflow
     * The $order passed will already be validated
     *
     * @param \WC_Order $order
     * @param string    $compare_type
     * @param array     $value_data
     * @return bool
     */
    
public function validate$order$compare_type$value_data ) {

        
$value_data $this->prepare_value_data$value_data );

        if ( ! 
is_array$value_data ) ) {
            return 
false;
        }

        
// Call getter method if it exists, to avoid warnings from `is_internal_meta_key` when calling for internal meta keys.
        
$key        $value_data['key'];
        
$method     $this->get_parameterless_internal_meta_key_getter$order$key );
        
$meta_value $method $order->$method() : $order->get_meta$key );

        return 
$this->validate_meta$meta_value$compare_type$value_data['value'] );
    }

    
/**
     * Get quick filter clause for the rule.
     *
     * @since 5.1.0
     *
     * @param string $compare_type textual representation of the comparison operator
     * @param array  $value array containing the custom meta key and value
     *
     * @return ClauseInterface StringClause, NumericClause, or NoOpClause
     *
     * @throws InvalidArgumentException When there's an error generating the clause.
     */
    
public function get_quick_filter_clause$compare_type$value ) {

        
$value_data $this->prepare_value_data$value );
        if ( ! 
is_array$value_data ) ) {
            throw new 
InvalidArgumentException();
        }

        
// Use NumericClause for numeric comparisons (greater/less/multiples) and for is/is not ONLY with numeric values
        
if ( $this->is_numeric_meta_field$compare_type$value_data['value'] ) ) {
            
$meta_clause $this->generate_numeric_quick_filter_clauseself::$property_prefix $value_data['key'], $compare_type$value_data['value'] );
        } else {
            
$meta_clause $this->generate_string_quick_filter_clauseself::$property_prefix $value_data['key'], $compare_type$value_data['value'] );
        }

        return 
$meta_clause;
    }
}