| 1 |
<?php |
| 2 |
|
| 3 |
class Welcart_Recent_Posts extends WP_Widget { |
| 4 |
|
| 5 |
function Welcart_Recent_Posts() { |
| 6 |
$widget_ops = array('classname' => 'usces_recent_entries', 'description' => (__( "The most recent posts on your site").'(商品以外)') ); |
| 7 |
$this->WP_Widget('usces-recent-posts', __('Welcart Recent Posts'), $widget_ops); |
| 8 |
$this->alt_option_name = 'usces_recent_entries'; |
| 9 |
|
| 10 |
add_action( 'save_post', array(&$this, 'flush_widget_cache') ); |
| 11 |
add_action( 'deleted_post', array(&$this, 'flush_widget_cache') ); |
| 12 |
add_action( 'switch_theme', array(&$this, 'flush_widget_cache') ); |
| 13 |
} |
| 14 |
|
| 15 |
function widget($args, $instance) { |
| 16 |
$cache = wp_cache_get('usces_recent_posts', 'widget'); |
| 17 |
|
| 18 |
if ( !is_array($cache) ) |
| 19 |
$cache = array(); |
| 20 |
|
| 21 |
if ( isset($cache[$args['widget_id']]) ) { |
| 22 |
echo $cache[$args['widget_id']]; |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
ob_start(); |
| 27 |
extract($args); |
| 28 |
|
| 29 |
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base); |
| 30 |
if ( !$number = (int) $instance['number'] ) |
| 31 |
$number = 10; |
| 32 |
else if ( $number < 1 ) |
| 33 |
$number = 1; |
| 34 |
else if ( $number > 15 ) |
| 35 |
$number = 15; |
| 36 |
|
| 37 |
$r = new WP_Query(array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'cat'=>-(USCES_ITEM_CAT_PARENT_ID), 'order'=>'DESC', 'orderby'=>'date' )); |
| 38 |
if ($r->have_posts()) : |
| 39 |
?> |
| 40 |
<?php echo $before_widget; ?> |
| 41 |
<?php if ( $title ) echo $before_title . $title . $after_title; ?> |
| 42 |
<ul> |
| 43 |
<?php while ($r->have_posts()) : $r->the_post(); ?> |
| 44 |
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></li> |
| 45 |
<?php endwhile; ?> |
| 46 |
</ul> |
| 47 |
<?php echo $after_widget; ?> |
| 48 |
<?php |
| 49 |
// Reset the global $the_post as this query will have stomped on it |
| 50 |
wp_reset_postdata(); |
| 51 |
|
| 52 |
endif; |
| 53 |
|
| 54 |
$cache[$args['widget_id']] = ob_get_flush(); |
| 55 |
wp_cache_set('usces_recent_posts', $cache, 'widget'); |
| 56 |
} |
| 57 |
|
| 58 |
function update( $new_instance, $old_instance ) { |
| 59 |
$instance = $old_instance; |
| 60 |
$instance['title'] = strip_tags($new_instance['title']); |
| 61 |
$instance['number'] = (int) $new_instance['number']; |
| 62 |
$this->flush_widget_cache(); |
| 63 |
|
| 64 |
$alloptions = wp_cache_get( 'alloptions', 'options' ); |
| 65 |
if ( isset($alloptions['usces_recent_entries']) ) |
| 66 |
delete_option('usces_recent_entries'); |
| 67 |
|
| 68 |
return $instance; |
| 69 |
} |
| 70 |
|
| 71 |
function flush_widget_cache() { |
| 72 |
wp_cache_delete('usces_recent_posts', 'widget'); |
| 73 |
} |
| 74 |
|
| 75 |
function form( $instance ) { |
| 76 |
$title = isset($instance['title']) ? esc_attr($instance['title']) : ''; |
| 77 |
if ( !isset($instance['number']) || !$number = (int) $instance['number'] ) |
| 78 |
$number = 5; |
| 79 |
?> |
| 80 |
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> |
| 81 |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> |
| 82 |
|
| 83 |
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label> |
| 84 |
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p> |
| 85 |
<?php |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
?> |
| 90 |
|