|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Handle Easy Digital Downloads Integration: add an opt-in checkbox to the EDD checkout page. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 6.0.0 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
// Prevent direct access to the file. |
|
9
|
|
|
defined( 'ABSPATH' ) || die( esc_html_e( "Whoops, you shouldn't be accessing this file directly. Abort!", 'yikes-inc-easy-mailchimp-extender' ) ); |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Handle Easy Digital Downloads Integration. |
|
13
|
|
|
*/ |
|
14
|
|
|
class Yikes_Easy_MC_EDD_Checkbox_Class extends Yikes_Easy_MC_Checkbox_Integration_Class { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The integration type. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string $type |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $type = 'easy_digital_downloads_checkout_form'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct() { |
|
27
|
|
|
add_action( 'edd_purchase_form_user_info_fields', array( $this, 'output_checkbox' ) ); |
|
28
|
|
|
add_action( 'edd_insert_payment', array( $this, 'update_payment_post_meta' ), 99999 ); |
|
29
|
|
|
add_action( 'edd_complete_purchase', array( $this, 'subscribe_from_edd_purchase' ), 50 ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Outputs the subscribe checkbox. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function output_checkbox() { |
|
36
|
|
|
if ( $this->is_user_already_subscribed( $this->type ) ) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
echo $this->yikes_get_checkbox(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Add the checkbox's checked value as post meta to the order. |
|
45
|
|
|
* |
|
46
|
|
|
* @param int $payment_id The payment's ID. |
|
47
|
|
|
* @param array $payment_data Array of payment data. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function update_payment_post_meta( $payment_id = 0, $payment_data = array() ) { |
|
|
|
|
|
|
50
|
|
|
if ( ! $this->was_checkbox_checked( $this->type ) ) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
update_post_meta( $payment_id, '_yikes_easy_mc_optin', '1' ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Subscribe the user if they so chose. |
|
58
|
|
|
* |
|
59
|
|
|
* @param int $payment_id The ID of the payment. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function subscribe_from_edd_purchase( $payment_id ) { |
|
62
|
|
|
$meta = get_post_meta( $payment_id, '_yikes_easy_mc_optin', true ); |
|
63
|
|
|
if ( empty( $meta ) ) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ( ! function_exists( 'edd_get_payment_user_email' ) ) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$email = (string) edd_get_payment_user_email( $payment_id ); |
|
72
|
|
|
|
|
73
|
|
|
if ( empty( $email ) ) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ( ! function_exists( 'edd_get_payment_meta_user_info' ) ) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$user_info = (array) edd_get_payment_meta_user_info( $payment_id ); |
|
82
|
|
|
$merge_vars = array(); |
|
83
|
|
|
if ( isset( $user_info['first_name'] ) ) { |
|
84
|
|
|
$merge_vars['FNAME'] = $user_info['first_name']; |
|
85
|
|
|
} |
|
86
|
|
|
if ( isset( $user_info['last_name'] ) ) { |
|
87
|
|
|
$merge_vars['LNAME'] = $user_info['last_name']; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$addl_vars = apply_filters( 'yikes_mailchimp_checkbox_integration_additional_vars', array( 'user' => $user_info, 'payment_id' => $payment_id ), $this->type ); |
|
91
|
|
|
|
|
92
|
|
|
// Subscribe the user. |
|
93
|
|
|
$this->subscribe_user_integration( $email, $this->type, $merge_vars, $addl_vars ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
$yikes_easy_mc_edd_checkbox_class = new Yikes_Easy_MC_EDD_Checkbox_Class(); |
|
98
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.