Make WordPress Core

Ticket #24856: 24856.5.diff

File 24856.5.diff, 4.4 KB (added by wonderboymusic, 10 years ago)
  • src/wp-includes/widgets/class-wp-author-widget.php

     
     1<?php
     2
     3/**
     4 * Author Widget class
     5 *
     6 * @since 4.4.0
     7 */
     8class WP_Author_Widget extends WP_Widget {
     9        public function __construct() {
     10                parent::__construct( 'authors', __( 'Authors' ), array(
     11                                'classname'   => 'author_widget',
     12                                'description' => __( "Display blog's authors with avatars and archive links." ),
     13                        ),
     14                        array(
     15                                'width' => 300,
     16                        )
     17                );
     18
     19                add_action( 'save_post',      array( $this, 'flush_widget_cache' ) );
     20                add_action( 'deleted_post',   array( $this, 'flush_widget_cache' ) );
     21                add_action( 'switch_theme',   array( $this, 'flush_widget_cache' ) );
     22                add_action( 'profile_update', array( $this, 'flush_widget_cache' ) );
     23
     24                if ( is_active_widget( false, false, $this->id_base ) ) {
     25                        add_action( 'wp_head', array( $this, 'author_style' ) );
     26                }
     27        }
     28
     29        public function widget( $args, $instance ) {
     30                $cache = get_transient( 'author_widget_' . $this->id );
     31
     32                if ( ! empty( $cache ) ) {
     33                        echo $cache;
     34                        return;
     35                }
     36
     37                $authors = get_users( array(
     38                        'fields' => array( 'ID', 'user_nicename', 'display_name' ),
     39                        'who'    => 'authors',
     40                ) );
     41
     42                $widget = $args['before_widget'];
     43                if ( ! empty( $instance['title'] ) ) {
     44                        $widget .= $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
     45                }
     46                $widget .= '<ul>';
     47
     48                foreach ( $authors as $author ) {
     49                        if ( ! $instance['all'] && ! count_user_posts( $author->ID ) ) {
     50                                continue;
     51                        }
     52
     53                        $avatar  = ( $instance['avatar_size'] > 0 ) ? get_avatar( $author->ID, $instance['avatar_size'] ) : '';
     54                        $widget .= sprintf(
     55                                '<li><a href="%1$s">%2$s<strong>%3$s</strong></a></li>',
     56                                esc_url( get_author_posts_url( $author->ID, $author->user_nicename ) ),
     57                                $avatar,
     58                                esc_html( $author->display_name )
     59                        );
     60                }
     61
     62                $widget .= '</ul>';
     63                $widget .= $args['after_widget'];
     64
     65                echo $widget;
     66
     67                set_transient( 'author_widget_' . $this->id, $widget, DAY_IN_SECONDS );
     68        }
     69
     70        public function flush_widget_cache() {
     71                delete_transient( 'author_widget_' . $this->id );
     72        }
     73
     74        public function update( $new_instance, $old_instance ) {
     75                $new_instance['title']       = strip_tags( $new_instance['title'] );
     76                $new_instance['all']         = isset( $new_instance['all'] );
     77                $new_instance['avatar_size'] = (int) $new_instance['avatar_size'];
     78
     79                $this->flush_widget_cache();
     80
     81                return $new_instance;
     82        }
     83
     84        public function form( $instance ) {
     85                $instance = wp_parse_args( $instance, array(
     86                        'title'       => '',
     87                        'all'         => false,
     88                        'avatar_size' => 48,
     89                ) );
     90
     91                ?>
     92                <p>
     93                        <label>
     94                                <?php _e( 'Title:' ); ?>
     95                                <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
     96                        </label>
     97                </p>
     98                <p>
     99                        <label>
     100                                <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" />
     101                                <?php _e( 'Include authors who have not written any posts' ); ?>
     102                        </label>
     103                </p>
     104                <p>
     105                        <label>
     106                                <?php _e( 'Avatar Size (px):' ); ?>
     107                                <select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>">
     108                                        <?php foreach ( array( '0' => __( 'No Avatars' ), '16' => '16x16', '32' => '32x32', '48' => '48x48', '96' => '96x96', '128' => '128x128' ) as $value => $label ) : ?>
     109                                                <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option>
     110                                        <?php endforeach; ?>
     111                                </select>
     112                        </label>
     113                </p>
     114                <?php
     115        }
     116
     117        public function author_style() {
     118                if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
     119                        || ! apply_filters( 'show_author_widget_style', true, $this->id_base ) ) {
     120                        return;
     121                }
     122                ?>
     123                <style type="text/css">
     124                        .author_widget .avatar {
     125                                vertical-align: middle !important;
     126                        }
     127                        .author_widget .avatar-16 {
     128                                margin-right: 5px;
     129                        }
     130                        .author_widget .avatar + strong {
     131                                display: block;
     132                                margin: 5px 0;
     133                        }
     134                        .author_widget .avatar-16 + strong {
     135                                display: inline;
     136                                margin: 0;
     137                        }
     138                </style>
     139                <?php
     140        }
     141}
     142 No newline at end of file