Completed
Push — add/follow-button-widget ( 528d3a )
by
unknown
213:38 queued 206:11
created

Follow_Button_Widget::update()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 2
dl 0
loc 5
rs 9.4285
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
	register_widget( 'Follow_Button_Widget' );
6
}
7
8
class Follow_Button_Widget extends WP_Widget {
9
10
	public function __construct() {
11
		parent::__construct(
12
			'follow_button_widget',
13
			__( 'Follow Button', 'jetpack' ),
14
			array( 'description' => __('Add a WordPress.com follow button to allow people to follow your blog easier', 'jetpack' ) )
15
		);
16
	}
17
18
	public function widget( $args, $instance ) {
19
		$attributes = array();
20
		$instance = wp_parse_args( (array) $instance, array( 'show_name' => 1, 'show_count' => 0 ) );
21
22
		if ( empty( $instance['show_name'] ) )
23
			$attributes[] = 'data-show-blog-name="false"';
24
25
		if ( ! empty( $instance['show_count'] ) )
26
			$attributes[] = 'data-show-follower-count="true"';
27
28
		echo $args['before_widget'];
29
		?>
30
31
		<a class="wordpress-follow-button" href="<?php echo site_url(); ?>" data-blog="<?php echo site_url(); ?>" data-lang="<?php echo get_locale(); ?>" <?php if ( ! empty( $attributes ) ) echo implode( ' ', $attributes ); ?>>Follow <?php bloginfo( 'name' ); ?> on WordPress.com</a>
32
		<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>
33
34
		<?php
35
		echo $args['after_widget'];
36
37
		/** This action is documented in modules/widgets/gravatar-profile.php */
38
		do_action( 'jetpack_stats_extra', 'widget_view', 'follow_button' );
39
	}
40
41
	public function form( $instance ) {
42
		$instance = wp_parse_args( (array) $instance, array( 'show_name' => 1, 'show_count' => 0 ) );
43
44
		$show_name = isset( $instance['show_name'] ) ? (bool) $instance['show_name'] : false;
45
		$show_count = isset( $instance['show_count'] ) ? (bool) $instance['show_count'] : false;
46
		?>
47
48
		<p>
49
		<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 ); ?> />
50
		<label for="<?php echo $this->get_field_id('show_name'); ?>"><?php _e( 'Show blog name' ); ?></label>
51
		<br />
52
		<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 ); ?> />
53
		<label for="<?php echo $this->get_field_id('show_count'); ?>"><?php _e( 'Show follower count' ); ?></label>
54
		</p>
55
56
		<?php
57
	}
58
59
	public function update( $new_instance, $old_instance ) {
60
		$old_instance['show_name'] = ! empty( $new_instance['show_name'] ) ? 1 : 0;
61
		$old_instance['show_count'] = ! empty( $new_instance['show_count'] ) ? 1 : 0;
62
		return $old_instance;
63
	}
64
}
65