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