Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
jetpack_twitter_widget_init
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
Jetpack_Widget_Twitter
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 4
72
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 / 31
0.00% covered (danger)
0.00%
0 / 1
20
 update
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 form
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Twitter widget class
4 * Display the latest N tweets from a Twitter screenname as a widget
5 * Customize screenname, maximum number of tweets displayed, show or hide @replies, and text displayed between tweet text and a timestamp
6 *
7 * @package widgets
8 */
9
10/**
11 * Twitter widget class.
12 */
13class Jetpack_Widget_Twitter extends WP_Widget {
14
15    /**
16     * Constructor.
17     */
18    public function __construct() {
19        parent::__construct(
20            'twitter',
21            apply_filters( 'jetpack_widget_name', __( 'Twitter', 'wpcomsh' ) ),
22            array(
23                'classname'   => 'widget_twitter',
24                'description' => __( 'Display your Tweets from Twitter', 'wpcomsh' ),
25            )
26        );
27    }
28
29    /**
30     * Display the widget.
31     *
32     * @param array $args     Widget arguments.
33     * @param array $instance Widget instance.
34     */
35    public function widget( $args, $instance ) {
36        $account = trim( rawurlencode( $instance['account'] ?? '' ) );
37
38        if ( empty( $account ) ) {
39            if ( current_user_can( 'edit_theme_options' ) ) {
40                echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
41                /* translators: %s is the URL to the widgets page */
42                echo '<p>';
43                printf(
44                    wp_kses(
45                        // translators: %s is a link to the widget settings page.
46                        __( 'Please configure your Twitter username for the <a href="%s">Twitter Widget</a>.', 'wpcomsh' ),
47                        array( 'a' => array( 'href' => array() ) )
48                    ),
49                    esc_url( admin_url( 'widgets.php' ) )
50                );
51                echo '</p>';
52                echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
53            }
54
55            return;
56        }
57
58        $title = apply_filters( 'widget_title', $instance['title'] );
59        if ( empty( $title ) ) {
60            $title = __( 'Twitter Updates', 'wpcomsh' );
61        }
62
63        echo "{$args['before_widget']}{$args['before_title']}<a href='" . esc_url( "http://twitter.com/{$account}" ) . "'>" . esc_html( $title ) . "</a>{$args['after_title']}"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
64
65        $anchor_text = sprintf(
66            /* translators: %s is the Twitter account name */
67            __( 'Tweets by %s', 'wpcomsh' ),
68            esc_html( $account )
69        );
70
71        printf(
72            '<a class="twitter-timeline" data-height="600" data-dnt="true" href="https://twitter.com/%1$s">%2$s</a>',
73            esc_attr( $account ),
74            esc_html( $anchor_text )
75        );
76
77        wp_enqueue_script( 'twitter-widgets', 'https://platform.twitter.com/widgets.js', array(), '20111117', true );
78
79        echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
80
81        /** This action is documented in modules/widgets/gravatar-profile.php */
82        do_action( 'jetpack_stats_extra', 'widget_view', 'twitter' );
83    }
84
85    /**
86     * Update the widget settings.
87     *
88     * @param array $new_instance New settings.
89     * @param array $old_instance Old settings.
90     */
91    public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
92        $instance = array();
93
94        $instance['title']   = wp_kses( $new_instance['title'], array() );
95        $instance['account'] = trim( wp_kses( $new_instance['account'], array() ) );
96        $instance['account'] = str_replace(
97            array(
98                'http://twitter.com/',
99                '/',
100                '@',
101                '#!',
102            ),
103            array(
104                '',
105                '',
106                '',
107                '',
108            ),
109            $instance['account']
110        );
111
112        delete_transient( 'widget-twitter-' . $this->number );
113        delete_transient( 'widget-twitter-error-' . $this->number );
114
115        return $instance;
116    }
117
118    /**
119     * Display the widget settings form.
120     *
121     * @param array $instance Current settings.
122     * @return never
123     */
124    public function form( $instance ) {
125        // Defaults.
126        $account = isset( $instance['account'] ) ? wp_kses( $instance['account'], array() ) : '';
127        $title   = $instance['title'] ?? '';
128
129        /*
130         * Urge people to upgrade to the new twitter timeline widget. While this widget will continue working, we may totally remove it in the future.
131         *
132         * @see http://socialp2.wordpress.com/2013/04/19/following-on-from-justins-previous-post-its-time/
133         */
134        ?>
135        <p><em><?php echo esc_html__( "Please switch to the 'Twitter Timeline' widget. This widget will be going away in the future and the new widget allows for more customization.", 'wpcomsh' ); ?></em></p>
136
137        <p>
138            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
139                <?php esc_html_e( 'Title:', 'wpcomsh' ); ?>
140                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
141            </label>
142        </p>
143
144        <p>
145            <label for="<?php echo esc_attr( $this->get_field_id( 'account' ) ); ?>">
146                <?php esc_html_e( 'Twitter username:', 'wpcomsh' ); ?> <a href="http://support.wordpress.com/widgets/twitter-widget/#twitter-username" target="_blank">( ? )</a>
147                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'account' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'account' ) ); ?>" type="text" value="<?php echo esc_attr( $account ); ?>" />
148            </label>
149        </p>
150
151        <?php
152    }
153}
154
155/**
156 * Register the widget for use in Appearance -> Widgets
157 */
158function jetpack_twitter_widget_init() { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
159    // Don't load this Widget for users who don't have preferences for it.
160    $widgets = get_option( 'widget_twitter' );
161    if ( ! is_array( $widgets ) ) {
162        return;
163    }
164
165    register_widget( 'Jetpack_Widget_Twitter' );
166}
167add_action( 'widgets_init', 'jetpack_twitter_widget_init' );