Completed
Push — add/photon-thumbnail-disabled-... ( 2ec753...128652 )
by
unknown
20:24 queued 13:34
created

Jetpack_Email_Subscribe::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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