Completed
Push — mailchimp/introduce-shortcode ( 8ffea5...75e3e5 )
by
unknown
06:48
created

Jetpack_Email_Subscribe::get_blog_id()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
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
9
/**
10
 * Class Jetpack_Email_Subscribe
11
 * This class encapsulates shortcode for subscribing to a MailChimp list.
12
 * It displays a simple signup form that gets an email address from the user and signs him for a list
13
 * selected in "Sharing" section in calypso.
14
 * Other Email services can be implemented as well in the future.
15
 */
16
class Jetpack_Email_Subscribe {
17
18
	private static $shortcode = 'jetpack-email-subscribe';
19
20
	private static $css_classname_prefix = 'jetpack-email-subscribe';
21
22
	private static $instance;
23
24
	private static $version = '1.0';
25
26
	private function __construct() {
27
	}
28
29
	/**
30
	 * This follows a classic singleton pattern.
31
	 *
32
	 * @return Jetpack_Email_Subscribe|null
33
	 */
34
	public static function get_instance() {
35
		// Do not load this at all if it's neither a WPCOM or a connected JP site.
36
		if ( ! ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_active() ) ) {
37
			return null;
38
		}
39
40
		if ( ! self::$instance ) {
41
			self::$instance = new self();
42
			self::$instance->register_init_hook();
43
		}
44
45
		return self::$instance;
46
	}
47
48
	private function register_scripts_and_styles() {
49
		wp_register_script( 'jetpack-email-subscribe', Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/jetpack-email-subscribe.min.js', 'modules/shortcodes/js/jetpack-email-subscribe.js' ), array( 'jquery' ), self::$version );
50
		wp_register_style( 'jetpack-email-subscribe', plugins_url( '/css/jetpack-email-subscribe.css', __FILE__ ), array(), self::$version );
51
	}
52
53
	private function register_init_hook() {
54
		add_action( 'init', array( $this, 'init_hook_action' ) );
55
	}
56
57
	private function register_shortcode() {
58
		add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
59
	}
60
61
	public function init_hook_action() {
62
		$this->register_scripts_and_styles();
63
		$this->register_shortcode();
64
	}
65
66
	private function get_blog_id() {
67
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
68
			return get_current_blog_id();
69
		}
70
71
		return Jetpack_Options::get_option( 'id' );
72
	}
73
74
75
	public function parse_shortcode( $attrs ) {
76
		// We allow for overriding the presentation labels.
77
		$data = shortcode_atts(
78
			array(
79
				'blog_id'           => $this->get_blog_id(),
80
				'title'             => __( 'Join my email list', 'jetpack' ),
81
				'email_placeholder' => __( 'Enter your email', 'jetpack' ),
82
				'submit_label'      => __( 'Join My Email List', 'jetpack' ),
83
				'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.', 'jetpack' ),
84
				'processing_label'  => __( 'Processing...', 'jetpack' ),
85
				'success_label'     => __( 'Success! You\'ve been added to the list.', 'jetpack' ),
86
				'error_label'       => __( "Oh no! Unfortunately there was an error.\nPlease try reloading this page and adding your email once more.", 'jetpack' ),
87
				'classname'         => self::$css_classname_prefix,
88
				'dom_id'            => uniqid( self::$css_classname_prefix . '_', false ),
89
			),
90
			$attrs
91
		);
92
93
		if ( ! wp_script_is( 'jetpack-email-subscribe', 'enqueued' ) ) {
94
			wp_enqueue_script( 'jetpack-email-subscribe' );
95
		}
96
97
		if ( ! wp_style_is( 'jetpack-email-subscribe', 'enqueue' ) ) {
98
			wp_enqueue_style( 'jetpack-email-subscribe' );
99
		}
100
101
		wp_add_inline_script(
102
			'jetpack-email-subscribe',
103
			sprintf(
104
				"try{JetpackEmailSubscribe.activate( '%s', '%s', '%s' );}catch(e){}",
105
				esc_js( $data['blog_id'] ),
106
				esc_js( $data['dom_id'] ),
107
				esc_js( $data['classname'] )
108
			)
109
		);
110
111
		return sprintf(
112
			'<div class="%1$s" id="%2$s">
113
				<h2>%3$s</h2>
114
				<form>
115
					<input type="email" class="%1$s-email" required placeholder="%4$s">
116
					<button type="submit" class="%1$s-submit">%6$s</button>
117
					<label class="%1$s-consent-label">
118
					    <small>%5$s</small>
119
					</label>
120
				</form>
121
				<div class="%1$s-processing">%7$s</div>
122
				<div class="%1$s-success">%8$s</div>
123
				<div class="%1$s-error">%9$s</div>
124
			</div>',
125
			esc_attr( $data['classname'] ),
126
			esc_attr( $data['dom_id'] ),
127
			esc_html( $data['title'] ),
128
			esc_html( $data['email_placeholder'] ),
129
			esc_html( $data['consent_text'] ),
130
			esc_html( $data['submit_label'] ),
131
			nl2br( esc_html( $data['processing_label'] ) ),
132
			nl2br( esc_html( $data['success_label'] ) ),
133
			nl2br( esc_html( $data['error_label'] ) )
134
		);
135
	}
136
137
}
138
139
Jetpack_Email_Subscribe::get_instance();
140