Bez popisu

authors.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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(
  60. $instance, array(
  61. 'title' => __( 'Authors', 'jetpack' ),
  62. 'all' => false,
  63. 'number' => 5,
  64. 'avatar_size' => 48,
  65. )
  66. );
  67. $instance['number'] = min( 10, max( 0, (int) $instance['number'] ) );
  68. // We need to query at least one post to determine whether an author has written any posts or not
  69. $query_number = max( $instance['number'], 1 );
  70. /**
  71. * Filter authors from the Widget Authors widget.
  72. *
  73. * @module widgets
  74. *
  75. * @deprecated 7.7.0 Use jetpack_widget_authors_params instead.
  76. *
  77. * @since 4.5.0
  78. *
  79. * @param array $default_excluded_authors Array of user ID's that will be excluded
  80. */
  81. $excluded_authors = apply_filters( 'jetpack_widget_authors_exclude', array() );
  82. /**
  83. * Filter the parameters of `get_users` call in the Widget Authors widget.
  84. *
  85. * See the following for `get_users` default arguments:
  86. * https://codex.wordpress.org/Function_Reference/get_users
  87. *
  88. * @module widgets
  89. *
  90. * @since 7.7.0
  91. *
  92. * @param array $get_author_params Array of params used in `get_user`
  93. */
  94. $get_author_params = apply_filters(
  95. 'jetpack_widget_authors_params',
  96. array(
  97. 'who' => 'authors',
  98. 'exclude' => (array) $excluded_authors,
  99. )
  100. );
  101. $authors = get_users( $get_author_params );
  102. echo $args['before_widget'];
  103. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  104. $title = apply_filters( 'widget_title', $instance['title'] );
  105. echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
  106. echo '<ul>';
  107. $default_post_type = 'post';
  108. /**
  109. * Filter types of posts that will be counted in the widget
  110. *
  111. * @module widgets
  112. *
  113. * @since 4.5.0
  114. *
  115. * @param string|array $default_post_type type(s) of posts to count for the widget.
  116. */
  117. $post_types = apply_filters( 'jetpack_widget_authors_post_types', $default_post_type );
  118. foreach ( $authors as $author ) {
  119. $r = new WP_Query(
  120. array(
  121. 'author' => $author->ID,
  122. 'posts_per_page' => $query_number,
  123. 'post_type' => $post_types,
  124. 'post_status' => 'publish',
  125. 'no_found_rows' => true,
  126. 'has_password' => false,
  127. )
  128. );
  129. if ( ! $r->have_posts() && ! $instance['all'] ) {
  130. continue;
  131. }
  132. echo '<li>';
  133. // Display avatar and author name
  134. if ( $r->have_posts() ) {
  135. echo '<a href="' . get_author_posts_url( $author->ID ) . '">';
  136. if ( $instance['avatar_size'] > 1 ) {
  137. echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
  138. }
  139. echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
  140. echo '</a>';
  141. } elseif ( $instance['all'] ) {
  142. if ( $instance['avatar_size'] > 1 ) {
  143. echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
  144. }
  145. echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
  146. }
  147. if ( 0 == $instance['number'] ) {
  148. echo '</li>';
  149. continue;
  150. }
  151. // Display a short list of recent posts for this author.
  152. if ( $r->have_posts() ) {
  153. echo '<ul>';
  154. while ( $r->have_posts() ) {
  155. $r->the_post();
  156. printf(
  157. '<li><a href="%1$s" title="%2$s"%3$s>%4$s</a></li>',
  158. esc_url( get_permalink() ),
  159. esc_attr( wp_kses( get_the_title(), array() ) ),
  160. ( get_queried_object_id() === get_the_ID() ? ' aria-current="page"' : '' ),
  161. esc_html( wp_kses( get_the_title(), array() ) )
  162. );
  163. }
  164. echo '</ul>';
  165. }
  166. echo '</li>';
  167. }
  168. echo '</ul>';
  169. echo $args['after_widget'];
  170. wp_reset_postdata();
  171. if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
  172. wp_cache_add( $cache_bucket, ob_get_flush(), 'widget' );
  173. }
  174. /** This action is documented in modules/widgets/gravatar-profile.php */
  175. do_action( 'jetpack_stats_extra', 'widget_view', 'authors' );
  176. }
  177. public function form( $instance ) {
  178. $instance = wp_parse_args(
  179. $instance, array(
  180. 'title' => '',
  181. 'all' => false,
  182. 'avatar_size' => 48,
  183. 'number' => 5,
  184. )
  185. );
  186. ?>
  187. <p>
  188. <label>
  189. <?php _e( 'Title:', 'jetpack' ); ?>
  190. <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
  191. </label>
  192. </p>
  193. <p>
  194. <label>
  195. <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" />
  196. <?php _e( 'Display all authors (including those who have not written any posts)', 'jetpack' ); ?>
  197. </label>
  198. </p>
  199. <p>
  200. <label>
  201. <?php _e( 'Number of posts to show for each author:', 'jetpack' ); ?>
  202. <input style="width: 50px; text-align: center;" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" />
  203. <?php _e( '(at most 10)', 'jetpack' ); ?>
  204. </label>
  205. </p>
  206. <p>
  207. <label>
  208. <?php _e( 'Avatar Size (px):', 'jetpack' ); ?>
  209. <select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>">
  210. <?php
  211. foreach ( array(
  212. '1' => __( 'No Avatars', 'jetpack' ),
  213. '16' => '16x16',
  214. '32' => '32x32',
  215. '48' => '48x48',
  216. '96' => '96x96',
  217. '128' => '128x128',
  218. ) as $value => $label ) {
  219. ?>
  220. <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option>
  221. <?php } ?>
  222. </select>
  223. </label>
  224. </p>
  225. <?php
  226. }
  227. /**
  228. * Updates the widget on save and flushes cache.
  229. *
  230. * @param array $new_instance
  231. * @param array $old_instance
  232. * @return array
  233. */
  234. public function update( $new_instance, $old_instance ) {
  235. $new_instance['title'] = strip_tags( $new_instance['title'] );
  236. $new_instance['all'] = isset( $new_instance['all'] );
  237. $new_instance['number'] = (int) $new_instance['number'];
  238. $new_instance['avatar_size'] = (int) $new_instance['avatar_size'];
  239. Jetpack_Widget_Authors::flush_cache();
  240. return $new_instance;
  241. }
  242. }
  243. add_action( 'widgets_init', 'jetpack_register_widget_authors' );
  244. function jetpack_register_widget_authors() {
  245. register_widget( 'Jetpack_Widget_Authors' );
  246. };