PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 15.1.2
Jetpack – WP Security, Backup, Speed, & Growth v15.1.2
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 11 months ago subscribe-floating-button 11 months ago subscribe-modal 1 year ago subscribe-overlay 11 months ago class-settings.php 1 year ago jetpack-user-content-link-redirection.php 1 year ago readme.md 7 years ago subscriptions.css 1 year ago views.php 1 year ago
views.php
1078 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 use Automattic\Jetpack\Assets;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit( 0 );
9 }
10
11 /**
12 * Jetpack_Subscriptions_Widget main view class.
13 */
14 class Jetpack_Subscriptions_Widget extends WP_Widget {
15
16 const ID_BASE = 'blog_subscription';
17
18 /**
19 * Track number of rendered Subscription widgets. The count is used for class names and widget IDs.
20 *
21 * @var int
22 */
23 public static $instance_count = 0;
24
25 /**
26 * When printing the submit button, what tags are allowed.
27 *
28 * @var array
29 */
30 public static $allowed_html_tags_for_submit_button = array(
31 'br' => array(),
32 's' => array(),
33 'strong' => array(),
34 'em' => array(),
35 );
36
37 /**
38 * Use this variable when printing the message after submitting an email in subscription widgets
39 *
40 * @var array what tags are allowed
41 */
42 public static $allowed_html_tags_for_message = array(
43 'a' => array(
44 'href' => array(),
45 'title' => array(),
46 'rel' => array(),
47 'target' => array(),
48 ),
49 'br' => array(),
50 );
51
52 /**
53 * Jetpack_Subscriptions_Widget constructor.
54 */
55 public function __construct() {
56 $widget_ops = array(
57 'classname' => 'widget_blog_subscription jetpack_subscription_widget',
58 'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ),
59 'customize_selective_refresh' => true,
60 'show_instance_in_rest' => true,
61 );
62
63 $name = self::is_jetpack() ?
64 /** This filter is documented in modules/widgets/facebook-likebox.php */
65 apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ) :
66 __( 'Follow Blog', 'jetpack' );
67
68 parent::__construct(
69 'blog_subscription',
70 $name,
71 $widget_ops
72 );
73
74 if (
75 self::is_jetpack()
76 && (
77 is_active_widget( false, false, $this->id_base ) ||
78 is_active_widget( false, false, 'monster' ) ||
79 is_customize_preview()
80 )
81 && ! wp_is_block_theme()
82 ) {
83 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
84 }
85
86 add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
87 }
88
89 /**
90 * Remove the "Blog Subscription" widget from the Legacy Widget block
91 *
92 * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block.
93 * @return array $widget_types New list of widgets that will be removed.
94 */
95 public function hide_widget_in_block_editor( $widget_types ) {
96 $widget_types[] = self::ID_BASE;
97 return $widget_types;
98 }
99
100 /**
101 * Enqueue the form's CSS.
102 *
103 * @since 4.5.0
104 */
105 public function enqueue_style() {
106 $path = Assets::get_file_url_for_environment(
107 '_inc/build/subscriptions/subscriptions.min.css',
108 'modules/subscriptions/subscriptions.css'
109 );
110
111 wp_enqueue_style(
112 'jetpack-subscriptions',
113 $path,
114 array(),
115 JETPACK__VERSION
116 );
117 wp_style_add_data( 'jetpack-subscriptions', 'path', $path );
118 }
119
120 /**
121 * Renders a full widget either within the context of WordPress widget, or in response to a shortcode.
122 *
123 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
124 * @param array $instance The settings for the particular instance of the widget.
125 */
126 public function widget( $args, $instance ) {
127 if ( self::is_wpcom() && ! self::wpcom_has_status_message() && self::is_current_user_subscribed() ) {
128 return null;
129 }
130
131 // Enqueue styles.
132 self::enqueue_style();
133
134 if ( self::is_jetpack() &&
135 /** This filter is documented in \Automattic\Jetpack\Forms\ContactForm\Contact_Form */
136 false === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
137 ) {
138 $subscribe_email = '';
139 } else {
140 $current_user = wp_get_current_user();
141 if ( ! empty( $current_user->user_email ) ) {
142 $subscribe_email = esc_attr( $current_user->user_email );
143 } else {
144 $subscribe_email = '';
145 }
146 }
147
148 $stats_action = self::is_jetpack() ? 'jetpack_subscriptions' : 'follow_blog';
149 /** This action is documented in modules/widgets/gravatar-profile.php */
150 do_action( 'jetpack_stats_extra', 'widget_view', $stats_action );
151
152 $after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
153 $before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
154 $instance = wp_parse_args( (array) $instance, static::defaults() );
155
156 echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
157
158 ++self::$instance_count;
159
160 self::render_widget_title( $args, $instance );
161
162 self::render_widget_status_messages( $instance );
163
164 self::render_widget_subscription_form( $args, $instance, $subscribe_email );
165
166 echo "\n" . $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
167 }
168
169 /**
170 * Prints the widget's title. If show_only_email_and_button is true, we will not show a title.
171 *
172 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
173 * @param array $instance The settings for the particular instance of the widget.
174 */
175 public static function render_widget_title( $args, $instance ) {
176 $show_only_email_and_button = $instance['show_only_email_and_button'];
177 $before_title = isset( $args['before_title'] ) ? $args['before_title'] : '';
178 $after_title = isset( $args['after_title'] ) ? $args['after_title'] : '';
179 if ( self::is_wpcom() && ! $show_only_email_and_button ) {
180 if ( self::is_current_user_subscribed() ) {
181 if ( ! empty( $instance['title_following'] ) ) {
182 printf(
183 '%1$s<label for="subscribe-field%2$s">%3$s</label>%4$s%5$s',
184 $before_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
185 ( self::$instance_count > 1 ? '-' . (int) self::$instance_count : '' ),
186 esc_html( $instance['title_following'] ),
187 $after_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
188 "\n"
189 );
190 }
191 } elseif ( ! empty( $instance['title'] ) ) {
192 printf(
193 '%1$s<label for="subscribe-field%2$s">%3$s</label>%4$s%5$s',
194 $before_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
195 ( self::$instance_count > 1 ? '-' . (int) self::$instance_count : '' ),
196 esc_html( $instance['title'] ),
197 $after_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
198 "\n"
199 );
200 }
201 }
202
203 if ( self::is_jetpack() && empty( $instance['show_only_email_and_button'] ) ) {
204 printf(
205 '%1$s%2$s%3$s%4$s',
206 $before_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
207 esc_html( $instance['title'] ),
208 $after_title, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
209 "\n"
210 );
211 }
212 }
213
214 /**
215 * Prints the subscription block's status messages after someone has attempted to subscribe.
216 * Either a success message or an error message.
217 *
218 * @param array $instance The settings for the particular instance of the widget.
219 */
220 public static function render_widget_status_messages( $instance ) {
221 if ( self::is_jetpack() && isset( $_GET['subscribe'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Non-sensitive informational output.
222 $success_message = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
223 $subscribers_total = self::fetch_subscriber_count();
224
225 switch ( $_GET['subscribe'] ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended
226 case 'invalid_email':
227 ?>
228 <p class="error"><?php esc_html_e( 'Oops! The email you used is invalid. Please try again.', 'jetpack' ); ?></p>
229 <?php
230 break;
231 case 'opted_out':
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 __( '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' ),
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 'already':
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 __( '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' ),
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 'many_pending_subs':
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 __( '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' ),
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 'pending':
283 ?>
284 <p class="error">
285 <?php
286 printf(
287 wp_kses(
288 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
289 __( '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' ),
290 self::$allowed_html_tags_for_message
291 ),
292 'https://subscribe.wordpress.com/',
293 esc_attr__( 'Subscriptions Manager', 'jetpack' )
294 );
295 ?>
296 </p>
297 <?php
298 break;
299 case 'success':
300 ?>
301 <div class="success"><?php echo wp_kses( wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total ), $success_message ) ), 'post' ); ?></div>
302 <?php
303 break;
304 default:
305 ?>
306 <p class="error"><?php esc_html_e( 'Oops! There was an error when subscribing. Please try again.', 'jetpack' ); ?></p>
307 <?php
308 break;
309 endswitch;
310 }
311
312 if ( self::is_wpcom() && self::wpcom_has_status_message() ) {
313 global $themecolors;
314 $message = '';
315
316 switch ( $_GET['blogsub'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated
317 case 'confirming':
318 $message = __( 'Thank you! You can now check your email to confirm your subscription.', 'jetpack' );
319 break;
320 case 'blocked':
321 $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' );
322 break;
323 case 'flooded':
324 $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' );
325 break;
326 case 'spammed':
327 /* translators: %s is a URL */
328 $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/' );
329 break;
330 case 'subscribed':
331 $message = __( 'Hey! You were already subscribed.', 'jetpack' );
332 break;
333 case 'pending':
334 $message = __( 'It seems you already tried to subscribe. We just sent you another email so you can confirm the subscription.', 'jetpack' );
335 break;
336 case 'confirmed':
337 $message = __( 'Cool! You are now subscribed. Now you can check your email for more details and how to manage the subscription.', 'jetpack' );
338 break;
339 }
340
341 $border_color = isset( $themecolors['border'] ) ? " #{$themecolors['border']}" : '';
342
343 $redirect_fragment = self::get_redirect_fragment();
344 printf(
345 '<div id="%1$s" class="jetpack-sub-notification">%3$s</div>',
346 esc_attr( $redirect_fragment ),
347 esc_attr( $border_color ),
348 wp_kses_post( $message )
349 );
350 }
351 }
352
353 /**
354 * Generates the redirect fragment used after form submission.
355 *
356 * @param string $id is the specific id that will appear in the redirect fragment. If none is provided self::$instance_count will be used.
357 */
358 protected static function get_redirect_fragment( $id = null ) {
359 if ( $id === null ) {
360 return 'subscribe-blog' . ( self::$instance_count > 1 ? '-' . self::$instance_count : '' );
361 }
362
363 return 'subscribe-blog-' . $id;
364 }
365
366 /**
367 * Renders a form allowing folks to subscribe to the blog.
368 *
369 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
370 * @param array $instance The settings for the particular instance of the widget.
371 * @param string $subscribe_email The email to use to prefill the form.
372 */
373 public static function render_widget_subscription_form( $args, $instance, $subscribe_email ) {
374 $show_only_email_and_button = $instance['show_only_email_and_button'];
375 $show_subscribers_total = (bool) $instance['show_subscribers_total'];
376 $subscribers_total = self::fetch_subscriber_count();
377 $subscribe_text = empty( $instance['show_only_email_and_button'] ) ?
378 wp_kses_post( stripslashes( $instance['subscribe_text'] ) ) :
379 false;
380 $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'] ) : '' ) );
381 $source = 'widget';
382 $widget_id = ! empty( $args['widget_id'] ) ? $args['widget_id'] : self::$instance_count;
383 $subscribe_button = ! empty( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : $instance['subscribe_button'];
384 $subscribe_placeholder = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
385 $submit_button_classes = isset( $instance['submit_button_classes'] ) ? 'wp-block-button__link ' . $instance['submit_button_classes'] : 'wp-block-button__link';
386 $submit_button_styles = isset( $instance['submit_button_styles'] ) ? $instance['submit_button_styles'] : '';
387 $submit_button_wrapper_styles = isset( $instance['submit_button_wrapper_styles'] ) ? $instance['submit_button_wrapper_styles'] : '';
388 $email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
389 $email_field_styles = isset( $instance['email_field_styles'] ) ? $instance['email_field_styles'] : '';
390
391 // We need to include those in case Jetpack blocks are disabled
392 require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
393 require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
394 $post_access_level = \Jetpack_Memberships::get_post_access_level();
395
396 if ( self::is_wpcom() && ! self::wpcom_has_status_message() ) {
397 global $current_blog;
398
399 $url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : '';
400 $form_id = self::get_redirect_fragment();
401 ?>
402
403 <div class="wp-block-jetpack-subscriptions__container">
404 <form
405 action="<?php echo esc_url( $url ); ?>"
406 method="post"
407 accept-charset="utf-8"
408 data-blog="<?php echo esc_attr( get_current_blog_id() ); ?>"
409 data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>"
410 id="<?php echo esc_attr( $form_id ); ?>"
411 >
412 <?php
413 if ( ! $show_only_email_and_button ) {
414 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
415 echo wpautop( $subscribe_text );
416 }
417 $email_field_id = 'subscribe-field';
418 $email_field_id .= self::$instance_count > 1
419 ? '-' . self::$instance_count
420 : '';
421 $label_field_id = $email_field_id . '-label';
422 ?>
423 <p id="subscribe-email">
424 <label
425 id="<?php echo esc_attr( $label_field_id ); ?>"
426 for="<?php echo esc_attr( $email_field_id ); ?>"
427 class="screen-reader-text"
428 >
429 <?php echo esc_html__( 'Email Address:', 'jetpack' ); ?>
430 </label>
431
432 <?php
433 printf(
434 '<input
435 type="email"
436 name="email"
437 %1$s
438 style="%2$s"
439 placeholder="%3$s"
440 value=""
441 id="%4$s"
442 required
443 />',
444 ( ! empty( $email_field_classes )
445 ? 'class="' . esc_attr( $email_field_classes ) . '"'
446 : ''
447 ),
448 ( ! empty( $email_field_styles )
449 ? esc_attr( $email_field_styles )
450 : 'width: 95%; padding: 1px 10px'
451 ),
452 ( empty( $subscribe_placeholder ) ? esc_attr__( 'Enter your email address', 'jetpack' ) : esc_attr( $subscribe_placeholder ) ),
453 esc_attr( $email_field_id )
454 );
455 ?>
456 </p>
457
458 <p id="subscribe-submit"
459 <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
460 style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
461 <?php } ?>
462 >
463 <input type="hidden" name="action" value="subscribe"/>
464 <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/>
465 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
466 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
467 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
468 <?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?>
469 <button type="submit"
470 <?php if ( ! empty( $submit_button_classes ) ) { ?>
471 class="<?php echo esc_attr( $submit_button_classes ); ?>"
472 <?php } ?>
473 <?php if ( ! empty( $submit_button_styles ) ) { ?>
474 style="<?php echo esc_attr( $submit_button_styles ); ?>"
475 <?php } ?>
476 >
477 <?php
478 echo wp_kses(
479 html_entity_decode( $subscribe_button, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
480 self::$allowed_html_tags_for_submit_button
481 );
482 ?>
483 </button>
484 </p>
485 </form>
486 <?php if ( $show_subscribers_total && $subscribers_total > 0 ) { ?>
487 <div class="wp-block-jetpack-subscriptions__subscount">
488 <?php
489 echo esc_html( Jetpack_Memberships::get_join_others_text( $subscribers_total ) );
490 ?>
491 </div>
492 <?php } ?>
493 </div>
494 <?php
495 }
496
497 if ( self::is_jetpack() ) {
498 /**
499 * Filter the subscription form's ID prefix.
500 *
501 * @module subscriptions
502 *
503 * @since 2.7.0
504 *
505 * @param string subscribe-field Subscription form field prefix.
506 * @param int $widget_id Widget ID.
507 */
508 $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
509
510 $form_id = self::get_redirect_fragment( $widget_id );
511 ?>
512 <div class="wp-block-jetpack-subscriptions__container">
513 <form action="#" method="post" accept-charset="utf-8" id="<?php echo esc_attr( $form_id ); ?>"
514 data-blog="<?php echo esc_attr( \Jetpack_Options::get_option( 'id' ) ); ?>"
515 data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>" >
516 <?php
517 if ( $subscribe_text && ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Non-sensitive informational output.
518 ?>
519 <div id="subscribe-text"><?php echo wp_kses( wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total ), $subscribe_text ) ), 'post' ); ?></div>
520 <?php
521 }
522
523 if ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display of unsubmitted form.
524 ?>
525 <p id="subscribe-email">
526 <label id="jetpack-subscribe-label"
527 class="screen-reader-text"
528 for="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>">
529 <?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?>
530 </label>
531 <input type="email" name="email" required="required"
532 <?php if ( ! empty( $email_field_classes ) ) { ?>
533 class="<?php echo esc_attr( $email_field_classes ); ?> required"
534 <?php } ?>
535 <?php if ( ! empty( $email_field_styles ) ) { ?>
536 style="<?php echo esc_attr( $email_field_styles ); ?>"
537 <?php } ?>
538 value="<?php echo esc_attr( $subscribe_email ); ?>"
539 id="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>"
540 placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"
541 />
542 </p>
543
544 <p id="subscribe-submit"
545 <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
546 style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
547 <?php } ?>
548 >
549 <input type="hidden" name="action" value="subscribe"/>
550 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
551 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
552 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
553 <?php wp_nonce_field( 'blogsub_subscribe_' . \Jetpack_Options::get_option( 'id' ) ); ?>
554 <button type="submit"
555 <?php if ( ! empty( $submit_button_classes ) ) { ?>
556 class="<?php echo esc_attr( $submit_button_classes ); ?>"
557 <?php } ?>
558 <?php if ( ! empty( $submit_button_styles ) ) { ?>
559 style="<?php echo esc_attr( $submit_button_styles ); ?>"
560 <?php } ?>
561 name="jetpack_subscriptions_widget"
562 >
563 <?php
564 echo wp_kses(
565 html_entity_decode( $subscribe_button, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
566 self::$allowed_html_tags_for_submit_button
567 );
568 ?>
569 </button>
570 </p>
571 <?php } ?>
572 </form>
573 <?php if ( $show_subscribers_total && 0 < $subscribers_total ) { ?>
574 <div class="wp-block-jetpack-subscriptions__subscount">
575 <?php
576 echo esc_html( Jetpack_Memberships::get_join_others_text( $subscribers_total ) );
577 ?>
578 </div>
579 <?php } ?>
580 </div>
581 <?php
582 }
583 }
584
585 /**
586 * Determines if the current user is subscribed to the blog.
587 *
588 * @return bool Is the person already subscribed.
589 */
590 public static function is_current_user_subscribed() {
591 $subscribed = isset( $_GET['subscribe'] ) && 'success' === $_GET['subscribe']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
592
593 if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
594 $subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
595 }
596
597 return $subscribed;
598 }
599
600 /**
601 * Is this script running in the wordpress.com environment?
602 *
603 * @return bool
604 */
605 public static function is_wpcom() {
606 return defined( 'IS_WPCOM' ) && IS_WPCOM;
607 }
608
609 /**
610 * Is this script running in a self-hosted environment?
611 *
612 * @return bool
613 */
614 public static function is_jetpack() {
615 return ! self::is_wpcom();
616 }
617
618 /**
619 * Used to determine if there is a valid status slug within the wordpress.com environment.
620 *
621 * @return bool
622 */
623 public static function wpcom_has_status_message() {
624 return isset( $_GET['blogsub'] ) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended
625 in_array(
626 $_GET['blogsub'], // phpcs:ignore WordPress.Security.NonceVerification.Recommended
627 array(
628 'confirming',
629 'blocked',
630 'flooded',
631 'spammed',
632 'subscribed',
633 'pending',
634 'confirmed',
635 ),
636 true
637 );
638 }
639
640 /**
641 * Determine the amount of folks currently subscribed to the blog.
642 *
643 * @return int
644 */
645 public static function fetch_subscriber_count() {
646 $subs_count = 0;
647 if ( self::is_jetpack() ) {
648 $cache_key = 'wpcom_subscribers_total';
649 $subs_count = get_transient( $cache_key );
650 if ( false === $subs_count || 'failed' === $subs_count['status'] ) {
651 $xml = new Jetpack_IXR_Client();
652 $xml->query( 'jetpack.fetchSubscriberCount' );
653
654 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.
655
656 $subs_count = array(
657 'status' => 'failed',
658 'code' => $xml->getErrorCode(),
659 'message' => $xml->getErrorMessage(),
660 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
661 );
662 } else {
663 $subs_count = array(
664 'status' => 'success',
665 'value' => $xml->getResponse(),
666 );
667 }
668 set_transient( $cache_key, $subs_count, 3600 ); // Try to cache the result for at least 1 hour.
669 }
670 return $subs_count['value'];
671 }
672
673 if ( self::is_wpcom() ) {
674 $subs_count = wpcom_reach_total_for_blog();
675 return $subs_count;
676 }
677 }
678
679 /**
680 * Updates a particular instance of a widget when someone saves it in wp-admin.
681 *
682 * @param array $new_instance New widget instance settings.
683 * @param array $old_instance Old widget instance settings.
684 *
685 * @return array
686 */
687 public function update( $new_instance, $old_instance ) {
688 // Merge new values over old, then fill in any missing keys with defaults
689 $instance = wp_parse_args( (array) $new_instance, wp_parse_args( (array) $old_instance, static::defaults() ) );
690
691 if ( self::is_jetpack() ) {
692 $instance['title'] = wp_kses( stripslashes( $instance['title'] ), array() );
693 $instance['subscribe_placeholder'] = wp_kses( stripslashes( $instance['subscribe_placeholder'] ), array() );
694 $instance['subscribe_button'] = wp_kses( stripslashes( $instance['subscribe_button'] ), array() );
695 $instance['success_message'] = wp_kses( stripslashes( $instance['success_message'] ), array() );
696 }
697
698 if ( self::is_wpcom() ) {
699 $instance['title'] = wp_strip_all_tags( stripslashes( $instance['title'] ) );
700 $instance['title_following'] = isset( $instance['title_following'] ) ? wp_strip_all_tags( stripslashes( $instance['title_following'] ) ) : '';
701 $instance['subscribe_logged_in'] = isset( $instance['subscribe_logged_in'] ) ? wp_filter_post_kses( stripslashes( $instance['subscribe_logged_in'] ) ) : '';
702 $instance['subscribe_button'] = wp_strip_all_tags( stripslashes( $instance['subscribe_button'] ) );
703 }
704
705 $instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
706 $instance['show_only_email_and_button'] = isset( $new_instance['show_only_email_and_button'] ) && $new_instance['show_only_email_and_button'];
707 $instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $instance['subscribe_text'] ) );
708
709 return $instance;
710 }
711
712 /**
713 * The default args for rendering a subscription form.
714 *
715 * @return array
716 */
717 public static function defaults() {
718 $defaults = array(
719 'show_subscribers_total' => true,
720 'show_only_email_and_button' => false,
721 'include_social_followers' => true,
722 );
723
724 $defaults['title'] = esc_html__( 'Subscribe to Blog via Email', 'jetpack' );
725 $defaults['subscribe_text'] = esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' );
726 $defaults['subscribe_placeholder'] = esc_html__( 'Email Address', 'jetpack' );
727 $defaults['subscribe_button'] = esc_html__( 'Subscribe', 'jetpack' );
728 $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' );
729
730 return $defaults;
731 }
732
733 /**
734 * Renders the widget's options form in wp-admin.
735 *
736 * @param array $instance Widget instance.
737 * @return string|void
738 */
739 public function form( $instance ) {
740 $instance = wp_parse_args( (array) $instance, static::defaults() );
741 $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
742
743 if ( self::is_wpcom() ) {
744 $title = ! empty( $instance['title'] ) ? esc_attr( stripslashes( $instance['title'] ) ) : '';
745 $title_following = ! empty( $instance['title_following'] ) ? esc_attr( stripslashes( $instance['title_following'] ) ) : '';
746 $subscribe_text = ! empty( $instance['subscribe_text'] ) ? esc_attr( stripslashes( $instance['subscribe_text'] ) ) : '';
747 $subscribe_logged_in = ! empty( $instance['subscribe_logged_in'] ) ? esc_attr( stripslashes( $instance['subscribe_logged_in'] ) ) : '';
748 $subscribe_button = ! empty( $instance['subscribe_button'] ) ? esc_attr( stripslashes( $instance['subscribe_button'] ) ) : '';
749 $subscribers_total = self::fetch_subscriber_count();
750 }
751
752 if ( self::is_jetpack() ) {
753 $title = ! empty( $instance['title'] ) ? stripslashes( $instance['title'] ) : '';
754 $subscribe_text = ! empty( $instance['subscribe_text'] ) ? stripslashes( $instance['subscribe_text'] ) : '';
755 $subscribe_placeholder = ! empty( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
756 $subscribe_button = ! empty( $instance['subscribe_button'] ) ? stripslashes( $instance['subscribe_button'] ) : '';
757 $success_message = ! empty( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
758 $subscribers_total = self::fetch_subscriber_count();
759 }
760
761 if ( self::is_wpcom() ) :
762 ?>
763 <p>
764 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
765 <?php esc_html_e( 'Widget title for non-followers:', 'jetpack' ); ?>
766 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
767 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
768 value="<?php echo esc_attr( $title ); ?>"/>
769 </label>
770 </p>
771 <p>
772 <label for="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>">
773 <?php esc_html_e( 'Widget title for followers:', 'jetpack' ); ?>
774 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>"
775 name="<?php echo esc_attr( $this->get_field_name( 'title_following' ) ); ?>" type="text"
776 value="<?php echo esc_attr( $title_following ); ?>"/>
777 </label>
778 </p>
779 <p>
780 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>">
781 <?php esc_html_e( 'Optional text to display to logged in WordPress.com users:', 'jetpack' ); ?>
782 <textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>"
783 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_logged_in' ) ); ?>"
784 type="text"><?php echo esc_html( $subscribe_logged_in ); ?></textarea>
785 </label>
786 </p>
787 <p>
788 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
789 <?php esc_html_e( 'Optional text to display to non-WordPress.com users:', 'jetpack' ); ?>
790 <textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
791 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
792 type="text"><?php echo esc_html( $subscribe_text ); ?></textarea>
793 </label>
794 </p>
795 <p>
796 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
797 <?php esc_html_e( 'Follow Button Text:', 'jetpack' ); ?>
798 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
799 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
800 value="<?php echo esc_attr( $subscribe_button ); ?>"/>
801 </label>
802 </p>
803 <p>
804 <label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
805 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
806 name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
807 value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
808 <?php
809 /* translators: %s: Number of followers. */
810 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 ) ) );
811 ?>
812 </label>
813 </p>
814 <?php
815 endif;
816
817 if ( self::is_jetpack() ) :
818 ?>
819 <p>
820 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
821 <?php esc_html_e( 'Widget title:', 'jetpack' ); ?>
822 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
823 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
824 value="<?php echo esc_attr( $title ); ?>"/>
825 </label>
826 </p>
827 <p>
828 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
829 <?php esc_html_e( 'Optional text to display to your readers:', 'jetpack' ); ?>
830 <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
831 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
832 rows="3"><?php echo esc_html( $subscribe_text ); ?></textarea>
833 </label>
834 </p>
835 <p>
836 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>">
837 <?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?>
838 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>"
839 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_placeholder' ) ); ?>" type="text"
840 value="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
841 </label>
842 </p>
843 <p>
844 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
845 <?php esc_html_e( 'Subscribe Button:', 'jetpack' ); ?>
846 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
847 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
848 value="<?php echo esc_attr( $subscribe_button ); ?>"/>
849 </label>
850 </p>
851 <p>
852 <label for="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>">
853 <?php esc_html_e( 'Success Message Text:', 'jetpack' ); ?>
854 <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>"
855 name="<?php echo esc_attr( $this->get_field_name( 'success_message' ) ); ?>"
856 rows="5"><?php echo esc_html( $success_message ); ?></textarea>
857 </label>
858 </p>
859 <p>
860 <label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
861 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
862 name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
863 value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
864 <?php
865 /* translators: %s: Number of subscribers. */
866 echo esc_html( sprintf( _n( 'Show total number of subscribers? (%s subscriber)', 'Show total number of subscribers? (%s subscribers)', $subscribers_total, 'jetpack' ), $subscribers_total ) );
867 ?>
868 </label>
869 </p>
870 <?php
871 endif;
872 }
873 }
874
875 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
876 class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' );
877 }
878
879 /**
880 * Classname / shortcode tag to use for the Subscriptions widget.
881 *
882 * @return string
883 */
884 function get_jetpack_blog_subscriptions_widget_classname() {
885 return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
886 'Blog_Subscription_Widget' :
887 'Jetpack_Subscriptions_Widget';
888 }
889
890 /**
891 * Subscriptions widget form HTML output.
892 *
893 * @param array $instance Widget instance data.
894 */
895 function jetpack_do_subscription_form( $instance ) {
896 if ( empty( $instance ) || ! is_array( $instance ) ) {
897 $instance = array();
898 }
899
900 if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
901 $instance['show_subscribers_total'] = false;
902 } else {
903 $instance['show_subscribers_total'] = true;
904 }
905
906 // the default behavior is to include the social followers
907 if ( empty( $instance['include_social_followers'] ) || 'true' === $instance['include_social_followers'] ) {
908 $instance['include_social_followers'] = true;
909 } else {
910 $instance['include_social_followers'] = false;
911 }
912
913 $show_only_email_and_button = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
914 $submit_button_text = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
915
916 // Build up a string with the submit button's classes and styles and set it on the instance.
917 $submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
918 $email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
919 $style = '';
920 $submit_button_styles = '';
921 $submit_button_wrapper_styles = '';
922 $email_field_styles = '';
923 $success_message = '';
924
925 if ( isset( $instance['custom_background_button_color'] ) && 'undefined' !== $instance['custom_background_button_color'] ) {
926 $submit_button_styles .= 'background: ' . $instance['custom_background_button_color'] . '; ';
927 }
928 if ( isset( $instance['custom_text_button_color'] ) && 'undefined' !== $instance['custom_text_button_color'] ) {
929 $submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . '; ';
930 }
931 if ( isset( $instance['custom_button_width'] ) && 'undefined' !== $instance['custom_button_width'] ) {
932 $submit_button_wrapper_styles .= 'width: ' . $instance['custom_button_width'] . '; ';
933 $submit_button_wrapper_styles .= 'max-width: 100%; ';
934
935 // Account for custom margins on inline forms.
936 if (
937 ! empty( $instance['custom_spacing'] ) &&
938 ! ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] )
939 ) {
940 $submit_button_styles .= 'width: calc(100% - ' . $instance['custom_spacing'] . 'px); ';
941 } else {
942 $submit_button_styles .= 'width: 100%; ';
943 }
944 }
945
946 if ( isset( $instance['custom_font_size'] ) && 'undefined' !== $instance['custom_font_size'] ) {
947 $style = 'font-size: ' . $instance['custom_font_size'];
948 $style .= is_numeric( $instance['custom_font_size'] ) ? 'px; ' : '; '; // Handle deprecated numeric font size values.
949
950 $submit_button_styles .= $style;
951 $email_field_styles .= $style;
952 }
953 if ( isset( $instance['custom_padding'] ) && 'undefined' !== $instance['custom_padding'] ) {
954 $style = 'padding: ' .
955 $instance['custom_padding'] . 'px ' .
956 round( floatval( $instance['custom_padding'] ) * 1.5 ) . 'px ' .
957 $instance['custom_padding'] . 'px ' .
958 round( floatval( $instance['custom_padding'] ) * 1.5 ) . 'px; ';
959
960 $submit_button_styles .= $style;
961 $email_field_styles .= $style;
962 }
963
964 $button_spacing = 0;
965 if ( ! empty( $instance['custom_spacing'] ) ) {
966 $button_spacing = $instance['custom_spacing'];
967 }
968 if ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] ) {
969 $submit_button_styles .= 'margin-top: ' . $button_spacing . 'px; ';
970 } else {
971 $submit_button_styles .= 'margin: 0; '; // Reset Safari's 2px default margin for buttons affecting input and button union
972 $submit_button_styles .= 'margin-left: ' . $button_spacing . 'px; ';
973 }
974
975 if ( isset( $instance['custom_border_radius'] ) && 'undefined' !== $instance['custom_border_radius'] ) {
976 $style = 'border-radius: ' . $instance['custom_border_radius'] . 'px; ';
977 $submit_button_styles .= $style;
978 $email_field_styles .= $style;
979 }
980 if ( isset( $instance['custom_border_weight'] ) && 'undefined' !== $instance['custom_border_weight'] ) {
981 $style = 'border-width: ' . $instance['custom_border_weight'] . 'px; ';
982 $submit_button_styles .= $style;
983 $email_field_styles .= $style;
984 }
985 if ( isset( $instance['custom_border_color'] ) && 'undefined' !== $instance['custom_border_color'] ) {
986 $style =
987 'border-color: ' . $instance['custom_border_color'] . '; ' .
988 'border-style: solid;';
989
990 $submit_button_styles .= $style;
991 $email_field_styles .= $style;
992 }
993 if ( isset( $instance['success_message'] ) && 'undefined' !== $instance['success_message'] ) {
994 $success_message = wp_kses( stripslashes( $instance['success_message'] ), array() );
995 }
996
997 $instance = shortcode_atts(
998 Jetpack_Subscriptions_Widget::defaults(),
999 $instance,
1000 'jetpack_subscription_form'
1001 );
1002
1003 // These must come after the call to shortcode_atts().
1004 $instance['submit_button_text'] = $submit_button_text;
1005 $instance['show_only_email_and_button'] = $show_only_email_and_button;
1006 if ( ! empty( $submit_button_classes ) ) {
1007 $instance['submit_button_classes'] = $submit_button_classes;
1008 }
1009 if ( ! empty( $email_field_classes ) ) {
1010 $instance['email_field_classes'] = $email_field_classes;
1011 }
1012
1013 if ( ! empty( $submit_button_styles ) ) {
1014 $instance['submit_button_styles'] = trim( $submit_button_styles );
1015 }
1016 if ( ! empty( $submit_button_wrapper_styles ) ) {
1017 $instance['submit_button_wrapper_styles'] = trim( $submit_button_wrapper_styles );
1018 }
1019 if ( ! empty( $email_field_styles ) ) {
1020 $instance['email_field_styles'] = trim( $email_field_styles );
1021 }
1022 if ( ! empty( $success_message ) ) {
1023 $instance['success_message'] = trim( $success_message );
1024 }
1025
1026 $args = array(
1027 'before_widget' => '<div class="jetpack_subscription_widget">',
1028 );
1029 ob_start();
1030 the_widget( get_jetpack_blog_subscriptions_widget_classname(), $instance, $args );
1031 $output = ob_get_clean();
1032
1033 return $output;
1034 }
1035
1036 add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' );
1037 add_shortcode( 'blog_subscription_form', 'jetpack_do_subscription_form' );
1038
1039 /**
1040 * Register the Subscriptions widget.
1041 */
1042 function jetpack_blog_subscriptions_init() {
1043 register_widget( get_jetpack_blog_subscriptions_widget_classname() );
1044 }
1045
1046 add_action( 'widgets_init', 'jetpack_blog_subscriptions_init' );
1047
1048 /**
1049 * Sets the default value for `subscription_options` site option.
1050 *
1051 * This default value is available across Simple, Atomic and Jetpack sites,
1052 * including the /sites/$site/settings endpoint.
1053 *
1054 * @param array $default Default `subscription_options` array.
1055 * @param string $option Option name.
1056 * @param bool $passed_default Whether `get_option()` passed a default value.
1057 *
1058 * @return array Default value of `subscription_options`.
1059 */
1060 function subscription_options_fallback( $default, $option, $passed_default ) {
1061 if ( $passed_default ) {
1062 return $default;
1063 }
1064
1065 $site_url = get_home_url();
1066 $display_url = preg_replace( '(^https?://)', '', untrailingslashit( $site_url ) );
1067
1068 return array(
1069 /* translators: Both %1$s and %2$s is site address */
1070 '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 ),
1071 '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' ),
1072 /* translators: %1$s is the site address */
1073 '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 ),
1074 );
1075 }
1076
1077 add_filter( 'default_option_subscription_options', 'subscription_options_fallback', 10, 3 );
1078