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

namespace AutomateWoo;

/**
 * Class PreSubmit
 * @package AutomateWoo
 * @since 2.9
 */
class PreSubmit {

    
/**
     * @return array
     */
    
static function get_email_capture_selectors() {
        return 
apply_filters'automatewoo/guest_capture_fields', [
            
'.woocommerce-checkout [type="email"]',
            
'#billing_email',
            
'.automatewoo-capture-guest-email',
            
'input[name="billing_email"]',
        ]);
    }


    
/**
     * @return array
     */
    
static function get_checkout_capture_fields() {
        return 
apply_filters'automatewoo/checkout_capture_fields', [
            
'billing_first_name',
            
'billing_last_name',
            
'billing_company',
            
'billing_phone',
            
'billing_country',
            
'billing_address_1',
            
'billing_address_2',
            
'billing_city',
            
'billing_state',
            
'billing_postcode'
        
]);
    }


    
/**
     * @return bool
     */
    
static function is_capture_permitted() {
        if ( ! 
Options::presubmit_capture_enabled() ) {
            return 
false;
        }

        if ( 
Session_Tracker::get_detected_user_id() ) {
            return 
false// don't use capture for registered users
        
}

        if ( ! 
Session_Tracker::cookies_permitted() ) {
            return 
false// sessions are disabled or blocked
        
}

        return 
true;
    }


    
/**
     * @param $field_name
     * @return bool
     */
    
static function is_checkout_capture_field$field_name ) {
        return 
in_array$field_nameself::get_checkout_capture_fields() );
    }


    
/**
     * Capture guest email
     */
    
static function ajax_capture_email() {
        
self::do_capture_permitted_check();

        
$email Clean::emailaw_request('email') );
        
$language Clean::stringaw_request'language' ) );
        
$checkout_fields Clean::recursiveaw_request'checkout_fields' ) );
        
$maybe_guest_or_user Customer_Factory::get_by_email$emailfalse );

        if ( 
$maybe_guest_or_user ) {
            
Ajax::send_json_error();
        }

        
$customer Session_Tracker::set_session_by_captured_email$email$language );

        if ( ! 
$customer ) {
            
Ajax::send_json_error();
        }

        if ( ! 
$guest $customer->get_guest() ) {
            
Ajax::send_json_error();
        }

        if ( 
is_array$checkout_fields ) )  {
            foreach ( 
$checkout_fields as $field_name => $field_value ) {

                if ( ! 
self::is_checkout_capture_field$field_name ) || empty( $field_value ) ) {
                  continue; 
// IMPORTANT don't save the field if it is empty
                
}

                
$guest->update_meta$field_namestripslashes$field_value ) );
            }
        }
        else {
            
$location wc_get_customer_default_location();
            if ( 
$location['country'] ) {
                
$guest->update_meta'billing_country'$location['country'] );
            }
        }

        
Ajax::send_json_success([
           
'guest_id' => $guest->get_id()
        ]);
    }


    
/**
     * Capture an additional field from the checkout page
     */
    
static function ajax_capture_checkout_field() {
        
self::do_capture_permitted_check();

        
$guest_id absintaw_request'guest_id' ) );
        
$field_name Clean::stringaw_request'field_name' ) );
        
$field_value stripslashesClean::stringaw_request'field_value' ) ) );

        
$guest Session_Tracker::get_current_guest();

        if ( ! 
$guest || $guest_id != $guest->get_id() ) {
            
Ajax::send_json_error();
        }

        if ( 
self::is_checkout_capture_field$field_name ) ) {
            
$guest->update_meta$field_name$field_value );
        }

        
Ajax::send_json_success();
    }


    
/**
     * @return array
     */
    
static function get_js_params() {
        
$params = [];

        
$guest Session_Tracker::get_current_guest();

        
$params['guest_id'] = $guest $guest->get_id() : 0;
        
$params['language'] = Language::get_current();
        
$params['email_capture_selectors'] = self::get_email_capture_selectors();
        
$params['checkout_capture_selectors'] = self::get_checkout_capture_fields();
        
$params['ajax_url'] = Ajax::get_endpoint'%%endpoint%%' );

        return 
$params;
    }


    
/**
     * Checks if pre-submit capture is permitted.
     * If capture, not permitted, error JSON will be sent and request will die.
     *
     * @since 4.4.0
     */
    
static function do_capture_permitted_check() {
        if ( ! 
self::is_capture_permitted() ) {
            
Ajax::send_json_error( [
                
'presubmit_capture_enabled' => Options::presubmit_capture_enabled(),
                
'session_tracking_enabled'  => Options::session_tracking_enabled(),
                
'session_cookies_permitted' => Session_Tracker::cookies_permitted(),
            ] );
        }
    }


}