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