Completed
Push — branch-4.5 ( a840a8...c54d36 )
by
unknown
24:18 queued 16:13
created

Follow_Button_Widget::widget()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 8
nop 2
dl 0
loc 31
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
add_action( 'widgets_init', 'follow_button_register_widget' );
4
function follow_button_register_widget() {
5
	if ( Jetpack::is_active() ) {
6
		register_widget( 'Follow_Button_Widget' );
7
	}
8
}
9
10
class Follow_Button_Widget extends WP_Widget {
11
12
	public function __construct() {
13
		parent::__construct(
14
			'follow_button_widget',
15
			/** This filter is documented in modules/widgets/facebook-likebox.php */
16
			apply_filters( 'jetpack_widget_name', __( 'Follow Button', 'jetpack' ) ),
17
			array(
18
				'description' => __( 'Add a WordPress.com follow button to allow people to follow your blog easier', 'jetpack' ),
19
				'customize_selective_refresh' => true,
20
			)
21
		);
22
	}
23
24
	public function widget( $args, $instance ) {
25
		$attributes = array();
26
		$instance = wp_parse_args( (array) $instance, array( 'show_name' => 1, 'show_count' => 0 ) );
27
28
		if ( empty( $instance['show_name'] ) ) {
29
			$attributes[] = 'data-show-blog-name="false"';
30
		}
31
32
		if ( ! empty( $instance['show_count'] ) ) {
33
			$attributes[] = 'data-show-follower-count="true"';
34
		}
35
36
		echo $args['before_widget'];
37
		?>
38
39
		<a
40
			class="wordpress-follow-button"
41
			href="<?php echo esc_url( site_url() ); ?>"
42
			data-blog="<?php echo esc_url( site_url() ); ?>"
43
			data-lang="<?php echo get_locale(); ?>" <?php if ( ! empty( $attributes ) ) echo implode( ' ', $attributes ); ?>
44
		>
45
			<?php sprintf( __( 'Follow %s on WordPress.com', 'jetpack' ), get_bloginfo( 'name' ) ); ?>
46
		</a>
47
		<script type="text/javascript">(function(d){var f = d.getElementsByTagName('SCRIPT')[0], p = d.createElement('SCRIPT');p.type = 'text/javascript';p.async = true;p.src = '//widgets.wp.com/platform.js';f.parentNode.insertBefore(p,f);}(document));</script>
48
49
		<?php
50
		echo $args['after_widget'];
51
52
		/** This action is documented in modules/widgets/gravatar-profile.php */
53
		do_action( 'jetpack_stats_extra', 'widget_view', 'follow_button' );
54
	}
55
56
	public function form( $instance ) {
57
		$instance = wp_parse_args( (array) $instance, array( 'show_name' => 1, 'show_count' => 0 ) );
58
59
		$show_name = isset( $instance['show_name'] ) ? (bool) $instance['show_name'] : false;
60
		$show_count = isset( $instance['show_count'] ) ? (bool) $instance['show_count'] : false;
61
		?>
62
63
		<p>
64
		<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_name'); ?>" name="<?php echo $this->get_field_name('show_name'); ?>"<?php checked( $show_name ); ?> />
65
		<label for="<?php echo $this->get_field_id('show_name'); ?>"><?php esc_html_e( 'Show blog name', 'jetpack' ); ?></label>
66
		<br />
67
		<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_count'); ?>" name="<?php echo $this->get_field_name('show_count'); ?>"<?php checked( $show_count ); ?> />
68
		<label for="<?php echo $this->get_field_id('show_count'); ?>"><?php esc_html_e( 'Show follower count', 'jetpack' ); ?></label>
69
		</p>
70
71
		<?php
72
	}
73
74
	public function update( $new_instance, $old_instance ) {
75
		$old_instance['show_name'] = ! empty( $new_instance['show_name'] ) ? 1 : 0;
76
		$old_instance['show_count'] = ! empty( $new_instance['show_count'] ) ? 1 : 0;
77
		return $old_instance;
78
	}
79
}
80