| @@ -1,89 +1,163 @@ | ||
| 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 | -?> | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Welcart Recent Posts Widget | |
| 4 | + * | |
| 5 | + * @package Welcart | |
| 6 | + */ | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * Welcart_Recent_Posts Class | |
| 10 | + * | |
| 11 | + * @see WP_Widget | |
| 12 | + */ | |
| 13 | +class Welcart_Recent_Posts extends WP_Widget { | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * Constructor. | |
| 17 | + */ | |
| 18 | + public function __construct() { | |
| 19 | + $widget_ops = array( | |
| 20 | + 'classname' => 'usces_recent_entries', | |
| 21 | + 'description' => ( __( 'Your site’s most recent posts.', 'usces' ) . __( 'Non-item', 'usces' ) ), | |
| 22 | + ); | |
| 23 | + parent::__construct( 'usces-recent-posts', 'Welcart ' . __( 'Recent Posts', 'usces' ), $widget_ops ); | |
| 24 | + $this->alt_option_name = 'usces_recent_entries'; | |
| 25 | + | |
| 26 | + add_action( 'save_post', array( &$this, 'flush_widget_cache' ) ); | |
| 27 | + add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) ); | |
| 28 | + add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) ); | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Echoes the widget content. | |
| 33 | + * | |
| 34 | + * @see WP_Widget::widget | |
| 35 | + * @param array $args Display arguments including 'before_title', 'after_title', | |
| 36 | + * 'before_widget', and 'after_widget'. | |
| 37 | + * @param array $instance The settings for the particular instance of the widget. | |
| 38 | + */ | |
| 39 | + public function widget( $args, $instance ) { | |
| 40 | + $cache = wp_cache_get( 'usces_recent_posts', 'widget' ); | |
| 41 | + | |
| 42 | + if ( ! is_array( $cache ) ) { | |
| 43 | + $cache = array(); | |
| 44 | + } | |
| 45 | + | |
| 46 | + if ( ! isset( $args['widget_id'] ) ) { | |
| 47 | + $args['widget_id'] = $this->id; | |
| 48 | + } | |
| 49 | + | |
| 50 | + if ( isset( $cache[ $args['widget_id'] ] ) ) { | |
| 51 | + wel_esc_script_e( $cache[ $args['widget_id'] ] ); | |
| 52 | + return; | |
| 53 | + } | |
| 54 | + | |
| 55 | + usces_remove_filter(); | |
| 56 | + | |
| 57 | + ob_start(); | |
| 58 | + extract( $args ); | |
| 59 | + $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); | |
| 60 | + $number = ( isset( $instance['number'] ) ) ? (int) $instance['number'] : 10; | |
| 61 | + if ( ! $number ) { | |
| 62 | + $number = 10; | |
| 63 | + } elseif ( $number < 1 ) { | |
| 64 | + $number = 1; | |
| 65 | + } elseif ( $number > 15 ) { | |
| 66 | + $number = 15; | |
| 67 | + } | |
| 68 | + | |
| 69 | + $r = new WP_Query( | |
| 70 | + array( | |
| 71 | + 'showposts' => $number, | |
| 72 | + 'nopaging' => 0, | |
| 73 | + 'post_status' => 'publish', | |
| 74 | + 'ignore_sticky_posts' => 1, | |
| 75 | + 'cat' => -( USCES_ITEM_CAT_PARENT_ID ), | |
| 76 | + 'order' => 'DESC', | |
| 77 | + 'orderby' => 'date', | |
| 78 | + ) | |
| 79 | + ); | |
| 80 | + if ( $r->have_posts() ) : | |
| 81 | + | |
| 82 | + wel_esc_script_e( $before_widget ); | |
| 83 | + if ( ! empty( $title ) ) { | |
| 84 | + wel_esc_script_e( $before_title . esc_html( $title ) . $after_title ); | |
| 85 | + } | |
| 86 | + ?> | |
| 87 | + <ul> | |
| 88 | + <?php | |
| 89 | + while ( $r->have_posts() ) : | |
| 90 | + $r->the_post(); | |
| 91 | + ?> | |
| 92 | + <li><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_title() ? get_the_title() : get_the_ID() ); ?>"> | |
| 93 | + <?php | |
| 94 | + if ( get_the_title() ) { | |
| 95 | + the_title(); | |
| 96 | + } else { | |
| 97 | + the_ID(); | |
| 98 | + } | |
| 99 | + ?> | |
| 100 | + </a></li> | |
| 101 | + <?php endwhile; ?> | |
| 102 | + </ul> | |
| 103 | + <?php | |
| 104 | + wel_esc_script_e( $after_widget ); | |
| 105 | + | |
| 106 | + // Reset the global $the_post as this query will have stomped on it. | |
| 107 | + wp_reset_postdata(); | |
| 108 | + | |
| 109 | + endif; | |
| 110 | + $cache[ $args['widget_id'] ] = ob_get_flush(); | |
| 111 | + wp_cache_set( 'usces_recent_posts', $cache, 'widget' ); | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Updates a particular instance of a widget. | |
| 116 | + * | |
| 117 | + * @see WP_Widget::update | |
| 118 | + * @param array $new_instance New settings for this instance as input by the user via | |
| 119 | + * WP_Widget::form(). | |
| 120 | + * @param array $old_instance Old settings for this instance. | |
| 121 | + * @return array Settings to save or bool false to cancel saving. | |
| 122 | + */ | |
| 123 | + public function update( $new_instance, $old_instance ) { | |
| 124 | + $instance = $old_instance; | |
| 125 | + $instance['title'] = strip_tags( $new_instance['title'] ); | |
| 126 | + $instance['number'] = (int) $new_instance['number']; | |
| 127 | + $this->flush_widget_cache(); | |
| 128 | + | |
| 129 | + $alloptions = wp_cache_get( 'alloptions', 'options' ); | |
| 130 | + if ( isset( $alloptions['usces_recent_entries'] ) ) { | |
| 131 | + delete_option( 'usces_recent_entries' ); | |
| 132 | + } | |
| 133 | + | |
| 134 | + return $instance; | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * Flushes the Recent Comments widget cache. | |
| 139 | + */ | |
| 140 | + public function flush_widget_cache() { | |
| 141 | + wp_cache_delete( 'usces_recent_posts', 'widget' ); | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Outputs the settings update form. | |
| 146 | + * | |
| 147 | + * @see WP_Widget::form | |
| 148 | + * @param array $instance Current settings. | |
| 149 | + */ | |
| 150 | + public function form( $instance ) { | |
| 151 | + $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; | |
| 152 | + if ( ! isset( $instance['number'] ) || ! $number = (int) $instance['number'] ) { | |
| 153 | + $number = 5; | |
| 154 | + } | |
| 155 | + ?> | |
| 156 | + <p><label for="<?php wel_esc_script_e( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'usces' ); ?></label> | |
| 157 | + <input class="widefat" id="<?php wel_esc_script_e( $this->get_field_id( 'title' ) ); ?>" name="<?php wel_esc_script_e( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php wel_esc_script_e( $title ); ?>" /></p> | |
| 158 | + | |
| 159 | + <p><label for="<?php wel_esc_script_e( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of posts to show:', 'usces' ); ?></label> | |
| 160 | + <input id="<?php wel_esc_script_e( $this->get_field_id( 'number' ) ); ?>" name="<?php wel_esc_script_e( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php wel_esc_script_e( $number ); ?>" size="3" /></p> | |
| 161 | + <?php | |
| 162 | + } | |
| 163 | +} | |