Completed
Push — mailchimp/introduce-shortcode ( 9b8adc...591a69 )
by
unknown
07:51
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
class Jetpack_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
		// Do not load this at all if it's neither a WPCOM or a connected JP site.
19
		if ( ! ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_active() ) ) {
20
			return null;
21
		}
22
23
		if ( ! self::$instance ) {
24
			self::$instance = new self();
25
			self::$instance->register_init_hook();
26
		}
27
		return self::$instance;
28
	}
29
30
	private function register_scripts_and_styles() {
31
		wp_register_script( 'jetpack-email-subscribe', plugins_url( '/js/jetpack-email-subscribe.js', __FILE__ ), array( 'jquery' ) );
32
		wp_register_style( 'jetpack-email-subscribe', plugins_url( '/js/jetpack-email-subscribe.css', __FILE__ ) );
33
	}
34
35
	private function register_init_hook() {
36
		add_action( 'init', array( $this, 'init_hook_action' ) );
37
	}
38
39
	private function register_shortcode() {
40
		add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
41
	}
42
43
	public function init_hook_action() {
44
		$this->register_scripts_and_styles();
45
		$this->register_shortcode();
46
	}
47
48
	function get_blog_id() {
49
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
50
			return get_current_blog_id();
51
		}
52
53
		return Jetpack_Options::get_option( 'id' );
54
	}
55
56
57
	function parse_shortcode( $attrs, $content = false ) {
58
		// We allow for overriding the presentation labels
59
		$data = shortcode_atts( array(
60
			'blog_id'     => $this->get_blog_id(),
61
			'title'       => __( 'Join my email list', 'jetpack' ),
62
			'email_placeholder' => __( 'Enter your email', 'jetpack' ),
63
			'submit_label' => __( 'Join My Email List', 'jetpack' ),
64
			'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' ),
65
			'processing_label' => __( 'Processing...', 'jetpack' ),
66
			'success_label' => __( 'Success! You\'ve been added to the list.', 'jetpack' ),
67
			'error_label' => __( "Oh no! Unfortunately there was an error.\nPlease try reloading this page and adding your email once more.", 'jetpack' ),
68
			'classname' => self::$css_classname_prefix,
69
			'dom_id' => uniqid( self::$css_classname_prefix . '_', false ),
70
		), $attrs );
71
72
73
		if ( ! wp_script_is( 'jetpack-email-subscribe', 'enqueued' ) ) {
74
			wp_enqueue_script( 'jetpack-email-subscribe' );
75
		}
76
77
		if( ! wp_style_is( 'jetpack-email-subscribe', 'enqueue' ) ) {
78
			wp_enqueue_style( 'jetpack-email-subscribe' );
79
		}
80
81
		wp_add_inline_script( 'jetpack-email-subscribe', sprintf(
82
			"try{JetpackEmailSubscribe.activate( '%s', '%s', '%s' );}catch(e){}",
83
			esc_js( $data['blog_id'] ),
84
			esc_js( $data['dom_id'] ),
85
			esc_js( $data['classname'] )
86
		) );
87
88
		return sprintf(
89
			'<div class="%1$s" id="%2$s">
90
				<h2>%3$s</h2>
91
				<form>
92
					<input type="email" class="%1$s-email" required placeholder="%4$s">
93
					<button type="submit" class="%1$s-submit">%6$s</button>
94
					<label class="%1$s-consent-label">
95
					    <small>%5$s</small>
96
					</label>
97
				</form>
98
				<div class="%1$s-processing">%7$s</div>
99
				<div class="%1$s-success">%8$s</div>
100
				<div class="%1$s-error">%9$s</div>
101
			</div>',
102
			esc_attr( $data['classname'] ),
103
			esc_attr( $data['dom_id'] ),
104
			esc_html( $data['title'] ),
105
			esc_html( $data['email_placeholder'] ),
106
			esc_html( $data['consent_text'] ),
107
			esc_html( $data['submit_label'] ),
108
			nl2br( esc_html( $data['processing_label'] ) ),
109
			nl2br( esc_html( $data['success_label'] ) ),
110
			nl2br( esc_html( $data['error_label'] ) )
111
		);
112
	}
113
114
}
115
Jetpack_Email_Subscribe::getInstance();
116