/modules/subscriptions.php

  1. <?php 
  2. /** 
  3. * Module Name: Subscriptions 
  4. * Module Description: Allow users to subscribe to your posts and comments and receive notifications via email. 
  5. * Jumpstart Description: Give visitors two easy subscription options * while commenting, or via a separate email subscription widget you can display. 
  6. * Sort Order: 9 
  7. * Recommendation Order: 8 
  8. * First Introduced: 1.2 
  9. * Requires Connection: Yes 
  10. * Auto Activate: Yes 
  11. * Module Tags: Social 
  12. * Feature: Jumpstart 
  13. * Additional Search Queries: subscriptions, subscription, email, follow, followers, subscribers, signup 
  14. */ 
  15.  
  16. add_action( 'jetpack_modules_loaded', 'jetpack_subscriptions_load' ); 
  17.  
  18. Jetpack_Sync::sync_options( 
  19. __FILE__,  
  20. 'home',  
  21. 'blogname',  
  22. 'siteurl',  
  23. 'page_on_front',  
  24. 'permalink_structure',  
  25. 'category_base',  
  26. 'rss_use_excerpt',  
  27. 'subscription_options',  
  28. 'stb_enabled',  
  29. 'stc_enabled',  
  30. 'tag_base' 
  31. ); 
  32.  
  33. Jetpack_Sync::sync_posts( __FILE__ ); 
  34. Jetpack_Sync::sync_comments( __FILE__ ); 
  35.  
  36. function jetpack_subscriptions_load() { 
  37. Jetpack::enable_module_configurable( __FILE__ ); 
  38. Jetpack::module_configuration_load( __FILE__, 'jetpack_subscriptions_configuration_load' ); 
  39.  
  40. function jetpack_subscriptions_configuration_load() { 
  41. wp_safe_redirect( admin_url( 'options-discussion.php#jetpack-subscriptions-settings' ) ); 
  42. exit; 
  43.  
  44. class Jetpack_Subscriptions { 
  45. public $jetpack = false; 
  46.  
  47. public static $hash; 
  48.  
  49. /** 
  50. * Singleton 
  51. * @static 
  52. */ 
  53. static function init() { 
  54. static $instance = false; 
  55.  
  56. if ( !$instance ) { 
  57. $instance = new Jetpack_Subscriptions; 
  58.  
  59. return $instance; 
  60.  
  61. function __construct() { 
  62. $this->jetpack = Jetpack::init(); 
  63.  
  64. // Don't use COOKIEHASH as it could be shared across installs && is non-unique in multisite. 
  65. // @see: https://twitter.com/nacin/status/378246957451333632 
  66. self::$hash = md5( get_option( 'siteurl' ) ); 
  67.  
  68. add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); 
  69.  
  70. // @todo remove sync from subscriptions and move elsewhere... 
  71.  
  72. // Add Configuration Page 
  73. add_action( 'admin_init', array( $this, 'configure' ) ); 
  74.  
  75. // Set up the subscription widget. 
  76. add_action( 'widgets_init', array( $this, 'widget_init' ) ); 
  77.  
  78. // Catch subscription widget submits 
  79. if ( isset( $_REQUEST['jetpack_subscriptions_widget'] ) ) 
  80. add_action( 'template_redirect', array( $this, 'widget_submit' ) ); 
  81.  
  82. // Set up the comment subscription checkboxes 
  83. add_action( 'comment_form', array( $this, 'comment_subscribe_init' ) ); 
  84.  
  85. // Catch comment posts and check for subscriptions. 
  86. add_action( 'comment_post', array( $this, 'comment_subscribe_submit' ), 50, 2 ); 
  87.  
  88. // Adds post meta checkbox in the post submit metabox 
  89. if ( 
  90. /** 
  91. * Filter whether or not to show the per-post subscription option. 
  92. * 
  93. * @module subscriptions 
  94. * 
  95. * @since 3.7.0 
  96. * 
  97. * @param bool true = show checkbox option on all new posts | false = hide the option. 
  98. */ 
  99. apply_filters( 'jetpack_allow_per_post_subscriptions', false ) ) 
  100. add_action( 'post_submitbox_misc_actions', array( $this, 'subscription_post_page_metabox' ) ); 
  101.  
  102. add_action( 'transition_post_status', array( $this, 'maybe_send_subscription_email' ), 10, 3 ); 
  103.  
  104. function post_is_public( $the_post ) { 
  105. if ( !$post = get_post( $the_post ) ) { 
  106. return false; 
  107.  
  108. if ( 'publish' === $post->post_status && strlen( (string) $post->post_password ) < 1 ) { 
  109. /** 
  110. * Filter whether posts can be emailed to subscribers. 
  111. * 
  112. * @module subscriptions 
  113. * 
  114. * @since 2.4.0 
  115. * 
  116. * @param bool true Can the post be emailed to Subscribers. Default to true. 
  117. */ 
  118. return apply_filters( 'jetpack_is_post_mailable', true ); 
  119.  
  120. /** 
  121. * Jetpack_Subscriptions::xmlrpc_methods() 
  122. * 
  123. * Register subscriptions methods with the Jetpack XML-RPC server. 
  124. * @param array $methods 
  125. */ 
  126. function xmlrpc_methods( $methods ) { 
  127. return array_merge( 
  128. $methods,  
  129. array( 
  130. 'jetpack.subscriptions.subscribe' => array( $this, 'subscribe' ),  
  131. ); 
  132.  
  133. /** 
  134. * Disable Subscribe on Single Post 
  135. * Register post meta 
  136. */ 
  137. function subscription_post_page_metabox() { 
  138. if ( has_filter( 'jetpack_subscriptions_exclude_these_categories' ) || has_filter( 'jetpack_subscriptions_include_only_these_categories' ) ) { 
  139. return; 
  140.  
  141. global $post; 
  142. $disable_subscribe_value = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ); 
  143. // Nonce it 
  144. wp_nonce_field( 'disable_subscribe', 'disable_subscribe_nonce' ); 
  145. // only show checkbox if post hasn't been published and is a 'post' post type. 
  146. if ( get_post_status( $post->ID ) !== 'publish' && get_post_type( $post->ID ) == 'post' ) : ?> 
  147. <div class="misc-pub-section"> 
  148. <label for="_jetpack_dont_email_post_to_subs"><?php _e( 'Jetpack Subscriptions:', 'jetpack' ); ?></label><br> 
  149. <input type="checkbox" name="_jetpack_dont_email_post_to_subs" id="jetpack-per-post-subscribe" value="1" <?php checked( $disable_subscribe_value, 1, true ); ?> /> 
  150. <?php _e( 'Don’t send this to subscribers', 'jetpack' ); ?> 
  151. </div> 
  152. <?php endif; 
  153.  
  154. /** 
  155. * Checks whether or not the post should be emailed to subscribers 
  156. * 
  157. * It checks for the following things in order: 
  158. * - Usage of filter jetpack_subscriptions_exclude_these_categories 
  159. * - Usage of filter jetpack_subscriptions_include_only_these_categories 
  160. * - Existence of the per-post checkbox option 
  161. * 
  162. * Only one of these can be used at any given time. 
  163. * 
  164. * @param $new_status string - the "new" post status of the transition when saved 
  165. * @param $old_status string - the "old" post status of the transition when saved 
  166. * @param $post obj - The post object 
  167. */ 
  168. function maybe_send_subscription_email( $new_status, $old_status, $post ) { 
  169. // Only do things on publish 
  170. if ( 'publish' !== $new_status ) { 
  171. return; 
  172. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 
  173. return; 
  174.  
  175. /** 
  176. * Array of categories that will never trigger subscription emails. 
  177. * 
  178. * Will not send subscription emails from any post from within these categories. 
  179. * 
  180. * @module subscriptions 
  181. * 
  182. * @since 3.7.0 
  183. * 
  184. * @param array $args Array of category slugs or ID's. 
  185. */ 
  186. $excluded_categories = apply_filters( 'jetpack_subscriptions_exclude_these_categories', array() ); 
  187.  
  188. // Never email posts from these categories 
  189. if ( ! empty( $excluded_categories ) && in_category( $excluded_categories, $post->ID ) ) { 
  190. update_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', 1 ); 
  191.  
  192. /** 
  193. * ONLY send subscription emails for these categories 
  194. * 
  195. * Will ONLY send subscription emails to these categories. 
  196. * 
  197. * @module subscriptions 
  198. * 
  199. * @since 3.7.0 
  200. * 
  201. * @param array $args Array of category slugs or ID's. 
  202. */ 
  203. $only_these_categories = apply_filters( 'jetpack_subscriptions_exclude_all_categories_except', array() ); 
  204.  
  205. // Only emails posts from these categories 
  206. if ( ! empty( $only_these_categories ) && ! in_category( $only_these_categories, $post->ID ) ) { 
  207. update_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', 1 ); 
  208.  
  209. // Email the post, depending on the checkbox option 
  210. if ( ! empty( $_POST['disable_subscribe_nonce'] ) && wp_verify_nonce( $_POST['disable_subscribe_nonce'], 'disable_subscribe' ) ) { 
  211. if ( isset( $_POST['_jetpack_dont_email_post_to_subs'] ) ) { 
  212. update_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', $_POST['_jetpack_dont_email_post_to_subs'] ); 
  213.  
  214. /** 
  215. * Jetpack_Subscriptions::configure() 
  216. * 
  217. * Jetpack Subscriptions configuration screen. 
  218. */ 
  219. function configure() { 
  220. // Create the section 
  221. add_settings_section( 
  222. 'jetpack_subscriptions',  
  223. __( 'Jetpack Subscriptions Settings', 'jetpack' ),  
  224. array( $this, 'subscriptions_settings_section' ),  
  225. 'discussion' 
  226. ); 
  227.  
  228. /** Subscribe to Posts ***************************************************/ 
  229.  
  230. add_settings_field( 
  231. 'jetpack_subscriptions_post_subscribe',  
  232. __( 'Follow Blog', 'jetpack' ),  
  233. array( $this, 'subscription_post_subscribe_setting' ),  
  234. 'discussion',  
  235. 'jetpack_subscriptions' 
  236. ); 
  237.  
  238. register_setting( 
  239. 'discussion',  
  240. 'stb_enabled' 
  241. ); 
  242.  
  243. /** Subscribe to Comments ******************************************************/ 
  244.  
  245. add_settings_field( 
  246. 'jetpack_subscriptions_comment_subscribe',  
  247. __( 'Follow Comments', 'jetpack' ),  
  248. array( $this, 'subscription_comment_subscribe_setting' ),  
  249. 'discussion',  
  250. 'jetpack_subscriptions' 
  251. ); 
  252.  
  253. register_setting( 
  254. 'discussion',  
  255. 'stc_enabled' 
  256. ); 
  257.  
  258. /** Subscription Messaging Options ******************************************************/ 
  259.  
  260. register_setting( 
  261. 'reading',  
  262. 'subscription_options',  
  263. array( $this, 'validate_settings' ) 
  264. ); 
  265.  
  266. add_settings_section( 
  267. 'email_settings',  
  268. __( 'Follower Settings', 'jetpack' ),  
  269. array( $this, 'reading_section' ),  
  270. 'reading' 
  271. ); 
  272.  
  273. add_settings_field( 
  274. 'invitation',  
  275. __( 'Blog follow email text', 'jetpack' ),  
  276. array( $this, 'setting_invitation' ),  
  277. 'reading',  
  278. 'email_settings' 
  279. ); 
  280.  
  281. add_settings_field( 
  282. 'comment-follow',  
  283. __( 'Comment follow email text', 'jetpack' ),  
  284. array( $this, 'setting_comment_follow' ),  
  285. 'reading',  
  286. 'email_settings' 
  287. ); 
  288.  
  289. /** 
  290. * Discussions setting section blurb 
  291. * 
  292. */ 
  293. function subscriptions_settings_section() { 
  294. ?> 
  295.  
  296. <p id="jetpack-subscriptions-settings"><?php _e( 'Change whether your visitors can subscribe to your posts or comments or both.', 'jetpack' ); ?></p> 
  297.  
  298. <?php 
  299.  
  300. /** 
  301. * Post Subscriptions Toggle 
  302. * 
  303. */ 
  304. function subscription_post_subscribe_setting() { 
  305.  
  306. $stb_enabled = get_option( 'stb_enabled', 1 ); ?> 
  307.  
  308. <p class="description"> 
  309. <input type="checkbox" name="stb_enabled" id="jetpack-post-subscribe" value="1" <?php checked( $stb_enabled, 1 ); ?> /> 
  310. <?php _e( "Show a <em>'follow blog'</em> option in the comment form", 'jetpack' ); ?> 
  311. </p> 
  312. <?php 
  313.  
  314. /** 
  315. * Comments Subscriptions Toggle 
  316. * 
  317. */ 
  318. function subscription_comment_subscribe_setting() { 
  319.  
  320. $stc_enabled = get_option( 'stc_enabled', 1 ); ?> 
  321.  
  322. <p class="description"> 
  323. <input type="checkbox" name="stc_enabled" id="jetpack-comment-subscribe" value="1" <?php checked( $stc_enabled, 1 ); ?> /> 
  324. <?php _e( "Show a <em>'follow comments'</em> option in the comment form", 'jetpack' ); ?> 
  325. </p> 
  326.  
  327. <?php 
  328.  
  329. function validate_settings( $settings ) { 
  330. global $allowedposttags; 
  331.  
  332. $default = $this->get_default_settings(); 
  333.  
  334. // Blog Follow 
  335. $settings['invitation'] = trim( wp_kses( $settings['invitation'], $allowedposttags ) ); 
  336. if ( empty( $settings['invitation'] ) ) 
  337. $settings['invitation'] = $default['invitation']; 
  338.  
  339. // Comments Follow (single post) 
  340. $settings['comment_follow'] = trim( wp_kses( $settings['comment_follow'], $allowedposttags ) ); 
  341. if ( empty( $settings['comment_follow'] ) ) 
  342. $settings['comment_follow'] = $default['comment_follow']; 
  343.  
  344. return $settings; 
  345.  
  346. public function reading_section() { 
  347. echo '<p id="follower-settings">'; 
  348. _e( 'These settings change emails sent from your blog to followers.', 'jetpack' ); 
  349. echo '</p>'; 
  350.  
  351. public function setting_invitation() { 
  352. $settings = $this->get_settings(); 
  353. echo '<textarea name="subscription_options[invitation]" class="large-text" cols="50" rows="5">' . esc_textarea( $settings['invitation'] ) . '</textarea>'; 
  354. echo '<p><span class="description">'.__( 'Introduction text sent when someone follows your blog. (Site and confirmation details will be automatically added for you.)', 'jetpack' ).'</span></p>'; 
  355.  
  356. public function setting_comment_follow() { 
  357. $settings = $this->get_settings(); 
  358. echo '<textarea name="subscription_options[comment_follow]" class="large-text" cols="50" rows="5">' . esc_textarea( $settings['comment_follow'] ) . '</textarea>'; 
  359. echo '<p><span class="description">'.__( 'Introduction text sent when someone follows a post on your blog. (Site and confirmation details will be automatically added for you.)', 'jetpack' ).'</span></p>'; 
  360.  
  361. function get_default_settings() { 
  362. return array( 
  363. 'invitation' => __( "Howdy.\n\nYou recently followed this blog's posts. This means you will receive each new post by email.\n\nTo activate, click confirm below. If you believe this is an error, ignore this message and we'll never bother you again.", 'jetpack' ),  
  364. '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' ) 
  365. ); 
  366.  
  367. function get_settings() { 
  368. return wp_parse_args( (array) get_option( 'subscription_options', array() ), $this->get_default_settings() ); 
  369.  
  370. /** 
  371. * Jetpack_Subscriptions::subscribe() 
  372. * 
  373. * Send a synchronous XML-RPC subscribe to blog posts or subscribe to post comments request. 
  374. * 
  375. * @param string $email 
  376. * @param array $post_ids (optional) defaults to 0 for blog posts only: array of post IDs to subscribe to blog's posts 
  377. * @param bool $async (optional) Should the subscription be performed asynchronously? Defaults to true. 
  378. * 
  379. * @return true|Jetpack_Error true on success 
  380. * invalid_email : not a valid email address 
  381. * invalid_post_id : not a valid post ID 
  382. * unknown_post_id : unknown post 
  383. * not_subscribed : strange error. Jetpack servers at WordPress.com could subscribe the email. 
  384. * disabled : Site owner has disabled subscriptions. 
  385. * active : Already subscribed. 
  386. * unknown : strange error. Jetpack servers at WordPress.com returned something malformed. 
  387. * unknown_status : strange error. Jetpack servers at WordPress.com returned something I didn't understand. 
  388. */ 
  389. function subscribe( $email, $post_ids = 0, $async = true, $extra_data = array() ) { 
  390. if ( !is_email( $email ) ) { 
  391. return new Jetpack_Error( 'invalid_email' ); 
  392.  
  393. if ( !$async ) { 
  394. Jetpack::load_xml_rpc_client(); 
  395. $xml = new Jetpack_IXR_ClientMulticall(); 
  396.  
  397. foreach ( (array) $post_ids as $post_id ) { 
  398. $post_id = (int) $post_id; 
  399. if ( $post_id < 0 ) { 
  400. return new Jetpack_Error( 'invalid_post_id' ); 
  401. } else if ( $post_id && !$post = get_post( $post_id ) ) { 
  402. return new Jetpack_Error( 'unknown_post_id' ); 
  403.  
  404. if ( $async ) { 
  405. Jetpack::xmlrpc_async_call( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) ); 
  406. } else { 
  407. $xml->addCall( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) ); 
  408.  
  409. if ( $async ) { 
  410. return; 
  411.  
  412. // Call 
  413. $xml->query(); 
  414.  
  415. if ( $xml->isError() ) { 
  416. return $xml->get_jetpack_error(); 
  417.  
  418. $responses = $xml->getResponse(); 
  419.  
  420. $r = array(); 
  421. foreach ( (array) $responses as $response ) { 
  422. if ( isset( $response['faultCode'] ) || isset( $response['faultString'] ) ) { 
  423. $r[] = $xml->get_jetpack_error( $response['faultCode'], $response['faultString'] ); 
  424. continue; 
  425.  
  426. if ( !is_array( $response[0] ) || empty( $response[0]['status'] ) ) { 
  427. $r[] = new Jetpack_Error( 'unknown' ); 
  428. continue; 
  429.  
  430. switch ( $response[0]['status'] ) { 
  431. case 'error' : 
  432. $r[] = new Jetpack_Error( 'not_subscribed' ); 
  433. continue 2; 
  434. case 'disabled' : 
  435. $r[] = new Jetpack_Error( 'disabled' ); 
  436. continue 2; 
  437. case 'active' : 
  438. $r[] = new Jetpack_Error( 'active' ); 
  439. continue 2; 
  440. case 'pending' : 
  441. $r[] = true; 
  442. continue 2; 
  443. default : 
  444. $r[] = new Jetpack_Error( 'unknown_status', (string) $response[0]['status'] ); 
  445. continue 2; 
  446.  
  447. return $r; 
  448.  
  449. /** 
  450. * Jetpack_Subscriptions::widget_init() 
  451. * 
  452. * Initialize and register the Jetpack Subscriptions widget. 
  453. */ 
  454. function widget_init() { 
  455. register_widget( 'Jetpack_Subscriptions_Widget' ); 
  456.  
  457. /** 
  458. * Jetpack_Subscriptions::widget_submit() 
  459. * 
  460. * When a user submits their email via the blog subscription widget, check the details and call the subsribe() method. 
  461. */ 
  462. function widget_submit() { 
  463. // Check the nonce. 
  464. if ( is_user_logged_in() ) { 
  465. check_admin_referer( 'blogsub_subscribe_' . get_current_blog_id() ); 
  466.  
  467. if ( empty( $_REQUEST['email'] ) ) 
  468. return false; 
  469.  
  470. $redirect_fragment = false; 
  471. if ( isset( $_REQUEST['redirect_fragment'] ) ) { 
  472. $redirect_fragment = preg_replace( '/[^a-z0-9_-]/i', '', $_REQUEST['redirect_fragment'] ); 
  473. if ( !$redirect_fragment ) { 
  474. $redirect_fragment = 'subscribe-blog'; 
  475.  
  476. $subscribe = Jetpack_Subscriptions::subscribe( 
  477. $_REQUEST['email'],  
  478. 0,  
  479. false,  
  480. array( 
  481. 'source' => 'widget',  
  482. 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',  
  483. 'comment_status' => '',  
  484. 'server_data' => $_SERVER,  
  485. ); 
  486.  
  487. if ( is_wp_error( $subscribe ) ) { 
  488. $error = $subscribe->get_error_code(); 
  489. } else { 
  490. $error = false; 
  491. foreach ( $subscribe as $response ) { 
  492. if ( is_wp_error( $response ) ) { 
  493. $error = $response->get_error_code(); 
  494. break; 
  495.  
  496. switch ( $error ) { 
  497. case false: 
  498. $result = 'success'; 
  499. break; 
  500. case 'invalid_email': 
  501. $result = $error; 
  502. break; 
  503. case 'active': 
  504. case 'blocked_email': 
  505. $result = 'opted_out'; 
  506. break; 
  507. case 'pending': 
  508. $result = 'already'; 
  509. break; 
  510. default: 
  511. $result = 'error'; 
  512. break; 
  513.  
  514. $redirect = add_query_arg( 'subscribe', $result ); 
  515.  
  516. /** 
  517. * Fires on each subscription form submission. 
  518. * 
  519. * @module subscriptions 
  520. * 
  521. * @since 3.7.0 
  522. * 
  523. * @param string $result Result of form submission: success, invalid_email, already, error. 
  524. */ 
  525. do_action( 'jetpack_subscriptions_form_submission', $result ); 
  526.  
  527. wp_safe_redirect( "$redirect#$redirect_fragment" ); 
  528. exit; 
  529.  
  530. /** 
  531. * Jetpack_Subscriptions::comment_subscribe_init() 
  532. * 
  533. * Set up and add the comment subscription checkbox to the comment form. 
  534. */ 
  535. function comment_subscribe_init() { 
  536. global $post; 
  537.  
  538. $comments_checked = ''; 
  539. $blog_checked = ''; 
  540.  
  541. // Check for a comment / blog submission and set a cookie to retain the setting and check the boxes. 
  542. if ( isset( $_COOKIE[ 'jetpack_comments_subscribe_' . self::$hash ] ) && $_COOKIE[ 'jetpack_comments_subscribe_' . self::$hash ] == $post->ID ) 
  543. $comments_checked = ' checked="checked"'; 
  544.  
  545. if ( isset( $_COOKIE[ 'jetpack_blog_subscribe_' . self::$hash ] ) ) 
  546. $blog_checked = ' checked="checked"'; 
  547.  
  548. // Some themes call this function, don't show the checkbox again 
  549. remove_action( 'comment_form', 'subscription_comment_form' ); 
  550.  
  551. // Check if Mark Jaquith's Subscribe to Comments plugin is active - if so, suppress Jetpack checkbox 
  552.  
  553. $str = ''; 
  554.  
  555. if ( FALSE === has_filter( 'comment_form', 'show_subscription_checkbox' ) && 1 == get_option( 'stc_enabled', 1 ) && empty( $post->post_password ) ) { 
  556. // Subscribe to comments checkbox 
  557. $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 . ' /> '; 
  558. $comment_sub_text = __( 'Notify me of follow-up comments by email.', 'jetpack' ); 
  559. $str .= '<label class="subscribe-label" id="subscribe-label" for="subscribe_comments">' . esc_html( 
  560. /** 
  561. * Filter the Subscribe to comments text appearing below the comment form. 
  562. * 
  563. * @module subscriptions 
  564. * 
  565. * @since 3.4.0 
  566. * 
  567. * @param string $comment_sub_text Subscribe to comments text. 
  568. */ 
  569. apply_filters( 'jetpack_subscribe_comment_label', $comment_sub_text ) 
  570. ) . '</label>'; 
  571. $str .= '</p>'; 
  572.  
  573. if ( 1 == get_option( 'stb_enabled', 1 ) ) { 
  574. // Subscribe to blog checkbox 
  575. $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 . ' /> '; 
  576. $blog_sub_text = __( 'Notify me of new posts by email.', 'jetpack' ); 
  577. $str .= '<label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog">' . esc_html( 
  578. /** 
  579. * Filter the Subscribe to blog text appearing below the comment form. 
  580. * 
  581. * @module subscriptions 
  582. * 
  583. * @since 3.4.0 
  584. * 
  585. * @param string $comment_sub_text Subscribe to blog text. 
  586. */ 
  587. apply_filters( 'jetpack_subscribe_blog_label', $blog_sub_text ) 
  588. ) . '</label>'; 
  589. $str .= '</p>'; 
  590.  
  591. /** 
  592. * Filter the output of the subscription options appearing below the comment form. 
  593. * 
  594. * @module subscriptions 
  595. * 
  596. * @since 1.2.0 
  597. * 
  598. * @param string $str Comment Subscription form HTML output. 
  599. */ 
  600. echo apply_filters( 'jetpack_comment_subscription_form', $str ); 
  601.  
  602. /** 
  603. * Jetpack_Subscriptions::comment_subscribe_init() 
  604. * 
  605. * When a user checks the comment subscribe box and submits a comment, subscribe them to the comment thread. 
  606. */ 
  607. function comment_subscribe_submit( $comment_id, $approved ) { 
  608. if ( 'spam' === $approved ) { 
  609. return; 
  610.  
  611. // Set cookies for this post/comment 
  612. $this->set_cookies( isset( $_REQUEST['subscribe_comments'] ), isset( $_REQUEST['subscribe_blog'] ) ); 
  613.  
  614. if ( !isset( $_REQUEST['subscribe_comments'] ) && !isset( $_REQUEST['subscribe_blog'] ) ) 
  615. return; 
  616.  
  617. $comment = get_comment( $comment_id ); 
  618. $post_ids = array(); 
  619.  
  620. if ( isset( $_REQUEST['subscribe_comments'] ) ) 
  621. $post_ids[] = $comment->comment_post_ID; 
  622.  
  623. if ( isset( $_REQUEST['subscribe_blog'] ) ) 
  624. $post_ids[] = 0; 
  625.  
  626. Jetpack_Subscriptions::subscribe( 
  627. $comment->comment_author_email,  
  628. $post_ids,  
  629. true,  
  630. array( 
  631. 'source' => 'comment-form',  
  632. 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',  
  633. 'comment_status' => $approved,  
  634. 'server_data' => $_SERVER,  
  635. ); 
  636.  
  637. /** 
  638. * Jetpack_Subscriptions::set_cookies() 
  639. * 
  640. * Set a cookie to save state on the comment and post subscription checkboxes. 
  641. */ 
  642. function set_cookies( $comments = true, $posts = true ) { 
  643. global $post; 
  644.  
  645. /** This filter is already documented in core/wp-includes/comment-functions.php */ 
  646. $cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 ); 
  647. /** 
  648. * Filter the Jetpack Comment cookie path. 
  649. * 
  650. * @module subscriptions 
  651. * 
  652. * @since 2.5.0 
  653. * 
  654. * @param string COOKIEPATH Cookie path. 
  655. */ 
  656. $cookie_path = apply_filters( 'jetpack_comment_cookie_path', COOKIEPATH ); 
  657. /** 
  658. * Filter the Jetpack Comment cookie domain. 
  659. * 
  660. * @module subscriptions 
  661. * 
  662. * @since 2.5.0 
  663. * 
  664. * @param string COOKIE_DOMAIN Cookie domain. 
  665. */ 
  666. $cookie_domain = apply_filters( 'jetpack_comment_cookie_domain', COOKIE_DOMAIN ); 
  667.  
  668. if ( $comments ) 
  669. setcookie( 'jetpack_comments_subscribe_' . self::$hash, $post->ID, time() + $cookie_lifetime, $cookie_path, $cookie_domain ); 
  670. else 
  671. setcookie( 'jetpack_comments_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain ); 
  672.  
  673. if ( $posts ) 
  674. setcookie( 'jetpack_blog_subscribe_' . self::$hash, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain ); 
  675. else 
  676. setcookie( 'jetpack_blog_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain ); 
  677.  
  678. Jetpack_Subscriptions::init(); 
  679.  
  680.  
  681. /*** 
  682. * Blog Subscription Widget 
  683. */ 
  684.  
  685. class Jetpack_Subscriptions_Widget extends WP_Widget { 
  686. function __construct() { 
  687. $widget_ops = array( 'classname' => 'jetpack_subscription_widget', 'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ) ); 
  688. $control_ops = array( 'width' => 300 ); 
  689.  
  690. parent::__construct( 
  691. 'blog_subscription',  
  692. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  693. apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ),  
  694. $widget_ops,  
  695. $control_ops 
  696. ); 
  697.  
  698. function widget( $args, $instance ) { 
  699. if ( 
  700. ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) && 
  701. /** This filter is already documented in modules/contact-form/grunion-contact-form.php */ 
  702. false === apply_filters( 'jetpack_auto_fill_logged_in_user', false ) 
  703. ) { 
  704. $subscribe_email = ''; 
  705. } else { 
  706. global $current_user; 
  707. if ( ! empty( $current_user->user_email ) ) { 
  708. $subscribe_email = esc_attr( $current_user->user_email ); 
  709. } else { 
  710. $subscribe_email = ''; 
  711.  
  712.  
  713.  
  714. $source = 'widget'; 
  715. $instance = wp_parse_args( (array) $instance, $this->defaults() ); 
  716. $subscribe_text = isset( $instance['subscribe_text'] ) ? stripslashes( $instance['subscribe_text'] ) : ''; 
  717. $subscribe_placeholder = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : ''; 
  718. $subscribe_button = isset( $instance['subscribe_button'] ) ? stripslashes( $instance['subscribe_button'] ) : ''; 
  719. $success_message = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : ''; 
  720. $widget_id = esc_attr( !empty( $args['widget_id'] ) ? esc_attr( $args['widget_id'] ) : mt_rand( 450, 550 ) ); 
  721.  
  722. $show_subscribers_total = (bool) $instance['show_subscribers_total']; 
  723. $subscribers_total = $this->fetch_subscriber_count(); // Only used for the shortcode [total-subscribers] 
  724.  
  725. // Give the input element a unique ID 
  726. /** 
  727. * Filter the subscription form's ID prefix. 
  728. * 
  729. * @module subscriptions 
  730. * 
  731. * @since 2.7.0 
  732. * 
  733. * @param string subscribe-field Subscription form field prefix. 
  734. * @param int $widget_id Widget ID. 
  735. */ 
  736. $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id ); 
  737.  
  738. // Enqueue the form's CSS 
  739. wp_register_style( 'jetpack-subscriptions', plugins_url( 'subscriptions/subscriptions.css', __FILE__ ) ); 
  740. wp_enqueue_style( 'jetpack-subscriptions' ); 
  741.  
  742. // Display the subscription form 
  743. echo $args['before_widget']; 
  744.  
  745. // Only show the title if there actually is a title 
  746. if( ! empty( $instance['title'] ) ) { 
  747. echo $args['before_title'] . esc_attr( $instance['title'] ) . $args['after_title'] . "\n"; 
  748.  
  749. $referer = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 
  750.  
  751. // Display any errors 
  752. if ( isset( $_GET['subscribe'] ) ) : 
  753. switch ( $_GET['subscribe'] ) : 
  754. case 'invalid_email' : ?> 
  755. <p class="error"><?php esc_html_e( 'The email you entered was invalid. Please check and try again.', 'jetpack' ); ?></p> 
  756. <?php break; 
  757. case 'opted_out' : ?> 
  758. <p class="error"><?php printf( __( 'The email address has opted out of subscription emails. <br /> You can manage your preferences at <a href="%1$s" title="%2$s" target="_blank">subscribe.wordpress.com</a>', 'jetpack' ),  
  759. 'https://subscribe.wordpress.com/',  
  760. __( 'Manage your email preferences.', 'jetpack' ) 
  761. ); ?> 
  762. <?php break; 
  763. case 'already' : ?> 
  764. <p class="error"><?php esc_html_e( 'You have already subscribed to this site. Please check your inbox.', 'jetpack' ); ?></p> 
  765. <?php break; 
  766. case 'success' : ?> 
  767. <div class="success"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $success_message ) ); ?></div> 
  768. <?php break; 
  769. default : ?> 
  770. <p class="error"><?php esc_html_e( 'There was an error when subscribing. Please try again.', 'jetpack' ); ?></p> 
  771. <?php break; 
  772. endswitch; 
  773. endif; 
  774.  
  775. // Display a subscribe form 
  776. if ( isset( $_GET['subscribe'] ) && 'success' == $_GET['subscribe'] ) { ?> 
  777. <?php 
  778. } else { ?> 
  779. <form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-<?php echo $widget_id; ?>"> 
  780. <?php 
  781. if ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) { 
  782. ?><div id="subscribe-text"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $subscribe_text ) ); ?></div><?php 
  783.  
  784. if ( $show_subscribers_total && 0 < $subscribers_total['value'] ) { 
  785. echo wpautop( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack' ), number_format_i18n( $subscribers_total['value'] ) ) ); 
  786. if ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) { ?> 
  787. <p id="subscribe-email"> 
  788. <label id="jetpack-subscribe-label" for="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>; ?>"> 
  789. <?php echo !empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?> 
  790. </label> 
  791. <input type="email" name="email" required="required" class="required" value="<?php echo esc_attr( $subscribe_email ); ?>" id="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>" placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>" /> 
  792. </p> 
  793.  
  794. <p id="subscribe-submit"> 
  795. <input type="hidden" name="action" value="subscribe" /> 
  796. <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>" /> 
  797. <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>" /> 
  798. <input type="hidden" name="redirect_fragment" value="<?php echo $widget_id; ?>" /> 
  799. <?php 
  800. if ( is_user_logged_in() ) { 
  801. wp_nonce_field( 'blogsub_subscribe_'. get_current_blog_id(), '_wpnonce', false ); 
  802. ?> 
  803. <input type="submit" value="<?php echo esc_attr( $subscribe_button ); ?>" name="jetpack_subscriptions_widget" /> 
  804. </p> 
  805. <?php }?> 
  806. </form> 
  807.  
  808. <script> 
  809. /* 
  810. Custom functionality for safari and IE 
  811. */ 
  812. (function( d ) { 
  813. // Creates placeholders for IE 
  814. if ( ( 'placeholder' in d.createElement( 'input' ) ) ) { 
  815. var label = d.getElementById( 'jetpack-subscribe-label' ); 
  816. label.style.clip = 'rect(1px, 1px, 1px, 1px)'; 
  817. label.style.position = 'absolute'; 
  818. label.style.height = '1px'; 
  819. label.style.width = '1px'; 
  820. label.style.overflow = 'hidden'; 
  821.  
  822. // Make sure the email value is filled in before allowing submit 
  823. var form = d.getElementById('subscribe-blog-<?php echo $widget_id; ?>'),  
  824. input = d.getElementById('<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>'),  
  825. handler = function( event ) { 
  826. if ( '' === input.value ) { 
  827. input.focus(); 
  828.  
  829. if ( event.preventDefault ) { 
  830. event.preventDefault(); 
  831.  
  832. return false; 
  833. }; 
  834.  
  835. if ( window.addEventListener ) { 
  836. form.addEventListener( 'submit', handler, false ); 
  837. } else { 
  838. form.attachEvent( 'onsubmit', handler ); 
  839. })( document ); 
  840. </script> 
  841. <?php } ?> 
  842. <?php 
  843.  
  844. echo "\n" . $args['after_widget']; 
  845.  
  846. function increment_subscriber_count( $current_subs_array = array() ) { 
  847. $current_subs_array['value']++; 
  848.  
  849. set_transient( 'wpcom_subscribers_total', $current_subs_array, 3600 ); // try to cache the result for at least 1 hour 
  850.  
  851. return $current_subs_array; 
  852.  
  853. function fetch_subscriber_count() { 
  854. $subs_count = get_transient( 'wpcom_subscribers_total' ); 
  855.  
  856. if ( FALSE === $subs_count || 'failed' == $subs_count['status'] ) { 
  857. Jetpack:: load_xml_rpc_client(); 
  858.  
  859. $xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER, ) ); 
  860.  
  861. $xml->query( 'jetpack.fetchSubscriberCount' ); 
  862.  
  863. 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 
  864. $subs_count = array( 
  865. 'status' => 'failed',  
  866. 'code' => $xml->getErrorCode(),  
  867. 'message' => $xml->getErrorMessage(),  
  868. 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,  
  869. ); 
  870. } else { 
  871. $subs_count = array( 
  872. 'status' => 'success',  
  873. 'value' => $xml->getResponse(),  
  874. ); 
  875.  
  876. set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour 
  877.  
  878. return $subs_count; 
  879.  
  880. function update( $new_instance, $old_instance ) { 
  881. $instance = $old_instance; 
  882.  
  883. $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() ); 
  884. $instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) ); 
  885. $instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() ); 
  886. $instance['subscribe_button'] = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() ); 
  887. $instance['success_message'] = wp_kses( stripslashes( $new_instance['success_message'] ), array() ); 
  888. $instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total']; 
  889.  
  890. return $instance; 
  891.  
  892. public static function defaults() { 
  893. return array( 
  894. 'title' => esc_html__( 'Subscribe to Blog via Email', 'jetpack' ),  
  895. 'subscribe_text' => esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' ),  
  896. 'subscribe_placeholder' => esc_html__( 'Email Address', 'jetpack' ),  
  897. 'subscribe_button' => esc_html__( 'Subscribe', 'jetpack' ),  
  898. 'success_message' => esc_html__( 'Success! An email was just sent to confirm your subscription. Please find the email now and click activate to start subscribing.', 'jetpack' ),  
  899. 'show_subscribers_total' => true,  
  900. ); 
  901.  
  902. function form( $instance ) { 
  903. $instance = wp_parse_args( (array) $instance, $this->defaults() ); 
  904.  
  905. $title = stripslashes( $instance['title'] ); 
  906. $subscribe_text = stripslashes( $instance['subscribe_text'] ); 
  907. $subscribe_placeholder = stripslashes( $instance['subscribe_placeholder'] ); 
  908. $subscribe_button = stripslashes( $instance['subscribe_button'] ); 
  909. $success_message = stripslashes( $instance['success_message']); 
  910. $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false ); 
  911.  
  912. $subs_fetch = $this->fetch_subscriber_count(); 
  913.  
  914. if ( 'failed' == $subs_fetch['status'] ) { 
  915. printf( '<div class="error inline"><p>' . __( '%s: %s', 'jetpack' ) . '</p></div>', esc_html( $subs_fetch['code'] ), esc_html( $subs_fetch['message'] ) ); 
  916. $subscribers_total = number_format_i18n( $subs_fetch['value'] ); 
  917. ?> 
  918. <p> 
  919. <label for="<?php echo $this->get_field_id( 'title' ); ?>"> 
  920. <?php _e( 'Widget title:', 'jetpack' ); ?> 
  921. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> 
  922. </label> 
  923. </p> 
  924. <p> 
  925. <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"> 
  926. <?php _e( 'Optional text to display to your readers:', 'jetpack' ); ?> 
  927. <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>" name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>" type="text"><?php echo esc_html( $subscribe_text ); ?></textarea> 
  928. </label> 
  929. </p> 
  930. <p> 
  931. <label for="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>"> 
  932. <?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?> 
  933. <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>" name="<?php echo $this->get_field_name( 'subscribe_placeholder' ); ?>" type="text" value="<?php echo esc_attr( $subscribe_placeholder ); ?>" /> 
  934. </label> 
  935. </p> 
  936. <p> 
  937. <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"> 
  938. <?php _e( 'Subscribe Button:', 'jetpack' ); ?> 
  939. <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>" name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text" value="<?php echo esc_attr( $subscribe_button ); ?>" /> 
  940. </label> 
  941. </p> 
  942. <p> 
  943. <label for="<?php echo $this->get_field_id( 'success_message' ); ?>"> 
  944. <?php _e( 'Success Message Text:', 'jetpack' ); ?> 
  945. <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'success_message' ); ?>" name="<?php echo $this->get_field_name( 'success_message' ); ?>" type="text"><?php echo esc_html( $success_message ); ?></textarea> 
  946. </label> 
  947. </p> 
  948. <p> 
  949. <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"> 
  950. <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>" name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>" value="1"<?php echo $show_subscribers_total; ?> /> 
  951. <?php echo esc_html( sprintf( _n( 'Show total number of subscribers? (%s subscriber)', 'Show total number of subscribers? (%s subscribers)', $subscribers_total, 'jetpack' ), $subscribers_total ) ); ?> 
  952. </label> 
  953. </p> 
  954. <?php 
  955.  
  956. add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' ); 
  957.  
  958. function jetpack_do_subscription_form( $instance ) { 
  959. $instance['show_subscribers_total'] = empty( $instance['show_subscribers_total'] ) ? false : true; 
  960. $instance = shortcode_atts( Jetpack_Subscriptions_Widget::defaults(), $instance, 'jetpack_subscription_form' ); 
  961. $args = array( 
  962. 'before_widget' => sprintf( '<div class="%s">', 'jetpack_subscription_widget' ),  
  963. ); 
  964. ob_start(); 
  965. the_widget( 'Jetpack_Subscriptions_Widget', $instance, $args ); 
  966. $output = ob_get_clean(); 
  967. return $output; 
.