PluginProbe
Category Posts Block / 4.1.4
Category Posts Block v4.1.4
5.0.0 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.2 2.3 3.1 3.2 3.3 4.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 All 62 releases
category-posts / cat-posts.php

cat-posts.php in Category Posts Block 4.1.4, at cat-posts.php

446 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Category Posts Widget
4 Plugin URI: http://mkrdip.me/category-posts-widget
5 Description: Adds a widget that shows the most recent posts from a single category.
6 Author: Mrinal Kanti Roy
7 Version: 4.1.4
8 Author URI: http://mkrdip.me
9 */
10
11 // Don't call the file directly
12 if ( !defined( 'ABSPATH' ) ) exit;
13
14 /**
15 * Register our styles
16 *
17 * @return void
18 */
19 add_action( 'wp_enqueue_scripts', 'category_posts_widget_styles' );
20
21 function category_posts_widget_styles() {
22 wp_register_style( 'category-posts', plugins_url( 'category-posts/cat-posts.css' ) );
23 wp_enqueue_style( 'category-posts' );
24 }
25
26 /**
27 * Register thumbnail sizes.
28 *
29 * @return void
30 */
31 function category_posts_add_image_size(){
32 $sizes = get_option('mkrdip_cat_post_thumb_sizes');
33
34 if ( $sizes ){
35 foreach ( $sizes as $id=>$size ) {
36 add_image_size( 'cat_post_thumb_size' . $id, $size[0], $size[1], true );
37 }
38 }
39 }
40
41 add_action( 'init', 'category_posts_add_image_size' );
42
43
44 /**
45 * Category Posts Widget Class
46 *
47 * Shows the single category posts with some configurable options
48 */
49 class CategoryPosts extends WP_Widget {
50
51 function __construct() {
52 $widget_ops = array('classname' => 'cat-post-widget', 'description' => __('List single category posts'));
53 parent::__construct('category-posts', __('Category Posts'), $widget_ops);
54 }
55
56 // Displays category posts widget on blog.
57 function widget($args, $instance) {
58 global $post;
59 $post_old = $post; // Save the post object.
60
61 extract( $args );
62
63 $sizes = get_option('mkrdip_cat_post_thumb_sizes');
64
65 // If not title, use the name of the category.
66 if( !$instance["title"] ) {
67 $category_info = get_category($instance["cat"]);
68 $instance["title"] = $category_info->name;
69 }
70
71 $valid_sort_orders = array('date', 'title', 'comment_count', 'rand');
72 if ( in_array($instance['sort_by'], $valid_sort_orders) ) {
73 $sort_by = $instance['sort_by'];
74 $sort_order = (bool) isset( $instance['asc_sort_order'] ) ? 'ASC' : 'DESC';
75 } else {
76 // by default, display latest first
77 $sort_by = 'date';
78 $sort_order = 'DESC';
79 }
80
81 // Exclude current post
82 $current_post_id = get_the_ID();
83 $exclude_current_post = (isset( $instance['exclude_current_post'] ) && $instance['exclude_current_post'] != -1) ? $current_post_id : "";
84
85 // Get array of post info.
86 $args = array(
87 'showposts' => $instance["num"],
88 'cat' => $instance["cat"],
89 'post__not_in' => array( $exclude_current_post ),
90 'orderby' => $sort_by,
91 'order' => $sort_order
92 );
93
94 if( isset( $instance['hideNoThumb'] ) ) {
95 $args = array_merge( $args, array( 'meta_query' => array(
96 array(
97 'key' => '_thumbnail_id',
98 'compare' => 'EXISTS' )
99 )
100 )
101 );
102 }
103
104 $cat_posts = new WP_Query( $args );
105
106 if ( !isset ( $instance["hide_if_empty"] ) || $cat_posts->have_posts() ) {
107
108 // Excerpt length filter
109 $new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";");
110 if ( $instance["excerpt_length"] > 0 )
111 add_filter('excerpt_length', $new_excerpt_length);
112
113 echo $before_widget;
114
115 // Widget title
116 if( !isset ( $instance["hide_title"] ) ) {
117 echo $before_title;
118 if( isset ( $instance["title_link"] ) ) {
119 echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>';
120 } else {
121 echo $instance["title"];
122 }
123 echo $after_title;
124 }
125
126 // Post list
127 echo "<ul>\n";
128
129 while ( $cat_posts->have_posts() )
130 {
131 $cat_posts->the_post(); ?>
132
133 <li <?php if( !isset( $instance['disable_css'] ) ) {
134 echo "class=\"cat-post-item";
135 if ( is_single(get_the_title() ) ) { echo " cat-post-current"; }
136 echo "\"";
137 } ?> >
138
139 <?php
140 if( isset( $instance["thumbTop"] ) ) :
141 if ( function_exists('the_post_thumbnail') &&
142 current_theme_supports("post-thumbnails") &&
143 isset( $instance["thumb"] ) &&
144 has_post_thumbnail() ) : ?>
145 <a <?php if( !isset( $instance['disable_css'] ) ) { echo "class=\"cat-post-thumbnail\""; } ?>
146 href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
147 <?php the_post_thumbnail( 'cat_post_thumb_size'.$this->id ); ?>
148 </a>
149 <?php endif;
150 endif; ?>
151
152 <a class="post-title <?php if( !isset( $instance['disable_css'] ) ) { echo " cat-post-title"; } ?>"
153 href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?>
154 </a>
155
156 <?php if ( isset( $instance['date'] ) ) : ?>
157 <?php if ( isset( $instance['date_format'] ) && strlen( trim( $instance['date_format'] ) ) > 0 ) { $date_format = $instance['date_format']; } else { $date_format = "j M Y"; } ?>
158 <p class="post-date <?php if( !isset( $instance['disable_css'] ) ) { echo "cat-post-date"; } ?>">
159 <?php if( isset ( $instance["date_link"] ) ) { ?> <a href="<?php the_permalink(); ?>"><?php } ?>
160 <?php the_time($date_format); ?>
161 <?php if( isset ( $instance["date_link"] ) ) { echo "</a>"; } ?>
162 </p>
163 <?php endif;
164
165 if( !isset( $instance["thumbTop"] ) ) :
166 if ( function_exists('the_post_thumbnail') &&
167 current_theme_supports("post-thumbnails") &&
168 isset( $instance["thumb"] ) &&
169 has_post_thumbnail() ) : ?>
170 <a <?php if( !isset( $instance['disable_css'] ) ) { echo "class=\"cat-post-thumbnail\""; } ?>
171 href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
172 <?php the_post_thumbnail( 'cat_post_thumb_size'.$this->id ); ?>
173 </a>
174 <?php endif;
175 endif;
176
177 if ( isset( $instance['excerpt'] ) ) :
178 the_excerpt();
179 endif;
180
181 if ( isset( $instance['comment_num'] ) ) : ?>
182 <p class="comment-num <?php if( !isset( $instance['disable_css'] ) ) { echo "cat-post-comment-num"; } ?>">
183 (<?php comments_number(); ?>)
184 </p>
185 <?php endif;
186
187 if ( isset( $instance['author'] ) ) : ?>
188 <p class="post-author <?php if( !isset( $instance['disable_css'] ) ) { echo "cat-post-author"; } ?>">
189 <?php the_author_posts_link(); ?>
190 </p>
191 <?php endif; ?>
192 </li>
193 <?php
194 }
195
196 echo "</ul>\n";
197
198 // Footer link to category page
199 if( isset ( $instance["footer_link"] ) ) {
200 echo "<a";
201 if( !isset( $instance['disable_css'] ) ) { echo " class:\"cat-post-footer-link\""; }
202 echo " href=\"" . get_category_link($instance["cat"]) . "\">" . $instance["footer_link"] . "</a>";
203 }
204
205 echo $after_widget;
206
207 remove_filter('excerpt_length', $new_excerpt_length);
208
209 if (function_exists ('wp_reset_postdata')) //wp_reset_postdata only exists in WordPress >3.0.0
210 wp_reset_postdata();
211
212 } // END if
213 } // END function
214
215 /**
216 * Update the options
217 *
218 * @param array $new_instance
219 * @param array $old_instance
220 * @return array
221 */
222 function update($new_instance, $old_instance) {
223
224 // thumbnail size
225 $sizes = get_option('mkrdip_cat_post_thumb_sizes');
226 if ( !$sizes ) {
227 $sizes = array();
228 }
229 $sizes[$this->id] = array($new_instance['thumb_w'], $new_instance['thumb_h']);
230 update_option('mkrdip_cat_post_thumb_sizes', $sizes);
231
232 return $new_instance;
233 }
234
235 /**
236 * The widget configuration form back end.
237 *
238 * @param array $instance
239 * @return void
240 */
241 function form($instance) {
242 $instance = wp_parse_args( ( array ) $instance, array(
243 'title' => __( '' ),
244 'hide_title' => __( '' ),
245 'cat' => __( '' ),
246 'num' => __( '' ),
247 'sort_by' => __( '' ),
248 'asc_sort_order' => __( '' ),
249 'exclude_current_post' => __( '' ),
250 'title_link' => __( '' ),
251 'footer_link' => __( '' ),
252 'excerpt' => __( '' ),
253 'excerpt_length' => __( '' ),
254 'comment_num' => __( '' ),
255 'author' => __( '' ),
256 'date' => __( '' ),
257 'date_link' => __( '' ),
258 'date_format' => __( '' ),
259 'thumb' => __( '' ),
260 'thumbTop' => __( '' ),
261 'hideNoThumb' => __( '' ),
262 'thumb_w' => __( '' ),
263 'thumb_h' => __( '' ),
264 'disable_css' => __( '' ),
265 'hide_if_empty' => __( '' )
266 ) );
267
268 $title = $instance['title'];
269 $hide_title = $instance['hide_title'];
270 $cat = $instance['cat'];
271 $num = $instance['num'];
272 $sort_by = $instance['sort_by'];
273 $asc_sort_order = $instance['asc_sort_order'];
274 $exclude_current_post = $instance['exclude_current_post'];
275 $title_link = $instance['title_link'];
276 $footer_link = $instance['footer_link'];
277 $excerpt = $instance['excerpt'];
278 $excerpt_length = $instance['excerpt_length'];
279 $comment_num = $instance['comment_num'];
280 $author = $instance['author'];
281 $date = $instance['date'];
282 $date_link = $instance['date_link'];
283 $date_format = $instance['date_format'];
284 $thumb = $instance['thumb'];
285 $thumbTop = $instance['thumbTop'];
286 $hideNoThumb = $instance['hideNoThumb'];
287 $thumb_w = $instance['thumb_w'];
288 $thumb_h = $instance['thumb_h'];
289 $disable_css = $instance['disable_css'];
290 $hide_if_empty = $instance['hide_if_empty'];
291
292 ?>
293 <p>
294 <label for="<?php echo $this->get_field_id("title"); ?>">
295 <?php _e( 'Title' ); ?>:
296 <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"]); ?>" />
297 </label>
298 </p>
299 <p>
300 <label for="<?php echo $this->get_field_id("title_link"); ?>">
301 <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 ); ?> />
302 <?php _e( 'Make widget title link' ); ?>
303 </label>
304 </p>
305 <p>
306 <label for="<?php echo $this->get_field_id("hide_title"); ?>">
307 <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 ); ?> />
308 <?php _e( 'Hide title' ); ?>
309 </label>
310 </p>
311 <p>
312 <label>
313 <?php _e( 'Category' ); ?>:
314 <?php wp_dropdown_categories( array( 'hide_empty'=> 0, 'name' => $this->get_field_name("cat"), 'selected' => $instance["cat"] ) ); ?>
315 </label>
316 </p>
317 <p>
318 <label for="<?php echo $this->get_field_id("num"); ?>">
319 <?php _e('Number of posts to show'); ?>:
320 <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' />
321 </label>
322 </p>
323 <p>
324 <label for="<?php echo $this->get_field_id("sort_by"); ?>">
325 <?php _e('Sort by'); ?>:
326 <select id="<?php echo $this->get_field_id("sort_by"); ?>" name="<?php echo $this->get_field_name("sort_by"); ?>">
327 <option value="date"<?php selected( $instance["sort_by"], "date" ); ?>>Date</option>
328 <option value="title"<?php selected( $instance["sort_by"], "title" ); ?>>Title</option>
329 <option value="comment_count"<?php selected( $instance["sort_by"], "comment_count" ); ?>>Number of comments</option>
330 <option value="rand"<?php selected( $instance["sort_by"], "rand" ); ?>>Random</option>
331 </select>
332 </label>
333 </p>
334 <p>
335 <label for="<?php echo $this->get_field_id("asc_sort_order"); ?>">
336 <input type="checkbox" class="checkbox"
337 id="<?php echo $this->get_field_id("asc_sort_order"); ?>"
338 name="<?php echo $this->get_field_name("asc_sort_order"); ?>"
339 <?php checked( (bool) $instance["asc_sort_order"], true ); ?> />
340 <?php _e( 'Reverse sort order (ascending)' ); ?>
341 </label>
342 </p>
343 <p>
344 <label for="<?php echo $this->get_field_id("exclude_current_post"); ?>">
345 <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 ); ?> />
346 <?php _e( 'Exclude current post' ); ?>
347 </label>
348 </p>
349 <p>
350 <label for="<?php echo $this->get_field_id("date"); ?>">
351 <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 ); ?> />
352 <?php _e( 'Show post date' ); ?>
353 </label>
354 </p>
355 <p>
356 <label for="<?php echo $this->get_field_id("date_format"); ?>">
357 <?php _e( 'Date format:' ); ?>
358 </label>
359 <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" />
360 </p>
361 <p>
362 <label for="<?php echo $this->get_field_id("date_link"); ?>">
363 <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 ); ?> />
364 <?php _e( 'Make widget date link' ); ?>
365 </label>
366 </p>
367 <p>
368 <label for="<?php echo $this->get_field_id("excerpt"); ?>">
369 <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 ); ?> />
370 <?php _e( 'Show post excerpt' ); ?>
371 </label>
372 </p>
373 <p>
374 <label for="<?php echo $this->get_field_id("excerpt_length"); ?>">
375 <?php _e( 'Excerpt length (in words):' ); ?>
376 </label>
377 <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" />
378 </p>
379 <?php if ( function_exists('the_post_thumbnail') && current_theme_supports("post-thumbnails") ) : ?>
380 <p>
381 <label for="<?php echo $this->get_field_id("thumb"); ?>">
382 <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 ); ?> />
383 <?php _e( 'Show post thumbnail' ); ?>
384 </label>
385 </p>
386 <p>
387 <label for="<?php echo $this->get_field_id("thumbTop"); ?>">
388 <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 ); ?> />
389 <?php _e( 'Thumbnail to top' ); ?>
390 </label>
391 </p>
392 <p>
393 <label>
394 <?php _e('Thumbnail dimensions (in pixels)'); ?>:<br />
395 <label for="<?php echo $this->get_field_id("thumb_w"); ?>">
396 Width: <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"]; ?>" />
397 </label>
398
399 <label for="<?php echo $this->get_field_id("thumb_h"); ?>">
400 Height: <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"]; ?>" />
401 </label>
402 </label>
403 </p>
404 <p>
405 <label for="<?php echo $this->get_field_id("hideNoThumb"); ?>">
406 <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 ); ?> />
407 <?php _e( 'Hide posts which have no thumbnail' ); ?>
408 </label>
409 </p>
410 <?php endif; ?>
411 <p>
412 <label for="<?php echo $this->get_field_id("comment_num"); ?>">
413 <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 ); ?> />
414 <?php _e( 'Show number of comments' ); ?>
415 </label>
416 </p>
417 <p>
418 <label for="<?php echo $this->get_field_id("author"); ?>">
419 <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 ); ?> />
420 <?php _e( 'Show post author' ); ?>
421 </label>
422 </p>
423 <p>
424 <label for="<?php echo $this->get_field_id("footer_link"); ?>">
425 <?php _e( 'Footer link text' ); ?>:
426 <input class="widefat" style="width:60%;" placeholder="... more by this topic" 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"]); ?>" />
427 </label>
428 </p>
429 <p>
430 <label for="<?php echo $this->get_field_id("disable_css"); ?>">
431 <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 ); ?> />
432 <?php _e( 'Disable widget CSS' ); ?>
433 </label>
434 </p>
435 <p>
436 <label for="<?php echo $this->get_field_id("hide_if_empty"); ?>">
437 <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 ); ?> />
438 <?php _e( 'Hide if empty' ); ?>
439 </label>
440 </p>
441 <?php
442 }
443 }
444
445 add_action( 'widgets_init', create_function('', 'return register_widget("CategoryPosts");') );
446