Completed
Push — mailchimp/introduce-shortcode ( 3708db )
by
unknown
09:29
created

Email_Subscribe::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * WARNING: This file is distributed verbatim in Jetpack.
4
 * There should be nothing WordPress.com specific in this file.
5
 *
6
 * @hide-in-jetpack
7
 */
8
class Email_Subscribe {
9
10
	static $shortcode = 'jetpack-email-subscribe';
11
12
	static $css_classname_prefix = 'jetpack-email-subscribe';
13
14
	// Classic singleton pattern:
15
	private static $instance;
16
	private function __construct() {}
17
	static function getInstance() {
18
		if ( ! self::$instance ) {
19
			self::$instance = new self();
20
			self::$instance->register_init_hook();
21
		}
22
		return self::$instance;
23
	}
24
25
	private function register_scripts_and_styles() {
26
		wp_register_script( 'jetpack-email-subscribe', plugins_url( '/jetpack-email-subscribe.js', __FILE__ ), array( 'jquery' ) );
27
		wp_register_style( 'jetpack-email-subscribe', plugins_url( '/jetpack-email-subscribe.css', __FILE__ ) );
28
	}
29
30
	private function register_init_hook() {
31
		add_action( 'init', array( $this, 'init_hook_action' ) );
32
	}
33
34
	private function register_shortcode() {
35
		add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
36
	}
37
38
	public function init_hook_action() {
39
		$this->register_scripts_and_styles();
40
		$this->register_shortcode();
41
	}
42
43
	function get_blog_id() {
44
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
45
			return get_current_blog_id();
46
		}
47
48
		return Jetpack_Options::get_option( 'id' );
49
	}
50
51
52
	function parse_shortcode( $attrs, $content = false ) {
53
		// We allow for overriding the presentation labels
54
		$data = shortcode_atts( array(
55
			'blog_id'     => $this->get_blog_id(),
56
			'title'       => __( 'Join my email list' ),
57
			'email_placeholder' => __( 'Enter your email' ),
58
			'submit_label' => __( 'Join My Email List' ),
59
			'consent_text' => __( 'By clicking submit, you agree to share your email address with the site owner and MailChimp to receive marketing, updates, and other emails from the site owner. Use the unsubscribe link in those emails to opt out at any time.' ),
60
			'processing_label' => __( 'Processing...' ),
61
			'success_label' => __( 'Success! You\'ve been added to the list.' ),
62
			'error_label' => __( "Oh no! Unfortunately there was an error.\nPlease try reloading this page and adding your email once more." ),
63
			'classname' => self::$css_classname_prefix,
64
			'dom_id' => uniqid( self::$css_classname_prefix . '_', false ),
65
		), $attrs );
66
67
68
		if ( ! wp_script_is( 'jetpack-email-subscribe', 'enqueued' ) ) {
69
			wp_enqueue_script( 'jetpack-email-subscribe' );
70
		}
71
72
		if( ! wp_style_is( 'jetpack-email-subscribe', 'enqueue' ) ) {
73
			wp_enqueue_style( 'jetpack-email-subscribe' );
74
		}
75
76
		wp_add_inline_script( 'jetpack-email-subscribe', sprintf(
77
			"try{JetpackEmailSubscribe.activate( '%s', '%s', '%s' );}catch(e){}",
78
			esc_js( $data['blog_id'] ),
79
			esc_js( $data['dom_id'] ),
80
			esc_js( $data['classname'] )
81
		) );
82
83
		return sprintf(
84
			'<div class="%1$s" id="%2$s">
85
				<h2>%3$s</h2>
86
				<form>
87
					<input type="email" class="%1$s-email" required placeholder="%4$s">
88
					<button type="submit" class="%1$s-submit">%6$s</button>
89
					<label class="%1$s-consent-label">
90
					    <small>%5$s</small>
91
					</label>
92
				</form>
93
				<div class="%1$s-processing">%7$s</div>
94
				<div class="%1$s-success">%8$s</div>
95
				<div class="%1$s-error">%9$s</div>
96
			</div>',
97
			esc_attr( $data['classname'] ),
98
			esc_attr( $data['dom_id'] ),
99
			esc_html( $data['title'] ),
100
			esc_html( $data['email_placeholder'] ),
101
			esc_html( $data['consent_text'] ),
102
			esc_html( $data['submit_label'] ),
103
			nl2br( esc_html( $data['processing_label'] ) ),
104
			nl2br( esc_html( $data['success_label'] ) ),
105
			nl2br( esc_html( $data['error_label'] ) )
106
		);
107
	}
108
109
}
110
Email_Subscribe::getInstance();
111