* @param string subscribe-field Subscription form field prefix.
* @param int $widget_id Widget ID.
$subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
$form_id = self::get_redirect_fragment( $widget_id );
<div class="wp-block-jetpack-subscriptions__container">
<form action="#" method="post" accept-charset="utf-8" id="<?php echo esc_attr( $form_id ); ?>"
data-blog="<?php echo esc_attr( \Jetpack_Options::get_option( 'id' ) ); ?>"
data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>" >
if ( $subscribe_text && ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Non-sensitive informational output.
<div id="subscribe-text"><?php echo wp_kses( wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total ), $subscribe_text ) ), 'post' ); ?></div>
if ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display of unsubmitted form.
<label id="jetpack-subscribe-label"
class="screen-reader-text"
for="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>">
<?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?>
<input type="email" name="email" required="required"
<?php if ( ! empty( $email_field_classes ) ) { ?>
class="<?php echo esc_attr( $email_field_classes ); ?> required"
<?php if ( ! empty( $email_field_styles ) ) { ?>
style="<?php echo esc_attr( $email_field_styles ); ?>"
value="<?php echo esc_attr( $subscribe_email ); ?>"
id="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>"
placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"
<?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
<input type="hidden" name="action" value="subscribe"/>
<input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
<input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
<input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
<?php wp_nonce_field( 'blogsub_subscribe_' . \Jetpack_Options::get_option( 'id' ) ); ?>
<?php if ( ! empty( $submit_button_classes ) ) { ?>
class="<?php echo esc_attr( $submit_button_classes ); ?>"
<?php if ( ! empty( $submit_button_styles ) ) { ?>
style="<?php echo esc_attr( $submit_button_styles ); ?>"
name="jetpack_subscriptions_widget"
html_entity_decode( $subscribe_button, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
self::$allowed_html_tags_for_submit_button
<?php if ( $show_subscribers_total && 0 < $subscribers_total ) { ?>
<div class="wp-block-jetpack-subscriptions__subscount">
echo esc_html( Jetpack_Memberships::get_join_others_text( $subscribers_total ) );
* Determines if the current user is subscribed to the blog.
* @return bool Is the person already subscribed.
public static function is_current_user_subscribed() {
$subscribed = isset( $_GET['subscribe'] ) && 'success' === $_GET['subscribe']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
$subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
* Is this script running in the wordpress.com environment?
public static function is_wpcom() {
return defined( 'IS_WPCOM' ) && IS_WPCOM;
* Is this script running in a self-hosted environment?
public static function is_jetpack() {
return ! self::is_wpcom();
* Used to determine if there is a valid status slug within the wordpress.com environment.
public static function wpcom_has_status_message() {
return isset( $_GET['blogsub'] ) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$_GET['blogsub'], // phpcs:ignore WordPress.Security.NonceVerification.Recommended
* Determine the amount of folks currently subscribed to the blog.
public static function fetch_subscriber_count() {
if ( self::is_jetpack() ) {
$cache_key = 'wpcom_subscribers_total';
$subs_count = get_transient( $cache_key );
if ( false === $subs_count || 'failed' === $subs_count['status'] ) {
$xml = new Jetpack_IXR_Client();
$xml->query( 'jetpack.fetchSubscriberCount' );
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.
'code' => $xml->getErrorCode(),
'message' => $xml->getErrorMessage(),
'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
'value' => $xml->getResponse(),
set_transient( $cache_key, $subs_count, 3600 ); // Try to cache the result for at least 1 hour.
return $subs_count['value'];
if ( self::is_wpcom() ) {
$subs_count = wpcom_reach_total_for_blog();
* Updates a particular instance of a widget when someone saves it in wp-admin.
* @param array $new_instance New widget instance settings.
* @param array $old_instance Old widget instance settings.
public function update( $new_instance, $old_instance ) {
// Merge new values over old, then fill in any missing keys with defaults
$instance = wp_parse_args( (array) $new_instance, wp_parse_args( (array) $old_instance, static::defaults() ) );
if ( self::is_jetpack() ) {
$instance['title'] = wp_kses( stripslashes( $instance['title'] ), array() );
$instance['subscribe_placeholder'] = wp_kses( stripslashes( $instance['subscribe_placeholder'] ), array() );
$instance['subscribe_button'] = wp_kses( stripslashes( $instance['subscribe_button'] ), array() );
$instance['success_message'] = wp_kses( stripslashes( $instance['success_message'] ), array() );
if ( self::is_wpcom() ) {
$instance['title'] = wp_strip_all_tags( stripslashes( $instance['title'] ) );
$instance['title_following'] = isset( $instance['title_following'] ) ? wp_strip_all_tags( stripslashes( $instance['title_following'] ) ) : '';
$instance['subscribe_logged_in'] = isset( $instance['subscribe_logged_in'] ) ? wp_filter_post_kses( stripslashes( $instance['subscribe_logged_in'] ) ) : '';
$instance['subscribe_button'] = wp_strip_all_tags( stripslashes( $instance['subscribe_button'] ) );
$instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
$instance['show_only_email_and_button'] = isset( $new_instance['show_only_email_and_button'] ) && $new_instance['show_only_email_and_button'];
$instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $instance['subscribe_text'] ) );
* The default args for rendering a subscription form.
public static function defaults() {
'show_subscribers_total' => true,
'show_only_email_and_button' => false,
'include_social_followers' => true,
$defaults['title'] = esc_html__( 'Subscribe to Blog via Email', 'jetpack' );
$defaults['subscribe_text'] = esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' );
$defaults['subscribe_placeholder'] = esc_html__( 'Email Address', 'jetpack' );
$defaults['subscribe_button'] = esc_html__( 'Subscribe', 'jetpack' );
$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' );
* Renders the widget's options form in wp-admin.
* @param array $instance Widget instance.
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, static::defaults() );
$show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
if ( self::is_wpcom() ) {
$title = ! empty( $instance['title'] ) ? esc_attr( stripslashes( $instance['title'] ) ) : '';
$title_following = ! empty( $instance['title_following'] ) ? esc_attr( stripslashes( $instance['title_following'] ) ) : '';
$subscribe_text = ! empty( $instance['subscribe_text'] ) ? esc_attr( stripslashes( $instance['subscribe_text'] ) ) : '';
$subscribe_logged_in = ! empty( $instance['subscribe_logged_in'] ) ? esc_attr( stripslashes( $instance['subscribe_logged_in'] ) ) : '';
$subscribe_button = ! empty( $instance['subscribe_button'] ) ? esc_attr( stripslashes( $instance['subscribe_button'] ) ) : '';
$subscribers_total = self::fetch_subscriber_count();
if ( self::is_jetpack() ) {
$title = ! empty( $instance['title'] ) ? stripslashes( $instance['title'] ) : '';
$subscribe_text = ! empty( $instance['subscribe_text'] ) ? stripslashes( $instance['subscribe_text'] ) : '';
$subscribe_placeholder = ! empty( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
$subscribe_button = ! empty( $instance['subscribe_button'] ) ? stripslashes( $instance['subscribe_button'] ) : '';
$success_message = ! empty( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
$subscribers_total = self::fetch_subscriber_count();
if ( self::is_wpcom() ) :
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
<?php esc_html_e( 'Widget title for non-followers:', 'jetpack' ); ?>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
value="<?php echo esc_attr( $title ); ?>"/>
<label for="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>">
<?php esc_html_e( 'Widget title for followers:', 'jetpack' ); ?>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title_following' ) ); ?>" type="text"
value="<?php echo esc_attr( $title_following ); ?>"/>
<label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>">
<?php esc_html_e( 'Optional text to display to logged in WordPress.com users:', 'jetpack' ); ?>
<textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'subscribe_logged_in' ) ); ?>"
type="text"><?php echo esc_html( $subscribe_logged_in ); ?></textarea>
<label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
<?php esc_html_e( 'Optional text to display to non-WordPress.com users:', 'jetpack' ); ?>
<textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
type="text"><?php echo esc_html( $subscribe_text ); ?></textarea>
<label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
<?php esc_html_e( 'Follow Button Text:', 'jetpack' ); ?>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
value="<?php echo esc_attr( $subscribe_button ); ?>"/>
<label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
<input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
/* translators: %s: Number of followers. */
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 ) ) );
if ( self::is_jetpack() ) :
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
<?php esc_html_e( 'Widget title:', 'jetpack' ); ?>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
value="<?php echo esc_attr( $title ); ?>"/>
<label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
<?php esc_html_e( 'Optional text to display to your readers:', 'jetpack' ); ?>
<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
rows="3"><?php echo esc_html( $subscribe_text ); ?></textarea>
<label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>">
<?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'subscribe_placeholder' ) ); ?>" type="text"
value="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
<label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
<?php esc_html_e( 'Subscribe Button:', 'jetpack' ); ?>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
value="<?php echo esc_attr( $subscribe_button ); ?>"/>
<label for="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>">
<?php esc_html_e( 'Success Message Text:', 'jetpack' ); ?>
<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'success_message' ) ); ?>"
rows="5"><?php echo esc_html( $success_message ); ?></textarea>
<label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
<input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
/* translators: %s: Number of subscribers. */
echo esc_html( sprintf( _n( 'Show total number of subscribers? (%s subscriber)', 'Show total number of subscribers? (%s subscribers)', $subscribers_total, 'jetpack' ), $subscribers_total ) );
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' );
* Classname / shortcode tag to use for the Subscriptions widget.
function get_jetpack_blog_subscriptions_widget_classname() {
return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
'Blog_Subscription_Widget' :
'Jetpack_Subscriptions_Widget';
* Subscriptions widget form HTML output.
* @param array $instance Widget instance data.
function jetpack_do_subscription_form( $instance ) {
if ( empty( $instance ) || ! is_array( $instance ) ) {
if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
$instance['show_subscribers_total'] = false;
$instance['show_subscribers_total'] = true;
// the default behavior is to include the social followers
if ( empty( $instance['include_social_followers'] ) || 'true' === $instance['include_social_followers'] ) {
$instance['include_social_followers'] = true;
$instance['include_social_followers'] = false;
$show_only_email_and_button = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
$submit_button_text = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
// Build up a string with the submit button's classes and styles and set it on the instance.
$submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
$email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
$submit_button_styles = '';
$submit_button_wrapper_styles = '';
$email_field_styles = '';
if ( isset( $instance['custom_background_button_color'] ) && 'undefined' !== $instance['custom_background_button_color'] ) {
$submit_button_styles .= 'background: ' . $instance['custom_background_button_color'] . '; ';
if ( isset( $instance['custom_text_button_color'] ) && 'undefined' !== $instance['custom_text_button_color'] ) {
$submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . '; ';
if ( isset( $instance['custom_button_width'] ) && 'undefined' !== $instance['custom_button_width'] ) {
$submit_button_wrapper_styles .= 'width: ' . $instance['custom_button_width'] . '; ';
$submit_button_wrapper_styles .= 'max-width: 100%; ';
// Account for custom margins on inline forms.
! empty( $instance['custom_spacing'] ) &&
! ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] )
$submit_button_styles .= 'width: calc(100% - ' . $instance['custom_spacing'] . 'px); ';
$submit_button_styles .= 'width: 100%; ';
if ( isset( $instance['custom_font_size'] ) && 'undefined' !== $instance['custom_font_size'] ) {
$style = 'font-size: ' . $instance['custom_font_size'];
$style .= is_numeric( $instance['custom_font_size'] ) ? 'px; ' : '; '; // Handle deprecated numeric font size values.
$submit_button_styles .= $style;
$email_field_styles .= $style;
if ( isset( $instance['custom_padding'] ) && 'undefined' !== $instance['custom_padding'] ) {
$instance['custom_padding'] . 'px ' .
round( floatval( $instance['custom_padding'] ) * 1.5 ) . 'px ' .
$instance['custom_padding'] . 'px ' .
round( floatval( $instance['custom_padding'] ) * 1.5 ) . 'px; ';
$submit_button_styles .= $style;
$email_field_styles .= $style;
if ( ! empty( $instance['custom_spacing'] ) ) {
$button_spacing = $instance['custom_spacing'];
if ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] ) {
$submit_button_styles .= 'margin-top: ' . $button_spacing . 'px; ';
$submit_button_styles .= 'margin: 0; '; // Reset Safari's 2px default margin for buttons affecting input and button union
$submit_button_styles .= 'margin-left: ' . $button_spacing . 'px; ';
if ( isset( $instance['custom_border_radius'] ) && 'undefined' !== $instance['custom_border_radius'] ) {
$style = 'border-radius: ' . $instance['custom_border_radius'] . 'px; ';
$submit_button_styles .= $style;
$email_field_styles .= $style;
if ( isset( $instance['custom_border_weight'] ) && 'undefined' !== $instance['custom_border_weight'] ) {
$style = 'border-width: ' . $instance['custom_border_weight'] . 'px; ';
$submit_button_styles .= $style;
$email_field_styles .= $style;
if ( isset( $instance['custom_border_color'] ) && 'undefined' !== $instance['custom_border_color'] ) {
'border-color: ' . $instance['custom_border_color'] . '; ' .
$submit_button_styles .= $style;
$email_field_styles .= $style;
if ( isset( $instance['success_message'] ) && 'undefined' !== $instance['success_message'] ) {
$success_message = wp_kses( stripslashes( $instance['success_message'] ), array() );
$instance = shortcode_atts(
Jetpack_Subscriptions_Widget::defaults(),
'jetpack_subscription_form'