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