Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 110
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Widget_Authors_Grid
0.00% covered (danger)
0.00%
0 / 101
0.00% covered (danger)
0.00%
0 / 7
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 flush_cache
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 widget
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
72
 form
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
6
 update
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 add_styles
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 form_styles
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2
3/**
4 * Widget to display a grid of author avatar images
5 * Default size is 32px
6 */
7class Widget_Authors_Grid extends WP_Widget {
8    /**
9     * Constructor.
10     */
11    public function __construct() {
12        parent::__construct(
13            'author_grid',
14            __( 'Author Grid', 'wpcomsh' ),
15            array(
16                'classname'   => 'widget_author_grid',
17                'description' => __( 'Show a grid of author avatar images.', 'wpcomsh' ),
18            )
19        );
20
21        add_action( 'publish_post', array( __CLASS__, 'flush_cache' ) );
22        add_action( 'deleted_post', array( __CLASS__, 'flush_cache' ) );
23        add_action( 'switch_theme', array( __CLASS__, 'flush_cache' ) );
24
25        add_action( 'customize_controls_print_styles', array( $this, 'form_styles' ) );
26
27        if ( ! is_admin() && is_active_widget( false, false, 'author_grid' ) ) {
28            add_action( 'wp_head', array( $this, 'add_styles' ) );
29        }
30    }
31
32    /**
33     * Flush cache.
34     */
35    public static function flush_cache() {
36        wp_cache_delete( 'widget_author_grid', 'widget' );
37        wp_cache_delete( 'widget_author_grid_ssl', 'widget' );
38    }
39
40    /**
41     * Display the widget.
42     *
43     * @param array $args     Widget arguments.
44     * @param array $instance Widget instance.
45     */
46    public function widget( $args, $instance ) {
47        $cache_bucket = is_ssl() ? 'widget_author_grid_ssl' : 'widget_author_grid';
48
49        if ( '%BEG_OF_TITLE%' !== $args['before_title'] ) {
50            $output = wp_cache_get( $cache_bucket, 'widget' );
51            if ( $output ) {
52                echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
53                return;
54            }
55
56            ob_start();
57        }
58
59        $instance = wp_parse_args(
60            $instance,
61            array(
62                'title'       => __( 'Authors', 'wpcomsh' ),
63                'all'         => false,
64                'avatar_size' => 32,
65            )
66        );
67
68        $authors = get_users(
69            array(
70                'fields' => 'all',
71                'who'    => 'authors',
72            )
73        );
74
75        echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
76        echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
77        echo '<ul>';
78
79        foreach ( $authors as $author ) {
80            // Unless we're displaying all authors, check to make sure the author has some posts.
81            if ( ! $instance['all'] ) {
82                $r = new WP_Query(
83                    array(
84                        'author'       => $author->ID,
85                        'showposts'    => 1,
86                        'what_to_show' => 'posts',
87                        'nopaging'     => 0,
88                        'post_status'  => 'publish',
89                    )
90                );
91
92                if ( ! $r->have_posts() ) {
93                    continue;
94                }
95            }
96
97            echo '<li>';
98            echo '<a href="' . esc_url( get_author_posts_url( $author->ID ) ) . '"> ';
99            echo get_avatar( $author->ID, $instance['avatar_size'], '', '', array( 'force_display' => true ) );
100            echo '</a>';
101            echo '</li>';
102        }
103
104        echo '</ul>';
105        echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
106
107        wp_reset_postdata();
108
109        if ( '%BEG_OF_TITLE%' !== $args['before_title'] ) {
110            wp_cache_add( $cache_bucket, ob_get_flush(), 'widget' );
111        }
112    }
113
114    /**
115     * Display the widget settings form.
116     *
117     * @param array $instance Current settings.
118     * @return never
119     */
120    public function form( $instance ) {
121        $instance = wp_parse_args(
122            $instance,
123            array(
124                'title'       => '',
125                'all'         => false,
126                'avatar_size' => 32,
127            )
128        );
129        ?>
130        <p>
131            <label>
132                <?php esc_html_e( 'Title:', 'wpcomsh' ); ?>
133                <input class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
134            </label>
135        </p>
136        <p>
137            <label class="widget_form-author_grid-checkbox-label">
138                <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo esc_attr( $this->get_field_name( 'all' ) ); ?>" /><?php esc_html_e( 'Display all authors (including those who have not written any posts)', 'wpcomsh' ); ?>
139            </label>
140        </p>
141        <p>
142            <label>
143                <?php esc_html_e( 'Avatar Size (px):', 'wpcomsh' ); ?>
144                <select name="<?php echo esc_attr( $this->get_field_name( 'avatar_size' ) ); ?>">
145                    <?php
146                    foreach ( array(
147                        '16'  => '16x16',
148                        '32'  => '32x32',
149                        '48'  => '48x48',
150                        '96'  => '96x96',
151                        '128' => '128x128',
152                    ) as $value => $label ) {
153                        ?>
154                        <option value="<?php echo esc_attr( (string) $value ); ?><?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option>
155                    <?php } ?>
156                </select>
157            </label>
158        </p>
159        <?php
160    }
161
162    /**
163     * Update the widget settings.
164     *
165     * @param array $new_instance New settings.
166     * @param array $old_instance Old settings.
167     */
168    public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
169        $new_instance['title']       = wp_strip_all_tags( $new_instance['title'] );
170        $new_instance['all']         = isset( $new_instance['all'] );
171        $new_instance['avatar_size'] = (int) $new_instance['avatar_size'];
172
173        self::flush_cache();
174
175        return $new_instance;
176    }
177
178    /**
179     * Enqueue widget styles.
180     */
181    public function add_styles() {
182        wp_enqueue_style( 'widget-author_grid', plugins_url( 'author-grid/author-grid.css', __FILE__ ), array(), WPCOMSH_VERSION );
183    }
184
185    /**
186     * Enqueue customizer styles.
187     */
188    public function form_styles() {
189        wp_enqueue_style( 'widget-author_grid_customizer', plugins_url( 'author-grid/author-grid-customizer.css', __FILE__ ), array(), WPCOMSH_VERSION );
190    }
191}
192
193add_action(
194    'widgets_init',
195    function () {
196        // Don't load this Widget for users who don't have preferences for it
197        $widgets = get_option( 'widget_author_grid' );
198        if ( ! is_array( $widgets ) ) {
199            return;
200        }
201        register_widget( 'Widget_Authors_Grid' );
202    }
203);