Created
March 1, 2012 19:01
-
-
Save micahwave/1952277 to your computer and use it in GitHub Desktop.
widget cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function widget($args, $instance) { | |
extract( $args ); | |
$title = apply_filters('widget_title', $instance['title']); | |
?> | |
<?php echo $before_widget; ?> | |
<?php if ( $title ) echo $before_title . $title . $after_title; ?> | |
<?php | |
global $cap, $post; | |
$cache_key = 'time_widget_'.$this->id; | |
$posts = wp_cache_get( $cache_key, 'time_widget_cache' ); | |
if( !$posts ) { | |
echo 'caching'; | |
$post_ids = $cap->editors_picks; | |
$ids = explode(',', $post_ids); | |
if( is_array( $ids ) ) { | |
global $time_post_in; | |
// lets clean the values | |
array_walk( $ids, 'intval' ); | |
// set the global so we can use our filter | |
$time_post_in = $ids; | |
// turning on filter that will order the posts by the order in which they were entered | |
add_filter( 'posts_orderby', 'time_orderby_in' ); | |
$posts = get_posts( | |
array( | |
'post__in' => $ids, | |
'posts_per_page' => 3, | |
'suppress_filters' => false, | |
) | |
); | |
remove_filter( 'posts_orderby', 'time_orderby_in' ); | |
if( $posts ) { | |
wp_cache_set( $cache_key, $posts, 'time_widget_cache', 21600 ); | |
} | |
} | |
} else { | |
echo 'served from cache'; | |
} | |
if( $posts ) : $i = 0; | |
?> | |
<ul> | |
<?php foreach( $posts as $post ) : setup_postdata( $post ); $i++; ?> | |
<?php if( has_post_thumbnail( $post->ID ) ) : // dont show it if there no image ?> | |
<li<?php if( $i == count($picks) ) echo ' class="last"'; ?>> | |
<div class="pick-right"> | |
<a href="<?php echo esc_url( time_tracking_tag( get_permalink(), 'editpicks' ) ); ?>"><?php the_post_thumbnail('thumb'); ?></a> | |
</div> | |
<div class="pick-left"> | |
<div class="entry-category"><?php time_the_slug(); ?></div> | |
<h2><a href="<?php echo esc_url( time_tracking_tag( get_permalink(), 'editpicks' ) ); ?>"><?php the_title(); ?></a></h2> | |
<span class="pick-author">By <a rel="author" title="Posts by <?php echo get_the_author($post->post_author); ?>" href="<?php echo esc_url( time_tracking_tag( get_author_posts_url($post->post_author), 'editpicks' ) ); ?>"><?php echo get_the_author($post->post_author); ?></a></span><br/> | |
</div> | |
</li> | |
<?php endif; ?> | |
<?php endforeach; ?> | |
</ul> | |
<?php | |
endif; | |
wp_reset_postdata(); | |
echo $after_widget; | |
} | |
function update( $new_instance, $old_instance ) { | |
// bust the cache on update | |
wp_cache_delete( 'time_widget_'.$this->id, 'time_widget_cache' ); | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
return $instance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment