Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
follow_button_register_widget
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
Jetpack_Follow_Button_Widget
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 4
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 widget
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
90
 form
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 update
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
8
9// @todo Fix performance issues before shipping.
10// add_action( 'widgets_init', 'follow_button_register_widget' );
11/**
12 * Register the Follow Button widget.
13 */
14function follow_button_register_widget() {
15    if ( Jetpack::is_connection_ready() ) {
16        register_widget( 'Jetpack_Follow_Button_Widget' );
17    }
18}
19
20/**
21 * Jetpack_Follow_Button_Widget main class.
22 */
23class Jetpack_Follow_Button_Widget extends WP_Widget {
24
25    /**
26     * Jetpack_Follow_Button_Widget constructor.
27     */
28    public function __construct() {
29        parent::__construct(
30            'follow_button_widget',
31            /** This filter is documented in modules/widgets/facebook-likebox.php */
32            apply_filters( 'jetpack_widget_name', __( 'Follow Button', 'jetpack' ) ),
33            array(
34                'description'                 => __( 'Add a WordPress.com follow button to allow people to follow your blog easier', 'jetpack' ),
35                'customize_selective_refresh' => true,
36            )
37        );
38    }
39
40    /**
41     * Front-end display of widget.
42     *
43     * @see WP_Widget::widget() for more information on widget parameters.
44     *
45     * @param array $args     Widget arguments.
46     * @param array $instance Saved values from database.
47     */
48    public function widget( $args, $instance ) {
49        $attributes = array();
50        $instance   = wp_parse_args(
51            (array) $instance,
52            array(
53                'show_name'  => 1,
54                'show_count' => 0,
55            )
56        );
57
58        $wpcom_locale = get_locale();
59
60        if ( ! class_exists( 'GP_Locales' ) ) {
61            if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
62                require JETPACK__GLOTPRESS_LOCALES_PATH;
63            }
64        }
65
66        if ( class_exists( 'GP_Locales' ) ) {
67            $wpcom_locale_object = GP_Locales::by_field( 'wp_locale', $wpcom_locale );
68            if ( $wpcom_locale_object instanceof GP_Locale ) {
69                $wpcom_locale = $wpcom_locale_object->slug;
70            }
71        }
72
73        if ( empty( $instance['show_name'] ) ) {
74            $attributes[] = 'data-show-blog-name="false"';
75        }
76
77        if ( ! empty( $instance['show_count'] ) ) {
78            $attributes[] = 'data-show-follower-count="true"';
79        }
80
81        $localized = array(
82            'titles' => array(
83                'timelines'    => __( 'Embeddable Timelines', 'jetpack' ),
84                'followButton' => __( 'Follow Button', 'jetpack' ),
85                'wpEmbeds'     => __( 'WordPress Embeds', 'jetpack' ),
86            ),
87        );
88
89        echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
90        ?>
91
92        <a
93            class="wordpress-follow-button"
94            href="<?php echo esc_url( home_url() ); ?>"
95            data-blog="<?php echo esc_url( home_url() ); ?>"
96            data-lang="<?php echo esc_attr( $wpcom_locale ); ?>"
97                                    <?php
98                                    if ( ! empty( $attributes ) ) {
99                                        echo implode( ' ', $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
100                                    }
101                                    ?>
102        >
103            <?php
104            /* Translators: %s is the site name. */
105            sprintf( __( 'Follow %s on WordPress.com', 'jetpack' ), get_bloginfo( 'name' ) );
106            ?>
107        </a>
108        <script type="text/javascript">(function(d){window.wpcomPlatform = <?php echo wp_json_encode( $localized, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?>;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>
109
110        <?php
111        echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
112
113        /** This action is documented in modules/widgets/gravatar-profile.php */
114        do_action( 'jetpack_stats_extra', 'widget_view', 'follow_button' );
115    }
116
117    /**
118     * Back-end widget form.
119     *
120     * @see WP_Widget::form() for more information on parameters.
121     *
122     * @param array $instance Previously saved values from database.
123     * @return string|void
124     */
125    public function form( $instance ) {
126        $instance = wp_parse_args(
127            (array) $instance,
128            array(
129                'show_name'  => 1,
130                'show_count' => 0,
131            )
132        );
133
134        $show_name  = isset( $instance['show_name'] ) ? (bool) $instance['show_name'] : false;
135        $show_count = isset( $instance['show_count'] ) ? (bool) $instance['show_count'] : false;
136        ?>
137
138        <p>
139        <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_name' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_name' ) ); ?>"<?php checked( $show_name ); ?> />
140        <label for="<?php echo esc_attr( $this->get_field_id( 'show_name' ) ); ?>"><?php esc_html_e( 'Show blog name', 'jetpack' ); ?></label>
141        <br />
142        <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>"<?php checked( $show_count ); ?> />
143        <label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php esc_html_e( 'Show follower count', 'jetpack' ); ?></label>
144        </p>
145
146        <?php
147    }
148
149    /**
150     * Update widget.
151     *
152     * @see WP_Widget::update()
153     *
154     * @param array $new_instance New widget instance data.
155     * @param array $old_instance Old widget instance data.
156     */
157    public function update( $new_instance, $old_instance ) {
158        $old_instance['show_name']  = ! empty( $new_instance['show_name'] ) ? 1 : 0;
159        $old_instance['show_count'] = ! empty( $new_instance['show_count'] ) ? 1 : 0;
160        return $old_instance;
161    }
162}