'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 .= '
$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").''); ?>