Jetpack_Widget_Authors

Widget to display blog authors with avatars and recent posts.

Defined (1)

The class is defined in the following location(s).

/modules/widgets/authors.php  
  1. class Jetpack_Widget_Authors extends WP_Widget { 
  2. public function __construct() { 
  3. parent::__construct( 
  4. 'authors',  
  5. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  6. apply_filters( 'jetpack_widget_name', __( 'Authors', 'jetpack' ) ),  
  7. array( 
  8. 'classname' => 'widget_authors',  
  9. 'description' => __( 'Display blogs authors with avatars and recent posts.', 'jetpack' ),  
  10. 'customize_selective_refresh' => true,  
  11. ); 
  12.  
  13. if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) { 
  14. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); 
  15.  
  16. add_action( 'publish_post', array( __CLASS__, 'flush_cache' ) ); 
  17. add_action( 'deleted_post', array( __CLASS__, 'flush_cache' ) ); 
  18. add_action( 'switch_theme', array( __CLASS__, 'flush_cache' ) ); 
  19.  
  20. /** 
  21. * Enqueue stylesheet to adapt the widget to various themes. 
  22. * @since 4.5.0 
  23. */ 
  24. function enqueue_style() { 
  25. wp_register_style( 'jetpack-authors-widget', plugins_url( 'authors/style.css', __FILE__ ), array(), '20161228' ); 
  26. wp_enqueue_style( 'jetpack-authors-widget' ); 
  27.  
  28. public static function flush_cache() { 
  29. wp_cache_delete( 'widget_authors', 'widget' ); 
  30. wp_cache_delete( 'widget_authors_ssl', 'widget' ); 
  31.  
  32. public function widget( $args, $instance ) { 
  33. $cache_bucket = is_ssl() ? 'widget_authors_ssl' : 'widget_authors'; 
  34.  
  35. if ( '%BEG_OF_TITLE%' != $args['before_title'] ) { 
  36. if ( $output = wp_cache_get( $cache_bucket, 'widget') ) { 
  37. echo $output; 
  38. return; 
  39.  
  40. ob_start(); 
  41.  
  42. $instance = wp_parse_args( $instance, array( 'title' => __( 'Authors', 'jetpack' ), 'all' => false, 'number' => 5, 'avatar_size' => 48 ) ); 
  43. $instance['number'] = min( 10, max( 0, (int) $instance['number'] ) ); 
  44.  
  45. // We need to query at least one post to determine whether an author has written any posts or not 
  46. $query_number = max( $instance['number'], 1 ); 
  47.  
  48. $default_excluded_authors = array(); 
  49. /** 
  50. * Filter authors from the Widget Authors widget. 
  51. * @module widgets 
  52. * @since 4.5.0 
  53. * @param array $default_excluded_authors Array of user ID's that will be excluded 
  54. */ 
  55. $excluded_authors = apply_filters( 'jetpack_widget_authors_exclude', $default_excluded_authors ); 
  56.  
  57. $authors = get_users( array( 
  58. 'fields' => 'all',  
  59. 'who' => 'authors',  
  60. 'exclude' => (array) $excluded_authors,  
  61. ) ); 
  62.  
  63. echo $args['before_widget']; 
  64. /** This filter is documented in core/src/wp-includes/default-widgets.php */ 
  65. $title = apply_filters( 'widget_title', $instance['title'] ); 
  66. echo $args['before_title'] . esc_html( $title ) . $args['after_title']; 
  67. echo '<ul>'; 
  68.  
  69. $default_post_type = 'post'; 
  70. /** 
  71. * Filter types of posts that will be counted in the widget 
  72. * @module widgets 
  73. * @since 4.5.0 
  74. * @param string|array $default_post_type type(s) of posts to count for the widget. 
  75. */ 
  76. $post_types = apply_filters( 'jetpack_widget_authors_post_types', $default_post_type ); 
  77.  
  78. foreach ( $authors as $author ) { 
  79. $r = new WP_Query( array( 
  80. 'author' => $author->ID,  
  81. 'posts_per_page' => $query_number,  
  82. 'post_type' => $post_types,  
  83. 'post_status' => 'publish',  
  84. 'no_found_rows' => true,  
  85. 'has_password' => false,  
  86. ) ); 
  87.  
  88. if ( ! $r->have_posts() && ! $instance['all'] ) { 
  89. continue; 
  90.  
  91. echo '<li>'; 
  92.  
  93. // Display avatar and author name 
  94. if ( $r->have_posts() ) { 
  95. echo '<a href="' . get_author_posts_url( $author->ID ) . '">'; 
  96.  
  97. if ( $instance['avatar_size'] > 1 ) { 
  98. echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; 
  99.  
  100. echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; 
  101. echo '</a>'; 
  102. else if ( $instance['all'] ) { 
  103. if ( $instance['avatar_size'] > 1 ) { 
  104. echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; 
  105.  
  106. echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; 
  107.  
  108. if ( 0 == $instance['number'] ) { 
  109. echo '</li>'; 
  110. continue; 
  111.  
  112. // Display a short list of recent posts for this author 
  113.  
  114. if ( $r->have_posts() ) { 
  115. echo '<ul>'; 
  116.  
  117. while ( $r->have_posts() ) { 
  118. $r->the_post(); 
  119. echo '<li><a href="' . get_permalink() . '">'; 
  120.  
  121. if ( get_the_title() ) { 
  122. echo get_the_title(); 
  123. } else { 
  124. echo get_the_ID(); 
  125.  
  126. echo '</a></li>'; 
  127.  
  128. echo '</ul>'; 
  129.  
  130. echo '</li>'; 
  131.  
  132. echo '</ul>'; 
  133. echo $args['after_widget']; 
  134.  
  135. wp_reset_postdata(); 
  136.  
  137. if ( '%BEG_OF_TITLE%' != $args['before_title'] ) { 
  138. wp_cache_add( $cache_bucket, ob_get_flush(), 'widget' ); 
  139.  
  140. /** This action is documented in modules/widgets/gravatar-profile.php */ 
  141. do_action( 'jetpack_stats_extra', 'widget_view', 'authors' ); 
  142.  
  143. public function form( $instance ) { 
  144. $instance = wp_parse_args( $instance, array( 'title' => '', 'all' => false, 'avatar_size' => 48, 'number' => 5 ) ); 
  145.  
  146. ?> 
  147. <p> 
  148. <label> 
  149. <?php _e( 'Title:', 'jetpack' ); ?> 
  150. <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> 
  151. </label> 
  152. </p> 
  153. <p> 
  154. <label> 
  155. <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" /> 
  156. <?php _e( 'Display all authors (including those who have not written any posts)', 'jetpack' ); ?> 
  157. </label> 
  158. </p> 
  159. <p> 
  160. <label> 
  161. <?php _e( 'Number of posts to show for each author:', 'jetpack' ); ?> 
  162. <input style="width: 50px; text-align: center;" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" /> 
  163. <?php _e( '(at most 10)', 'jetpack' ); ?> 
  164. </label> 
  165. </p> 
  166. <p> 
  167. <label> 
  168. <?php _e( 'Avatar Size (px):', 'jetpack' ); ?> 
  169. <select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>"> 
  170. <?php foreach( array( '1' => __( 'No Avatars', 'jetpack' ), '16' => '16x16', '32' => '32x32', '48' => '48x48', '96' => '96x96', '128' => '128x128' ) as $value => $label ) { ?> 
  171. <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option> 
  172. <?php } ?> 
  173. </select> 
  174. </label> 
  175. </p> 
  176. <?php 
  177.  
  178. /** 
  179. * Updates the widget on save and flushes cache. 
  180. * @param array $new_instance 
  181. * @param array $old_instance 
  182. * @return array 
  183. */ 
  184. public function update( $new_instance, $old_instance ) { 
  185. $new_instance['title'] = strip_tags( $new_instance['title'] ); 
  186. $new_instance['all'] = isset( $new_instance['all'] ); 
  187. $new_instance['number'] = (int) $new_instance['number']; 
  188. $new_instance['avatar_size'] = (int) $new_instance['avatar_size']; 
  189.  
  190. Jetpack_Widget_Authors::flush_cache(); 
  191.  
  192. return $new_instance;