'posts', 'description' => _a('List posts based on filters you choose')); $control_ops = array('width' => 500); $this->WP_Widget('atom-posts', _a('Posts'), $widget_ops, $control_ops); atom_add('ajax_requests', array(&$this, 'ajax')); // register thumbnail size atom_add('set_thumb_sizes', array(&$this, 'set_thumb_size')); // include in jQuery(document).ready() atom_add('jquery_init', array(&$this, 'js')); // 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 set_thumb_size($sizes){ // 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}"; // register thumb size if the widget is active if(is_active_widget(false, $id, $this->id_base)) add_image_size($id, $options['thumb_size'], $options['thumb_size'], true); endforeach; return $sizes; } 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)): ?> $("# a.more").click(function(event){ event.preventDefault(); $.ajax({ type: "GET", url: "", data: { instance: , offset: parseInt($(this).attr('rel')), _ajax_nonce: "", atom: 'get_posts' }, dataType: 'json', context: this, error: function(e, m, d) {alert(d); }, 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]; // ID needed to identify post thumbnail size $settings['id'] = "{$this->id_base}-{$instance}"; $offset = intval($_GET['offset']); $output = $this->get_posts($settings, $next, $offset); $offset = $offset + $settings['number']; echo json_encode(array('output' => $output, 'more' => $next, 'offset' => $offset)); exit(); endif; } function flush_cache(){ wp_cache_delete('widget_posts', 'widget'); } function template($mode = 'full'){ switch($mode): case 'full': return "\n" ." {THUMBNAIL}\n" ." \n" ." {TITLE} ({COMMENT_COUNT})\n" ." {CONTENT}\n" ." {DATE}\n" ." \n" ."\n"; break; case 'images': return "\n" ." {THUMBNAIL}\n" ."\n"; break; case 'brief': return "\n" ." \n" ." {TITLE} ({COMMENT_COUNT})\n" ." \n" ."\n"; break; case 'detailed': return "\n" ." \n" ." {TITLE} ({COMMENT_COUNT})\n" ." {CONTENT}\n" ." {DATE}\n" ." \n" ."\n"; break; endswitch; } function get_posts($args, &$more, $offset = 0){ global $post; extract($args); // build query, we get the number of posts + 1 just to check if more posts are available $query = array('orderby' => $order_by, 'post_type' => $post_type, 'caller_get_posts' => 1, 'posts_per_page' => $number+1, 'offset' => $offset); if($category != 0) $query['cat'] = $category; // exclude current post if we're on the single page if(is_single()) $query['post__not_in'] = array($post->ID); if($related && is_single()): // tag-related posts ? $tags = wp_get_post_tags($post->ID); if(!empty($tags)): $tag_ids = array(); foreach($tags as $tag) $tag_ids[] = $tag->term_id; $query['tag__in'] = $tag_ids; else: return false; // no tags = no related posts endif; elseif($related): return false; // not a single page endif; $posts = new WP_Query(); $posts->query($query); /*/ site-wide posts -- @todo global $wpdb; foreach (atom_get_sites(array('limit_results' => $number)) as $blog) $selects[] = "(SELECT ID, post_date_gmt, {$blog['blog_id']} AS blog_id FROM ".$wpdb->get_blog_prefix($blog['blog_id'])."posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT {$number})"; // real number is (number * # of blogs) $p = $wpdb->get_results(implode(" UNION ALL ", $selects)." ORDER BY post_date_gmt DESC", ARRAY_A); foreach($p as $post): $data = get_blog_post($post["blog_id"], $post["ID"]); echo $data->post_title,"
"; echo atom_time_since(abs(strtotime($data->post_date_gmt))); endforeach; echo $posts->found_posts; //*/ // do we have more results? $more = ($posts->post_count > $number) ? true : false; $output = ''; $count = 0; if($mode == 'template') $template = $args['template']; else $template = $this->template($mode); // output posts, minus the last one while($posts->have_posts() && ($posts->current_post < $number-1)): $posts->the_post(); $count++; $output .= ''; $categories = get_the_category(); // output template $output .= atom_render_template($template, array( 'TITLE' => atom_get_post_title(), 'COMMENT_COUNT' => $post->comment_count, 'THUMBNAIL' => atom_get_post_thumb($post->ID, str_replace('instance-', '', $id)), 'URL' => atom_get_post_title_url(), 'CONTENT' => convert_smilies(atom_filter_content(NULL, array( 'limit' => $word_count, 'allowed_tags' => explode(',', $allowed_tags), 'more' => $content_filter_more, ))), 'EXCERPT' => get_the_excerpt(), 'DATE' => atom_get_post_date(), 'AUTHOR' => esc_attr(get_the_author()), // not used by default 'CATEGORY' => isset($categories[0]->name) ? $categories[0]->name : '', // not used by default 'INDEX' => $posts->current_post+1, // number 'ID' => $post->ID, // not used by default )); $output .= ''; endwhile; wp_reset_query(); return $output; } function widget($args, $instance){ $cache = wp_cache_get('widget_posts', 'widget'); if (!is_array($cache)) $cache = array(); if (isset($cache[$args['widget_id']])): echo $cache[$args['widget_id']]; return; endif; $instance = wp_parse_args($instance, $this->defaults()); $instance['id'] = $this->id; extract($args, EXTR_SKIP); $posts = $this->get_posts($instance, $next); if(!$posts) return atom_add_debug_message("No ".($instance['related'] ? 'related posts' : 'relevant entries')." were 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; $id = "instance-{$this->id}"; $output .= ""; if($instance['more'] && $next && atom_get_options('jquery')) $output .= ""._a("Show More").""; $output .= $after_widget; echo $output; if($instance['order_by'] != 'rand'): // we can't cache random posts (they wouldn't be random :) $cache[$args['widget_id']] = $output; wp_cache_set('widget_posts', $cache, 'widget'); endif; } function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = esc_attr($new_instance['title']); $instance['post_type'] = esc_attr($new_instance['post_type']); $instance['mode'] = esc_attr($new_instance['mode']); $instance['category'] = intval($new_instance['category']); $instance['order_by'] = esc_attr($new_instance['order_by']); $instance['number'] = min(max(intval($new_instance['number']), 1), 20); $instance['word_count'] = intval($new_instance['word_count']); $instance['thumb_size'] = intval($new_instance['thumb_size']); $instance['more'] = (bool)$new_instance['more']; $instance['related'] = (bool)$new_instance['related']; // 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(){ // default settings return apply_filters('atom_widget_posts_defaults', array( 'title' => _a("Recent Posts"), 'post_type' => 'post', 'mode' => 'full', 'order_by' => 'date', 'category' => 0, 'number' => 5, 'word_count' => 20, 'thumb_size' => 48, 'more' => true, 'related' => false, 'template' => $this->template('full'), // internal settings (not worth adding forms for this) 'allowed_tags' => 'abbr,acronym,b,cite,code,del,dfn,em,i,ins,q,strong,sub,sup', 'content_filter_more' => '[…]', ), $this); } function form($instance){ $instance = wp_parse_args($instance, $this->defaults()); ?>
>

$this->get_field_name('category'), 'id' => $this->get_field_id('category'), 'selected' => intval($instance['category']), 'show_option_all' => _a('-- All categories --'), 'hide_empty' => 0, 'orderby' => 'name', 'show_count' => 1, 'echo' => 0, 'class' => 'widefat', 'hierarchical' => 1, 'extra_attributes' => 'followRules rules="DEPENDS ON '.$this->get_field_name('post_type').' BEING post"', )); ?>


'; ?>


type="checkbox" id="get_field_id('more'); ?>" name="get_field_name('more'); ?>" />

followRules rules="DEPENDS ON get_field_name('post_type'); ?> BEING post" />


Important: %1$s sized thumbnails have to be created if you just added this widget, or if you\'re changing the thumbnail size. Read more about thumbnail sizes %2$s'), intval($instance['thumb_size']).'x'.intval($instance['thumb_size']), ''._a("here").''); ?>