| 1 |
<?php |
| 2 |
/** |
| 3 |
* Zone Posts widget class |
| 4 |
*/ |
| 5 |
class Zoninator_ZonePosts_Widget extends WP_Widget { |
| 6 |
|
| 7 |
function Zoninator_ZonePosts_Widget() { |
| 8 |
$widget_ops = array( 'classname' => 'widget-zone-posts', 'description' => __( 'Use this widget to display a list of posts from any zone.', 'zoninator' ) ); |
| 9 |
$this->WP_Widget( 'widget-zone-posts', __( 'Zone Posts', 'zoninator' ), $widget_ops ); |
| 10 |
$this->alt_option_name = 'widget_zone_posts'; |
| 11 |
|
| 12 |
add_action( 'save_post', array( &$this, 'flush_widget_cache' ) ); |
| 13 |
add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) ); |
| 14 |
add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
function widget( $args, $instance ) { |
| 18 |
$cache = wp_cache_get( 'widget-zone-posts', 'widget' ); |
| 19 |
|
| 20 |
if ( !is_array( $cache ) ) |
| 21 |
$cache = array(); |
| 22 |
|
| 23 |
if ( isset( $cache[$args['widget_id']] ) ) { |
| 24 |
echo $cache[$args['widget_id']]; |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
ob_start(); |
| 29 |
extract( $args ); |
| 30 |
|
| 31 |
$zone_id = $instance['zone_id'] ? $instance['zone_id'] : 0; |
| 32 |
$show_description = $instance['show_description'] ? 1 : 0; |
| 33 |
|
| 34 |
if( ! $zone_id ) |
| 35 |
return; |
| 36 |
|
| 37 |
$zone = z_get_zone( $zone_id ); |
| 38 |
|
| 39 |
if( ! $zone ) |
| 40 |
return; |
| 41 |
|
| 42 |
$posts = z_get_posts_in_zone( $zone_id ); |
| 43 |
|
| 44 |
if( empty( $posts ) ) |
| 45 |
return; |
| 46 |
|
| 47 |
?> |
| 48 |
<?php echo $before_widget; ?> |
| 49 |
|
| 50 |
<?php echo $before_title . esc_html( $zone->name ) . $after_title; ?> |
| 51 |
|
| 52 |
<?php if( ! empty( $zone->description ) && $show_description ) : ?> |
| 53 |
<p class="description"><?php echo esc_html( $zone->description ); ?></p> |
| 54 |
<?php endif; ?> |
| 55 |
|
| 56 |
<ul> |
| 57 |
<?php foreach( $posts as $post ) : ?> |
| 58 |
<li> |
| 59 |
<a href="<?php echo get_permalink( $post->ID ); ?>"> |
| 60 |
<?php echo esc_html( get_the_title( $post->ID ) ); ?> |
| 61 |
</a> |
| 62 |
</li> |
| 63 |
<?php endforeach; ?> |
| 64 |
</ul> |
| 65 |
|
| 66 |
<?php echo $after_widget; ?> |
| 67 |
<?php |
| 68 |
$cache[$args['widget_id']] = ob_get_flush(); |
| 69 |
wp_cache_set( 'widget-zone-posts', $cache, 'widget' ); |
| 70 |
} |
| 71 |
|
| 72 |
function update( $new_instance, $old_instance ) { |
| 73 |
$instance = $old_instance; |
| 74 |
$new_instance = wp_parse_args( (array) $new_instance, array( 'zone_id' => 0, 'show_description' => 0 ) ); |
| 75 |
|
| 76 |
$instance['zone_id'] = absint( $new_instance['zone_id'] ); |
| 77 |
$instance['show_description'] = $new_instance['show_description'] ? 1 : 0; |
| 78 |
|
| 79 |
$this->flush_widget_cache(); |
| 80 |
|
| 81 |
$alloptions = wp_cache_get( 'alloptions', 'options' ); |
| 82 |
if ( isset($alloptions['widget-zone-posts']) ) |
| 83 |
delete_option( 'widget-zone-posts' ); |
| 84 |
|
| 85 |
return $instance; |
| 86 |
} |
| 87 |
|
| 88 |
function flush_widget_cache() { |
| 89 |
wp_cache_delete( 'widget-zone-posts', 'widget' ); |
| 90 |
} |
| 91 |
|
| 92 |
function form( $instance ) { |
| 93 |
// select - zone |
| 94 |
// checkbox - show description |
| 95 |
|
| 96 |
$zones = z_get_zones(); |
| 97 |
|
| 98 |
if( empty( $zones ) ) { |
| 99 |
_e( 'You need to create at least one zone before you use this widget!', 'zoninator' ); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$zone_id = isset( $instance['zone_id'] ) ? absint( $instance['zone_id'] ) : 0; |
| 104 |
$show_description = isset( $instance['show_description'] ) ? (bool)$instance['show_description'] : true; |
| 105 |
?> |
| 106 |
|
| 107 |
<p> |
| 108 |
<label for="<?php echo $this->get_field_id( 'zone_id' ); ?>"><?php _e('Zone:'); ?></label> |
| 109 |
<select class="widefat" id="<?php echo $this->get_field_id( 'zone_id' ); ?>" name="<?php echo $this->get_field_name( 'zone_id' ); ?>"> |
| 110 |
<option value="0" <?php selected( $zone_id, 0 ); ?>> |
| 111 |
<?php _e( '-- Select a zone --', 'zoninator' ); ?> |
| 112 |
</option> |
| 113 |
|
| 114 |
<?php foreach( $zones as $zone ) : ?> |
| 115 |
<option value="<?php echo $zone->term_id; ?>" <?php selected( $zone_id, $zone->term_id ); ?>> |
| 116 |
<?php echo esc_html( $zone->name ); ?> |
| 117 |
</option> |
| 118 |
<?php endforeach; ?> |
| 119 |
</select> |
| 120 |
</p> |
| 121 |
|
| 122 |
<p> |
| 123 |
<label for="<?php echo $this->get_field_id( 'show_description' ); ?>"> |
| 124 |
<input id="<?php echo $this->get_field_id( 'show_description' ); ?>" name="<?php echo $this->get_field_name( 'show_description' ); ?>" <?php checked( true, $show_description ); ?> type="checkbox" value="1" /> |
| 125 |
<?php _e( 'Show zone description in widget', 'zoninator' ); ?> |
| 126 |
</label> |
| 127 |
</p> |
| 128 |
<?php |
| 129 |
} |
| 130 |
} |