'authors', 'description' => _a("A list of authors from your blog")); $control_ops = array('width' => 500); $this->WP_Widget('atom-authors', _a('Authors'), $widget_ops, $control_ops); // flush cache when posts are changed add_action('save_post', array(&$this, 'flush_cache')); add_action('deleted_post', array(&$this, 'flush_cache')); add_action('switch_theme', array(&$this, 'flush_cache')); } function sort_by_display_name($a, $b){ return ($a->display_name > $b->display_name); } function sort_by_post_count($a, $b){ return ($a->post_count < $b->post_count); } function get_authors($sort_by = '') { $users = get_users_of_blog(); $author_ids = array(); foreach($users as $user) $author_ids[] = $user->user_id; $author_post_count = count_many_users_posts($author_ids); foreach($users as $user) $user->post_count = $author_post_count[$user->user_id]; // sort usort($users, array(&$this, "sort_by_$sort_by")); return $users; } function flush_cache(){ wp_cache_delete('widget_authors', 'widget'); } function template($mode = 'full'){ switch($mode): case 'full': return "\n" ." {AVATAR}\n" ." \n" ." {NAME}\n" ." {POST_COUNT}\n" ." \n" ."\n"; break; case 'images': return "\n" ." {AVATAR}\n" ."\n"; break; case 'brief': return "\n" ." \n" ." {NAME}\n" ." \n" ."\n"; break; case 'detailed': return "\n" ." \n" ." {NAME}\n" ." {POST_COUNT}\n" ." \n" ."\n"; break; endswitch; } function widget($args, $instance){ $cache = wp_cache_get('widget_authors', 'widget'); if (!is_array($cache)) $cache = array(); if (isset($cache[$args['widget_id']])): echo $cache[$args['widget_id']]; return; endif; extract($args); $instance = wp_parse_args($instance, $this->defaults()); $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); $show_avatars = ($instance['mode'] != 'brief' && $instance['mode'] != 'detailed'); $details = ($instance['mode'] == 'detailed' || $instance['mode'] == 'full'); $authors = $this->get_authors($instance['sort_by']); if(empty($authors)) atom_add_debug_message("No authors found in {$args['widget_id']} ({$args['widget_name']}). Widget marked as inactive"); $output = $before_widget; if ($title) $output .= $before_title.$title.$after_title; $output .= "
'; $output .= $after_widget; echo $output; $cache[$args['widget_id']] = $output; wp_cache_set('widget_authors', $cache, 'widget'); } function update($new_instance, $old_instance){ $instance['title'] = esc_attr($new_instance['title']); $instance['mode'] = esc_attr($new_instance['mode']); $instance['hide_empty'] = (bool)$new_instance['hide_empty']; $instance['sort_by'] = esc_attr($new_instance['sort_by']); $instance['exclude_admin'] = (bool)$new_instance['exclude_admin']; $instance['avatar_size'] = intval($new_instance['avatar_size']); // html template if(isset($new_instance['template']) && current_user_can('edit_themes')) $instance['template'] = $new_instance['template']; $this->flush_cache(); return wp_parse_args($instance, $this->defaults()); } function defaults(){ return apply_filters('atom_widget_authors_defaults', array( 'title' => _a("Authors"), 'mode' => 'full', 'avatar_size' => 48, 'sort_by' => 'post_count', 'hide_empty' => true, 'exclude_admin' => false, 'template' => $this->template('full'), ), $this); } function form($instance){ $instance = wp_parse_args($instance, $this->defaults()); ?>