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

namespace AutomateWoo;

defined'ABSPATH' ) || exit;

/**
 * Class Frontend_Form_Handler
 * @since 3.9
 */
class Frontend_Form_Handler {

    
/** @var string */
    
public static $current_action '';


    private static 
$actions = [
        
'automatewoo_save_communication_preferences',
        
'automatewoo_save_communication_signup',
    ];



    
/**
     * Handle frontend form post
     */
    
static function handle() {
        
$action              Clean::string$_POST['action'] );
        
$honeypot_field_name apply_filters'automatewoo/honeypot_field/name''firstname' );

        if ( ! 
in_array$actionself::$actions ) || empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce$_POST['_wpnonce'], $action ) ) {
            return;
        }

        if ( ! empty( 
$_POST$honeypot_field_name ] ) ) {
            
wc_add_notice(
                
sprintf(
                    
/* translators: %s Error code when form can not be submitted. */
                    
__'The form could not be submitted. Error code: %s''automatewoo' ),
                    
1
                
),
                
'error'
            
);
            return;
        }

        
$action str_replace'automatewoo_'''$action );
        
self::$current_action $action;

        
nocache_headers();

        
call_user_func( [ __CLASS__$action ] );
    }



    static function 
save_communication_preferences() {
        
$customer = isset( $_POST['customer_key'] ) ? Customer_Factory::get_by_key$_POST['customer_key'] ): false;

        if ( ! 
$customer ) {
            return;
        }

        
self::update_customer_preferences$customer );

        
wc_add_notice__'Your communication preferences were updated.''automatewoo' ) );
    }



    static function 
save_communication_signup() {

        
$email = isset( $_POST['email'] ) ? sanitize_email$_POST['email'] ) : '';

        
$maybe_customer Customer_Factory::get_by_email$emailfalse );

        if ( 
$maybe_customer ) {
            
wc_add_notice__'It was not possible to update communication preferences for this email.''automatewoo' ), 'error' );
            return;
        }

        
$customer Customer_Factory::get_by_email$email );

        if ( ! 
$customer ) {
            
wc_add_notice__'Please enter a valid email address.''automatewoo' ), 'error' );
            return;
        }

        
self::update_customer_preferences$customer );

        if ( 
$customer->is_opted_in() ) {
            
wc_add_notice__'Thanks! Your signup was successful.''automatewoo' ) );
        }
        else {
            
wc_add_notice__"Saved successfully! You won't receive marketing communications from us."'automatewoo' ) );
        }

    }


    
/**
     * @param Customer $customer
     */
    
protected static function update_customer_preferences$customer ) {
        if ( isset( 
$_POST['subscribe'] ) ) {
            
$customer->opt_in();
        }
        else {
            
$customer->opt_out();
        }

        
// try and start session tracking the customer
        
Session_Tracker::set_session_customer$customer );

        
do_action'automatewoo/communication_page/save_preferences'$customer );

    }

}