Make WordPress Core

Ticket #24856: 24856.2.diff

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