<input type="checkbox" name="social_notifications_subscribe" id="social_notifications_subscribe" value="1" <?php checked( $checked ); ?> />
/* translators: this is a label for a setting that starts with "Email me whenever" */
esc_html_e( 'Someone subscribes to my blog', 'jetpack' );
* Validate "Someone subscribes to my blog" option
* @param String $input the input string to be validated.
public function social_notifications_subscribe_validate( $input ) {
// If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'.
if ( ! $input || 'off' === $input ) {
// Otherwise we return 'on'.
* Jetpack_Subscriptions::subscribe()
* Send a synchronous XML-RPC subscribe to blog posts or subscribe to post comments request.
* @param string $email being subscribed.
* @param array $post_ids (optional) defaults to 0 for blog posts only: array of post IDs to subscribe to blog's posts.
* @param bool $async (optional) Should the subscription be performed asynchronously? Defaults to true.
* @param array $extra_data Additional data passed to the `jetpack.subscribeToSite` call.
* @return true|WP_Error true on success
* invalid_email : not a valid email address
* invalid_post_id : not a valid post ID
* unknown_post_id : unknown post
* not_subscribed : strange error. Jetpack servers at WordPress.com could subscribe the email.
* disabled : Site owner has disabled subscriptions.
* active : Already subscribed.
* pending : Tried to subscribe before but the confirmation link is never clicked. No confirmation email is sent.
* unknown : strange error. Jetpack servers at WordPress.com returned something malformed.
* unknown_status : strange error. Jetpack servers at WordPress.com returned something I didn't understand.
public function subscribe( $email, $post_ids = 0, $async = true, $extra_data = array() ) {
if ( ! is_email( $email ) ) {
return new WP_Error( 'invalid_email' );
$xml = new Jetpack_IXR_ClientMulticall();
foreach ( (array) $post_ids as $post_id ) {
$post_id = (int) $post_id;
return new WP_Error( 'invalid_post_id' );
} elseif ( $post_id && ! get_post( $post_id ) ) {
return new WP_Error( 'unknown_post_id' );
XMLRPC_Async_Call::add_call( 'jetpack.subscribeToSite', 0, $email, $post_id, serialize( $extra_data ) ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
$xml->addCall( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
return $xml->get_jetpack_error();
$responses = $xml->getResponse();
foreach ( (array) $responses as $response ) {
if ( isset( $response['faultCode'] ) || isset( $response['faultString'] ) ) {
$r[] = $xml->get_jetpack_error( $response['faultCode'], $response['faultString'] );
if ( ! is_array( $response[0] ) || empty( $response[0]['status'] ) ) {
$r[] = new WP_Error( 'unknown' );
switch ( $response[0]['status'] ) {
$r[] = new WP_Error( 'not_subscribed' );
$r[] = new WP_Error( 'disabled' );
$r[] = new WP_Error( 'active' );
$r[] = new WP_Error( 'pending' );
$r[] = new WP_Error( 'unknown_status', (string) $response[0]['status'] );
* Jetpack_Subscriptions::widget_submit()
* When a user submits their email via the blog subscription widget, check the details and call the subsribe() method.
public function widget_submit() {
if ( ! wp_verify_nonce( isset( $_REQUEST['_wpnonce'] ) ? sanitize_key( $_REQUEST['_wpnonce'] ) : '', 'blogsub_subscribe_' . \Jetpack_Options::get_option( 'id' ) ) ) {
if ( empty( $_REQUEST['email'] ) || ! is_string( $_REQUEST['email'] ) ) {
$redirect_fragment = false;
if ( isset( $_REQUEST['redirect_fragment'] ) ) {
$redirect_fragment = preg_replace( '/[^a-z0-9_-]/i', '', $_REQUEST['redirect_fragment'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is manually unslashing and sanitizing.
if ( ! $redirect_fragment || ! is_string( $redirect_fragment ) ) {
$redirect_fragment = 'subscribe-blog';
$subscribe = self::subscribe(
isset( $_REQUEST['email'] ) ? wp_unslash( $_REQUEST['email'] ) : null, // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated inside self::subscribe().
'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
if ( is_wp_error( $subscribe ) ) {
$error = $subscribe->get_error_code();
foreach ( $subscribe as $response ) {
if ( is_wp_error( $response ) ) {
$error = $response->get_error_code();
$result = 'many_pending_subs';
$redirect = add_query_arg( 'subscribe', $result );
* Fires on each subscription form submission.
* @param string $result Result of form submission: success, invalid_email, already, error.
do_action( 'jetpack_subscriptions_form_submission', $result );
wp_safe_redirect( "$redirect#$redirect_fragment" );
* Jetpack_Subscriptions::comment_subscribe_init()
* Set up and add the comment subscription checkbox to the comment form.
* @param string $submit_button HTML markup for the submit field.
public function comment_subscribe_init( $submit_button ) {
// Subscriptions are only available for posts so far.
if ( ! $post || 'post' !== $post->post_type ) {
// Check for a comment / blog submission and set a cookie to retain the setting and check the boxes.
if ( isset( $_COOKIE[ 'jetpack_comments_subscribe_' . self::$hash . '_' . $post->ID ] ) ) {
$comments_checked = ' checked="checked"';
if ( isset( $_COOKIE[ 'jetpack_blog_subscribe_' . self::$hash ] ) ) {
$blog_checked = ' checked="checked"';
// Some themes call this function, don't show the checkbox again.
remove_action( 'comment_form', 'subscription_comment_form' );
// Check if Mark Jaquith's Subscribe to Comments plugin is active - if so, suppress Jetpack checkbox.
if ( false === has_filter( 'comment_form', 'show_subscription_checkbox' ) && 1 === (int) get_option( 'stc_enabled', 1 ) && empty( $post->post_password ) && 'post' === get_post_type() ) {
// Subscribe to comments checkbox.
$str .= '<p class="comment-subscription-form"><input type="checkbox" name="subscribe_comments" id="subscribe_comments" value="subscribe" style="width: auto; -moz-appearance: checkbox; -webkit-appearance: checkbox;"' . $comments_checked . ' /> ';
$comment_sub_text = __( 'Notify me of follow-up comments by email.', 'jetpack' );
$str .= '<label class="subscribe-label" id="subscribe-label" for="subscribe_comments">' . esc_html(
* Filter the Subscribe to comments text appearing below the comment form.
* @param string $comment_sub_text Subscribe to comments text.
apply_filters( 'jetpack_subscribe_comment_label', $comment_sub_text )
if ( 1 === (int) get_option( 'stb_enabled', 1 ) ) {
// Subscribe to blog checkbox.
$str .= '<p class="comment-subscription-form"><input type="checkbox" name="subscribe_blog" id="subscribe_blog" value="subscribe" style="width: auto; -moz-appearance: checkbox; -webkit-appearance: checkbox;"' . $blog_checked . ' /> ';
$blog_sub_text = __( 'Notify me of new posts by email.', 'jetpack' );
$str .= '<label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog">' . esc_html(
* Filter the Subscribe to blog text appearing below the comment form.
* @param string $comment_sub_text Subscribe to blog text.
apply_filters( 'jetpack_subscribe_blog_label', $blog_sub_text )
* Filter the output of the subscription options appearing below the comment form.
* @param string $str Comment Subscription form HTML output.
$str = apply_filters( 'jetpack_comment_subscription_form', $str );
return $str . $submit_button;
* Jetpack_Subscriptions::comment_subscribe_init()
* When a user checks the comment subscribe box and submits a comment, subscribe them to the comment thread.
* @param int|string $comment_id Comment thread being subscribed to.
* @param string $approved Comment status.
public function comment_subscribe_submit( $comment_id, $approved ) {
if ( 'spam' === $approved ) {
$comment = get_comment( $comment_id );
// Set cookies for this post/comment.
$this->set_cookies( isset( $_REQUEST['subscribe_comments'] ), $comment->comment_post_ID, isset( $_REQUEST['subscribe_blog'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_REQUEST['subscribe_comments'] ) && ! isset( $_REQUEST['subscribe_blog'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_REQUEST['subscribe_comments'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$post_ids[] = $comment->comment_post_ID;
if ( isset( $_REQUEST['subscribe_blog'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$result = self::subscribe(
$comment->comment_author_email,
'source' => 'comment-form',
'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
'comment_status' => $approved,
'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
* Fires on each comment subscription form submission.
* @param NULL|WP_Error $result Result of form submission: NULL on success, WP_Error otherwise.
* @param array $post_ids An array of post IDs that the user subscribed to, 0 means blog subscription.
do_action( 'jetpack_subscriptions_comment_form_submission', $result, $post_ids );
* Jetpack_Subscriptions::set_cookies()
* Set a cookie to save state on the comment and post subscription checkboxes.
* @param bool $subscribe_to_post Whether the user chose to subscribe to subsequent comments on this post.
* @param int $post_id If $subscribe_to_post is true, the post ID they've subscribed to.
* @param bool $subscribe_to_blog Whether the user chose to subscribe to all new posts on the blog.
public function set_cookies( $subscribe_to_post = false, $post_id = null, $subscribe_to_blog = false ) {
$post_id = (int) $post_id;
/** This filter is already documented in core/wp-includes/comment-functions.php */
$cookie_lifetime = apply_filters( 'comment_cookie_lifetime', YEAR_IN_SECONDS );
* Filter the Jetpack Comment cookie path.
* @param string COOKIEPATH Cookie path.
$cookie_path = apply_filters( 'jetpack_comment_cookie_path', COOKIEPATH );
* Filter the Jetpack Comment cookie domain.
* @param string COOKIE_DOMAIN Cookie domain.
$cookie_domain = apply_filters( 'jetpack_comment_cookie_domain', COOKIE_DOMAIN );
if ( $subscribe_to_post && $post_id >= 0 ) {
setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true );
setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, '', time() - 3600, $cookie_path, $cookie_domain, is_ssl(), true );
if ( $subscribe_to_blog ) {
setcookie( 'jetpack_blog_subscribe_' . self::$hash, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true );
setcookie( 'jetpack_blog_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain, is_ssl(), true );
* Set the social_notifications_subscribe option to `off` when the Subscriptions module is activated in the first time.
public function set_social_notifications_subscribe() {
if ( false === get_option( 'social_notifications_subscribe' ) ) {
add_option( 'social_notifications_subscribe', 'off' );
* Set the featured image in email option to `1` when the Subscriptions module is activated in the first time.
public function set_featured_image_in_email_default() {
add_option( 'wpcom_featured_image_in_email', 1 );
* Save a flag when a post was ever published.
* It saves the post meta when the post was published and becomes a draft.
* Then this meta is used to hide subscription messaging in Publish panel.
* @param string $new_status Tthe "new" post status of the transition when saved.
* @param string $old_status The "old" post status of the transition when saved.
* @param object $post obj The post object.
public function maybe_set_first_published_status( $new_status, $old_status, $post ) {
$was_post_ever_published = get_post_meta( $post->ID, '_jetpack_post_was_ever_published', true );
if ( ! $was_post_ever_published && 'publish' === $old_status && 'draft' === $new_status ) {
update_post_meta( $post->ID, '_jetpack_post_was_ever_published', true );
* Checks if the current user can publish posts.
public function first_published_status_meta_auth_callback() {
* Filter the capability to view if a post was ever published in the Subscription Module.
* @param string $capability User capability needed to view if a post was ever published. Default to publish_posts.
$capability = apply_filters( 'jetpack_subscriptions_post_was_ever_published_capability', 'publish_posts' );
if ( current_user_can( $capability ) ) {
* Registers the 'post_was_ever_published' post meta for use in the REST API.
public function register_post_meta() {
$jetpack_post_was_ever_published = array(
'description' => __( 'Whether the post was ever published.', 'jetpack' ),
'name' => 'jetpack_post_was_ever_published',
'auth_callback' => array( $this, 'first_published_status_meta_auth_callback' ),
register_meta( 'post', '_jetpack_post_was_ever_published', $jetpack_post_was_ever_published );
* Create a Subscribers menu displayed on self-hosted sites.
* - It is not displayed on WordPress.com sites.
* - It directs you to Calypso to the existing Subscribers page.
public function add_subscribers_menu() {
* Enables the new in development subscribers in wp-admin dashboard.
* @param bool If the new dashboard is enabled. Default false.