Completed
Push — mailchimp/introduce-shortcode ( b95af1...0c8836 )
by
unknown
06:34
created

Jetpack_Email_Subscribe::get_block_properties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 32
rs 9.408
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Jetpack_Email_Subscribe::register_shortcode() 0 3 1
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 $option_name = 'jetpack_mailchimp';
23
24
	private static $block_name = 'mailchimp';
25
26
	private static $instance;
27
28
	private static $version = '1.0';
29
30
	private function __construct() {
31
	}
32
33
	/**
34
	 * This follows a classic singleton pattern.
35
	 *
36
	 * @return Jetpack_Email_Subscribe|null
37
	 */
38
	public static function get_instance() {
39
		// Do not load this at all if it's neither a WPCOM or a connected JP site.
40
		if ( ! ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_active() ) ) {
41
			return null;
42
		}
43
44
		if ( ! self::$instance ) {
45
			self::$instance = new self();
46
			self::$instance->register_init_hook();
47
		}
48
49
		return self::$instance;
50
	}
51
52
	private function register_scripts_and_styles() {
53
		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 );
54
		wp_register_style( 'jetpack-email-subscribe', plugins_url( '/css/jetpack-email-subscribe.css', __FILE__ ), array(), self::$version );
55
	}
56
57
	private function register_init_hook() {
58
		add_action( 'init', array( $this, 'init_hook_action' ) );
59
		add_action( 'jetpack_options_whitelist', array( $this, 'filter_whitelisted_options' ), 10, 1 );
60
		add_action( 'jetpack_blocks_to_register', array( $this, 'prevent_jetpack_register_block' ), 10, 1 );
61
	}
62
63
	public function prevent_jetpack_register_block( $blocks ) {
64
		return array_filter( $blocks, array( $this, 'filter_out_this_block' ) );
65
	}
66
67
	public function filter_out_this_block( $block_name ) {
68
		return ( $block_name !== self::$block_name );
69
	}
70
71
	public function filter_whitelisted_options( $options ) {
72
		$options[] = self::$option_name;
73
		return $options;
74
	}
75
76
	private function register_shortcode() {
77
		add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
78
	}
79
80
	private function register_gutenberg_block() {
81
		if ( Jetpack_Gutenberg::is_gutenberg_available() ) {
82
			register_block_type( 'jetpack/' . self::$block_name, array(
83
				'attributes' => array(
84
					'title' => array(
85
						'type' => 'string',
86
					),
87
					'email_placeholder' => array(
88
						'type' => 'string',
89
					),
90
					'submit_label' => array(
91
						'type' => 'string',
92
					),
93
					'consent_text' => array(
94
						'type' => 'string',
95
					),
96
					'processing_label' => array(
97
						'type' => 'string',
98
					),
99
					'success_label' => array(
100
						'type' => 'string',
101
					),
102
					'error_label' => array(
103
						'type' => 'string',
104
					),
105
					'className' => array(
106
						'type' => 'string',
107
					),
108
				),
109
				'style' => 'jetpack-email-subscribe',
110
				'render_callback' => array( $this, 'parse_shortcode' ),
111
			) );
112
			jetpack_register_block( self::$block_name, array(), array( 'available' => true ) );
113
		}
114
	}
115
116
	public function init_hook_action() {
117
		$this->register_scripts_and_styles();
118
		$this->register_shortcode();
119
		$this->register_gutenberg_block();
120
	}
121
122
	private function get_blog_id() {
123
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
124
			return get_current_blog_id();
125
		}
126
127
		return Jetpack_Options::get_option( 'id' );
128
	}
129
130 View Code Duplication
	private function is_set_up() {
131
		$option = get_option( self::$option_name );
132
		if ( ! $option ) {
133
			return false;
134
		}
135
		$data = json_decode( $option, true );
136
		if ( isset( $data['follower_list_id'], $data['follower_list_id'] ) ) {
137
			return true;
138
		}
139
		return false;
140
	}
141
142
	private function get_site_slug() {
143
		if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) {
144
			return Jetpack::build_raw_urls( home_url() );
145
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
146
			return WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
147
		}
148
		return '';
149
	}
150
151
	public function parse_shortcode( $attrs ) {
152
		// Lets check if everything is set up.
153
		if (
154
			! $this->is_set_up() &&
155
			current_user_can( 'edit_posts' )
156
		) {
157
			return sprintf(
158
				'<div class="components-placeholder">
159
					<div class="components-placeholder__label">%s</div>
160
					<div class="components-placeholder__instructions">%s</div>
161
					<a class="components-button is-button" href="https://wordpress.com/sharing/%s" target="_blank">%s</a>
162
				</div>',
163
				__( 'MailChimp', 'jetpack' ),
164
				__( 'You need to connect your MailChimp account and choose a list in order to start collecting Email subscribers.', 'jetpack' ),
165
				$this->get_site_slug(),
166
				__( 'Set up MailChimp ', 'jetpack' )
167
			);
168
		}
169
170
		// We allow for overriding the presentation labels.
171
		$data = shortcode_atts(
172
			array(
173
				'blog_id'           => $this->get_blog_id(),
174
				'title'             => __( 'Join my email list', 'jetpack' ),
175
				'email_placeholder' => __( 'Enter your email', 'jetpack' ),
176
				'submit_label'      => __( 'Join My Email List', 'jetpack' ),
177
				'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' ),
178
				'processing_label'  => __( 'Processing...', 'jetpack' ),
179
				'success_label'     => __( 'Success! You\'ve been added to the list.', 'jetpack' ),
180
				'error_label'       => __( "Oh no! Unfortunately there was an error.\nPlease try reloading this page and adding your email once more.", 'jetpack' ),
181
				'classname'         => self::$css_classname_prefix,
182
				'dom_id'            => uniqid( self::$css_classname_prefix . '_', false ),
183
			),
184
			is_array( $attrs ) ? array_filter( $attrs ) : array()
185
		);
186
187
		if ( ! wp_script_is( 'jetpack-email-subscribe', 'enqueued' ) ) {
188
			wp_enqueue_script( 'jetpack-email-subscribe' );
189
		}
190
191
		if ( ! wp_style_is( 'jetpack-email-subscribe', 'enqueue' ) ) {
192
			wp_enqueue_style( 'jetpack-email-subscribe' );
193
		}
194
195
		wp_add_inline_script(
196
			'jetpack-email-subscribe',
197
			sprintf(
198
				"try{JetpackEmailSubscribe.activate( '%s', '%s', '%s' );}catch(e){}",
199
				esc_js( $data['blog_id'] ),
200
				esc_js( $data['dom_id'] ),
201
				esc_js( $data['classname'] )
202
			)
203
		);
204
205
		return sprintf(
206
			'<div class="%1$s" id="%2$s">
207
				<h2>%3$s</h2>
208
				<form>
209
					<input type="email" class="%1$s-email" required placeholder="%4$s">
210
					<button type="submit" class="%1$s-submit">%6$s</button>
211
					<label class="%1$s-consent-label">
212
					    <small>%5$s</small>
213
					</label>
214
				</form>
215
				<div class="%1$s-processing">%7$s</div>
216
				<div class="%1$s-success">%8$s</div>
217
				<div class="%1$s-error">%9$s</div>
218
			</div>',
219
			esc_attr( $data['classname'] ),
220
			esc_attr( $data['dom_id'] ),
221
			esc_html( $data['title'] ),
222
			esc_html( $data['email_placeholder'] ),
223
			esc_html( $data['consent_text'] ),
224
			esc_html( $data['submit_label'] ),
225
			nl2br( esc_html( $data['processing_label'] ) ),
226
			nl2br( esc_html( $data['success_label'] ) ),
227
			nl2br( esc_html( $data['error_label'] ) )
228
		);
229
	}
230
231
}
232
233
Jetpack_Email_Subscribe::get_instance();
234