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

namespace AutomateWoo;

use 
AutomateWoo\DataTypes\DataTypes;
use 
AutomateWoo\Workflows\VariableParsing\ExcludedParsedVariable;
use 
AutomateWoo\Workflows\VariableParsing\ParsedVariable;
use 
AutomateWoo\Workflows\VariableParsing\VariableParser;

/**
 * Process variables into values. Is used on workflows and action options.
 *
 * @class Variable_Processor
 * @since 2.0.2
 */
class Variables_Processor {

    
/** @var Workflow */
    
public $workflow;


    
/**
     * @param Workflow $workflow
     */
    
public function __construct$workflow ) {
        
$this->workflow $workflow;
    }


    
/**
     * @param string $text
     * @param bool   $allow_html
     * @return string
     */
    
public function process_field$text$allow_html false ) {

        
$replacer = new Replace_Helper$text, [ $this'callback_process_field' ], 'variables' );
        
$value    $replacer->process();

        if ( ! 
$allow_html ) {
            
$value html_entity_decodewp_strip_all_tags$value ) );
        }

        return 
$value;
    }


    
/**
     * Callback function to process a variable string.
     *
     * @param string $string
     *
     * @return string
     */
    
public function callback_process_field$string ) {
        
$variable self::parse_variable$string );

        if ( 
$variable instanceof ExcludedParsedVariable ) {
            
// Return excluded variable without processing
            
return "{{ $variable->variable_string }}";
        }

        if ( ! 
$variable instanceof ParsedVariable ) {
            return 
'';
        }

        
$parameters $variable->parameters;
        
$value      $this->get_variable_value$variable->type$variable->field$parameters );
        
$value      = (string) apply_filters'automatewoo/variables/after_get_value'$value$variable->type$variable->field$parameters$this->workflow );

        if ( 
$value === '' ) {
            
// backwards compatibility
            
if ( isset( $parameters['default'] ) ) {
                
$parameters['fallback'] = $parameters['default'];
            }

            
// show default if set and no real value
            
if ( isset( $parameters['fallback'] ) ) {
                
$value $parameters['fallback'];
            }
        }

        return 
$value;
    }


    
/**
     * Sanitize and parse a variable string into a usable object.
     *
     * @param string $string
     *
     * @return ParsedVariable|ExcludedParsedVariable|null
     */
    
public static function parse_variable$string ) {
        
$parser = new VariableParser();
        try {
            
$parsed_variable $parser->parse$string );
        } catch ( 
\Exception $e ) {
            return 
null;
        }

        return 
$parsed_variable;
    }


    
/**
     * Get the value of a variable.
     *
     * @param string $data_type
     * @param string $data_field
     * @param array  $parameters
     *
     * @return string
     */
    
public function get_variable_value$data_type$data_field$parameters = [] ) {

        
// Short circuit filter for the variable value
        
$short_circuit = (string) apply_filters'automatewoo_text_variable_value'false$data_type$data_field );

        if ( 
$short_circuit ) {
            return 
$short_circuit;
        }

        
$this->convert_legacy_variables$data_type$data_field$parameters );

        
$variable_name "$data_type.$data_field";
        
$variable      Variables::get_variable$variable_name );

        
$value '';

        if ( 
$variable && method_exists$variable'get_value' ) ) {

            if ( 
DataTypes::is_non_stored_data_type$data_type ) ) {
                
$value $variable->get_value$parameters$this->workflow );
            } else {
                
$data_item $this->workflow->get_data_item$variable->get_data_type() );

                if ( 
$data_item ) {
                    
$value $variable->get_value$data_item$parameters$this->workflow );
                }
            }
        }

        return (string) 
apply_filters'automatewoo/variables/get_variable_value', (string) $value$this$variable );
    }

    
/**
     * Handle legacy variable compatibility.
     *
     * @param string $data_type
     * @param string $data_field
     * @param array  $parameters
     */
    
private function convert_legacy_variables( &$data_type, &$data_field, &$parameters ) {
        if ( 
$data_type === 'site' ) {
            
$data_type 'shop';
        }

        if ( 
$data_type === 'shop' ) {
            if ( 
$data_field === 'products_on_sale' ) {
                
$data_field         'products';
                
$parameters['type'] = 'sale';
            }

            if ( 
$data_field === 'products_recent' ) {
                
$data_field         'products';
                
$parameters['type'] = 'recent';
            }

            if ( 
$data_field === 'products_featured' ) {
                
$data_field         'products';
                
$parameters['type'] = 'featured';
            }
        }
    }
}