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