'blogs', 'description' => _a('A list of site blogs')); $control_ops = array('width' => 500); $this->WP_Widget('atom-blogs', _a('Network Blogs'), $widget_ops, $control_ops); atom_add('ajax_requests', array(&$this, 'ajax')); // include in jQuery(document).ready() atom_add('jquery_init', array(&$this, 'js')); // flush cache when blog is updated add_action('wpmu_blog_updated', array(&$this, 'flush_cache')); } function flush_cache(){ wp_cache_delete('widget_blogs', 'widget'); } function js(){ // we need to process all instances because this function gets to run only once $widget_settings = atom_get_options($this->option_name); foreach((array)$widget_settings as $instance => $options): // identify instance $id = "{$this->id_base}-{$instance}"; $block_id = "instance-{$id}"; if (isset($options['more']) && $options['more'] && is_active_widget(false, $id, $this->id_base)): ?> $("#").delegate("a.more", "click", function(){ event.preventDefault(); $.ajax({ type: "GET", url: "", data: { instance: , offset: parseInt($(this).attr('rel')), _ajax_nonce: "", atom: "get_blogs" }, dataType: 'json', context: this, beforeSend: function() { $(this).addClass("loading"); }, complete: function() { $(this).removeClass("loading"); }, success: function(response){ var link = $(this); // append to list & update tab container height (if we're inside a tabbed widget) if(response.output != '') $(response.output).appendTo("# ul").hide().each(function(i){ $(this).delay(333*i).animate( { "opacity": "show", "height": "show", "marginTop": "show", "marginBottom": "show", "paddingTop": "show", "paddingBottom": "show" }, { duration: 333, step: function(now, fx){ link.parents('li.block .sections').height((link.parents('#').height()) + 5); } }); }); link.attr('rel', response.offset); // no more data? if(!response.more) link.hide(); } }); }); option_name); $instance = intval($_GET['instance']); $settings = $settings[$instance]; $offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0; $output = $this->get_blogs($settings, $next, $offset); $offset = $offset + $settings['number']; echo json_encode(array('output' => $output, 'more' => $next, 'offset' => $offset)); exit(); endif; } function template(){ return "\n" ." {AVATAR}\n" ." \n" ." {TITLE}\n" ." {DESCRIPTION}\n" ." Updated {UPDATED}\n" ." \n" ."\n"; } function get_blogs($args, &$more, $offset = 0){ extract($args); $sites = atom_get_sites(array( 'exclude_id' => $exclude, 'exclude_mature' => $exclude_mature, 'sort_column' => $order_by, 'order' => ($order_by == 'id') ? 'ASC' : 'DESC', 'limit_results' => $number + 1, 'start' => $offset )); // because mysql rand() is slowwwww if($order_by == 'rand') shuffle($sites); $more = count($sites) > $number ? true : false; $count = 1; $output = ''; foreach((array)$sites as $site): if($count++ == $number+1) break; $output .= ''; // output template $output .= atom_render_template($args['template'], array( 'TITLE' => get_blog_option($site['blog_id'], 'blogname'), 'URL' => get_blogaddress_by_domain($site['domain'], $site['path']), // should be faster than get_blog_option($site['blog_id'], 'siteurl'), 'AVATAR' => atom_get_avatar($site['email'], $avatar_size, ''), 'POST_COUNT' => get_blog_option($site['blog_id'], 'post_count'), 'DESCRIPTION' => convert_smilies(get_blog_option($site['blog_id'], 'blogdescription')), 'UPDATED' => atom_time_since(abs(strtotime($site['last_updated']))), 'REGISTERED' => atom_time_since(abs(strtotime($site['registered']))), 'EMAIL' => esc_url($site['email']), // should not be used 'ID' => $site['blog_id'], )); $output .= ''; endforeach; return $output; } function widget($args, $instance){ $cache = wp_cache_get('widget_blogs', 'widget'); if (!is_array($cache)) $cache = array(); if (isset($cache[$args['widget_id']])): echo $cache[$args['widget_id']]; return; endif; extract($args, EXTR_SKIP); $instance = wp_parse_args($instance, $this->defaults()); $blogs = $this->get_blogs($instance, $next); if(!$blogs) return atom_add_debug_message("No blogs found in {$args['widget_id']} ({$args['widget_name']}). Widget marked as inactive"); $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); $output = $before_widget; if($title) $output .= $before_title.$title.$after_title; $output .= ""; if($instance['more'] && $next && atom_get_options('jquery')) $output .= ""._a("Show More").""; $output .= $after_widget; echo $output; $cache[$args['widget_id']] = $output; wp_cache_set('widget_blogs', $cache, 'widget'); } function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = esc_attr($new_instance['title']); $instance['order_by'] = esc_attr($new_instance['order_by']); $instance['number'] = min(max(intval($new_instance['number']), 1), 20); $instance['exclude'] = esc_attr($new_instance['exclude']); $instance['exclude_mature'] = (bool)$new_instance['exclude_mature']; $instance['avatar_size'] = intval($new_instance['avatar_size']); $instance['more'] = (bool)$new_instance['more']; // html template if(current_user_can('edit_themes')) $instance['template'] = $new_instance['template']; $this->flush_cache(); return wp_parse_args($instance, $this->defaults()); } function defaults(){ // default settings return apply_filters('atom_widget_blogs_defaults', array( 'title' => _a("Recently updated blogs"), 'order_by' => 'last_updated', 'number' => 10, 'exclude' => '', 'exclude_mature' => false, 'avatar_size' => 48, 'more' => true, 'template' => $this->template(), ), $this); } function form($instance){ $instance = wp_parse_args($instance, $this->defaults()); ?>
>