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