-
Notifications
You must be signed in to change notification settings - Fork 2
/
widget-category-posts-single.php
308 lines (218 loc) · 8.89 KB
/
widget-category-posts-single.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
// Add Category Posts Single Widget
class Smartline_Category_Posts_Single_Widget extends WP_Widget {
function __construct() {
// Setup Widget
$widget_ops = array(
'classname' => 'smartline_category_posts_single',
'description' => esc_html__( 'Displays a single post from a selected category. Please use this widget ONLY in the Magazine Homepage widget area.', 'smartline-lite' )
);
parent::__construct('smartline_category_posts_single', sprintf( esc_html__( 'Category Posts: Single (%s)', 'smartline-lite' ), 'Smartline' ), $widget_ops);
// Delete Widget Cache on certain actions
add_action( 'save_post', array( $this, 'delete_widget_cache' ) );
add_action( 'deleted_post', array( $this, 'delete_widget_cache' ) );
add_action( 'switch_theme', array( $this, 'delete_widget_cache' ) );
}
public function delete_widget_cache() {
wp_cache_delete('widget_smartline_category_posts_single', 'widget');
}
private function default_settings() {
$defaults = array(
'title' => '',
'category' => 0,
'number' => 1,
'category_link' => false,
'postmeta' => 4
);
return $defaults;
}
// Display Widget
function widget($args, $instance) {
$cache = array();
// Get Widget Object Cache
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'widget_smartline_category_posts_single', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
// Display Widget from Cache if exists
if ( isset( $cache[ $this->id ] ) ) {
echo $cache[ $this->id ];
return;
}
// Start Output Buffering
ob_start();
// Extract Sidebar Arguments
extract($args);
// Get Widget Settings
$defaults = $this->default_settings();
extract( wp_parse_args( $instance, $defaults ) );
// Output
echo $before_widget;
?>
<div id="widget-category-posts-single" class="widget-category-posts clearfix">
<?php // Display Title
$this->display_widget_title($args, $instance); ?>
<div class="widget-category-posts-content">
<?php $this->render($instance); ?>
</div>
</div>
<?php
echo $after_widget;
// Set Cache
if ( ! $this->is_preview() ) {
$cache[ $this->id ] = ob_get_flush();
wp_cache_set( 'widget_smartline_category_posts_single', $cache, 'widget' );
} else {
ob_end_flush();
}
}
// Render Widget Content
function render($instance) {
// Get Widget Settings
$defaults = $this->default_settings();
extract( wp_parse_args( $instance, $defaults ) );
// Get latest posts from database
$query_arguments = array(
'posts_per_page' => (int)$number,
'ignore_sticky_posts' => true,
'cat' => (int)$category
);
$posts_query = new WP_Query($query_arguments);
$i = 0;
// Check if there are posts
if( $posts_query->have_posts() ) :
// Display Posts
while( $posts_query->have_posts() ) : $posts_query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('single-post'); ?>>
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail('category_posts_single'); ?></a>
<?php the_title( sprintf( '<h1 class="entry-title post-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>
<div class="postmeta"><?php $this->display_postmeta($instance); ?></div>
<div class="entry">
<?php the_excerpt(); ?>
<a href="<?php esc_url(the_permalink()) ?>" class="more-link"><?php esc_html_e( '» Read more', 'smartline-lite' ); ?></a>
</div>
</article>
<?php endwhile;
endif;
// Reset Postdata
wp_reset_postdata();
}
// Display Postmeta
function display_postmeta( $instance ) {
// Get Widget Settings
$defaults = $this->default_settings();
extract( wp_parse_args( $instance, $defaults ) );
// Display Date unless deactivated
if ( $postmeta > 0 ) :
smartline_meta_date();
endif;
// Display Author unless deactivated
if ( $postmeta == 2 or $postmeta == 4 ) :
smartline_meta_author();
endif;
// Display Comments
if ( $postmeta >= 3 and comments_open() ) :
smartline_meta_comments();
endif;
}
// Display Widget Title
function display_widget_title($args, $instance) {
// Get Sidebar Arguments
extract($args);
// Get Widget Settings
$defaults = $this->default_settings();
extract( wp_parse_args( $instance, $defaults ) );
// Add Widget Title Filter
$widget_title = apply_filters('widget_title', $title, $instance, $this->id_base);
if( !empty( $widget_title ) ) :
echo $before_title;
// Link Category Title
if( $category_link == true ) :
// Check if "All Categories" is selected
if( $category == 0 ) :
$link_title = esc_html__( 'View all posts', 'smartline-lite' );
// Set Link URL to always point to latest posts page
if ( get_option( 'show_on_front' ) == 'page' ) :
$link_url = esc_url( get_permalink( get_option('page_for_posts' ) ) );
else :
$link_url = esc_url( home_url('/') );
endif;
else :
// Set Link URL and Title for Category
$link_title = sprintf( esc_html__( 'View all posts from category %s', 'smartline-lite' ), get_cat_name( $category ) );
$link_url = esc_url( get_category_link( $category ) );
endif;
// Display linked Widget Title
echo '<a href="'. $link_url .'" title="'. $link_title . '">'. $widget_title . '</a>';
echo '<a class="category-archive-link" href="'. $link_url .'" title="'. $link_title . '"><span class="genericon-next"></span></a>';
else:
echo $widget_title;
endif;
echo $after_title;
endif;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field($new_instance['title']);
$instance['category'] = (int)$new_instance['category'];
$instance['number'] = (int)$new_instance['number'];
$instance['category_link'] = !empty($new_instance['category_link']);
$instance['postmeta'] = (int)$new_instance['postmeta'];
$this->delete_widget_cache();
return $instance;
}
function form( $instance ) {
// Get Widget Settings
$defaults = $this->default_settings();
extract( wp_parse_args( $instance, $defaults ) );
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e( 'Title:', 'smartline-lite' ); ?>
<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; ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('category'); ?>"><?php esc_html_e( 'Category:', 'smartline-lite' ); ?></label><br/>
<?php // Display Category Select
$args = array(
'show_option_all' => esc_html__( 'All Categories', 'smartline-lite' ),
'show_count' => true,
'hide_empty' => false,
'selected' => $category,
'name' => $this->get_field_name('category'),
'id' => $this->get_field_id('category')
);
wp_dropdown_categories( $args );
?>
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>"><?php esc_html_e( 'Number of posts:', 'smartline-lite' ); ?>
<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" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('category_link'); ?>">
<input class="checkbox" type="checkbox" <?php checked( $category_link ) ; ?> id="<?php echo $this->get_field_id('category_link'); ?>" name="<?php echo $this->get_field_name('category_link'); ?>" />
<?php esc_html_e( 'Link Widget Title to Category Archive page', 'smartline-lite' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'postmeta' ); ?>"><?php esc_html_e( 'Post Meta:', 'smartline-lite' ); ?></label><br/>
<select id="<?php echo $this->get_field_id( 'postmeta' ); ?>" name="<?php echo $this->get_field_name( 'postmeta' ); ?>">
<option value="0" <?php selected($postmeta, 0); ?>><?php esc_html_e( 'Hide post meta', 'smartline-lite' ); ?></option>
<option value="1" <?php selected($postmeta, 1); ?>><?php esc_html_e( 'Display post date', 'smartline-lite' ); ?></option>
<option value="2" <?php selected($postmeta, 2); ?>><?php esc_html_e( 'Display date and author', 'smartline-lite' ); ?></option>
<option value="3" <?php selected($postmeta, 3); ?>><?php esc_html_e( 'Display date and comments', 'smartline-lite' ); ?></option>
<option value="4" <?php selected($postmeta, 4); ?>><?php esc_html_e( 'Display date, author and comments', 'smartline-lite' ); ?></option>
</select>
</p>
<?php
}
}
// Register Widget
add_action( 'widgets_init', 'smartline_register_category_posts_single_widget' );
function smartline_register_category_posts_single_widget() {
register_widget('Smartline_Category_Posts_Single_Widget');
}