| | 1165 | /* |
| | 1166 | * Author Widget class |
| | 1167 | * |
| | 1168 | * @since 3.8 |
| | 1169 | */ |
| | 1170 | class WP_Author_Widget extends WP_Widget { |
| | 1171 | function __construct() { |
| | 1172 | parent::__construct( 'authors', __( 'Authors' ), array( |
| | 1173 | 'classname' => 'author_widget', |
| | 1174 | 'description' => __( 'Display blogs authors with avatars and archive links.' ), |
| | 1175 | ), |
| | 1176 | array( |
| | 1177 | 'width' => 300 |
| | 1178 | ) |
| | 1179 | ); |
| | 1180 | add_action( 'save_post', array( &$this, 'flush_widget_cache' ) ); |
| | 1181 | add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) ); |
| | 1182 | add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) ); |
| | 1183 | } |
| | 1184 | |
| | 1185 | function widget( $args, $instance ) { |
| | 1186 | $cache = wp_cache_get( 'author_widget', 'widget' ); |
| | 1187 | |
| | 1188 | if ( !is_array( $cache ) ) |
| | 1189 | $cache = array(); |
| | 1190 | |
| | 1191 | if ( ! isset( $args['widget_id'] ) ) |
| | 1192 | $args['widget_id'] = null; |
| | 1193 | |
| | 1194 | if ( isset( $cache[ $args['widget_id'] ] ) ) { |
| | 1195 | echo $cache[ $args['widget_id'] ]; |
| | 1196 | return; |
| | 1197 | } |
| | 1198 | |
| | 1199 | $instance = wp_parse_args( $instance, array( |
| | 1200 | 'title' => __( 'Authors' ), |
| | 1201 | 'all' => false, |
| | 1202 | 'avatar_size' => 48, |
| | 1203 | ) ); |
| | 1204 | |
| | 1205 | $user_args = array( |
| | 1206 | 'fields' => 'all', |
| | 1207 | 'who' => 'authors', |
| | 1208 | ); |
| | 1209 | |
| | 1210 | $user_args = apply_filters( 'author_widget_user_args', $user_args ); |
| | 1211 | $authors = get_users( $user_args ); |
| | 1212 | |
| | 1213 | ob_start(); |
| | 1214 | |
| | 1215 | echo $args['before_widget']; |
| | 1216 | echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title']; |
| | 1217 | echo '<ul>'; |
| | 1218 | |
| | 1219 | foreach ( $authors as $author ) : |
| | 1220 | if ( ! $instance['all'] && ! count_user_posts( $author->ID ) ) |
| | 1221 | continue; |
| | 1222 | ?> |
| | 1223 | |
| | 1224 | <li> |
| | 1225 | <a href="<?php echo esc_url( get_author_posts_url( $author->ID, $author->user_nicename ) ); ?>"> |
| | 1226 | <?php if ( $instance['avatar_size'] > 0 ) echo get_avatar( $author->ID, $instance['avatar_size'] ); ?> |
| | 1227 | <strong><?php echo esc_html( $author->display_name ); ?></strong> |
| | 1228 | </a> |
| | 1229 | </li> |
| | 1230 | |
| | 1231 | <?php |
| | 1232 | endforeach; |
| | 1233 | |
| | 1234 | echo '</ul>'; |
| | 1235 | echo $args['after_widget']; |
| | 1236 | $cache[ $args['widget_id'] ] = ob_get_flush(); |
| | 1237 | wp_cache_set( 'author_widget', $cache, 'widget' ); |
| | 1238 | } |
| | 1239 | |
| | 1240 | public function flush_widget_cache() { |
| | 1241 | wp_cache_delete( 'author_widget', 'widget' ); |
| | 1242 | } |
| | 1243 | |
| | 1244 | public function update( $new_instance, $old_instance ) { |
| | 1245 | $new_instance['title'] = strip_tags( $new_instance['title'] ); |
| | 1246 | $new_instance['all'] = isset( $new_instance['all'] ); |
| | 1247 | $new_instance['avatar_size'] = (int) $new_instance['avatar_size']; |
| | 1248 | |
| | 1249 | return $new_instance; |
| | 1250 | } |
| | 1251 | |
| | 1252 | public function form( $instance ) { |
| | 1253 | $instance = wp_parse_args( $instance, array( |
| | 1254 | 'title' => '', |
| | 1255 | 'all' => false, |
| | 1256 | 'avatar_size' => 48, |
| | 1257 | ) ); |
| | 1258 | |
| | 1259 | ?> |
| | 1260 | <p> |
| | 1261 | <label> |
| | 1262 | <?php _e( 'Title:' ); ?> |
| | 1263 | <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
| | 1264 | </label> |
| | 1265 | </p> |
| | 1266 | <p> |
| | 1267 | <label> |
| | 1268 | <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" /> |
| | 1269 | <?php _e( 'Include authors who have not written any posts' ); ?> |
| | 1270 | </label> |
| | 1271 | </p> |
| | 1272 | <p> |
| | 1273 | <label> |
| | 1274 | <?php _e( 'Avatar Size (px):' ); ?> |
| | 1275 | <select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>"> |
| | 1276 | <?php foreach( array( '0' => __( 'No Avatars' ), '16' => '16x16', '32' => '32x32', '48' => '48x48', '96' => '96x96', '128' => '128x128' ) as $value => $label ) : ?> |
| | 1277 | <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option> |
| | 1278 | <?php endforeach; ?> |
| | 1279 | </select> |
| | 1280 | </label> |
| | 1281 | </p> |
| | 1282 | <?php |
| | 1283 | } |
| | 1284 | } |
| | 1285 | |