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