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
|
|
|
private function register_gutenberg_block() { |
67
|
|
|
jetpack_register_block( self::$block_name, array( |
68
|
|
|
'attributes' => array( |
69
|
|
|
'title' => array( |
70
|
|
|
'type' => 'string', |
71
|
|
|
), |
72
|
|
|
'email_placeholder' => array( |
73
|
|
|
'type' => 'string', |
74
|
|
|
), |
75
|
|
|
'submit_label' => array( |
76
|
|
|
'type' => 'string', |
77
|
|
|
), |
78
|
|
|
'consent_text' => array( |
79
|
|
|
'type' => 'string', |
80
|
|
|
), |
81
|
|
|
'processing_label' => array( |
82
|
|
|
'type' => 'string', |
83
|
|
|
), |
84
|
|
|
'success_label' => array( |
85
|
|
|
'type' => 'string', |
86
|
|
|
), |
87
|
|
|
'error_label' => array( |
88
|
|
|
'type' => 'string', |
89
|
|
|
), |
90
|
|
|
'className' => array( |
91
|
|
|
'type' => 'string', |
92
|
|
|
), |
93
|
|
|
), |
94
|
|
|
'style' => 'jetpack-email-subscribe', |
95
|
|
|
'render_callback' => array( $this, 'parse_shortcode' ), |
96
|
|
|
) ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function init_hook_action() { |
100
|
|
|
$this->register_scripts_and_styles(); |
101
|
|
|
$this->register_shortcode(); |
102
|
|
|
$this->register_gutenberg_block(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function get_blog_id() { |
106
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
107
|
|
|
return get_current_blog_id(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return Jetpack_Options::get_option( 'id' ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
private function is_set_up() { |
114
|
|
|
$option = get_option( self::$option_name ); |
115
|
|
|
if ( ! $option ) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
$data = json_decode( $option, true ); |
119
|
|
|
if ( isset( $data['follower_list_id'], $data['follower_list_id'] ) ) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function get_site_slug() { |
126
|
|
|
if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'build_raw_urls' ) ) { |
127
|
|
|
return Jetpack::build_raw_urls( home_url() ); |
128
|
|
|
} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) { |
129
|
|
|
return WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() ); |
130
|
|
|
} |
131
|
|
|
return ''; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function parse_shortcode( $attrs ) { |
135
|
|
|
// Lets check if everything is set up. |
136
|
|
|
if ( |
137
|
|
|
! $this->is_set_up() && |
138
|
|
|
current_user_can( 'edit_posts' ) |
139
|
|
|
) { |
140
|
|
|
return sprintf( |
141
|
|
|
'<div class="components-placeholder"> |
142
|
|
|
<div class="components-placeholder__label">%s</div> |
143
|
|
|
<div class="components-placeholder__instructions">%s</div> |
144
|
|
|
<a class="components-button is-button" href="https://wordpress.com/sharing/%s" target="_blank">%s</a> |
145
|
|
|
</div>', |
146
|
|
|
__( 'MailChimp form', 'jetpack' ), |
147
|
|
|
__( 'You need to connect your MailChimp account and choose a list in order to start collecting Email subscribers.', 'jetpack' ), |
148
|
|
|
$this->get_site_slug(), |
149
|
|
|
__( 'Set up MailChimp form', 'jetpack' ) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// We allow for overriding the presentation labels. |
154
|
|
|
$data = shortcode_atts( |
155
|
|
|
array( |
156
|
|
|
'title' => '', |
157
|
|
|
'email_placeholder' => __( 'Enter your email', 'jetpack' ), |
158
|
|
|
'submit_label' => __( 'Join My Email List', 'jetpack' ), |
159
|
|
|
'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' ), |
160
|
|
|
'processing_label' => __( 'Processing...', 'jetpack' ), |
161
|
|
|
'success_label' => __( 'Success! You\'ve been added to the list.', 'jetpack' ), |
162
|
|
|
'error_label' => __( "Oh no! Unfortunately there was an error.\nPlease try reloading this page and adding your email once more.", 'jetpack' ), |
163
|
|
|
), |
164
|
|
|
is_array( $attrs ) ? array_filter( $attrs ) : array() |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
// We don't allow users to change these parameters: |
168
|
|
|
$data = array_merge( $data, array( |
169
|
|
|
'blog_id' => $this->get_blog_id(), |
170
|
|
|
'classname' => self::$css_classname_prefix, |
171
|
|
|
'dom_id' => uniqid( self::$css_classname_prefix . '_', false ), |
172
|
|
|
) ); |
173
|
|
|
|
174
|
|
|
if ( ! wp_script_is( 'jetpack-email-subscribe', 'enqueued' ) ) { |
175
|
|
|
wp_enqueue_script( 'jetpack-email-subscribe' ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ( ! wp_style_is( 'jetpack-email-subscribe', 'enqueue' ) ) { |
179
|
|
|
wp_enqueue_style( 'jetpack-email-subscribe' ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
wp_add_inline_script( |
183
|
|
|
'jetpack-email-subscribe', |
184
|
|
|
sprintf( |
185
|
|
|
"try{JetpackEmailSubscribe.activate( '%s', '%s', '%s' );}catch(e){}", |
186
|
|
|
esc_js( $data['blog_id'] ), |
187
|
|
|
esc_js( $data['dom_id'] ), |
188
|
|
|
esc_js( $data['classname'] ) |
189
|
|
|
) |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
return sprintf( |
193
|
|
|
'<div class="%1$s" id="%2$s"> |
194
|
|
|
%3$s |
195
|
|
|
<form> |
196
|
|
|
<input type="email" class="%1$s-email" required placeholder="%4$s"> |
197
|
|
|
<button type="submit" class="%1$s-submit">%6$s</button> |
198
|
|
|
<label class="%1$s-consent-label"> |
199
|
|
|
<small>%5$s</small> |
200
|
|
|
</label> |
201
|
|
|
</form> |
202
|
|
|
<div class="%1$s-processing">%7$s</div> |
203
|
|
|
<div class="%1$s-success">%8$s</div> |
204
|
|
|
<div class="%1$s-error">%9$s</div> |
205
|
|
|
</div>', |
206
|
|
|
esc_attr( $data['classname'] ), |
207
|
|
|
esc_attr( $data['dom_id'] ), |
208
|
|
|
$data['title'] ? '<h3>' . esc_html( $data['title'] ) . '</h3>' : '', |
209
|
|
|
esc_html( $data['email_placeholder'] ), |
210
|
|
|
esc_html( $data['consent_text'] ), |
211
|
|
|
esc_html( $data['submit_label'] ), |
212
|
|
|
nl2br( esc_html( $data['processing_label'] ) ), |
213
|
|
|
nl2br( esc_html( $data['success_label'] ) ), |
214
|
|
|
nl2br( esc_html( $data['error_label'] ) ) |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
Jetpack_Email_Subscribe::get_instance(); |
221
|
|
|
|