Make WordPress Core

Ticket #24856: 24856.4.diff

File 24856.4.diff, 6.0 KB (added by MikeHansenMe, 11 years ago)
  • src/wp-includes/default-widgets.php

     
    14301430}
    14311431
    14321432/**
     1433 * Author Widget class
     1434 *
     1435 * @since 4.3
     1436 */
     1437class WP_Author_Widget extends WP_Widget {
     1438        public function __construct() {
     1439                parent::__construct( 'authors', __( 'Authors' ), array(
     1440                                'classname'   => 'author_widget',
     1441                                'description' => __( "Display blog's authors with avatars and archive links." ),
     1442                        ),
     1443                        array(
     1444                                'width' => 300,
     1445                        )
     1446                );
     1447
     1448                add_action( 'save_post',      array( $this, 'flush_widget_cache' ) );
     1449                add_action( 'deleted_post',   array( $this, 'flush_widget_cache' ) );
     1450                add_action( 'switch_theme',   array( $this, 'flush_widget_cache' ) );
     1451                add_action( 'profile_update', array( $this, 'flush_widget_cache' ) );
     1452
     1453                if ( is_active_widget( false, false, $this->id_base ) ) {
     1454                        add_action( 'wp_head', array( $this, 'author_style' ) );
     1455                }
     1456        }
     1457
     1458        public function widget( $args, $instance ) {
     1459                $cache = get_transient( 'author_widget_' . $this->id );
     1460
     1461                if ( ! empty( $cache ) ) {
     1462                        echo $cache;
     1463                        return;
     1464                }
     1465
     1466                $authors = get_users( array(
     1467                        'fields' => array( 'ID', 'user_nicename', 'display_name' ),
     1468                        'who'    => 'authors',
     1469                ) );
     1470
     1471                $widget = $args['before_widget'];
     1472                if ( ! empty( $instance['title'] ) ) {
     1473                        $widget .= $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
     1474                }
     1475                $widget .= '<ul>';
     1476
     1477                foreach ( $authors as $author ) {
     1478                        if ( ! $instance['all'] && ! count_user_posts( $author->ID ) ) {
     1479                                continue;
     1480                        }
     1481
     1482                        $avatar  = ( $instance['avatar_size'] > 0 ) ? get_avatar( $author->ID, $instance['avatar_size'] ) : '';
     1483                        $widget .= sprintf(
     1484                                '<li><a href="%1$s">%2$s<strong>%3$s</strong></a></li>',
     1485                                esc_url( get_author_posts_url( $author->ID, $author->user_nicename ) ),
     1486                                $avatar,
     1487                                esc_html( $author->display_name )
     1488                        );
     1489                }
     1490
     1491                $widget .= '</ul>';
     1492                $widget .= $args['after_widget'];
     1493
     1494                echo $widget;
     1495
     1496                set_transient( 'author_widget_' . $this->id, $widget, DAY_IN_SECONDS );
     1497        }
     1498
     1499        public function flush_widget_cache() {
     1500                delete_transient( 'author_widget_' . $this->id );
     1501        }
     1502
     1503        public function update( $new_instance, $old_instance ) {
     1504                $new_instance['title']       = strip_tags( $new_instance['title'] );
     1505                $new_instance['all']         = isset( $new_instance['all'] );
     1506                $new_instance['avatar_size'] = (int) $new_instance['avatar_size'];
     1507
     1508                $this->flush_widget_cache();
     1509
     1510                return $new_instance;
     1511        }
     1512
     1513        public function form( $instance ) {
     1514                $instance = wp_parse_args( $instance, array(
     1515                        'title'       => '',
     1516                        'all'         => false,
     1517                        'avatar_size' => 48,
     1518                ) );
     1519
     1520                ?>
     1521                <p>
     1522                        <label>
     1523                                <?php _e( 'Title:' ); ?>
     1524                                <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
     1525                        </label>
     1526                </p>
     1527                <p>
     1528                        <label>
     1529                                <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" />
     1530                                <?php _e( 'Include authors who have not written any posts' ); ?>
     1531                        </label>
     1532                </p>
     1533                <p>
     1534                        <label>
     1535                                <?php _e( 'Avatar Size (px):' ); ?>
     1536                                <select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>">
     1537                                        <?php foreach ( array( '0' => __( 'No Avatars' ), '16' => '16x16', '32' => '32x32', '48' => '48x48', '96' => '96x96', '128' => '128x128' ) as $value => $label ) : ?>
     1538                                                <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option>
     1539                                        <?php endforeach; ?>
     1540                                </select>
     1541                        </label>
     1542                </p>
     1543                <?php
     1544        }
     1545
     1546        public function author_style() {
     1547                if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
     1548                        || ! apply_filters( 'show_author_widget_style', true, $this->id_base ) )
     1549                        return;
     1550                ?>
     1551                <style type="text/css">
     1552                        .author_widget .avatar {
     1553                                vertical-align: middle !important;
     1554                        }
     1555                        .author_widget .avatar-16 {
     1556                                margin-right: 5px;
     1557                        }
     1558                        .author_widget .avatar + strong {
     1559                                display: block;
     1560                                margin: 5px 0;
     1561                        }
     1562                        .author_widget .avatar-16 + strong {
     1563                                display: inline;
     1564                                margin: 0;
     1565                        }
     1566                </style>
     1567                <?php
     1568        }
     1569}
     1570
     1571/**
    14331572 * Register all of the default WordPress widgets on startup.
    14341573 *
    14351574 * Calls 'widgets_init' action after all of the WordPress widgets have been
     
    14381577 * @since 2.2.0
    14391578 */
    14401579function wp_widgets_init() {
    1441         if ( !is_blog_installed() )
     1580        if ( ! is_blog_installed() ) {
    14421581                return;
     1582        }
    14431583
    1444         register_widget('WP_Widget_Pages');
     1584        register_widget( 'WP_Widget_Pages' );
    14451585
    1446         register_widget('WP_Widget_Calendar');
     1586        register_widget( 'WP_Widget_Calendar' );
    14471587
    1448         register_widget('WP_Widget_Archives');
     1588        register_widget( 'WP_Widget_Archives' );
    14491589
    1450         if ( get_option( 'link_manager_enabled' ) )
    1451                 register_widget('WP_Widget_Links');
     1590        if ( get_option( 'link_manager_enabled' ) ) {
     1591                register_widget( 'WP_Widget_Links' );
     1592        }
    14521593
    1453         register_widget('WP_Widget_Meta');
     1594        register_widget( 'WP_Widget_Meta' );
    14541595
    1455         register_widget('WP_Widget_Search');
     1596        register_widget( 'WP_Widget_Search' );
    14561597
    1457         register_widget('WP_Widget_Text');
     1598        register_widget( 'WP_Widget_Text' );
    14581599
    1459         register_widget('WP_Widget_Categories');
     1600        register_widget( 'WP_Widget_Categories' );
    14601601
    1461         register_widget('WP_Widget_Recent_Posts');
     1602        register_widget( 'WP_Widget_Recent_Posts' );
    14621603
    1463         register_widget('WP_Widget_Recent_Comments');
     1604        register_widget( 'WP_Widget_Recent_Comments' );
    14641605
    1465         register_widget('WP_Widget_RSS');
     1606        register_widget( 'WP_Widget_RSS' );
    14661607
    1467         register_widget('WP_Widget_Tag_Cloud');
     1608        register_widget( 'WP_Widget_Tag_Cloud' );
    14681609
    1469         register_widget('WP_Nav_Menu_Widget');
     1610        register_widget( 'WP_Nav_Menu_Widget' );
    14701611
     1612        register_widget( 'WP_Author_Widget' );
     1613
    14711614        /**
    14721615         * Fires after all default WordPress widgets have been registered.
    14731616         *
     
    14761619        do_action( 'widgets_init' );
    14771620}
    14781621
    1479 add_action('init', 'wp_widgets_init', 1);
     1622add_action( 'init', 'wp_widgets_init', 1 );