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

Yikes_Easy_MC_Registration_Checkbox_Class   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A output_checkbox() 0 3 1
A subscribe_from_registration() 0 19 4
1
<?php
2
/**
3
 * Handle WordPress Registration Integration: add an opt-in checkbox to WordPress' native registration form.
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 WordPress Registration Integration.
13
 */
14
class Yikes_Easy_MC_Registration_Checkbox_Class extends Yikes_Easy_MC_Checkbox_Integration_Class {
15
16
	/**
17
	 * The integration type.
18
	 *
19
	 * @var string $type
20
	 */
21
	protected $type = 'registration_form';
22
23
	/**
24
	 * Constructor.
25
	 */
26
	public function __construct() {
27
		add_action( 'register_form', array( $this, 'output_checkbox' ), 20 );
28
		add_action( 'user_register', array( $this, 'subscribe_from_registration' ), 90, 1 );
29
	}
30
31
	/**
32
	 * Outputs the subscribe checkbox.
33
	 */
34
	public function output_checkbox() {
35
		echo $this->yikes_get_checkbox();
36
	}
37
38
	/**
39
	 * Subscribe the user if they so chose.
40
	 *
41
	 * @param int $user_id The WP User's ID.
42
	 */
43
	public function subscribe_from_registration( $user_id ) {
44
		if ( false === $this->was_checkbox_checked( $this->type ) ) {
45
			return false;
46
		}
47
48
		$user = get_userdata( $user_id );
49
50
		if ( false === $user ) {
51
			return false;
52
		}
53
54
		// Fetch the user's data.
55
		$merge_variables = $this->user_merge_vars( $user );
56
		$addl_vars       = apply_filters( 'yikes_mailchimp_checkbox_integration_additional_vars', array( 'user' => $user ), $this->type );
57
58
		if ( false === $this->is_user_already_subscribed( $this->type, $user->user_email ) ) {
59
			$this->subscribe_user_integration( $user->user_email, $this->type, $merge_variables, $addl_vars );
60
		}
61
	}
62
}
63
$yikes_easy_mc_registration_checkbox_class = new Yikes_Easy_MC_Registration_Checkbox_Class();
64