Няма описание

views.php 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. <?php
  2. class Jetpack_Subscriptions_Widget extends WP_Widget {
  3. static $instance_count = 0;
  4. /**
  5. * @var array When printing the submit button, what tags are allowed
  6. */
  7. public static $allowed_html_tags_for_submit_button = array(
  8. 'br' => array(),
  9. 's' => array(),
  10. 'strong' => array(),
  11. 'em' => array(),
  12. );
  13. /**
  14. * Use this variable when printing the message after submitting an email in subscription widgets
  15. *
  16. * @var array what tags are allowed
  17. */
  18. public static $allowed_html_tags_for_message = array(
  19. 'a' => array(
  20. 'href' => array(),
  21. 'title' => array(),
  22. 'rel' => array(),
  23. 'target' => array(),
  24. ),
  25. 'br' => array(),
  26. );
  27. function __construct() {
  28. $widget_ops = array(
  29. 'classname' => 'widget_blog_subscription jetpack_subscription_widget',
  30. 'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ),
  31. 'customize_selective_refresh' => true,
  32. );
  33. $name = self::is_jetpack() ?
  34. /** This filter is documented in modules/widgets/facebook-likebox.php */
  35. apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ) :
  36. __( 'Follow Blog', 'jetpack' );
  37. parent::__construct(
  38. 'blog_subscription',
  39. $name,
  40. $widget_ops
  41. );
  42. if ( self::is_jetpack() &&
  43. (
  44. is_active_widget( false, false, $this->id_base ) ||
  45. is_active_widget( false, false, 'monster' ) ||
  46. is_customize_preview()
  47. )
  48. ) {
  49. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
  50. }
  51. }
  52. /**
  53. * Enqueue the form's CSS.
  54. *
  55. * @since 4.5.0
  56. */
  57. public function enqueue_style() {
  58. wp_register_style(
  59. 'jetpack-subscriptions',
  60. plugins_url( 'subscriptions.css', __FILE__ ),
  61. array(),
  62. JETPACK__VERSION
  63. );
  64. wp_enqueue_style( 'jetpack-subscriptions' );
  65. }
  66. /**
  67. * Renders a full widget either within the context of WordPress widget, or in response to a shortcode.
  68. *
  69. * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
  70. * @param array $instance The settings for the particular instance of the widget.
  71. */
  72. function widget( $args, $instance ) {
  73. if ( self::is_jetpack() &&
  74. /** This filter is documented in modules/contact-form/grunion-contact-form.php */
  75. false === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
  76. ) {
  77. $subscribe_email = '';
  78. } else {
  79. $current_user = wp_get_current_user();
  80. if ( ! empty( $current_user->user_email ) ) {
  81. $subscribe_email = esc_attr( $current_user->user_email );
  82. } else {
  83. $subscribe_email = '';
  84. }
  85. }
  86. wp_enqueue_script(
  87. 'jetpack-subscriptions-js',
  88. plugins_url( 'subscriptions.js', __FILE__ ),
  89. array(),
  90. JETPACK__VERSION,
  91. true
  92. );
  93. $stats_action = self::is_jetpack() ? 'jetpack_subscriptions' : 'follow_blog';
  94. /** This action is documented in modules/widgets/gravatar-profile.php */
  95. do_action( 'jetpack_stats_extra', 'widget_view', $stats_action );
  96. $after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
  97. $before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
  98. $instance = wp_parse_args( (array) $instance, $this->defaults() );
  99. echo $before_widget;
  100. Jetpack_Subscriptions_Widget::$instance_count ++;
  101. self::render_widget_title( $args, $instance );
  102. self::render_widget_status_messages( $instance );
  103. self::render_widget_subscription_form( $args, $instance, $subscribe_email );
  104. echo "\n" . $after_widget;
  105. }
  106. /**
  107. * Prints the widget's title. If show_only_email_and_button is true, we will not show a title.
  108. *
  109. * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
  110. * @param array $instance The settings for the particular instance of the widget.
  111. */
  112. static function render_widget_title( $args, $instance ) {
  113. $show_only_email_and_button = $instance['show_only_email_and_button'];
  114. $before_title = isset( $args['before_title'] ) ? $args['before_title'] : '';
  115. $after_title = isset( $args['after_title'] ) ? $args['after_title'] : '';
  116. if ( self::is_wpcom() && ! $show_only_email_and_button ) {
  117. if ( self::is_current_user_subscribed() ) {
  118. if ( ! empty( $instance['title_following'] ) ) {
  119. echo $before_title . '<label for="subscribe-field' . ( Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '' ) . '">' . esc_attr( $instance['title_following'] ) . '</label>' . $after_title . "\n";
  120. }
  121. } else {
  122. if ( ! empty( $instance['title'] ) ) {
  123. echo $before_title . '<label for="subscribe-field' . ( Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '' ) . '">' . esc_attr( $instance['title'] ) . '</label>' . $after_title . "\n";
  124. }
  125. }
  126. }
  127. if ( self::is_jetpack() && empty( $instance['show_only_email_and_button'] ) ) {
  128. echo $args['before_title'] . esc_attr( $instance['title'] ) . $args['after_title'] . "\n";
  129. }
  130. }
  131. /**
  132. * Prints the subscription block's status messages after someone has attempted to subscribe.
  133. * Either a success message or an error message.
  134. *
  135. * @param array $instance The settings for the particular instance of the widget.
  136. */
  137. static function render_widget_status_messages( $instance ) {
  138. if ( self::is_jetpack() && isset( $_GET['subscribe'] ) ) {
  139. $success_message = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
  140. $subscribers_total = self::fetch_subscriber_count();
  141. switch ( $_GET['subscribe'] ) :
  142. case 'invalid_email' : ?>
  143. <p class="error"><?php esc_html_e( 'The email you entered was invalid. Please check and try again.', 'jetpack' ); ?></p>
  144. <?php break;
  145. case 'opted_out' : ?>
  146. <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' ),
  147. 'https://subscribe.wordpress.com/',
  148. __( 'Manage your email preferences.', 'jetpack' )
  149. ); ?></p>
  150. <?php break;
  151. case 'already' : ?>
  152. <p class="error"><?php printf( __( 'You have already subscribed to this site. Please check your inbox. <br /> You can manage your preferences at <a href="%1$s" title="%2$s" target="_blank">subscribe.wordpress.com</a>', 'jetpack' ),
  153. 'https://subscribe.wordpress.com/',
  154. __( 'Manage your email preferences.', 'jetpack' )
  155. ); ?></p>
  156. <?php break;
  157. case 'many_pending_subs':
  158. ?>
  159. <p class="error">
  160. <?php
  161. printf(
  162. wp_kses(
  163. /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
  164. __( 'You already have several pending email subscriptions. <br /> Approve or delete a few subscriptions at <a href="%1$s" title="%2$s" target="_blank" rel="noopener noreferrer">subscribe.wordpress.com</a> before continuing.', 'jetpack' ),
  165. self::$allowed_html_tags_for_message
  166. ),
  167. 'https://subscribe.wordpress.com/',
  168. esc_attr__( 'Manage your email preferences.', 'jetpack' )
  169. );
  170. ?>
  171. </p>
  172. <?php break;
  173. case 'pending':
  174. ?>
  175. <p class="error">
  176. <?php
  177. printf(
  178. wp_kses(
  179. /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
  180. __( 'You subscribed this site before but you have not clicked the confirmation link yet. Please check your inbox. <br /> Otherwise, you can manage your preferences at <a href="%1$s" title="%2$s" target="_blank" rel="noopener noreferrer">subscribe.wordpress.com</a>.', 'jetpack' ),
  181. self::$allowed_html_tags_for_message
  182. ),
  183. 'https://subscribe.wordpress.com/',
  184. esc_attr__( 'Manage your email preferences.', 'jetpack' )
  185. );
  186. ?>
  187. </p>
  188. <?php
  189. break;
  190. case 'success' : ?>
  191. <div class="success"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $success_message ) ); ?></div>
  192. <?php break;
  193. default : ?>
  194. <p class="error"><?php esc_html_e( 'There was an error when subscribing. Please try again.', 'jetpack' ); ?></p>
  195. <?php break;
  196. endswitch;
  197. }
  198. if ( self::is_wpcom() && self::wpcom_has_status_message() ) {
  199. global $themecolors;
  200. $message = '';
  201. switch ( $_GET['blogsub'] ) {
  202. case 'confirming':
  203. $message = __( 'Thanks for subscribing! You&rsquo;ll get an email with a link to confirm your subscription. If you don&rsquo;t get it, please <a href="https://en.support.wordpress.com/contact/">contact us</a>.', 'jetpack' );
  204. break;
  205. case 'blocked':
  206. $message = __( 'Subscriptions have been blocked for this email address.', 'jetpack' );
  207. break;
  208. case 'flooded':
  209. $message = __( 'You already have several pending email subscriptions. Approve or delete a few through your <a href="https://subscribe.wordpress.com/">Subscription Manager</a> before attempting to subscribe to more blogs.', 'jetpack' );
  210. break;
  211. case 'spammed':
  212. /* translators: %s is a URL */
  213. $message = sprintf( __( 'Because there are many pending subscriptions for this email address, we have blocked the subscription. Please <a href="%s">activate or delete</a> pending subscriptions before attempting to subscribe.', 'jetpack' ), 'https://subscribe.wordpress.com/' );
  214. break;
  215. case 'subscribed':
  216. $message = __( 'You&rsquo;re already subscribed to this site.', 'jetpack' );
  217. break;
  218. case 'pending':
  219. $message = __( 'You have a pending subscription already; we just sent you another email. Click the link or <a href="https://en.support.wordpress.com/contact/">contact us</a> if you don&rsquo;t receive it.', 'jetpack' );
  220. break;
  221. case 'confirmed':
  222. $message = __( 'Congrats, you&rsquo;re subscribed! You&rsquo;ll get an email with the details of your subscription and an unsubscribe link.', 'jetpack' );
  223. break;
  224. }
  225. $border_color = isset( $themecolors['border'] ) ? " #{$themecolors['border']}" : '';
  226. printf(
  227. '<div class="jetpack-sub-notification" style="border: 1px solid%1$s; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;">%2$s</div>',
  228. esc_attr( $border_color ),
  229. wp_kses_post( $message )
  230. );
  231. }
  232. }
  233. /**
  234. * Renders a form allowing folks to subscribe to the blog.
  235. *
  236. * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
  237. * @param array $instance The settings for the particular instance of the widget.
  238. * @param string $subscribe_email The email to use to prefill the form.
  239. */
  240. static function render_widget_subscription_form( $args, $instance, $subscribe_email ) {
  241. $show_only_email_and_button = $instance['show_only_email_and_button'];
  242. $show_subscribers_total = (bool) $instance['show_subscribers_total'];
  243. $subscribe_text = empty( $instance['show_only_email_and_button'] ) ?
  244. stripslashes( $instance['subscribe_text'] ) :
  245. false;
  246. $referer = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  247. $source = 'widget';
  248. $widget_id = esc_attr( ! empty( $args['widget_id'] ) ? esc_attr( $args['widget_id'] ) : wp_rand( 450, 550 ) );
  249. $subscribe_button = ! empty( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : $instance['subscribe_button'];
  250. $subscribers_total = self::fetch_subscriber_count();
  251. $subscribe_placeholder = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
  252. $submit_button_classes = isset( $instance['submit_button_classes'] ) ? 'wp-block-button__link ' . $instance['submit_button_classes'] : 'wp-block-button__link';
  253. $submit_button_styles = isset( $instance['submit_button_styles'] ) ? $instance['submit_button_styles'] : '';
  254. $submit_button_wrapper_styles = isset( $instance['submit_button_wrapper_styles'] ) ? $instance['submit_button_wrapper_styles'] : '';
  255. $email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
  256. $email_field_styles = isset( $instance['email_field_styles'] ) ? $instance['email_field_styles'] : '';
  257. if ( self::is_wpcom() && ! self::wpcom_has_status_message() ) {
  258. global $current_blog;
  259. $url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : '';
  260. $form_id = 'subscribe-blog' . self::$instance_count > 1
  261. ? '-' . self::$instance_count
  262. : '';
  263. ?>
  264. <form
  265. action="<?php echo esc_url( $url ); ?>"
  266. method="post"
  267. accept-charset="utf-8"
  268. id="<?php echo esc_attr( $form_id ); ?>"
  269. >
  270. <?php
  271. if ( ! $show_only_email_and_button ) {
  272. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  273. echo wpautop( $subscribe_text );
  274. }
  275. if ( $show_subscribers_total && $subscribers_total ) {
  276. ?>
  277. <div class="jetpack-subscribe-count">
  278. <p>
  279. <?php
  280. /* translators: %s: number of folks following the blog */
  281. echo esc_html( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $subscribers_total, 'jetpack' ), number_format_i18n( $subscribers_total ) ) );
  282. ?>
  283. </p>
  284. </div>
  285. <?php
  286. }
  287. $email_field_id = 'subscribe-field';
  288. $email_field_id .= self::$instance_count > 1
  289. ? '-' . self::$instance_count
  290. : '';
  291. $label_field_id = $email_field_id . '-label';
  292. ?>
  293. <p id="subscribe-email">
  294. <label
  295. id="<?php echo esc_attr( $label_field_id ); ?>"
  296. for="<?php echo esc_attr( $email_field_id ); ?>"
  297. class="screen-reader-text"
  298. >
  299. <?php echo esc_html__( 'Email Address:', 'jetpack' ); ?>
  300. </label>
  301. <?php
  302. printf(
  303. '<input
  304. type="email"
  305. name="email"
  306. %1$s
  307. style="%2$s"
  308. placeholder="%3$s"
  309. value=""
  310. id="%4$s"
  311. />',
  312. ( ! empty( $email_field_classes )
  313. ? 'class="' . esc_attr( $email_field_classes ) . '"'
  314. : ''
  315. ),
  316. ( ! empty( $email_field_styles )
  317. ? esc_attr( $email_field_styles )
  318. : 'width: 95%; padding: 1px 10px'
  319. ),
  320. esc_attr__( 'Enter your email address', 'jetpack' ),
  321. esc_attr( $email_field_id )
  322. );
  323. ?>
  324. </p>
  325. <p id="subscribe-submit"
  326. <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
  327. style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
  328. <?php }; ?>
  329. >
  330. <input type="hidden" name="action" value="subscribe"/>
  331. <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/>
  332. <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
  333. <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
  334. <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $widget_id ); ?>"/>
  335. <?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?>
  336. <button type="submit"
  337. <?php if ( ! empty( $submit_button_classes ) ) { ?>
  338. class="<?php echo esc_attr( $submit_button_classes ); ?>"
  339. <?php }; ?>
  340. <?php if ( ! empty( $submit_button_styles ) ) { ?>
  341. style="<?php echo esc_attr( $submit_button_styles ); ?>"
  342. <?php }; ?>
  343. >
  344. <?php
  345. echo wp_kses(
  346. $subscribe_button,
  347. self::$allowed_html_tags_for_submit_button
  348. );
  349. ?>
  350. </button>
  351. </p>
  352. </form>
  353. <?php
  354. }
  355. if ( self::is_jetpack() ) {
  356. /**
  357. * Filter the subscription form's ID prefix.
  358. *
  359. * @module subscriptions
  360. *
  361. * @since 2.7.0
  362. *
  363. * @param string subscribe-field Subscription form field prefix.
  364. * @param int $widget_id Widget ID.
  365. */
  366. $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
  367. ?>
  368. <form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-<?php echo $widget_id; ?>">
  369. <?php
  370. if ( $subscribe_text && ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) ) {
  371. ?>
  372. <div id="subscribe-text"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $subscribe_text ) ); ?></div><?php
  373. }
  374. if ( $show_subscribers_total && 0 < $subscribers_total['value'] ) {
  375. ?>
  376. <div class="jetpack-subscribe-count">
  377. <p>
  378. <?php
  379. /* translators: %s: number of folks following the blog */
  380. echo esc_html( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack' ), number_format_i18n( $subscribers_total['value'] ) ) );
  381. ?>
  382. </p>
  383. </div>
  384. <?php
  385. }
  386. if ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) { ?>
  387. <p id="subscribe-email">
  388. <label id="jetpack-subscribe-label"
  389. class="screen-reader-text"
  390. for="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>">
  391. <?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?>
  392. </label>
  393. <input type="email" name="email" required="required"
  394. <?php if ( ! empty( $email_field_classes ) ) { ?>
  395. class="<?php echo esc_attr( $email_field_classes ); ?> required"
  396. <?php }; ?>
  397. <?php if ( ! empty( $email_field_styles ) ) { ?>
  398. style="<?php echo esc_attr( $email_field_styles ); ?>"
  399. <?php }; ?>
  400. value="<?php echo esc_attr( $subscribe_email ); ?>"
  401. id="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>"
  402. placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"
  403. />
  404. </p>
  405. <p id="subscribe-submit"
  406. <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
  407. style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
  408. <?php }; ?>
  409. >
  410. <input type="hidden" name="action" value="subscribe"/>
  411. <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
  412. <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
  413. <input type="hidden" name="redirect_fragment" value="<?php echo $widget_id; ?>"/>
  414. <?php
  415. if ( is_user_logged_in() ) {
  416. wp_nonce_field( 'blogsub_subscribe_' . get_current_blog_id(), '_wpnonce', false );
  417. }
  418. ?>
  419. <button type="submit"
  420. <?php if ( ! empty( $submit_button_classes ) ) { ?>
  421. class="<?php echo esc_attr( $submit_button_classes ); ?>"
  422. <?php }; ?>
  423. <?php if ( ! empty( $submit_button_styles ) ) { ?>
  424. style="<?php echo esc_attr( $submit_button_styles ); ?>"
  425. <?php }; ?>
  426. name="jetpack_subscriptions_widget"
  427. >
  428. <?php
  429. echo wp_kses(
  430. $subscribe_button,
  431. self::$allowed_html_tags_for_submit_button
  432. ); ?>
  433. </button>
  434. </p>
  435. <?php } ?>
  436. </form>
  437. <?php }
  438. }
  439. /**
  440. * Determines if the current user is subscribed to the blog.
  441. *
  442. * @return bool Is the person already subscribed.
  443. */
  444. static function is_current_user_subscribed() {
  445. $subscribed = isset( $_GET['subscribe'] ) && 'success' == $_GET['subscribe'];
  446. if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
  447. $subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
  448. }
  449. return $subscribed;
  450. }
  451. /**
  452. * Is this script running in the wordpress.com environment?
  453. *
  454. * @return bool
  455. */
  456. static function is_wpcom() {
  457. return defined( 'IS_WPCOM' ) && IS_WPCOM;
  458. }
  459. /**
  460. * Is this script running in a self-hosted environment?
  461. *
  462. * @return bool
  463. */
  464. static function is_jetpack() {
  465. return ! self::is_wpcom();
  466. }
  467. /**
  468. * Used to determine if there is a valid status slug within the wordpress.com environment.
  469. *
  470. * @return bool
  471. */
  472. static function wpcom_has_status_message() {
  473. return isset( $_GET['blogsub'] ) &&
  474. in_array(
  475. $_GET['blogsub'],
  476. array(
  477. 'confirming',
  478. 'blocked',
  479. 'flooded',
  480. 'spammed',
  481. 'subscribed',
  482. 'pending',
  483. 'confirmed',
  484. )
  485. );
  486. }
  487. /**
  488. * Determine the amount of folks currently subscribed to the blog.
  489. *
  490. * @return int|array
  491. */
  492. static function fetch_subscriber_count() {
  493. $subs_count = 0;
  494. if ( self::is_jetpack() ) {
  495. $subs_count = get_transient( 'wpcom_subscribers_total' );
  496. if ( false === $subs_count || 'failed' == $subs_count['status'] ) {
  497. $xml = new Jetpack_IXR_Client();
  498. $xml->query( 'jetpack.fetchSubscriberCount' );
  499. 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
  500. $subs_count = array(
  501. 'status' => 'failed',
  502. 'code' => $xml->getErrorCode(),
  503. 'message' => $xml->getErrorMessage(),
  504. 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
  505. );
  506. } else {
  507. $subs_count = array(
  508. 'status' => 'success',
  509. 'value' => $xml->getResponse(),
  510. );
  511. }
  512. set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour
  513. }
  514. }
  515. if ( self::is_wpcom() && function_exists( 'wpcom_reach_total_for_blog' ) ) {
  516. $subs_count = wpcom_reach_total_for_blog();
  517. }
  518. return $subs_count;
  519. }
  520. /**
  521. * Updates a particular instance of a widget when someone saves it in wp-admin.
  522. *
  523. * @param array $new_instance
  524. * @param array $old_instance
  525. *
  526. * @return array
  527. */
  528. function update( $new_instance, $old_instance ) {
  529. $instance = $old_instance;
  530. if ( self::is_jetpack() ) {
  531. $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
  532. $instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() );
  533. $instance['subscribe_button'] = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() );
  534. $instance['success_message'] = wp_kses( stripslashes( $new_instance['success_message'] ), array() );
  535. }
  536. if ( self::is_wpcom() ) {
  537. $instance['title'] = strip_tags( stripslashes( $new_instance['title'] ) );
  538. $instance['title_following'] = strip_tags( stripslashes( $new_instance['title_following'] ) );
  539. $instance['subscribe_logged_in'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_logged_in'] ) );
  540. $instance['subscribe_button'] = strip_tags( stripslashes( $new_instance['subscribe_button'] ) );
  541. }
  542. $instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
  543. $instance['show_only_email_and_button'] = isset( $new_instance['show_only_email_and_button'] ) && $new_instance['show_only_email_and_button'];
  544. $instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) );
  545. return $instance;
  546. }
  547. /**
  548. * The default args for rendering a subscription form.
  549. *
  550. * @return array
  551. */
  552. static function defaults() {
  553. $defaults = array(
  554. 'show_subscribers_total' => true,
  555. 'show_only_email_and_button' => false
  556. );
  557. $defaults['title'] = esc_html__( 'Subscribe to Blog via Email', 'jetpack' );
  558. $defaults['subscribe_text'] = esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' );
  559. $defaults['subscribe_placeholder'] = esc_html__( 'Email Address', 'jetpack' );
  560. $defaults['subscribe_button'] = esc_html__( 'Subscribe', 'jetpack' );
  561. $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' );
  562. return $defaults;
  563. }
  564. /**
  565. * Renders the widget's options form in wp-admin.
  566. *
  567. * @param array $instance
  568. */
  569. function form( $instance ) {
  570. $instance = wp_parse_args( (array) $instance, $this->defaults() );
  571. $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
  572. if ( self::is_wpcom() ) {
  573. $title = esc_attr( stripslashes( $instance['title'] ) );
  574. $title_following = esc_attr( stripslashes( $instance['title_following'] ) );
  575. $subscribe_text = esc_attr( stripslashes( $instance['subscribe_text'] ) );
  576. $subscribe_logged_in = esc_attr( stripslashes( $instance['subscribe_logged_in'] ) );
  577. $subscribe_button = esc_attr( stripslashes( $instance['subscribe_button'] ) );
  578. $subscribers_total = self::fetch_subscriber_count();
  579. }
  580. if ( self::is_jetpack() ) {
  581. $title = stripslashes( $instance['title'] );
  582. $subscribe_text = stripslashes( $instance['subscribe_text'] );
  583. $subscribe_placeholder = stripslashes( $instance['subscribe_placeholder'] );
  584. $subscribe_button = stripslashes( $instance['subscribe_button'] );
  585. $success_message = stripslashes( $instance['success_message'] );
  586. $subs_fetch = self::fetch_subscriber_count();
  587. if ( 'failed' == $subs_fetch['status'] ) {
  588. printf( '<div class="error inline"><p>%s: %s</p></div>', esc_html( $subs_fetch['code'] ), esc_html( $subs_fetch['message'] ) );
  589. }
  590. $subscribers_total = number_format_i18n( $subs_fetch['value'] );
  591. }
  592. if ( self::is_wpcom() ) : ?>
  593. <p>
  594. <label for="<?php echo $this->get_field_id( 'title' ); ?>">
  595. <?php _e( 'Widget title for non-followers:' ); ?>
  596. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
  597. name="<?php echo $this->get_field_name( 'title' ); ?>" type="text"
  598. value="<?php echo $title; ?>"/>
  599. </label>
  600. </p>
  601. <p>
  602. <label for="<?php echo $this->get_field_id( 'title_following' ); ?>">
  603. <?php _e( 'Widget title for followers:' ); ?>
  604. <input class="widefat" id="<?php echo $this->get_field_id( 'title_following' ); ?>"
  605. name="<?php echo $this->get_field_name( 'title_following' ); ?>" type="text"
  606. value="<?php echo $title_following; ?>"/>
  607. </label>
  608. </p>
  609. <p>
  610. <label for="<?php echo $this->get_field_id( 'subscribe_logged_in' ); ?>">
  611. <?php _e( 'Optional text to display to logged in WordPress.com users:' ); ?>
  612. <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_logged_in' ); ?>"
  613. name="<?php echo $this->get_field_name( 'subscribe_logged_in' ); ?>"
  614. type="text"><?php echo $subscribe_logged_in; ?></textarea>
  615. </label>
  616. </p>
  617. <p>
  618. <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>">
  619. <?php _e( 'Optional text to display to non-WordPress.com users:' ); ?>
  620. <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"
  621. name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>"
  622. type="text"><?php echo $subscribe_text; ?></textarea>
  623. </label>
  624. </p>
  625. <p>
  626. <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>">
  627. <?php _e( 'Follow Button Text:' ); ?>
  628. <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"
  629. name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text"
  630. value="<?php echo $subscribe_button; ?>"/>
  631. </label>
  632. </p>
  633. <p>
  634. <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>">
  635. <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"
  636. name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>"
  637. value="1"<?php echo $show_subscribers_total; ?> />
  638. <?php echo esc_html( sprintf( _n( 'Show total number of followers? (%s follower)', 'Show total number of followers? (%s followers)', $subscribers_total ), number_format_i18n( $subscribers_total ) ) ); ?>
  639. </label>
  640. </p>
  641. <?php endif;
  642. if ( self::is_jetpack() ) : ?>
  643. <p>
  644. <label for="<?php echo $this->get_field_id( 'title' ); ?>">
  645. <?php _e( 'Widget title:', 'jetpack' ); ?>
  646. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
  647. name="<?php echo $this->get_field_name( 'title' ); ?>" type="text"
  648. value="<?php echo esc_attr( $title ); ?>"/>
  649. </label>
  650. </p>
  651. <p>
  652. <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>">
  653. <?php _e( 'Optional text to display to your readers:', 'jetpack' ); ?>
  654. <textarea class="widefat" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"
  655. name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>"
  656. rows="3"><?php echo esc_html( $subscribe_text ); ?></textarea>
  657. </label>
  658. </p>
  659. <p>
  660. <label for="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>">
  661. <?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?>
  662. <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>"
  663. name="<?php echo $this->get_field_name( 'subscribe_placeholder' ); ?>" type="text"
  664. value="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
  665. </label>
  666. </p>
  667. <p>
  668. <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>">
  669. <?php _e( 'Subscribe Button:', 'jetpack' ); ?>
  670. <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"
  671. name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text"
  672. value="<?php echo esc_attr( $subscribe_button ); ?>"/>
  673. </label>
  674. </p>
  675. <p>
  676. <label for="<?php echo $this->get_field_id( 'success_message' ); ?>">
  677. <?php _e( 'Success Message Text:', 'jetpack' ); ?>
  678. <textarea class="widefat" id="<?php echo $this->get_field_id( 'success_message' ); ?>"
  679. name="<?php echo $this->get_field_name( 'success_message' ); ?>"
  680. rows="5"><?php echo esc_html( $success_message ); ?></textarea>
  681. </label>
  682. </p>
  683. <p>
  684. <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>">
  685. <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"
  686. name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>"
  687. value="1"<?php echo $show_subscribers_total; ?> />
  688. <?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 ) ); ?>
  689. </label>
  690. </p>
  691. <?php endif;
  692. }
  693. }
  694. if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'class_alias' ) ) {
  695. class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' );
  696. }
  697. function get_jetpack_blog_subscriptions_widget_classname() {
  698. return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
  699. 'Blog_Subscription_Widget' :
  700. 'Jetpack_Subscriptions_Widget';
  701. }
  702. function jetpack_do_subscription_form( $instance ) {
  703. if ( empty( $instance ) || ! is_array( $instance ) ) {
  704. $instance = array();
  705. }
  706. if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
  707. $instance['show_subscribers_total'] = false;
  708. } else {
  709. $instance['show_subscribers_total'] = true;
  710. }
  711. $show_only_email_and_button = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
  712. $submit_button_text = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
  713. // Build up a string with the submit button's classes and styles and set it on the instance
  714. $submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
  715. $email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
  716. $style = '';
  717. $submit_button_styles = '';
  718. $submit_button_wrapper_styles = '';
  719. $email_field_styles = '';
  720. if ( isset( $instance['custom_background_button_color'] ) && 'undefined' !== $instance['custom_background_button_color'] ) {
  721. $submit_button_styles .= 'background: ' . $instance['custom_background_button_color'] . '; ';
  722. }
  723. if ( isset( $instance['custom_text_button_color'] ) && 'undefined' !== $instance['custom_text_button_color'] ) {
  724. $submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . '; ';
  725. }
  726. if ( isset( $instance['custom_button_width'] ) && 'undefined' !== $instance['custom_button_width'] ) {
  727. $submit_button_wrapper_styles .= 'width: ' . $instance['custom_button_width'] . '; ';
  728. $submit_button_wrapper_styles .= 'max-width: 100%; ';
  729. // Account for custom margins on inline forms.
  730. if (
  731. ! empty( $instance['custom_spacing'] ) &&
  732. ! ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] )
  733. ) {
  734. $submit_button_styles .= 'width: calc(100% - ' . $instance['custom_spacing'] . 'px); ';
  735. } else {
  736. $submit_button_styles .= 'width: 100%; ';
  737. }
  738. }
  739. if ( isset( $instance['custom_font_size'] ) && 'undefined' !== $instance['custom_font_size'] ) {
  740. $style = 'font-size: ' . $instance['custom_font_size'];
  741. $style .= is_numeric( $instance['custom_font_size'] ) ? 'px; ' : '; '; // Handle deprecated numeric font size values.
  742. $submit_button_styles .= $style;
  743. $email_field_styles .= $style;
  744. }
  745. if ( isset( $instance['custom_padding'] ) && 'undefined' !== $instance['custom_padding'] ) {
  746. $style = 'padding: ' .
  747. $instance['custom_padding'] . 'px ' .
  748. round( $instance['custom_padding'] * 1.5 ) . 'px ' .
  749. $instance['custom_padding'] . 'px ' .
  750. round( $instance['custom_padding'] * 1.5 ) . 'px; ';
  751. $submit_button_styles .= $style;
  752. $email_field_styles .= $style;
  753. }
  754. $button_spacing = 0;
  755. if ( ! empty( $instance['custom_spacing'] ) ) {
  756. $button_spacing = $instance['custom_spacing'];
  757. }
  758. if ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] ) {
  759. $submit_button_styles .= 'margin-top: ' . $button_spacing . 'px; ';
  760. } else {
  761. $submit_button_styles .= 'margin-left: ' . $button_spacing . 'px; ';
  762. }
  763. if ( isset( $instance['custom_border_radius'] ) && 'undefined' !== $instance['custom_border_radius'] ) {
  764. $style = 'border-radius: ' . $instance['custom_border_radius'] . 'px; ';
  765. $submit_button_styles .= $style;
  766. $email_field_styles .= $style;
  767. }
  768. if ( isset( $instance['custom_border_weight'] ) && 'undefined' !== $instance['custom_border_weight'] ) {
  769. $style = 'border-width: ' . $instance['custom_border_weight'] . 'px; ';
  770. $submit_button_styles .= $style;
  771. $email_field_styles .= $style;
  772. }
  773. if ( isset( $instance['custom_border_color'] ) && 'undefined' !== $instance['custom_border_color'] ) {
  774. $style =
  775. 'border-color: ' . $instance['custom_border_color'] . '; ' .
  776. 'border-style: solid;';
  777. $submit_button_styles .= $style;
  778. $email_field_styles .= $style;
  779. }
  780. $instance = shortcode_atts(
  781. Jetpack_Subscriptions_Widget::defaults(),
  782. $instance,
  783. 'jetpack_subscription_form'
  784. );
  785. // These must come after the call to shortcode_atts()
  786. $instance['submit_button_text'] = $submit_button_text;
  787. $instance['show_only_email_and_button'] = $show_only_email_and_button;
  788. if ( ! empty( $submit_button_classes ) ) {
  789. $instance['submit_button_classes'] = $submit_button_classes;
  790. }
  791. if ( ! empty( $email_field_classes ) ) {
  792. $instance['email_field_classes'] = $email_field_classes;
  793. }
  794. if ( ! empty( $submit_button_styles ) ) {
  795. $instance['submit_button_styles'] = trim( $submit_button_styles );
  796. }
  797. if ( ! empty( $submit_button_wrapper_styles ) ) {
  798. $instance['submit_button_wrapper_styles'] = trim( $submit_button_wrapper_styles );
  799. }
  800. if ( ! empty( $email_field_styles ) ) {
  801. $instance['email_field_styles'] = trim( $email_field_styles );
  802. }
  803. $args = array(
  804. 'before_widget' => '<div class="jetpack_subscription_widget">',
  805. );
  806. ob_start();
  807. the_widget( get_jetpack_blog_subscriptions_widget_classname(), $instance, $args );
  808. $output = ob_get_clean();
  809. return $output;
  810. }
  811. add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' );
  812. add_shortcode( 'blog_subscription_form', 'jetpack_do_subscription_form' );
  813. function jetpack_blog_subscriptions_init() {
  814. register_widget( get_jetpack_blog_subscriptions_widget_classname() );
  815. }
  816. add_action( 'widgets_init', 'jetpack_blog_subscriptions_init' );