Completed
Push — add/jetpack-plan-support ( c36d23...95100b )
by
unknown
14:33 queued 05:53
created

Jetpack_Follow_Button_Widget::widget()   C

Complexity

Conditions 9
Paths 72

Size

Total Lines 46
Code Lines 27

Duplication

Lines 6
Ratio 13.04 %

Importance

Changes 0
Metric Value
cc 9
eloc 27
nc 72
nop 2
dl 6
loc 46
rs 5.0942
c 0
b 0
f 0
1
<?php
2
3
// @todo Fix performance issues before shipping.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
4
//add_action( 'widgets_init', 'follow_button_register_widget' );
5
function follow_button_register_widget() {
6
	if ( Jetpack::is_active() ) {
7
		register_widget( 'Jetpack_Follow_Button_Widget' );
8
	}
9
}
10
11
class Jetpack_Follow_Button_Widget extends WP_Widget {
12
13 View Code Duplication
	public function __construct() {
14
		parent::__construct(
15
			'follow_button_widget',
16
			/** This filter is documented in modules/widgets/facebook-likebox.php */
17
			apply_filters( 'jetpack_widget_name', __( 'Follow Button', 'jetpack' ) ),
18
			array(
19
				'description' => __( 'Add a WordPress.com follow button to allow people to follow your blog easier', 'jetpack' ),
20
				'customize_selective_refresh' => true,
21
			)
22
		);
23
	}
24
25
	public function widget( $args, $instance ) {
26
		$attributes = array();
27
		$instance = wp_parse_args( (array) $instance, array( 'show_name' => 1, 'show_count' => 0 ) );
28
29
		$wpcom_locale = get_locale();
30
31
		if ( ! class_exists( 'GP_Locales' ) ) {
32
			if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
33
				require JETPACK__GLOTPRESS_LOCALES_PATH;
34
			}
35
		}
36
37 View Code Duplication
		if ( class_exists( 'GP_Locales' ) ) {
38
			$wpcom_locale_object = GP_Locales::by_field( 'wp_locale', $wpcom_locale );
39
			if ( $wpcom_locale_object instanceof GP_Locale ) {
40
				$wpcom_locale = $wpcom_locale_object->slug;
41
			}
42
		}
43
44
		if ( empty( $instance['show_name'] ) ) {
45
			$attributes[] = 'data-show-blog-name="false"';
46
		}
47
48
		if ( ! empty( $instance['show_count'] ) ) {
49
			$attributes[] = 'data-show-follower-count="true"';
50
		}
51
52
		echo $args['before_widget'];
53
		?>
54
55
		<a
56
			class="wordpress-follow-button"
57
			href="<?php echo esc_url( home_url() ); ?>"
58
			data-blog="<?php echo esc_url( home_url() ); ?>"
59
			data-lang="<?php echo esc_attr( $wpcom_locale ); ?>" <?php if ( ! empty( $attributes ) ) echo implode( ' ', $attributes ); ?>
60
		>
61
			<?php sprintf( __( 'Follow %s on WordPress.com', 'jetpack' ), get_bloginfo( 'name' ) ); ?>
62
		</a>
63
		<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>
64
65
		<?php
66
		echo $args['after_widget'];
67
68
		/** This action is documented in modules/widgets/gravatar-profile.php */
69
		do_action( 'jetpack_stats_extra', 'widget_view', 'follow_button' );
70
	}
71
72
	public function form( $instance ) {
73
		$instance = wp_parse_args( (array) $instance, array( 'show_name' => 1, 'show_count' => 0 ) );
74
75
		$show_name = isset( $instance['show_name'] ) ? (bool) $instance['show_name'] : false;
76
		$show_count = isset( $instance['show_count'] ) ? (bool) $instance['show_count'] : false;
77
		?>
78
79
		<p>
80
		<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 ); ?> />
81
		<label for="<?php echo $this->get_field_id('show_name'); ?>"><?php esc_html_e( 'Show blog name', 'jetpack' ); ?></label>
82
		<br />
83
		<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 ); ?> />
84
		<label for="<?php echo $this->get_field_id('show_count'); ?>"><?php esc_html_e( 'Show follower count', 'jetpack' ); ?></label>
85
		</p>
86
87
		<?php
88
	}
89
90
	public function update( $new_instance, $old_instance ) {
91
		$old_instance['show_name'] = ! empty( $new_instance['show_name'] ) ? 1 : 0;
92
		$old_instance['show_count'] = ! empty( $new_instance['show_count'] ) ? 1 : 0;
93
		return $old_instance;
94
	}
95
}
96