forked from tiptoppress/category-posts-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat-posts.php
More file actions
459 lines (413 loc) · 18.4 KB
/
Copy pathcat-posts.php
File metadata and controls
459 lines (413 loc) · 18.4 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
/*
Plugin Name: Category Posts Widget
Plugin URI: http://mkrdip.me/category-posts-widget
Description: Adds a widget that shows the most recent posts from a single category.
Author: Mrinal Kanti Roy
Version: 4.1.3
Author URI: http://mkrdip.me
*/
// Don't call the file directly
if ( !defined( 'ABSPATH' ) ) exit;
/*
Check if CSS needs to be enqueued by traversing all active widgets on the page
and checking if they all have disabled CSS.
Return: false if CSS should not be enqueued, true if it should
*/
function category_posts_should_enqueue($id_base,$class) {
global $wp_registered_widgets;
$ret = false;
$sidebars_widgets = wp_get_sidebars_widgets();
if ( is_array($sidebars_widgets) ) {
foreach ( $sidebars_widgets as $sidebar => $widgets ) {
if ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
continue;
}
if ( is_array($widgets) ) {
foreach ( $widgets as $widget ) {
$widget_base = _get_widget_id_base($widget);
if ( $widget_base == $id_base ) {
$widgetclass = new $class();
$allsettings = $widgetclass->get_settings();
$settings = $allsettings[str_replace($widget_base.'-','',$widget)];
if (!isset($settings['disable_css'])) // checks if css disable is not set
$ret = true;
}
}
}
}
}
return $ret;
}
/**
* Register our styles
*
* @return void
*/
add_action( 'wp_enqueue_scripts', 'category_posts_widget_styles' );
function category_posts_widget_styles() {
$enqueue = category_posts_should_enqueue('category-posts','CategoryPosts');
if ($enqueue) {
wp_register_style( 'category-posts', plugins_url( 'category-posts/cat-posts.css' ) );
wp_enqueue_style( 'category-posts' );
}
}
/**
* Load plugin textdomain.
*
*/
function category_posts_load_textdomain() {
load_plugin_textdomain( 'categoryposts', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'admin_init', 'category_posts_load_textdomain' );
/**
* Category Posts Widget Class
*
* Shows the single category posts with some configurable options
*/
class CategoryPosts extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'cat-post-widget', 'description' => __('List single category posts','categoryposts'));
parent::__construct('category-posts', __('Category Posts','categoryposts'), $widget_ops);
}
// Displays category posts widget on blog.
function widget($args, $instance) {
extract( $args );
// If not title, use the name of the category.
if( !$instance["title"] ) {
$category_info = get_category($instance["cat"]);
$instance["title"] = $category_info->name;
}
$valid_sort_orders = array('date', 'title', 'comment_count', 'rand');
if ( in_array($instance['sort_by'], $valid_sort_orders) ) {
$sort_by = $instance['sort_by'];
$sort_order = (bool) isset( $instance['asc_sort_order'] ) ? 'ASC' : 'DESC';
} else {
// by default, display latest first
$sort_by = 'date';
$sort_order = 'DESC';
}
// Exclude current post
$current_post_id = get_the_ID();
$exclude_current_post = (isset( $instance['exclude_current_post'] ) && $instance['exclude_current_post'] != -1) ? $current_post_id : "";
// Get array of post info.
$args = array(
'showposts' => $instance["num"],
'cat' => $instance["cat"],
'post__not_in' => array( $exclude_current_post ),
'orderby' => $sort_by,
'order' => $sort_order
);
if( isset( $instance['hideNoThumb'] ) ) {
$args = array_merge( $args, array( 'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS' )
)
)
);
}
$cat_posts = new WP_Query( $args );
if ( !isset ( $instance["hide_if_empty"] ) || $cat_posts->have_posts() ) {
// Excerpt length filter
$new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";");
if ( $instance["excerpt_length"] > 0 )
add_filter('excerpt_length', $new_excerpt_length);
echo $before_widget;
// Widget title
if( !isset ( $instance["hide_title"] ) ) {
echo $before_title;
if( isset ( $instance["title_link"] ) ) {
echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>';
} else {
echo $instance["title"];
}
echo $after_title;
}
// Post list
echo "<ul>\n";
while ( $cat_posts->have_posts() )
{
$cat_posts->the_post(); ?>
<li <?php if( !isset( $instance['disable_css'] ) ) {
echo "class=\"cat-post-item";
if ( is_single(get_the_title() ) ) { echo " cat-post-current"; }
echo "\"";
} ?> >
<?php
if( isset( $instance["thumbTop"] ) ) :
if ( current_theme_supports("post-thumbnails") &&
isset( $instance["thumb"] ) &&
has_post_thumbnail() ) : ?>
<a <?php if( !isset( $instance['disable_css'] ) ) { echo "class=\"cat-post-thumbnail\""; } ?>
href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( array($instance['thumb_w'],$instance['thumb_h'])); ?>
</a>
<?php endif;
endif; ?>
<a class="post-title <?php if( !isset( $instance['disable_css'] ) ) { echo " cat-post-title"; } ?>"
href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?>
</a>
<?php if ( isset( $instance['date'] ) ) : ?>
<?php if ( isset( $instance['date_format'] ) && strlen( trim( $instance['date_format'] ) ) > 0 ) { $date_format = $instance['date_format']; } else { $date_format = "j M Y"; } ?>
<p class="post-date <?php if( !isset( $instance['disable_css'] ) ) { echo "cat-post-date"; } ?>">
<?php if( isset ( $instance["date_link"] ) ) { ?> <a href="<?php the_permalink(); ?>"><?php } ?>
<?php the_time($date_format); ?>
<?php if( isset ( $instance["date_link"] ) ) { echo "</a>"; } ?>
</p>
<?php endif;
if( !isset( $instance["thumbTop"] ) ) :
if ( current_theme_supports("post-thumbnails") &&
isset( $instance["thumb"] ) &&
has_post_thumbnail() ) : ?>
<a <?php if( !isset( $instance['disable_css'] ) ) { echo "class=\"cat-post-thumbnail\""; } ?>
href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( array($instance['thumb_w'],$instance['thumb_h'])); ?>
</a>
<?php endif;
endif;
if ( isset( $instance['excerpt'] ) ) :
the_excerpt();
endif;
if ( isset( $instance['comment_num'] ) ) : ?>
<p class="comment-num <?php if( !isset( $instance['disable_css'] ) ) { echo "cat-post-comment-num"; } ?>">
(<?php comments_number(); ?>)
</p>
<?php endif;
if ( isset( $instance['author'] ) ) : ?>
<p class="post-author <?php if( !isset( $instance['disable_css'] ) ) { echo "cat-post-author"; } ?>">
<?php the_author_posts_link(); ?>
</p>
<?php endif; ?>
</li>
<?php
}
echo "</ul>\n";
// Footer link to category page
if( isset ( $instance["footer_link"] ) && $instance["footer_link"] ) {
echo "<a";
if( !isset( $instance['disable_css'] ) ) { echo " class=\"cat-post-footer-link\""; }
echo " href=\"" . get_category_link($instance["cat"]) . "\">" . $instance["footer_link"] . "</a>";
}
echo $after_widget;
remove_filter('excerpt_length', $new_excerpt_length);
wp_reset_postdata();
} // END if
} // END function
/**
* Update the options
*
* @param array $new_instance
* @param array $old_instance
* @return array
*/
function update($new_instance, $old_instance) {
return $new_instance;
}
/**
* The widget configuration form back end.
*
* @param array $instance
* @return void
*/
function form($instance) {
$instance = wp_parse_args( ( array ) $instance, array(
'title' => '',
'hide_title' => '',
'cat' => '',
'num' => '',
'sort_by' => '',
'asc_sort_order' => '',
'exclude_current_post' => '',
'title_link' => '',
'footer_link' => '',
'excerpt' => '',
'excerpt_length' => '',
'comment_num' => '',
'author' => '',
'date' => '',
'date_link' => '',
'date_format' => '',
'thumb' => '',
'thumbTop' => '',
'hideNoThumb' => '',
'thumb_w' => '',
'thumb_h' => '',
'disable_css' => '',
'hide_if_empty' => ''
) );
$title = $instance['title'];
$hide_title = $instance['hide_title'];
$cat = $instance['cat'];
$num = $instance['num'];
$sort_by = $instance['sort_by'];
$asc_sort_order = $instance['asc_sort_order'];
$exclude_current_post = $instance['exclude_current_post'];
$title_link = $instance['title_link'];
$footer_link = $instance['footer_link'];
$excerpt = $instance['excerpt'];
$excerpt_length = $instance['excerpt_length'];
$comment_num = $instance['comment_num'];
$author = $instance['author'];
$date = $instance['date'];
$date_link = $instance['date_link'];
$date_format = $instance['date_format'];
$thumb = $instance['thumb'];
$thumbTop = $instance['thumbTop'];
$hideNoThumb = $instance['hideNoThumb'];
$thumb_w = $instance['thumb_w'];
$thumb_h = $instance['thumb_h'];
$disable_css = $instance['disable_css'];
$hide_if_empty = $instance['hide_if_empty'];
?>
<p>
<label for="<?php echo $this->get_field_id("title"); ?>">
<?php _e( 'Title','categoryposts' ); ?>:
<input class="widefat" style="width:80%;" id="<?php echo $this->get_field_id("title"); ?>" name="<?php echo $this->get_field_name("title"); ?>" type="text" value="<?php echo esc_attr($instance["title"]); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("title_link"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("title_link"); ?>" name="<?php echo $this->get_field_name("title_link"); ?>"<?php checked( (bool) $instance["title_link"], true ); ?> />
<?php _e( 'Make widget title link','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("hide_title"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("hide_title"); ?>" name="<?php echo $this->get_field_name("hide_title"); ?>"<?php checked( (bool) $instance["hide_title"], true ); ?> />
<?php _e( 'Hide title','categoryposts' ); ?>
</label>
</p>
<p>
<label>
<?php _e( 'Category','categoryposts' ); ?>:
<?php wp_dropdown_categories( array( 'hide_empty'=> 0, 'name' => $this->get_field_name("cat"), 'selected' => $instance["cat"] ) ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("num"); ?>">
<?php _e('Number of posts to show','categoryposts'); ?>:
<input style="text-align: center;" id="<?php echo $this->get_field_id("num"); ?>" name="<?php echo $this->get_field_name("num"); ?>" type="text" value="<?php echo absint($instance["num"]); ?>" size='3' />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("sort_by"); ?>">
<?php _e('Sort by','categoryposts'); ?>:
<select id="<?php echo $this->get_field_id("sort_by"); ?>" name="<?php echo $this->get_field_name("sort_by"); ?>">
<option value="date"<?php selected( $instance["sort_by"], "date" ); ?>><?php _e('Date','categoryposts')?></option>
<option value="title"<?php selected( $instance["sort_by"], "title" ); ?>><?php _e('Title','categoryposts')?></option>
<option value="comment_count"<?php selected( $instance["sort_by"], "comment_count" ); ?>><?php _e('Number of comments','categoryposts')?></option>
<option value="rand"<?php selected( $instance["sort_by"], "rand" ); ?>><?php _e('Random','categoryposts')?></option>
</select>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("asc_sort_order"); ?>">
<input type="checkbox" class="checkbox"
id="<?php echo $this->get_field_id("asc_sort_order"); ?>"
name="<?php echo $this->get_field_name("asc_sort_order"); ?>"
<?php checked( (bool) $instance["asc_sort_order"], true ); ?> />
<?php _e( 'Reverse sort order (ascending)','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("exclude_current_post"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("exclude_current_post"); ?>" name="<?php echo $this->get_field_name("exclude_current_post"); ?>"<?php checked( (bool) $instance["exclude_current_post"], true ); ?> />
<?php _e( 'Exclude current post','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("date"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("date"); ?>" name="<?php echo $this->get_field_name("date"); ?>"<?php checked( (bool) $instance["date"], true ); ?> />
<?php _e( 'Show post date','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("date_format"); ?>">
<?php _e( 'Date format:','categoryposts' ); ?>
</label>
<input class="text" placeholder="j M Y" id="<?php echo $this->get_field_id("date_format"); ?>" name="<?php echo $this->get_field_name("date_format"); ?>" type="text" value="<?php echo esc_attr($instance["date_format"]); ?>" size="8" />
</p>
<p>
<label for="<?php echo $this->get_field_id("date_link"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("date_link"); ?>" name="<?php echo $this->get_field_name("date_link"); ?>"<?php checked( (bool) $instance["date_link"], true ); ?> />
<?php _e( 'Make widget date link','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("excerpt"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("excerpt"); ?>" name="<?php echo $this->get_field_name("excerpt"); ?>"<?php checked( (bool) $instance["excerpt"], true ); ?> />
<?php _e( 'Show post excerpt','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("excerpt_length"); ?>">
<?php _e( 'Excerpt length (in words):','categoryposts' ); ?>
</label>
<input style="text-align: center;" type="text" id="<?php echo $this->get_field_id("excerpt_length"); ?>" name="<?php echo $this->get_field_name("excerpt_length"); ?>" value="<?php echo $instance["excerpt_length"]; ?>" size="3" />
</p>
<?php if ( current_theme_supports("post-thumbnails") ) : ?>
<p>
<label for="<?php echo $this->get_field_id("thumb"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("thumb"); ?>" name="<?php echo $this->get_field_name("thumb"); ?>"<?php checked( (bool) $instance["thumb"], true ); ?> />
<?php _e( 'Show post thumbnail','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("thumbTop"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("thumbTop"); ?>" name="<?php echo $this->get_field_name("thumbTop"); ?>"<?php checked( (bool) $instance["thumbTop"], true ); ?> />
<?php _e( 'Thumbnail to top','categoryposts' ); ?>
</label>
</p>
<p>
<label>
<?php _e('Thumbnail dimensions (in pixels)','categoryposts'); ?>:<br />
<label for="<?php echo $this->get_field_id("thumb_w"); ?>">
<?php _e('Width:','categoryposts')?> <input class="widefat" style="width:30%;" type="text" id="<?php echo $this->get_field_id("thumb_w"); ?>" name="<?php echo $this->get_field_name("thumb_w"); ?>" value="<?php echo $instance["thumb_w"]; ?>" />
</label>
<label for="<?php echo $this->get_field_id("thumb_h"); ?>">
<?php _e('Height:','categoryposts')?> <input class="widefat" style="width:30%;" type="text" id="<?php echo $this->get_field_id("thumb_h"); ?>" name="<?php echo $this->get_field_name("thumb_h"); ?>" value="<?php echo $instance["thumb_h"]; ?>" />
</label>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("hideNoThumb"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("hideNoThumb"); ?>" name="<?php echo $this->get_field_name("hideNoThumb"); ?>"<?php checked( (bool) $instance["hideNoThumb"], true ); ?> />
<?php _e( 'Hide posts which have no thumbnail','categoryposts' ); ?>
</label>
</p>
<?php endif; ?>
<p>
<label for="<?php echo $this->get_field_id("comment_num"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("comment_num"); ?>" name="<?php echo $this->get_field_name("comment_num"); ?>"<?php checked( (bool) $instance["comment_num"], true ); ?> />
<?php _e( 'Show number of comments','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("author"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("author"); ?>" name="<?php echo $this->get_field_name("author"); ?>"<?php checked( (bool) $instance["author"], true ); ?> />
<?php _e( 'Show post author','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("footer_link"); ?>">
<?php _e( 'Footer link text','categoryposts' ); ?>:
<input class="widefat" style="width:60%;" placeholder="<?php _e('... more by this topic','categoryposts')?>" id="<?php echo $this->get_field_id("footer_link"); ?>" name="<?php echo $this->get_field_name("footer_link"); ?>" type="text" value="<?php echo esc_attr($instance["footer_link"]); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("disable_css"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("disable_css"); ?>" name="<?php echo $this->get_field_name("disable_css"); ?>"<?php checked( (bool) $instance["disable_css"], true ); ?> />
<?php _e( 'Disable widget CSS','categoryposts' ); ?>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id("hide_if_empty"); ?>">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("hide_if_empty"); ?>" name="<?php echo $this->get_field_name("hide_if_empty"); ?>"<?php checked( (bool) $instance["hide_if_empty"], true ); ?> />
<?php _e( 'Hide if empty','categoryposts' ); ?>
</label>
</p>
<?php
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("CategoryPosts");') );