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

Yikes_Easy_MC_Comment_Checkbox_Class   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 1
A __construct() 0 4 1
A output_checkbox() 0 7 2
A subscribe_from_comment() 0 21 3
1
<?php
2
/**
3
 * Handle Comment Integration: add a checkbox to the comments area.
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 Comment Integration.
13
 */
14
class Yikes_Easy_MC_Comment_Checkbox_Class extends Yikes_Easy_MC_Checkbox_Integration_Class {
15
16
	/**
17
	 * The integration type.
18
	 *
19
	 * @var string $type
20
	 */
21
	protected $type = 'comment_form';
22
23
	/**
24
	 * Constructor.
25
	 */
26
	public function __construct() {
27
		add_action( 'comment_post', array( $this, 'subscribe_from_comment' ), 40, 3 );
28
		add_action( 'init', array( $this, 'init' ) );
29
	}
30
31
	/**
32
	 * Allows us to filter the filter that we're adding.
33
	 */
34
	public function init() {
35
36
		/**
37
		 * Decide the placement of the subscription checkbox. Default ('comment_form_field_comment') is after the "Comment" box.
38
		 *
39
		 * @return string The name of a WP comment field's filter
40
		 */
41
		$checkbox_placement = apply_filters( 'yikes-mailchimp-wp-comment-integration-placement', 'comment_form_field_comment' );
42
43
		add_filter( $checkbox_placement, array( $this, 'output_checkbox' ), 10, 1 );
44
	}
45
46
	/**
47
	 * Outputs a checkbox, if user is not already subscribed
48
	 *
49
	 * @param string $comment_field The content of the comment textarea field.
50
	 */
51
	public function output_checkbox( $comment_field ) {
52
		if ( $this->is_user_already_subscribed( $this->type ) ) {
53
			return $comment_field;
54
		}
55
56
		return $comment_field . $this->yikes_get_checkbox();
57
	}
58
59
	/**
60
	 * Subscribe the user if they so chose.
61
	 *
62
	 * @param int    $comment_id       The comment's ID.
63
	 * @param string $comment_approved The comment's status.
64
	 * @param array  $comment_data     The comment data.
65
	 */
66
	public function subscribe_from_comment( $comment_id, $comment_approved, $comment_data ) {
67
		if ( false === $this->was_checkbox_checked( $this->type ) ) {
68
			return false;
69
		}
70
71
		// Is this a spam comment?
72
		if ( 'spam' === $comment_approved ) {
73
			return false;
74
		}
75
76
		// Create merge variables based on comment data.
77
		$merge_vars = array(
78
			'FNAME'    => $comment_data['comment_author'],
79
			'OPTIN_IP' => $comment_data['comment_author_IP'],
80
		);
81
82
		$addl_vars = apply_filters( 'yikes_mailchimp_checkbox_integration_additional_vars', array( 'comment_data' => $comment_data ), $this->type );
83
84
		// Subscribe the user.
85
		$this->subscribe_user_integration( $comment_data['comment_author_email'], $this->type, $merge_vars, $addl_vars );
86
	}
87
}
88
$yikes_easy_mc_comment_checkbox_class = new Yikes_Easy_MC_Comment_Checkbox_Class();
89