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