| | 1433 | * Author Widget class |
| | 1434 | * |
| | 1435 | * @since 4.3 |
| | 1436 | */ |
| | 1437 | class 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 | /** |