| 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 |
} |
| 164 |
|