authors.php 7.2 KB

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