Completed
Push — staging ( 99a729...9693fc )
by
unknown
19:21
created

Yikes_Easy_MC_EDD_Checkbox_Class   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A output_checkbox() 0 6 2
A update_payment_post_meta() 0 6 2
B subscribe_from_edd_purchase() 0 34 7
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() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $payment_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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