Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 172
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
jetpack_gravatar_widget_init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
Gravatar_Widget
0.00% covered (danger)
0.00%
0 / 170
0.00% covered (danger)
0.00%
0 / 4
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 widget
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
182
 form
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 1
12
 update
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
20
1<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2
3/*
4 * File copied from WP.com
5 */
6
7/**
8 * Gravatar Widget
9 */
10class Gravatar_Widget extends WP_Widget {
11    /**
12     * Constructor.
13     */
14    public function __construct() {
15        $widget_ops = array(
16            'classname'   => 'widget_gravatar',
17            'description' => __( 'Insert a Gravatar image', 'wpcomsh' ),
18        );
19
20        parent::__construct( 'gravatar', __( 'Gravatar', 'wpcomsh' ), $widget_ops );
21    }
22
23    /**
24     * Display the widget
25     *
26     * @param array $args Widget arguments.
27     * @param array $instance Widget instance.
28     * @return void
29     **/
30    public function widget( $args, $instance ) {
31        $instance = wp_parse_args(
32            (array) $instance,
33            array(
34                'title'          => '',
35                'gravatar_size'  => 128,
36                'gravatar_align' => 'none',
37                'gravatar_text'  => '',
38                'email'          => '',
39                'email_user'     => -1,
40                'gravatar_url'   => '',
41            )
42        );
43        $title    = apply_filters( 'widget_title', $instance['title'] );
44
45        echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
46
47        if ( $title ) {
48            echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
49        }
50
51        // Widget
52        $text = '';
53        if ( $instance['email'] ) {
54            $text = get_avatar( $instance['email'], $instance['gravatar_size'], '', '', array( 'force_display' => true ) );
55
56            if ( $instance['gravatar_align'] === 'left' ) {
57                $text = str_replace(
58                    array( '/>', 'avatar-' . $instance['gravatar_size'] ),
59                    array( ' style="margin-top: 3px; padding: 0 0.5em 0 0; float: left" />', 'avatar-' . $instance['gravatar_size'] . ' grav-widget-left' ),
60                    $text
61                );
62            } elseif ( $instance['gravatar_align'] === 'right' ) {
63                $text = str_replace(
64                    array( '/>', 'avatar-' . $instance['gravatar_size'] ),
65                    array( ' style="margin-top: 3px; padding: 0 0 0 0.5em; float: right" />', 'avatar-' . $instance['gravatar_size'] . ' grav-widget-right' ),
66                    $text
67                );
68            } elseif ( $instance['gravatar_align'] === 'center' ) {
69                $text = str_replace(
70                    array( '/>', 'avatar-' . $instance['gravatar_size'] ),
71                    array( ' style="display: block; margin: 0 auto;" />', 'avatar-' . $instance['gravatar_size'] . ' grav-widget-center' ),
72                    $text
73                );
74            } else {
75                $text = str_replace(
76                    array( 'avatar-' . $instance['gravatar_size'] ),
77                    array( 'avatar-' . $instance['gravatar_size'] . ' grav-widget-none' ),
78                    $text
79                );
80            }
81
82            if ( $instance['gravatar_url'] ) {
83                $text = '<a href="' . esc_url( $instance['gravatar_url'] ) . '">' . $text . '</a>';
84            }
85
86            if ( $instance['gravatar_text'] && 'center' === $instance['gravatar_align'] ) {
87                $text .= '<br />'; // Get the text on its own line
88            }
89
90            if ( $instance['gravatar_text'] && 'none' === $instance['gravatar_align'] ) {
91                $text .= '<br /><br />'; // So that we get a new P tag from autop
92            }
93        } elseif ( current_user_can( 'edit_theme_options' ) ) {
94            echo '<p>';
95            printf(
96                wp_kses(
97                    // translators: %s is the URL to the widget settings.
98                    __( 'You need to pick a user or enter an email address in your <a href="%s">Gravatar Widget</a> settings.', 'wpcomsh' ),
99                    array(
100                        'a' => array( 'href' => array() ),
101                    )
102                ),
103                esc_url( admin_url( 'widgets.php' ) )
104            );
105            echo '</p>';
106        }
107
108        if ( $instance['gravatar_text'] ) {
109            $text .= stripslashes( $instance['gravatar_text'] );
110        }
111
112        echo wpautop( $text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
113
114        // After
115        echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
116    }
117
118    /**
119     * Display config interface
120     *
121     * @param array $instance Widget instance.
122     * @return never
123     */
124    public function form( $instance ) {
125        $instance = wp_parse_args(
126            (array) $instance,
127            array(
128                'title'          => '',
129                'gravatar_size'  => 128,
130                'gravatar_align' => 'none',
131                'gravatar_text'  => '',
132                'email'          => '',
133                'email_user'     => -1,
134                'gravatar_url'   => '',
135            )
136        );
137
138        $title          = stripslashes( $instance['title'] );
139        $gravatar_size  = $instance['gravatar_size'];
140        $gravatar_align = $instance['gravatar_align'];
141        $gravatar_text  = stripslashes( $instance['gravatar_text'] );
142        $gravatar_url   = $instance['gravatar_url'];
143        $email          = $instance['email'];
144        $email_user     = $instance['email_user'];
145
146        $sizes  = array(
147            64  => __( 'Small (64 pixels)', 'wpcomsh' ),
148            96  => __( 'Medium (96 pixels)', 'wpcomsh' ),
149            128 => __( 'Large (128 pixels)', 'wpcomsh' ),
150            256 => __( 'Extra Large (256 pixels)', 'wpcomsh' ),
151        );
152        $aligns = array(
153            'none'   => __( 'None', 'wpcomsh' ),
154            'left'   => __( 'Left', 'wpcomsh' ),
155            'right'  => __( 'Right', 'wpcomsh' ),
156            'center' => __( 'Center', 'wpcomsh' ),
157        );
158        ?>
159<p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'wpcomsh' ); ?> <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 ); ?>" /></label></p>
160
161<p><?php esc_html_e( 'Select a user or pick "custom" and enter a custom email address.', 'wpcomsh' ); ?></p>
162<p>
163        <?php
164        wp_dropdown_users(
165            array(
166                'show_option_none' => 'Custom',
167                'selected'         => $email_user,
168                'name'             => $this->get_field_name( 'email_user' ),
169            )
170        );
171        ?>
172        </p>
173
174<p id="gravatar_email_user"><label for="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>"><?php esc_html_e( 'Custom Email Address:', 'wpcomsh' ); ?> <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>" type="text" value="<?php echo esc_attr( $email ); ?>" /></label></p>
175
176<p>
177    <label for="<?php echo esc_attr( $this->get_field_id( 'gravatar_size' ) ); ?>"><?php esc_html_e( 'Size:', 'wpcomsh' ); ?>
178        <select id="<?php echo esc_attr( $this->get_field_id( 'gravatar_size' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'gravatar_size' ) ); ?>">
179            <?php foreach ( $sizes as $size => $name ) : ?>
180                <option value="<?php echo $size; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- size is an integer set in the same function.*/ ?>"
181                    <?php selected( $gravatar_size, $size ); ?>
182                ><?php echo esc_html( $name ); ?></option>
183            <?php endforeach; ?>
184        </select>
185    </label>
186</p>
187<p>
188    <label for="<?php echo esc_attr( $this->get_field_id( 'gravatar_align' ) ); ?>"><?php esc_html_e( 'Gravatar alignment:', 'wpcomsh' ); ?>
189        <select id="<?php echo esc_attr( $this->get_field_id( 'gravatar_align' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'gravatar_align' ) ); ?>">
190            <?php foreach ( $aligns as $align => $name ) : ?>
191                <option value="<?php echo esc_attr( $align ); ?>"
192                    <?php selected( $gravatar_align, $align ); ?>
193                ><?php echo esc_html( $name ); ?></option>
194            <?php endforeach; ?>
195        </select>
196    </label>
197</p>
198<p><label for="<?php echo esc_attr( $this->get_field_id( 'gravatar_url' ) ); ?>"><?php esc_html_e( 'Gravatar link. This is an optional URL that will be used when anyone clicks on your Gravatar:', 'wpcomsh' ); ?> <input  class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'gravatar_url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'gravatar_url' ) ); ?>" type="text" value="<?php echo esc_attr( $gravatar_url ); ?>" /></label></p>
199<p><label for="<?php echo esc_attr( $this->get_field_id( 'gravatar_text' ) ); ?>"><?php esc_html_e( 'Text displayed after Gravatar. This is optional and can be used to describe yourself or what your blog is about.', 'wpcomsh' ); ?><br/> <textarea class="widefat" style="font-size: 0.9em" id="<?php echo esc_attr( $this->get_field_id( 'gravatar_text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'gravatar_text' ) ); ?>" rows="5"><?php echo htmlspecialchars( $gravatar_text, ENT_COMPAT ); /* // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ ?></textarea></label></p>
200<p>
201        <?php
202        printf(
203            wp_kses(
204                // translators: %s is a link to the WordPress user profile.
205                __( 'You can modify your Gravatar from your <a href="%s">profile page</a>.', 'wpcomsh' ),
206                array(
207                    'a' => array( 'href' => array() ),
208                )
209            ),
210            esc_url( admin_url( 'profile.php' ) )
211        );
212        ?>
213    </p>
214        <?php
215    }
216
217    /**
218     * Save widget data
219     *
220     * @param array $new_instance New widget settings.
221     * @param array $old_instance Old widget settings.
222     *
223     * @return array
224     **/
225    public function update( $new_instance, $old_instance ) {
226        $instance     = $old_instance;
227        $new_instance = wp_parse_args(
228            (array) $new_instance,
229            array(
230                'title'          => '',
231                'gravatar_size'  => 128,
232                'gravatar_align' => 'none',
233                'gravatar_text'  => '',
234                'email'          => '',
235                'email_user'     => -1,
236            )
237        );
238
239        $instance['title']          = wp_filter_nohtml_kses( $new_instance['title'] );
240        $instance['gravatar_size']  = intval( $new_instance['gravatar_size'] );
241        $instance['gravatar_text']  = wp_filter_post_kses( $new_instance['gravatar_text'] );
242        $instance['email']          = wp_filter_nohtml_kses( $new_instance['email'] );
243        $instance['email_user']     = intval( $new_instance['email_user'] );
244        $instance['gravatar_url']   = esc_url_raw( $new_instance['gravatar_url'] );
245        $instance['gravatar_align'] = $new_instance['gravatar_align'];
246
247        if ( $instance['email_user'] > 0 ) {
248            $user = get_userdata( $instance['email_user'] );
249
250            $instance['email'] = $user->user_email;
251        }
252
253        if ( ! in_array( $instance['gravatar_size'], array( 64, 96, 128, 256 ), true ) ) {
254            $instance['gravatar_size'] = 96;
255        }
256
257        if ( ! in_array( $instance['gravatar_align'], array( 'none', 'left', 'right', 'center' ), true ) ) {
258            $instance['gravatar_align'] = 'none';
259        }
260
261        return $instance;
262    }
263}
264
265/**
266 * Register the widget for use in Appearance -> Widgets
267 */
268function jetpack_gravatar_widget_init() { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
269    register_widget( 'Gravatar_Widget' );
270}
271add_action( 'widgets_init', 'jetpack_gravatar_widget_init' );