Jetpack_Subscriptions_Widget::widget PHP Метод

widget() публичный Метод

public widget ( $args, $instance )
    function widget($args, $instance)
    {
        if ((!defined('IS_WPCOM') || !IS_WPCOM) && false === apply_filters('jetpack_auto_fill_logged_in_user', false)) {
            $subscribe_email = '';
        } else {
            global $current_user;
            if (!empty($current_user->user_email)) {
                $subscribe_email = esc_attr($current_user->user_email);
            } else {
                $subscribe_email = '';
            }
        }
        /** This action is already documented in modules/widgets/gravatar-profile.php */
        do_action('jetpack_stats_extra', 'widget_view', 'jetpack_subscriptions');
        $source = 'widget';
        $instance = wp_parse_args((array) $instance, $this->defaults());
        $subscribe_text = isset($instance['subscribe_text']) ? stripslashes($instance['subscribe_text']) : '';
        $subscribe_placeholder = isset($instance['subscribe_placeholder']) ? stripslashes($instance['subscribe_placeholder']) : '';
        $subscribe_button = isset($instance['subscribe_button']) ? stripslashes($instance['subscribe_button']) : '';
        $success_message = isset($instance['success_message']) ? stripslashes($instance['success_message']) : '';
        $widget_id = esc_attr(!empty($args['widget_id']) ? esc_attr($args['widget_id']) : mt_rand(450, 550));
        $show_subscribers_total = (bool) $instance['show_subscribers_total'];
        $subscribers_total = $this->fetch_subscriber_count();
        // Only used for the shortcode [total-subscribers]
        // Give the input element a unique ID
        /**
         * Filter the subscription form's ID prefix.
         *
         * @module subscriptions
         *
         * @since 2.7.0
         *
         * @param string subscribe-field Subscription form field prefix.
         * @param int $widget_id Widget ID.
         */
        $subscribe_field_id = apply_filters('subscribe_field_id', 'subscribe-field', $widget_id);
        // Enqueue the form's CSS
        wp_register_style('jetpack-subscriptions', plugins_url('subscriptions/subscriptions.css', __FILE__));
        wp_enqueue_style('jetpack-subscriptions');
        // Display the subscription form
        echo $args['before_widget'];
        // Only show the title if there actually is a title
        if (!empty($instance['title'])) {
            echo $args['before_title'] . esc_attr($instance['title']) . $args['after_title'] . "\n";
        }
        $referer = set_url_scheme('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        // Display any errors
        if (isset($_GET['subscribe'])) {
            switch ($_GET['subscribe']) {
                case 'invalid_email':
                    ?>
					<p class="error"><?php 
                    esc_html_e('The email you entered was invalid. Please check and try again.', 'jetpack');
                    ?>
</p>
				<?php 
                    break;
                case 'opted_out':
                    ?>
					<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'), 'https://subscribe.wordpress.com/', __('Manage your email preferences.', 'jetpack'));
                    ?>
				<?php 
                    break;
                case 'already':
                    ?>
					<p class="error"><?php 
                    esc_html_e('You have already subscribed to this site. Please check your inbox.', 'jetpack');
                    ?>
</p>
				<?php 
                    break;
                case 'success':
                    ?>
					<div class="success"><?php 
                    echo wpautop(str_replace('[total-subscribers]', number_format_i18n($subscribers_total['value']), $success_message));
                    ?>
</div>
					<?php 
                    break;
                default:
                    ?>
					<p class="error"><?php 
                    esc_html_e('There was an error when subscribing. Please try again.', 'jetpack');
                    ?>
</p>
				<?php 
                    break;
            }
        }
        // Display a subscribe form
        if (isset($_GET['subscribe']) && 'success' == $_GET['subscribe']) {
            ?>
			<?php 
        } else {
            ?>
			<form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-<?php 
            echo $widget_id;
            ?>
">
				<?php 
            if (!isset($_GET['subscribe']) || 'success' != $_GET['subscribe']) {
                ?>
<div id="subscribe-text"><?php 
                echo wpautop(str_replace('[total-subscribers]', number_format_i18n($subscribers_total['value']), $subscribe_text));
                ?>
</div><?php 
            }
            if ($show_subscribers_total && 0 < $subscribers_total['value']) {
                echo wpautop(sprintf(_n('Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack'), number_format_i18n($subscribers_total['value'])));
            }
            if (!isset($_GET['subscribe']) || 'success' != $_GET['subscribe']) {
                ?>
					<p id="subscribe-email">
						<label id="jetpack-subscribe-label" for="<?php 
                echo esc_attr($subscribe_field_id) . '-' . esc_attr($widget_id);
                ?>
">
							<?php 
                echo !empty($subscribe_placeholder) ? esc_html($subscribe_placeholder) : esc_html__('Email Address:', 'jetpack');
                ?>
						</label>
						<input type="email" name="email" required="required" class="required" value="<?php 
                echo esc_attr($subscribe_email);
                ?>
" id="<?php 
                echo esc_attr($subscribe_field_id) . '-' . esc_attr($widget_id);
                ?>
" placeholder="<?php 
                echo esc_attr($subscribe_placeholder);
                ?>
" />
					</p>

					<p id="subscribe-submit">
						<input type="hidden" name="action" value="subscribe" />
						<input type="hidden" name="source" value="<?php 
                echo esc_url($referer);
                ?>
" />
						<input type="hidden" name="sub-type" value="<?php 
                echo esc_attr($source);
                ?>
" />
						<input type="hidden" name="redirect_fragment" value="<?php 
                echo $widget_id;
                ?>
" />
						<?php 
                if (is_user_logged_in()) {
                    wp_nonce_field('blogsub_subscribe_' . get_current_blog_id(), '_wpnonce', false);
                }
                ?>
						<input type="submit" value="<?php 
                echo esc_attr($subscribe_button);
                ?>
" name="jetpack_subscriptions_widget" />
					</p>
				<?php 
            }
            ?>
			</form>

			<script>
			/*
			Custom functionality for safari and IE
			 */
			(function( d ) {
				// In case the placeholder functionality is available we remove labels
				if ( ( 'placeholder' in d.createElement( 'input' ) ) ) {
					var label = d.querySelector( 'label[for=subscribe-field-<?php 
            echo $widget_id;
            ?>
]' );
						label.style.clip 	 = 'rect(1px, 1px, 1px, 1px)';
						label.style.position = 'absolute';
						label.style.height   = '1px';
						label.style.width    = '1px';
						label.style.overflow = 'hidden';
				}

				// Make sure the email value is filled in before allowing submit
				var form = d.getElementById('subscribe-blog-<?php 
            echo $widget_id;
            ?>
'),
					input = d.getElementById('<?php 
            echo esc_attr($subscribe_field_id) . '-' . esc_attr($widget_id);
            ?>
'),
					handler = function( event ) {
						if ( '' === input.value ) {
							input.focus();

							if ( event.preventDefault ){
								event.preventDefault();
							}

							return false;
						}
					};

				if ( window.addEventListener ) {
					form.addEventListener( 'submit', handler, false );
				} else {
					form.attachEvent( 'onsubmit', handler );
				}
			})( document );
			</script>
		<?php 
        }
        ?>
		<?php 
        echo "\n" . $args['after_widget'];
    }