array() ); function __construct() { $widget_ops = array( 'classname' => 'widget_blog_subscription jetpack_subscription_widget', 'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ), 'customize_selective_refresh' => true, ); $name = self::is_jetpack() ? /** This filter is documented in modules/widgets/facebook-likebox.php */ apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ) : __( 'Follow Blog', 'jetpack' ); parent::__construct( 'blog_subscription', $name, $widget_ops ); if ( self::is_jetpack() && ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) ) { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); } } /** * Enqueue the form's CSS. * * @since 4.5.0 */ function enqueue_style() { wp_register_style( 'jetpack-subscriptions', plugins_url( 'subscriptions.css', __FILE__ ), array(), JETPACK__VERSION ); wp_enqueue_style( 'jetpack-subscriptions' ); } /** * Renders a full widget either within the context of WordPress widget, or in response to a shortcode. * * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'. * @param array $instance The settings for the particular instance of the widget. */ function widget( $args, $instance ) { if ( self::is_jetpack() && /** This filter is documented in modules/contact-form/grunion-contact-form.php */ false === apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) { $subscribe_email = ''; } else { $current_user = wp_get_current_user(); if ( ! empty( $current_user->user_email ) ) { $subscribe_email = esc_attr( $current_user->user_email ); } else { $subscribe_email = ''; } } $stats_action = self::is_jetpack() ? 'jetpack_subscriptions' : 'follow_blog'; /** This action is documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', $stats_action ); $after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : ''; $before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : ''; $instance = wp_parse_args( (array) $instance, $this->defaults() ); echo $before_widget; Jetpack_Subscriptions_Widget::$instance_count ++; self::render_widget_title( $args, $instance ); self::render_widget_status_messages( $instance ); if ( self::is_current_user_subscribed() ) { self::render_widget_already_subscribed( $instance ); } else { self::render_widget_subscription_form( $args, $instance, $subscribe_email ); } echo "\n" . $after_widget; } /** * Prints the widget's title. If show_only_email_and_button is true, we will not show a title. * * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'. * @param array $instance The settings for the particular instance of the widget. */ static function render_widget_title( $args, $instance ) { $show_only_email_and_button = $instance['show_only_email_and_button']; $before_title = isset( $args['before_title'] ) ? $args['before_title'] : ''; $after_title = isset( $args['after_title'] ) ? $args['after_title'] : ''; if ( self::is_wpcom() && ! $show_only_email_and_button ) { if ( self::is_current_user_subscribed() ) { if ( ! empty( $instance['title_following'] ) ) { echo $before_title . '' . $after_title . "\n"; } } else { if ( ! empty( $instance['title'] ) ) { echo $before_title . '' . $after_title . "\n"; } } } if ( self::is_jetpack() && empty( $instance['show_only_email_and_button'] ) ) { echo $args['before_title'] . esc_attr( $instance['title'] ) . $args['after_title'] . "\n"; } } /** * Prints the subscription block's status messages after someone has attempted to subscribe. * Either a success message or an error message. * * @param array $instance The settings for the particular instance of the widget. */ static function render_widget_status_messages( $instance ) { if ( self::is_jetpack() && isset( $_GET['subscribe'] ) ) { $success_message = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : ''; $subscribers_total = self::fetch_subscriber_count(); switch ( $_GET['subscribe'] ) : case 'invalid_email' : ?>

You can manage your preferences at subscribe.wordpress.com', 'jetpack' ), 'https://subscribe.wordpress.com/', __( 'Manage your email preferences.', 'jetpack' ) ); ?>

You can manage your preferences at subscribe.wordpress.com', 'jetpack' ), 'https://subscribe.wordpress.com/', __( 'Manage your email preferences.', 'jetpack' ) ); ?>

"; _e( 'Thanks for subscribing! You’ll get an email with a link to confirm your subscription. If you don’t get it, please contact us.' ); echo ""; break; case 'blocked': echo "
"; _e( 'Subscriptions have been blocked for this email address.' ); echo "
"; break; case 'flooded': echo "
"; _e( 'You already have several pending email subscriptions. Approve or delete a few through your Subscription Manager before attempting to subscribe to more blogs.' ); echo "
"; break; case 'spammed': echo "
"; echo wp_kses_post( sprintf( __( 'Because there are many pending subscriptions for this email address, we have blocked the subscription. Please activate or delete pending subscriptions before attempting to subscribe.' ), 'https://subscribe.wordpress.com/' ) ); echo "
"; break; case 'subscribed': echo "
"; _e( 'You’re already subscribed to this site.' ); echo "
"; break; case 'pending': echo "
"; _e( 'You have a pending subscription already; we just sent you another email. Click the link or contact us if you don’t receive it.' ); echo "
"; break; case 'confirmed': echo "
"; _e( 'Congrats, you’re subscribed! You’ll get an email with the details of your subscription and an unsubscribe link.' ); echo "
"; break; } } } /** * Renders a message to folks who are already subscribed. * * @param array $instance The settings for the particular instance of the widget. * * @return void */ static function render_widget_already_subscribed( $instance ) { if ( self::is_wpcom() ) { $subscribers_total = self::fetch_subscriber_count(); $edit_subs_url = 'https://wordpress.com/following/edit/'; if ( function_exists( 'localized_wpcom_url' ) ) { $edit_subs_url = localized_wpcom_url( http() . '://wordpress.com/following/edit/', get_user_locale() ); } $show_subscribers_total = (bool) $instance['show_subscribers_total']; if ( $show_subscribers_total && $subscribers_total > 1 ) : $subscribers_not_me = $subscribers_total - 1; /* translators: %s: number of folks following the blog */ ?>

manage).', 'You are following this blog, along with %s other amazing people (manage).', $subscribers_not_me ), number_format_i18n( $subscribers_not_me ), $edit_subs_url ) ?>

manage).' ), $edit_subs_url ) ?>

blog_id, '_wpnonce', false ); ?>

JETPACK_MASTER_USER, ) ); $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 $subs_count = array( 'status' => 'failed', 'code' => $xml->getErrorCode(), 'message' => $xml->getErrorMessage(), 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0, ); } else { $subs_count = array( 'status' => 'success', 'value' => $xml->getResponse(), ); } set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour } } if ( self::is_wpcom() && function_exists( 'wpcom_reach_total_for_blog' ) ) { $subs_count = wpcom_reach_total_for_blog(); } return $subs_count; } /** * Updates a particular instance of a widget when someone saves it in wp-admin. * * @param array $new_instance * @param array $old_instance * * @return array */ function update( $new_instance, $old_instance ) { $instance = $old_instance; if ( self::is_jetpack() ) { $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() ); $instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() ); $instance['subscribe_button'] = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() ); $instance['success_message'] = wp_kses( stripslashes( $new_instance['success_message'] ), array() ); } if ( self::is_wpcom() ) { $instance['title'] = strip_tags( stripslashes( $new_instance['title'] ) ); $instance['title_following'] = strip_tags( stripslashes( $new_instance['title_following'] ) ); $instance['subscribe_logged_in'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_logged_in'] ) ); $instance['subscribe_button'] = strip_tags( stripslashes( $new_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( $new_instance['subscribe_text'] ) ); return $instance; } /** * The default args for rendering a subscription form. * * @return array */ static function defaults() { $defaults = array( 'show_subscribers_total' => true, 'show_only_email_and_button' => false ); if ( self::is_jetpack() ) { $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 Follow' to start subscribing.", 'jetpack' ); } if ( self::is_wpcom() ) { $defaults['title'] = __( 'Follow Blog via Email' ); $defaults['title_following'] = __( 'You are following this blog' ); $defaults['subscribe_text'] = __( 'Enter your email address to follow this blog and receive notifications of new posts by email.' ); $defaults['subscribe_button'] = __( 'Follow' ); $defaults['subscribe_logged_in'] = __( 'Click to follow this blog and receive notifications of new posts by email.' ); } return $defaults; } /** * Renders the widget's options form in wp-admin. * * @param array $instance */ function form( $instance ) { $instance = wp_parse_args( (array) $instance, $this->defaults() ); $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false ); if ( self::is_wpcom() ) { $title = esc_attr( stripslashes( $instance['title'] ) ); $title_following = esc_attr( stripslashes( $instance['title_following'] ) ); $subscribe_text = esc_attr( stripslashes( $instance['subscribe_text'] ) ); $subscribe_logged_in = esc_attr( stripslashes( $instance['subscribe_logged_in'] ) ); $subscribe_button = esc_attr( stripslashes( $instance['subscribe_button'] ) ); $subscribers_total = self::fetch_subscriber_count(); } if ( self::is_jetpack() ) { $title = stripslashes( $instance['title'] ); $subscribe_text = stripslashes( $instance['subscribe_text'] ); $subscribe_placeholder = stripslashes( $instance['subscribe_placeholder'] ); $subscribe_button = stripslashes( $instance['subscribe_button'] ); $success_message = stripslashes( $instance['success_message'] ); $subs_fetch = self::fetch_subscriber_count(); if ( 'failed' == $subs_fetch['status'] ) { printf( '

%s: %s

', esc_html( $subs_fetch['code'] ), esc_html( $subs_fetch['message'] ) ); } $subscribers_total = number_format_i18n( $subs_fetch['value'] ); } if ( self::is_wpcom() ) : ?>

'
', ); ob_start(); the_widget( get_jetpack_blog_subscriptions_widget_classname(), $instance, $args ); $output = ob_get_clean(); return $output; } add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' ); add_shortcode( 'blog_subscription_form', 'jetpack_do_subscription_form' ); function jetpack_blog_subscriptions_init() { register_widget( get_jetpack_blog_subscriptions_widget_classname() ); } add_action( 'widgets_init', 'jetpack_blog_subscriptions_init' ); function jetpack_register_subscriptions_block() { if ( class_exists( 'WP_Block_Type_Registry' ) && ! WP_Block_Type_Registry::get_instance()->is_registered( 'jetpack/subscriptions' ) ) { jetpack_register_block( 'jetpack/subscriptions' ); } } add_action( 'init', 'jetpack_register_subscriptions_block' );