Make WordPress Core

Ticket #24856: 24856.3.diff

File 24856.3.diff, 4.6 KB (added by obenland, 12 years ago)
  • wp-includes/default-widgets.php

     
    11631163}
    11641164
    11651165/**
     1166 * Author Widget class
     1167 *
     1168 * @since 3.8
     1169 */
     1170class WP_Author_Widget extends WP_Widget {
     1171        public function __construct() {
     1172                parent::__construct( 'authors', __( 'Authors' ), array(
     1173                                'classname'   => 'author_widget',
     1174                                'description' => __( "Display blog's authors with avatars and archive links." ),
     1175                        ),
     1176                        array(
     1177                                'width' => 300,
     1178                        )
     1179                );
     1180
     1181                add_action( 'save_post',      array( $this, 'flush_widget_cache' ) );
     1182                add_action( 'deleted_post',   array( $this, 'flush_widget_cache' ) );
     1183                add_action( 'switch_theme',   array( $this, 'flush_widget_cache' ) );
     1184                add_action( 'profile_update', array( $this, 'flush_widget_cache' ) );
     1185
     1186                if ( is_active_widget( false, false, $this->id_base ) ) {
     1187                        add_action( 'wp_head', array( $this, 'author_style' ) );
     1188                }
     1189        }
     1190
     1191        public function widget( $args, $instance ) {
     1192                $cache = get_transient( 'author_widget_' . $this->id );
     1193
     1194                if ( ! empty( $cache ) ) {
     1195                        echo $cache;
     1196                        return;
     1197                }
     1198
     1199                $authors = get_users( array(
     1200                        'fields' => array( 'ID', 'user_nicename', 'display_name' ),
     1201                        'who'    => 'authors',
     1202                ) );
     1203
     1204                $widget = $args['before_widget'];
     1205                if ( ! empty( $instance['title'] ) ) {
     1206                        $widget .= $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
     1207                }
     1208                $widget .= '<ul>';
     1209
     1210                foreach ( $authors as $author ) {
     1211                        if ( ! $instance['all'] && ! count_user_posts( $author->ID ) ) {
     1212                                continue;
     1213                        }
     1214
     1215                        $avatar  = ( $instance['avatar_size'] > 0 ) ? get_avatar( $author->ID, $instance['avatar_size'] ) : '';
     1216                        $widget .= sprintf(
     1217                                '<li><a href="%1$s">%2$s<strong>%3$s</strong></a></li>',
     1218                                esc_url( get_author_posts_url( $author->ID, $author->user_nicename ) ),
     1219                                $avatar,
     1220                                esc_html( $author->display_name )
     1221                        );
     1222                }
     1223
     1224                $widget .= '</ul>';
     1225                $widget .= $args['after_widget'];
     1226
     1227                echo $widget;
     1228
     1229                set_transient( 'author_widget_' . $this->id, $widget, DAY_IN_SECONDS );
     1230        }
     1231
     1232        public function flush_widget_cache() {
     1233                delete_transient( 'author_widget_' . $this->id );
     1234        }
     1235
     1236        public function update( $new_instance, $old_instance ) {
     1237                $new_instance['title']       = strip_tags( $new_instance['title'] );
     1238                $new_instance['all']         = isset( $new_instance['all'] );
     1239                $new_instance['avatar_size'] = (int) $new_instance['avatar_size'];
     1240
     1241                $this->flush_widget_cache();
     1242
     1243                return $new_instance;
     1244        }
     1245
     1246        public function form( $instance ) {
     1247                $instance = wp_parse_args( $instance, array(
     1248                        'title'       => '',
     1249                        'all'         => false,
     1250                        'avatar_size' => 48,
     1251                ) );
     1252
     1253                ?>
     1254                <p>
     1255                        <label>
     1256                                <?php _e( 'Title:' ); ?>
     1257                                <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
     1258                        </label>
     1259                </p>
     1260                <p>
     1261                        <label>
     1262                                <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" />
     1263                                <?php _e( 'Include authors who have not written any posts' ); ?>
     1264                        </label>
     1265                </p>
     1266                <p>
     1267                        <label>
     1268                                <?php _e( 'Avatar Size (px):' ); ?>
     1269                                <select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>">
     1270                                        <?php foreach( array( '0' => __( 'No Avatars' ), '16' => '16x16', '32' => '32x32', '48' => '48x48', '96' => '96x96', '128' => '128x128' ) as $value => $label ) : ?>
     1271                                                <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option>
     1272                                        <?php endforeach; ?>
     1273                                </select>
     1274                        </label>
     1275                </p>
     1276                <?php
     1277        }
     1278
     1279        public function author_style() {
     1280                if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
     1281                        || ! apply_filters( 'show_author_widget_style', true, $this->id_base ) )
     1282                        return;
     1283                ?>
     1284                <style type="text/css">
     1285                        .author_widget .avatar {
     1286                                vertical-align: middle !important;
     1287                        }
     1288                        .author_widget .avatar-16 {
     1289                                margin-right: 5px;
     1290                        }
     1291                        .author_widget .avatar + strong {
     1292                                display: block;
     1293                                margin: 5px 0;
     1294                        }
     1295                        .author_widget .avatar-16 + strong {
     1296                                display: inline;
     1297                                margin: 0;
     1298                        }
     1299                </style>
     1300                <?php
     1301        }
     1302}
     1303
     1304/**
    11661305 * Register all of the default WordPress widgets on startup.
    11671306 *
    11681307 * Calls 'widgets_init' action after all of the WordPress widgets have been
     
    12011340
    12021341        register_widget('WP_Nav_Menu_Widget');
    12031342
     1343        register_widget('WP_Author_Widget');
     1344
    12041345        do_action('widgets_init');
    12051346}
    12061347