PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 14.5.1
Jetpack – WP Security, Backup, Speed, & Growth v14.5.1
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / subscriptions / views.php
jetpack / modules / subscriptions Last commit date
newsletter-widget 1 year ago subscribe-floating-button 1 year ago subscribe-modal 1 year ago subscribe-overlay 1 year ago class-settings.php 1 year ago jetpack-user-content-link-redirection.php 1 year ago readme.md 7 years ago subscriptions.css 7 years ago views.php 1 year ago
views.php
1059 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
4
5 /**
6 * Jetpack_Subscriptions_Widget main view class.
7 */
8 class Jetpack_Subscriptions_Widget extends WP_Widget {
9
10 const ID_BASE = 'blog_subscription';
11
12 /**
13 * Track number of rendered Subscription widgets. The count is used for class names and widget IDs.
14 *
15 * @var int
16 */
17 public static $instance_count = 0;
18
19 /**
20 * When printing the submit button, what tags are allowed.
21 *
22 * @var array
23 */
24 public static $allowed_html_tags_for_submit_button = array(
25 'br' => array(),
26 's' => array(),
27 'strong' => array(),
28 'em' => array(),
29 );
30
31 /**
32 * Use this variable when printing the message after submitting an email in subscription widgets
33 *
34 * @var array what tags are allowed
35 */
36 public static $allowed_html_tags_for_message = array(
37 'a' => array(
38 'href' => array(),
39 'title' => array(),
40 'rel' => array(),
41 'target' => array(),
42 ),
43 'br' => array(),
44 );
45
46 /**
47 * Jetpack_Subscriptions_Widget constructor.
48 */
49 public function __construct() {
50 $widget_ops = array(
51 'classname' => 'widget_blog_subscription jetpack_subscription_widget',
52 'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ),
53 'customize_selective_refresh' => true,
54 'show_instance_in_rest' => true,
55 );
56
57 $name = self::is_jetpack() ?
58 /** This filter is documented in modules/widgets/facebook-likebox.php */
59 apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ) :
60 __( 'Follow Blog', 'jetpack' );
61
62 parent::__construct(
63 'blog_subscription',
64 $name,
65 $widget_ops
66 );
67
68 if ( self::is_jetpack() &&
69 (
70 is_active_widget( false, false, $this->id_base ) ||
71 is_active_widget( false, false, 'monster' ) ||
72 is_customize_preview()
73 )
74 ) {
75 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
76 }
77
78 add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
79 }
80
81 /**
82 * Remove the "Blog Subscription" widget from the Legacy Widget block
83 *
84 * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block.
85 * @return array $widget_types New list of widgets that will be removed.
86 */
87 public function hide_widget_in_block_editor( $widget_types ) {
88 $widget_types[] = self::ID_BASE;
89 return $widget_types;
90 }
91
92 /**
93 * Enqueue the form's CSS.
94 *
95 * @since 4.5.0
96 */
97 public function enqueue_style() {
98 wp_register_style(
99 'jetpack-subscriptions',
100 plugins_url( 'subscriptions.css', __FILE__ ),
101 array(),
102 JETPACK__VERSION
103 );
104 wp_enqueue_style( 'jetpack-subscriptions' );
105 }
106
107 /**
108 * Renders a full widget either within the context of WordPress widget, or in response to a shortcode.
109 *
110 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
111 * @param array $instance The settings for the particular instance of the widget.
112 */
113 public function widget( $args, $instance ) {
114 if ( self::is_wpcom() && ! self::wpcom_has_status_message() && self::is_current_user_subscribed() ) {
115 return null;
116 }
117 if ( self::is_jetpack() &&
118 /** This filter is documented in \Automattic\Jetpack\Forms\ContactForm\Contact_Form */
119 false === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
120 ) {
121 $subscribe_email = '';
122 } else {
123 $current_user = wp_get_current_user();
124 if ( ! empty( $current_user->user_email ) ) {
125 $subscribe_email = esc_attr( $current_user->user_email );
126 } else {
127 $subscribe_email = '';
128 }
129 }
130
131 $stats_action = self::is_jetpack() ? 'jetpack_subscriptions' : 'follow_blog';
132 /** This action is documented in modules/widgets/gravatar-profile.php */
133 do_action( 'jetpack_stats_extra', 'widget_view', $stats_action );
134
135 $after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
136 $before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
137 $instance = wp_parse_args( (array) $instance, static::defaults() );
138
139 echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
140
141 ++self::$instance_count;
142
143 self::render_widget_title( $args, $instance );
144
145 self::render_widget_status_messages( $instance );
146
147 self::render_widget_subscription_form( $args, $instance, $subscribe_email );
148
149 echo "\n" . $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
150 }
151
152 /**
153 * Prints the widget's title. If show_only_email_and_button is true, we will not show a title.
154 *
155 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
156 * @param array $instance The settings for the particular instance of the widget.
157 */
158 public static function render_widget_title( $args, $instance ) {
159 $show_only_email_and_button = $instance['show_only_email_and_button'];
160 $before_title = isset( $args['before_title'] ) ? $args['before_title'] : '';
161 $after_title = isset( $args['after_title'] ) ? $args['after_title'] : '';
162 if ( self::is_wpcom() && ! $show_only_email_and_button ) {
163 if ( self::is_current_user_subscribed() ) {
164 if ( ! empty( $instance['title_following'] ) ) {
165 printf(
166 '%1$s<label for="subscribe-field%2$s">%3$s</label>%4$s%5$s',
167 $before_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
168 ( self::$instance_count > 1 ? '-' . (int) self::$instance_count : '' ),
169 esc_html( $instance['title_following'] ),
170 $after_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
171 "\n"
172 );
173 }
174 } elseif ( ! empty( $instance['title'] ) ) {
175 printf(
176 '%1$s<label for="subscribe-field%2$s">%3$s</label>%4$s%5$s',
177 $before_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
178 ( self::$instance_count > 1 ? '-' . (int) self::$instance_count : '' ),
179 esc_html( $instance['title'] ),
180 $after_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
181 "\n"
182 );
183 }
184 }
185
186 if ( self::is_jetpack() && empty( $instance['show_only_email_and_button'] ) ) {
187 printf(
188 '%1$s%2$s%3$s%4$s',
189 $before_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
190 esc_html( $instance['title'] ),
191 $after_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
192 "\n"
193 );
194 }
195 }
196
197 /**
198 * Prints the subscription block's status messages after someone has attempted to subscribe.
199 * Either a success message or an error message.
200 *
201 * @param array $instance The settings for the particular instance of the widget.
202 */
203 public static function render_widget_status_messages( $instance ) {
204 if ( self::is_jetpack() && isset( $_GET['subscribe'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Non-sensitive informational output.
205 $success_message = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
206 $subscribers_total = self::fetch_subscriber_count();
207
208 switch ( $_GET['subscribe'] ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended
209 case 'invalid_email':
210 ?>
211 <p class="error"><?php esc_html_e( 'Oops! The email you used is invalid. Please try again.', 'jetpack' ); ?></p>
212 <?php
213 break;
214 case 'opted_out':
215 ?>
216 <p class="error">
217 <?php
218 printf(
219 wp_kses(
220 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link. */
221 __( 'Oops! It seems that the email you used has opted out of subscriptions. You can manage your preferences from the <a href="%1$s" title="%2$s" target="_blank">Subscriptions Manager</a>', 'jetpack' ),
222 self::$allowed_html_tags_for_message
223 ),
224 'https://subscribe.wordpress.com/',
225 esc_attr__( 'Subscriptions Manager', 'jetpack' )
226 );
227 ?>
228 </p>
229 <?php
230 break;
231 case 'already':
232 ?>
233 <p class="error">
234 <?php
235 printf(
236 wp_kses(
237 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link. */
238 __( 'You have already subscribed to this site. Please check your email inbox. You can manage your preferences from the <a href="%1$s" title="%2$s" target="_blank">Subscriptions Manager</a>.', 'jetpack' ),
239 self::$allowed_html_tags_for_message
240 ),
241 'https://subscribe.wordpress.com/',
242 esc_attr__( 'Subscriptions Manager', 'jetpack' )
243 );
244 ?>
245 </p>
246 <?php
247 break;
248 case 'many_pending_subs':
249 ?>
250 <p class="error">
251 <?php
252 printf(
253 wp_kses(
254 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
255 __( 'Oops! It seems you have several subscriptions pending confirmation. You can confirm or unsubscribe some from the <a href="%1$s" title="%2$s" target="_blank" rel="noopener noreferrer">Subscriptions Manager</a> before adding more.', 'jetpack' ),
256 self::$allowed_html_tags_for_message
257 ),
258 'https://subscribe.wordpress.com/',
259 esc_attr__( 'Subscriptions Manager', 'jetpack' )
260 );
261 ?>
262 </p>
263 <?php
264 break;
265 case 'pending':
266 ?>
267 <p class="error">
268 <?php
269 printf(
270 wp_kses(
271 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
272 __( 'It seems you already tried to subscribe with this email, but have not confirmed from the email link we sent. Please check your email inbox to confirm or you can manage your preferences from the <a href="%1$s" title="%2$s" target="_blank" rel="noopener noreferrer">Subscriptions Manager</a>.', 'jetpack' ),
273 self::$allowed_html_tags_for_message
274 ),
275 'https://subscribe.wordpress.com/',
276 esc_attr__( 'Subscriptions Manager', 'jetpack' )
277 );
278 ?>
279 </p>
280 <?php
281 break;
282 case 'success':
283 ?>
284 <div class="success"><?php echo wp_kses( wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total ), $success_message ) ), 'post' ); ?></div>
285 <?php
286 break;
287 default:
288 ?>
289 <p class="error"><?php esc_html_e( 'Oops! There was an error when subscribing. Please try again.', 'jetpack' ); ?></p>
290 <?php
291 break;
292 endswitch;
293 }
294
295 if ( self::is_wpcom() && self::wpcom_has_status_message() ) {
296 global $themecolors;
297 $message = '';
298
299 switch ( $_GET['blogsub'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated
300 case 'confirming':
301 $message = __( 'Thank you! You can now check your email to confirm your subscription.', 'jetpack' );
302 break;
303 case 'blocked':
304 $message = __( 'Sorry but this email has been blocked for this subscription. <a href="https://en.support.wordpress.com/contact/">Contact us</a> if needed.', 'jetpack' );
305 break;
306 case 'flooded':
307 $message = __( 'Oops! It seems you have several subscriptions pending confirmation. You can confirm or unsubscribe some from the <a href="https://subscribe.wordpress.com/">Subscriptions Manager</a> before adding more.', 'jetpack' );
308 break;
309 case 'spammed':
310 /* translators: %s is a URL */
311 $message = sprintf( __( 'Sorry but this email has been blocked. It has too many subscriptions pending confirmation. Please confirm or unsubscribe some from the <a href="%s">Subscriptions Manager</a>.', 'jetpack' ), 'https://subscribe.wordpress.com/' );
312 break;
313 case 'subscribed':
314 $message = __( 'Hey! You were already subscribed.', 'jetpack' );
315 break;
316 case 'pending':
317 $message = __( 'It seems you already tried to subscribe. We just sent you another email so you can confirm the subscription.', 'jetpack' );
318 break;
319 case 'confirmed':
320 $message = __( 'Cool! You are now subscribed. Now you can check your email for more details and how to manage the subscription.', 'jetpack' );
321 break;
322 }
323
324 $border_color = isset( $themecolors['border'] ) ? " #{$themecolors['border']}" : '';
325
326 $redirect_fragment = self::get_redirect_fragment();
327 printf(
328 '<div id="%1$s" class="jetpack-sub-notification">%3$s</div>',
329 esc_attr( $redirect_fragment ),
330 esc_attr( $border_color ),
331 wp_kses_post( $message )
332 );
333 }
334 }
335
336 /**
337 * Generates the redirect fragment used after form submission.
338 *
339 * @param string $id is the specific id that will appear in the redirect fragment. If none is provided self::$instance_count will be used.
340 */
341 protected static function get_redirect_fragment( $id = null ) {
342 if ( $id === null ) {
343 return 'subscribe-blog' . ( self::$instance_count > 1 ? '-' . self::$instance_count : '' );
344 }
345
346 return 'subscribe-blog-' . $id;
347 }
348
349 /**
350 * Renders a form allowing folks to subscribe to the blog.
351 *
352 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
353 * @param array $instance The settings for the particular instance of the widget.
354 * @param string $subscribe_email The email to use to prefill the form.
355 */
356 public static function render_widget_subscription_form( $args, $instance, $subscribe_email ) {
357 $show_only_email_and_button = $instance['show_only_email_and_button'];
358 $show_subscribers_total = (bool) $instance['show_subscribers_total'];
359 $subscribers_total = self::fetch_subscriber_count();
360 $subscribe_text = empty( $instance['show_only_email_and_button'] ) ?
361 wp_kses_post( stripslashes( $instance['subscribe_text'] ) ) :
362 false;
363 $referer = esc_url_raw( ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) );
364 $source = 'widget';
365 $widget_id = ! empty( $args['widget_id'] ) ? $args['widget_id'] : self::$instance_count;
366 $subscribe_button = ! empty( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : $instance['subscribe_button'];
367 $subscribe_placeholder = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
368 $submit_button_classes = isset( $instance['submit_button_classes'] ) ? 'wp-block-button__link ' . $instance['submit_button_classes'] : 'wp-block-button__link';
369 $submit_button_styles = isset( $instance['submit_button_styles'] ) ? $instance['submit_button_styles'] : '';
370 $submit_button_wrapper_styles = isset( $instance['submit_button_wrapper_styles'] ) ? $instance['submit_button_wrapper_styles'] : '';
371 $email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
372 $email_field_styles = isset( $instance['email_field_styles'] ) ? $instance['email_field_styles'] : '';
373
374 // We need to include those in case Jetpack blocks are disabled
375 require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
376 require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
377 $post_access_level = \Jetpack_Memberships::get_post_access_level();
378
379 if ( self::is_wpcom() && ! self::wpcom_has_status_message() ) {
380 global $current_blog;
381
382 $url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : '';
383 $form_id = self::get_redirect_fragment();
384 ?>
385
386 <div class="wp-block-jetpack-subscriptions__container">
387 <form
388 action="<?php echo esc_url( $url ); ?>"
389 method="post"
390 accept-charset="utf-8"
391 data-blog="<?php echo esc_attr( get_current_blog_id() ); ?>"
392 data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>"
393 id="<?php echo esc_attr( $form_id ); ?>"
394 >
395 <?php
396 if ( ! $show_only_email_and_button ) {
397 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
398 echo wpautop( $subscribe_text );
399 }
400 $email_field_id = 'subscribe-field';
401 $email_field_id .= self::$instance_count > 1
402 ? '-' . self::$instance_count
403 : '';
404 $label_field_id = $email_field_id . '-label';
405 ?>
406 <p id="subscribe-email">
407 <label
408 id="<?php echo esc_attr( $label_field_id ); ?>"
409 for="<?php echo esc_attr( $email_field_id ); ?>"
410 class="screen-reader-text"
411 >
412 <?php echo esc_html__( 'Email Address:', 'jetpack' ); ?>
413 </label>
414
415 <?php
416 printf(
417 '<input
418 type="email"
419 name="email"
420 %1$s
421 style="%2$s"
422 placeholder="%3$s"
423 value=""
424 id="%4$s"
425 required
426 />',
427 ( ! empty( $email_field_classes )
428 ? 'class="' . esc_attr( $email_field_classes ) . '"'
429 : ''
430 ),
431 ( ! empty( $email_field_styles )
432 ? esc_attr( $email_field_styles )
433 : 'width: 95%; padding: 1px 10px'
434 ),
435 ( empty( $subscribe_placeholder ) ? esc_attr__( 'Enter your email address', 'jetpack' ) : esc_attr( $subscribe_placeholder ) ),
436 esc_attr( $email_field_id )
437 );
438 ?>
439 </p>
440
441 <p id="subscribe-submit"
442 <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
443 style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
444 <?php } ?>
445 >
446 <input type="hidden" name="action" value="subscribe"/>
447 <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/>
448 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
449 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
450 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
451 <?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?>
452 <button type="submit"
453 <?php if ( ! empty( $submit_button_classes ) ) { ?>
454 class="<?php echo esc_attr( $submit_button_classes ); ?>"
455 <?php } ?>
456 <?php if ( ! empty( $submit_button_styles ) ) { ?>
457 style="<?php echo esc_attr( $submit_button_styles ); ?>"
458 <?php } ?>
459 >
460 <?php
461 echo wp_kses(
462 html_entity_decode( $subscribe_button, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
463 self::$allowed_html_tags_for_submit_button
464 );
465 ?>
466 </button>
467 </p>
468 </form>
469 <?php if ( $show_subscribers_total && $subscribers_total > 0 ) { ?>
470 <div class="wp-block-jetpack-subscriptions__subscount">
471 <?php
472 echo esc_html( Jetpack_Memberships::get_join_others_text( $subscribers_total ) );
473 ?>
474 </div>
475 <?php } ?>
476 </div>
477 <?php
478 }
479
480 if ( self::is_jetpack() ) {
481 /**
482 * Filter the subscription form's ID prefix.
483 *
484 * @module subscriptions
485 *
486 * @since 2.7.0
487 *
488 * @param string subscribe-field Subscription form field prefix.
489 * @param int $widget_id Widget ID.
490 */
491 $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
492
493 $form_id = self::get_redirect_fragment( $widget_id );
494 ?>
495 <div class="wp-block-jetpack-subscriptions__container">
496 <form action="#" method="post" accept-charset="utf-8" id="<?php echo esc_attr( $form_id ); ?>"
497 data-blog="<?php echo esc_attr( \Jetpack_Options::get_option( 'id' ) ); ?>"
498 data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>" >
499 <?php
500 if ( $subscribe_text && ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Non-sensitive informational output.
501 ?>
502 <div id="subscribe-text"><?php echo wp_kses( wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total ), $subscribe_text ) ), 'post' ); ?></div>
503 <?php
504 }
505
506 if ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display of unsubmitted form.
507 ?>
508 <p id="subscribe-email">
509 <label id="jetpack-subscribe-label"
510 class="screen-reader-text"
511 for="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>">
512 <?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?>
513 </label>
514 <input type="email" name="email" required="required"
515 <?php if ( ! empty( $email_field_classes ) ) { ?>
516 class="<?php echo esc_attr( $email_field_classes ); ?> required"
517 <?php } ?>
518 <?php if ( ! empty( $email_field_styles ) ) { ?>
519 style="<?php echo esc_attr( $email_field_styles ); ?>"
520 <?php } ?>
521 value="<?php echo esc_attr( $subscribe_email ); ?>"
522 id="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>"
523 placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"
524 />
525 </p>
526
527 <p id="subscribe-submit"
528 <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
529 style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
530 <?php } ?>
531 >
532 <input type="hidden" name="action" value="subscribe"/>
533 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
534 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
535 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
536 <?php wp_nonce_field( 'blogsub_subscribe_' . \Jetpack_Options::get_option( 'id' ) ); ?>
537 <button type="submit"
538 <?php if ( ! empty( $submit_button_classes ) ) { ?>
539 class="<?php echo esc_attr( $submit_button_classes ); ?>"
540 <?php } ?>
541 <?php if ( ! empty( $submit_button_styles ) ) { ?>
542 style="<?php echo esc_attr( $submit_button_styles ); ?>"
543 <?php } ?>
544 name="jetpack_subscriptions_widget"
545 >
546 <?php
547 echo wp_kses(
548 html_entity_decode( $subscribe_button, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
549 self::$allowed_html_tags_for_submit_button
550 );
551 ?>
552 </button>
553 </p>
554 <?php } ?>
555 </form>
556 <?php if ( $show_subscribers_total && 0 < $subscribers_total ) { ?>
557 <div class="wp-block-jetpack-subscriptions__subscount">
558 <?php
559 echo esc_html( Jetpack_Memberships::get_join_others_text( $subscribers_total ) );
560 ?>
561 </div>
562 <?php } ?>
563 </div>
564 <?php
565 }
566 }
567
568 /**
569 * Determines if the current user is subscribed to the blog.
570 *
571 * @return bool Is the person already subscribed.
572 */
573 public static function is_current_user_subscribed() {
574 $subscribed = isset( $_GET['subscribe'] ) && 'success' === $_GET['subscribe']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
575
576 if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
577 $subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
578 }
579
580 return $subscribed;
581 }
582
583 /**
584 * Is this script running in the wordpress.com environment?
585 *
586 * @return bool
587 */
588 public static function is_wpcom() {
589 return defined( 'IS_WPCOM' ) && IS_WPCOM;
590 }
591
592 /**
593 * Is this script running in a self-hosted environment?
594 *
595 * @return bool
596 */
597 public static function is_jetpack() {
598 return ! self::is_wpcom();
599 }
600
601 /**
602 * Used to determine if there is a valid status slug within the wordpress.com environment.
603 *
604 * @return bool
605 */
606 public static function wpcom_has_status_message() {
607 return isset( $_GET['blogsub'] ) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended
608 in_array(
609 $_GET['blogsub'], // phpcs:ignore WordPress.Security.NonceVerification.Recommended
610 array(
611 'confirming',
612 'blocked',
613 'flooded',
614 'spammed',
615 'subscribed',
616 'pending',
617 'confirmed',
618 ),
619 true
620 );
621 }
622
623 /**
624 * Determine the amount of folks currently subscribed to the blog.
625 *
626 * @return int
627 */
628 public static function fetch_subscriber_count() {
629 $subs_count = 0;
630 if ( self::is_jetpack() ) {
631 $cache_key = 'wpcom_subscribers_total';
632 $subs_count = get_transient( $cache_key );
633 if ( false === $subs_count || 'failed' === $subs_count['status'] ) {
634 $xml = new Jetpack_IXR_Client();
635 $xml->query( 'jetpack.fetchSubscriberCount' );
636
637 if ( $xml->isError() ) { // If we get an error from .com, set the status to failed so that we will try again next time the data is requested.
638
639 $subs_count = array(
640 'status' => 'failed',
641 'code' => $xml->getErrorCode(),
642 'message' => $xml->getErrorMessage(),
643 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
644 );
645 } else {
646 $subs_count = array(
647 'status' => 'success',
648 'value' => $xml->getResponse(),
649 );
650 }
651 set_transient( $cache_key, $subs_count, 3600 ); // Try to cache the result for at least 1 hour.
652 }
653 return $subs_count['value'];
654 }
655
656 if ( self::is_wpcom() ) {
657 $subs_count = wpcom_reach_total_for_blog();
658 return $subs_count;
659 }
660 }
661
662 /**
663 * Updates a particular instance of a widget when someone saves it in wp-admin.
664 *
665 * @param array $new_instance New widget instance settings.
666 * @param array $old_instance Old widget instance settings.
667 *
668 * @return array
669 */
670 public function update( $new_instance, $old_instance ) {
671 $instance = $old_instance;
672
673 if ( self::is_jetpack() ) {
674 $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
675 $instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() );
676 $instance['subscribe_button'] = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() );
677 $instance['success_message'] = wp_kses( stripslashes( $new_instance['success_message'] ), array() );
678 }
679
680 if ( self::is_wpcom() ) {
681 $instance['title'] = wp_strip_all_tags( stripslashes( $new_instance['title'] ) );
682 $instance['title_following'] = wp_strip_all_tags( stripslashes( $new_instance['title_following'] ) );
683 $instance['subscribe_logged_in'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_logged_in'] ) );
684 $instance['subscribe_button'] = wp_strip_all_tags( stripslashes( $new_instance['subscribe_button'] ) );
685 }
686
687 $instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
688 $instance['show_only_email_and_button'] = isset( $new_instance['show_only_email_and_button'] ) && $new_instance['show_only_email_and_button'];
689 $instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) );
690
691 return $instance;
692 }
693
694 /**
695 * The default args for rendering a subscription form.
696 *
697 * @return array
698 */
699 public static function defaults() {
700 $defaults = array(
701 'show_subscribers_total' => true,
702 'show_only_email_and_button' => false,
703 'include_social_followers' => true,
704 );
705
706 $defaults['title'] = esc_html__( 'Subscribe to Blog via Email', 'jetpack' );
707 $defaults['subscribe_text'] = esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' );
708 $defaults['subscribe_placeholder'] = esc_html__( 'Email Address', 'jetpack' );
709 $defaults['subscribe_button'] = esc_html__( 'Subscribe', 'jetpack' );
710 $defaults['success_message'] = esc_html__( "Success! An email was just sent to confirm your subscription. Please find the email now and click 'Confirm' to start subscribing.", 'jetpack' );
711
712 return $defaults;
713 }
714
715 /**
716 * Renders the widget's options form in wp-admin.
717 *
718 * @param array $instance Widget instance.
719 */
720 public function form( $instance ) {
721 $instance = wp_parse_args( (array) $instance, static::defaults() );
722 $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
723
724 if ( self::is_wpcom() ) {
725 $title = ! empty( $instance['title'] ) ? esc_attr( stripslashes( $instance['title'] ) ) : '';
726 $title_following = ! empty( $instance['title_following'] ) ? esc_attr( stripslashes( $instance['title_following'] ) ) : '';
727 $subscribe_text = ! empty( $instance['subscribe_text'] ) ? esc_attr( stripslashes( $instance['subscribe_text'] ) ) : '';
728 $subscribe_logged_in = ! empty( $instance['subscribe_logged_in'] ) ? esc_attr( stripslashes( $instance['subscribe_logged_in'] ) ) : '';
729 $subscribe_button = ! empty( $instance['subscribe_button'] ) ? esc_attr( stripslashes( $instance['subscribe_button'] ) ) : '';
730 $subscribers_total = self::fetch_subscriber_count();
731 }
732
733 if ( self::is_jetpack() ) {
734 $title = ! empty( $instance['title'] ) ? stripslashes( $instance['title'] ) : '';
735 $subscribe_text = ! empty( $instance['subscribe_text'] ) ? stripslashes( $instance['subscribe_text'] ) : '';
736 $subscribe_placeholder = ! empty( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
737 $subscribe_button = ! empty( $instance['subscribe_button'] ) ? stripslashes( $instance['subscribe_button'] ) : '';
738 $success_message = ! empty( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
739 $subscribers_total = self::fetch_subscriber_count();
740 }
741
742 if ( self::is_wpcom() ) :
743 ?>
744 <p>
745 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
746 <?php esc_html_e( 'Widget title for non-followers:', 'jetpack' ); ?>
747 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
748 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
749 value="<?php echo esc_attr( $title ); ?>"/>
750 </label>
751 </p>
752 <p>
753 <label for="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>">
754 <?php esc_html_e( 'Widget title for followers:', 'jetpack' ); ?>
755 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>"
756 name="<?php echo esc_attr( $this->get_field_name( 'title_following' ) ); ?>" type="text"
757 value="<?php echo esc_attr( $title_following ); ?>"/>
758 </label>
759 </p>
760 <p>
761 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>">
762 <?php esc_html_e( 'Optional text to display to logged in WordPress.com users:', 'jetpack' ); ?>
763 <textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>"
764 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_logged_in' ) ); ?>"
765 type="text"><?php echo esc_html( $subscribe_logged_in ); ?></textarea>
766 </label>
767 </p>
768 <p>
769 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
770 <?php esc_html_e( 'Optional text to display to non-WordPress.com users:', 'jetpack' ); ?>
771 <textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
772 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
773 type="text"><?php echo esc_html( $subscribe_text ); ?></textarea>
774 </label>
775 </p>
776 <p>
777 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
778 <?php esc_html_e( 'Follow Button Text:', 'jetpack' ); ?>
779 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
780 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
781 value="<?php echo esc_attr( $subscribe_button ); ?>"/>
782 </label>
783 </p>
784 <p>
785 <label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
786 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
787 name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
788 value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
789 <?php
790 /* translators: %s: Number of followers. */
791 echo esc_html( sprintf( _n( 'Show total number of followers? (%s follower)', 'Show total number of followers? (%s followers)', $subscribers_total, 'jetpack' ), number_format_i18n( $subscribers_total ) ) );
792 ?>
793 </label>
794 </p>
795 <?php
796 endif;
797
798 if ( self::is_jetpack() ) :
799 ?>
800 <p>
801 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
802 <?php esc_html_e( 'Widget title:', 'jetpack' ); ?>
803 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
804 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
805 value="<?php echo esc_attr( $title ); ?>"/>
806 </label>
807 </p>
808 <p>
809 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
810 <?php esc_html_e( 'Optional text to display to your readers:', 'jetpack' ); ?>
811 <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
812 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
813 rows="3"><?php echo esc_html( $subscribe_text ); ?></textarea>
814 </label>
815 </p>
816 <p>
817 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>">
818 <?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?>
819 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>"
820 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_placeholder' ) ); ?>" type="text"
821 value="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
822 </label>
823 </p>
824 <p>
825 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
826 <?php esc_html_e( 'Subscribe Button:', 'jetpack' ); ?>
827 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
828 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
829 value="<?php echo esc_attr( $subscribe_button ); ?>"/>
830 </label>
831 </p>
832 <p>
833 <label for="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>">
834 <?php esc_html_e( 'Success Message Text:', 'jetpack' ); ?>
835 <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>"
836 name="<?php echo esc_attr( $this->get_field_name( 'success_message' ) ); ?>"
837 rows="5"><?php echo esc_html( $success_message ); ?></textarea>
838 </label>
839 </p>
840 <p>
841 <label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
842 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
843 name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
844 value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
845 <?php
846 /* translators: %s: Number of subscribers. */
847 echo esc_html( sprintf( _n( 'Show total number of subscribers? (%s subscriber)', 'Show total number of subscribers? (%s subscribers)', $subscribers_total, 'jetpack' ), $subscribers_total ) );
848 ?>
849 </label>
850 </p>
851 <?php
852 endif;
853 }
854 }
855
856 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
857 class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' );
858 }
859
860 /**
861 * Classname / shortcode tag to use for the Subscriptions widget.
862 *
863 * @return string
864 */
865 function get_jetpack_blog_subscriptions_widget_classname() {
866 return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
867 'Blog_Subscription_Widget' :
868 'Jetpack_Subscriptions_Widget';
869 }
870
871 /**
872 * Subscriptions widget form HTML output.
873 *
874 * @param array $instance Widget instance data.
875 */
876 function jetpack_do_subscription_form( $instance ) {
877 if ( empty( $instance ) || ! is_array( $instance ) ) {
878 $instance = array();
879 }
880
881 if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
882 $instance['show_subscribers_total'] = false;
883 } else {
884 $instance['show_subscribers_total'] = true;
885 }
886
887 // the default behavior is to include the social followers
888 if ( empty( $instance['include_social_followers'] ) || 'true' === $instance['include_social_followers'] ) {
889 $instance['include_social_followers'] = true;
890 } else {
891 $instance['include_social_followers'] = false;
892 }
893
894 $show_only_email_and_button = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
895 $submit_button_text = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
896
897 // Build up a string with the submit button's classes and styles and set it on the instance.
898 $submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
899 $email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
900 $style = '';
901 $submit_button_styles = '';
902 $submit_button_wrapper_styles = '';
903 $email_field_styles = '';
904 $success_message = '';
905
906 if ( isset( $instance['custom_background_button_color'] ) && 'undefined' !== $instance['custom_background_button_color'] ) {
907 $submit_button_styles .= 'background: ' . $instance['custom_background_button_color'] . '; ';
908 }
909 if ( isset( $instance['custom_text_button_color'] ) && 'undefined' !== $instance['custom_text_button_color'] ) {
910 $submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . '; ';
911 }
912 if ( isset( $instance['custom_button_width'] ) && 'undefined' !== $instance['custom_button_width'] ) {
913 $submit_button_wrapper_styles .= 'width: ' . $instance['custom_button_width'] . '; ';
914 $submit_button_wrapper_styles .= 'max-width: 100%; ';
915
916 // Account for custom margins on inline forms.
917 if (
918 ! empty( $instance['custom_spacing'] ) &&
919 ! ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] )
920 ) {
921 $submit_button_styles .= 'width: calc(100% - ' . $instance['custom_spacing'] . 'px); ';
922 } else {
923 $submit_button_styles .= 'width: 100%; ';
924 }
925 }
926
927 if ( isset( $instance['custom_font_size'] ) && 'undefined' !== $instance['custom_font_size'] ) {
928 $style = 'font-size: ' . $instance['custom_font_size'];
929 $style .= is_numeric( $instance['custom_font_size'] ) ? 'px; ' : '; '; // Handle deprecated numeric font size values.
930
931 $submit_button_styles .= $style;
932 $email_field_styles .= $style;
933 }
934 if ( isset( $instance['custom_padding'] ) && 'undefined' !== $instance['custom_padding'] ) {
935 $style = 'padding: ' .
936 $instance['custom_padding'] . 'px ' .
937 round( floatval( $instance['custom_padding'] ) * 1.5 ) . 'px ' .
938 $instance['custom_padding'] . 'px ' .
939 round( floatval( $instance['custom_padding'] ) * 1.5 ) . 'px; ';
940
941 $submit_button_styles .= $style;
942 $email_field_styles .= $style;
943 }
944
945 $button_spacing = 0;
946 if ( ! empty( $instance['custom_spacing'] ) ) {
947 $button_spacing = $instance['custom_spacing'];
948 }
949 if ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] ) {
950 $submit_button_styles .= 'margin-top: ' . $button_spacing . 'px; ';
951 } else {
952 $submit_button_styles .= 'margin: 0px; '; // Reset Safari's 2px default margin for buttons affecting input and button union
953 $submit_button_styles .= 'margin-left: ' . $button_spacing . 'px; ';
954 }
955
956 if ( isset( $instance['custom_border_radius'] ) && 'undefined' !== $instance['custom_border_radius'] ) {
957 $style = 'border-radius: ' . $instance['custom_border_radius'] . 'px; ';
958 $submit_button_styles .= $style;
959 $email_field_styles .= $style;
960 }
961 if ( isset( $instance['custom_border_weight'] ) && 'undefined' !== $instance['custom_border_weight'] ) {
962 $style = 'border-width: ' . $instance['custom_border_weight'] . 'px; ';
963 $submit_button_styles .= $style;
964 $email_field_styles .= $style;
965 }
966 if ( isset( $instance['custom_border_color'] ) && 'undefined' !== $instance['custom_border_color'] ) {
967 $style =
968 'border-color: ' . $instance['custom_border_color'] . '; ' .
969 'border-style: solid;';
970
971 $submit_button_styles .= $style;
972 $email_field_styles .= $style;
973 }
974 if ( isset( $instance['success_message'] ) && 'undefined' !== $instance['success_message'] ) {
975 $success_message = wp_kses( stripslashes( $instance['success_message'] ), array() );
976 }
977
978 $instance = shortcode_atts(
979 Jetpack_Subscriptions_Widget::defaults(),
980 $instance,
981 'jetpack_subscription_form'
982 );
983
984 // These must come after the call to shortcode_atts().
985 $instance['submit_button_text'] = $submit_button_text;
986 $instance['show_only_email_and_button'] = $show_only_email_and_button;
987 if ( ! empty( $submit_button_classes ) ) {
988 $instance['submit_button_classes'] = $submit_button_classes;
989 }
990 if ( ! empty( $email_field_classes ) ) {
991 $instance['email_field_classes'] = $email_field_classes;
992 }
993
994 if ( ! empty( $submit_button_styles ) ) {
995 $instance['submit_button_styles'] = trim( $submit_button_styles );
996 }
997 if ( ! empty( $submit_button_wrapper_styles ) ) {
998 $instance['submit_button_wrapper_styles'] = trim( $submit_button_wrapper_styles );
999 }
1000 if ( ! empty( $email_field_styles ) ) {
1001 $instance['email_field_styles'] = trim( $email_field_styles );
1002 }
1003 if ( ! empty( $success_message ) ) {
1004 $instance['success_message'] = trim( $success_message );
1005 }
1006
1007 $args = array(
1008 'before_widget' => '<div class="jetpack_subscription_widget">',
1009 );
1010 ob_start();
1011 the_widget( get_jetpack_blog_subscriptions_widget_classname(), $instance, $args );
1012 $output = ob_get_clean();
1013
1014 return $output;
1015 }
1016
1017 add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' );
1018 add_shortcode( 'blog_subscription_form', 'jetpack_do_subscription_form' );
1019
1020 /**
1021 * Register the Subscriptions widget.
1022 */
1023 function jetpack_blog_subscriptions_init() {
1024 register_widget( get_jetpack_blog_subscriptions_widget_classname() );
1025 }
1026
1027 add_action( 'widgets_init', 'jetpack_blog_subscriptions_init' );
1028
1029 /**
1030 * Sets the default value for `subscription_options` site option.
1031 *
1032 * This default value is available across Simple, Atomic and Jetpack sites,
1033 * including the /sites/$site/settings endpoint.
1034 *
1035 * @param array $default Default `subscription_options` array.
1036 * @param string $option Option name.
1037 * @param bool $passed_default Whether `get_option()` passed a default value.
1038 *
1039 * @return array Default value of `subscription_options`.
1040 */
1041 function subscription_options_fallback( $default, $option, $passed_default ) {
1042 if ( $passed_default ) {
1043 return $default;
1044 }
1045
1046 $site_url = get_home_url();
1047 $display_url = preg_replace( '(^https?://)', '', untrailingslashit( $site_url ) );
1048
1049 return array(
1050 /* translators: Both %1$s and %2$s is site address */
1051 'invitation' => sprintf( __( "Howdy,\nYou recently subscribed to <a href='%1\$s'>%2\$s</a> and we need to verify the email you provided. Once you confirm below, you'll be able to receive and read new posts.\n\nIf you believe this is an error, ignore this message and nothing more will happen.", 'jetpack' ), $site_url, $display_url ),
1052 'comment_follow' => __( "Howdy.\n\nYou recently followed one of my posts. This means you will receive an email when new comments are posted.\n\nTo activate, click confirm below. If you believe this is an error, ignore this message and we'll never bother you again.", 'jetpack' ),
1053 /* translators: %1$s is the site address */
1054 'welcome' => sprintf( __( 'Cool, you are now subscribed to %1$s and will receive an email notification when a new post is published.', 'jetpack' ), $display_url ),
1055 );
1056 }
1057
1058 add_filter( 'default_option_subscription_options', 'subscription_options_fallback', 10, 3 );
1059