Jetpack_Subscriptions_Widget

The Jetpack by WordPress.com Jetpack Subscriptions Widget class.

Defined (1)

The class is defined in the following location(s).

/modules/subscriptions.php  
  1. class Jetpack_Subscriptions_Widget extends WP_Widget { 
  2. function __construct() { 
  3. $widget_ops = array( 'classname' => 'jetpack_subscription_widget', 'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ) ); 
  4. $control_ops = array( 'width' => 300 ); 
  5.  
  6. parent::__construct( 
  7. 'blog_subscription',  
  8. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  9. apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ),  
  10. $widget_ops,  
  11. $control_ops 
  12. ); 
  13.  
  14. function widget( $args, $instance ) { 
  15. if ( 
  16. ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) && 
  17. /** This filter is already documented in modules/contact-form/grunion-contact-form.php */ 
  18. false === apply_filters( 'jetpack_auto_fill_logged_in_user', false ) 
  19. ) { 
  20. $subscribe_email = ''; 
  21. } else { 
  22. global $current_user; 
  23. if ( ! empty( $current_user->user_email ) ) { 
  24. $subscribe_email = esc_attr( $current_user->user_email ); 
  25. } else { 
  26. $subscribe_email = ''; 
  27.  
  28.  
  29.  
  30. $source = 'widget'; 
  31. $instance = wp_parse_args( (array) $instance, $this->defaults() ); 
  32. $subscribe_text = isset( $instance['subscribe_text'] ) ? stripslashes( $instance['subscribe_text'] ) : ''; 
  33. $subscribe_placeholder = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : ''; 
  34. $subscribe_button = isset( $instance['subscribe_button'] ) ? stripslashes( $instance['subscribe_button'] ) : ''; 
  35. $success_message = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : ''; 
  36. $widget_id = esc_attr( !empty( $args['widget_id'] ) ? esc_attr( $args['widget_id'] ) : mt_rand( 450, 550 ) ); 
  37.  
  38. $show_subscribers_total = (bool) $instance['show_subscribers_total']; 
  39. $subscribers_total = $this->fetch_subscriber_count(); // Only used for the shortcode [total-subscribers] 
  40.  
  41. // Give the input element a unique ID 
  42. /** 
  43. * Filter the subscription form's ID prefix. 
  44. * @module subscriptions 
  45. * @since 2.7.0 
  46. * @param string subscribe-field Subscription form field prefix. 
  47. * @param int $widget_id Widget ID. 
  48. */ 
  49. $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id ); 
  50.  
  51. // Enqueue the form's CSS 
  52. wp_register_style( 'jetpack-subscriptions', plugins_url( 'subscriptions/subscriptions.css', __FILE__ ) ); 
  53. wp_enqueue_style( 'jetpack-subscriptions' ); 
  54.  
  55. // Display the subscription form 
  56. echo $args['before_widget']; 
  57.  
  58. // Only show the title if there actually is a title 
  59. if( ! empty( $instance['title'] ) ) { 
  60. echo $args['before_title'] . esc_attr( $instance['title'] ) . $args['after_title'] . "\n"; 
  61.  
  62. $referer = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 
  63.  
  64. // Display any errors 
  65. if ( isset( $_GET['subscribe'] ) ) : 
  66. switch ( $_GET['subscribe'] ) : 
  67. case 'invalid_email' : ?> 
  68. <p class="error"><?php esc_html_e( 'The email you entered was invalid. Please check and try again.', 'jetpack' ); ?></p> 
  69. <?php break; 
  70. case 'opted_out' : ?> 
  71. <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' ),  
  72. 'https://subscribe.wordpress.com/',  
  73. __( 'Manage your email preferences.', 'jetpack' ) 
  74. ); ?> 
  75. <?php break; 
  76. case 'already' : ?> 
  77. <p class="error"><?php esc_html_e( 'You have already subscribed to this site. Please check your inbox.', 'jetpack' ); ?></p> 
  78. <?php break; 
  79. case 'success' : ?> 
  80. <div class="success"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $success_message ) ); ?></div> 
  81. <?php break; 
  82. default : ?> 
  83. <p class="error"><?php esc_html_e( 'There was an error when subscribing. Please try again.', 'jetpack' ); ?></p> 
  84. <?php break; 
  85. endswitch; 
  86. endif; 
  87.  
  88. // Display a subscribe form 
  89. if ( isset( $_GET['subscribe'] ) && 'success' == $_GET['subscribe'] ) { ?> 
  90. <?php 
  91. } else { ?> 
  92. <form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-<?php echo $widget_id; ?>"> 
  93. <?php 
  94. if ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) { 
  95. ?><div id="subscribe-text"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $subscribe_text ) ); ?></div><?php 
  96.  
  97. if ( $show_subscribers_total && 0 < $subscribers_total['value'] ) { 
  98. echo wpautop( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack' ), number_format_i18n( $subscribers_total['value'] ) ) ); 
  99. if ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) { ?> 
  100. <p id="subscribe-email"> 
  101. <label id="jetpack-subscribe-label" for="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>; ?>"> 
  102. <?php echo !empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?> 
  103. </label> 
  104. <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 ); ?>" /> 
  105. </p> 
  106.  
  107. <p id="subscribe-submit"> 
  108. <input type="hidden" name="action" value="subscribe" /> 
  109. <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>" /> 
  110. <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>" /> 
  111. <input type="hidden" name="redirect_fragment" value="<?php echo $widget_id; ?>" /> 
  112. <?php 
  113. if ( is_user_logged_in() ) { 
  114. wp_nonce_field( 'blogsub_subscribe_'. get_current_blog_id(), '_wpnonce', false ); 
  115. ?> 
  116. <input type="submit" value="<?php echo esc_attr( $subscribe_button ); ?>" name="jetpack_subscriptions_widget" /> 
  117. </p> 
  118. <?php }?> 
  119. </form> 
  120.  
  121. <script> 
  122. /* 
  123. Custom functionality for safari and IE 
  124. */ 
  125. (function( d ) { 
  126. // Creates placeholders for IE 
  127. if ( ( 'placeholder' in d.createElement( 'input' ) ) ) { 
  128. var label = d.getElementById( 'jetpack-subscribe-label' ); 
  129. label.style.clip = 'rect(1px, 1px, 1px, 1px)'; 
  130. label.style.position = 'absolute'; 
  131. label.style.height = '1px'; 
  132. label.style.width = '1px'; 
  133. label.style.overflow = 'hidden'; 
  134.  
  135. // Make sure the email value is filled in before allowing submit 
  136. var form = d.getElementById('subscribe-blog-<?php echo $widget_id; ?>'),  
  137. input = d.getElementById('<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>'),  
  138. handler = function( event ) { 
  139. if ( '' === input.value ) { 
  140. input.focus(); 
  141.  
  142. if ( event.preventDefault ) { 
  143. event.preventDefault(); 
  144.  
  145. return false; 
  146. }; 
  147.  
  148. if ( window.addEventListener ) { 
  149. form.addEventListener( 'submit', handler, false ); 
  150. } else { 
  151. form.attachEvent( 'onsubmit', handler ); 
  152. })( document ); 
  153. </script> 
  154. <?php } ?> 
  155. <?php 
  156.  
  157. echo "\n" . $args['after_widget']; 
  158.  
  159. function increment_subscriber_count( $current_subs_array = array() ) { 
  160. $current_subs_array['value']++; 
  161.  
  162. set_transient( 'wpcom_subscribers_total', $current_subs_array, 3600 ); // try to cache the result for at least 1 hour 
  163.  
  164. return $current_subs_array; 
  165.  
  166. function fetch_subscriber_count() { 
  167. $subs_count = get_transient( 'wpcom_subscribers_total' ); 
  168.  
  169. if ( FALSE === $subs_count || 'failed' == $subs_count['status'] ) { 
  170. Jetpack:: load_xml_rpc_client(); 
  171.  
  172. $xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER, ) ); 
  173.  
  174. $xml->query( 'jetpack.fetchSubscriberCount' ); 
  175.  
  176. 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 
  177. $subs_count = array( 
  178. 'status' => 'failed',  
  179. 'code' => $xml->getErrorCode(),  
  180. 'message' => $xml->getErrorMessage(),  
  181. 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,  
  182. ); 
  183. } else { 
  184. $subs_count = array( 
  185. 'status' => 'success',  
  186. 'value' => $xml->getResponse(),  
  187. ); 
  188.  
  189. set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour 
  190.  
  191. return $subs_count; 
  192.  
  193. function update( $new_instance, $old_instance ) { 
  194. $instance = $old_instance; 
  195.  
  196. $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() ); 
  197. $instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) ); 
  198. $instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() ); 
  199. $instance['subscribe_button'] = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() ); 
  200. $instance['success_message'] = wp_kses( stripslashes( $new_instance['success_message'] ), array() ); 
  201. $instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total']; 
  202.  
  203. return $instance; 
  204.  
  205. public static function defaults() { 
  206. return array( 
  207. 'title' => esc_html__( 'Subscribe to Blog via Email', 'jetpack' ),  
  208. 'subscribe_text' => esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' ),  
  209. 'subscribe_placeholder' => esc_html__( 'Email Address', 'jetpack' ),  
  210. 'subscribe_button' => esc_html__( 'Subscribe', 'jetpack' ),  
  211. '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' ),  
  212. 'show_subscribers_total' => true,  
  213. ); 
  214.  
  215. function form( $instance ) { 
  216. $instance = wp_parse_args( (array) $instance, $this->defaults() ); 
  217.  
  218. $title = stripslashes( $instance['title'] ); 
  219. $subscribe_text = stripslashes( $instance['subscribe_text'] ); 
  220. $subscribe_placeholder = stripslashes( $instance['subscribe_placeholder'] ); 
  221. $subscribe_button = stripslashes( $instance['subscribe_button'] ); 
  222. $success_message = stripslashes( $instance['success_message']); 
  223. $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false ); 
  224.  
  225. $subs_fetch = $this->fetch_subscriber_count(); 
  226.  
  227. if ( 'failed' == $subs_fetch['status'] ) { 
  228. printf( '<div class="error inline"><p>' . __( '%s: %s', 'jetpack' ) . '</p></div>', esc_html( $subs_fetch['code'] ), esc_html( $subs_fetch['message'] ) ); 
  229. $subscribers_total = number_format_i18n( $subs_fetch['value'] ); 
  230. ?> 
  231. <p> 
  232. <label for="<?php echo $this->get_field_id( 'title' ); ?>"> 
  233. <?php _e( 'Widget title:', 'jetpack' ); ?> 
  234. <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 ); ?>" /> 
  235. </label> 
  236. </p> 
  237. <p> 
  238. <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"> 
  239. <?php _e( 'Optional text to display to your readers:', 'jetpack' ); ?> 
  240. <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> 
  241. </label> 
  242. </p> 
  243. <p> 
  244. <label for="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>"> 
  245. <?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?> 
  246. <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 ); ?>" /> 
  247. </label> 
  248. </p> 
  249. <p> 
  250. <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"> 
  251. <?php _e( 'Subscribe Button:', 'jetpack' ); ?> 
  252. <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 ); ?>" /> 
  253. </label> 
  254. </p> 
  255. <p> 
  256. <label for="<?php echo $this->get_field_id( 'success_message' ); ?>"> 
  257. <?php _e( 'Success Message Text:', 'jetpack' ); ?> 
  258. <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> 
  259. </label> 
  260. </p> 
  261. <p> 
  262. <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"> 
  263. <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; ?> /> 
  264. <?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 ) ); ?> 
  265. </label> 
  266. </p> 
  267. <?php