Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.51 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Category Posts Sidebar Widget
  4. Plugin URI: http://www.junaidiqbal.net
  5. Version: 1.0
  6. Description: This plugin provides site admin to use this widget on sidebars. The widget has options to set the parameters for multiple categories selection, number of posts, coomments count, sorting, widget class, title class, show date and featured image thumbnails.
  7. Author: Junaid Iqbal
  8. Author UI: http://www.junaidiqbal.net
  9. */
  10.  
  11.  
  12. class CategoryPostsSidebarWidget extends WP_Widget {
  13.  
  14. function CategoryPostsSidebarWidget() {
  15. $widget_desc = array(
  16. 'classname' => 'catgory_posts_sidebar_widget',
  17. 'description' => __( "The widget has options to set the parameters for multiple categories selection, number of posts, coomments count, sorting, widget class, title class, show date and featured image thumbnails" )
  18. );
  19. parent::WP_Widget(false, $name='Category Posts Sidebar Widget', $widget_desc);
  20. }
  21.  
  22.  
  23. // Shows category posts widget with multiple options on website sidebar
  24. function widget($args, $instance) {
  25. global $post;
  26. $post_pre = $post; // Save the post object.
  27.  
  28. extract( $args );
  29.  
  30. // post featured image sizes using add_image_size method
  31. if ( function_exists('add_image_size') )
  32. {
  33. $sizes = get_option('featured_image_thumb_sizes');
  34. if ( $sizes )
  35. {
  36. foreach ( $sizes as $id=>$size )
  37. add_image_size( 'featured_image_thumb_size' . $id, $size[0], 0, true );
  38. }
  39. }
  40.  
  41. $valid_sort_orders = array('date', 'title', 'comment_count', 'rand');
  42. if ( in_array($instance['sort_by'], $valid_sort_orders) ) {
  43. $sort_by = $instance['sort_by'];
  44. $sort_order = (bool) $instance['asc_sort_order'] ? 'ASC' : 'DESC';
  45. } else {
  46. $sort_by = 'date';
  47. $sort_order = 'DESC';
  48. }
  49.  
  50. // Multiple category array
  51. $cat_arr = @implode(",",$instance["cats"]);
  52.  
  53. // Get array of posts
  54. $cat_posts = new WP_Query(
  55. "showposts=" . $instance["num"] .
  56. "&cat=" . $cat_arr .
  57. "&orderby=" . $sort_by .
  58. "&order=" . $sort_order
  59. );
  60.  
  61.  
  62. // For limit excerpt count
  63. $excerpt_count = $instance["excerpt_count"];
  64. add_filter('excerpt_length',
  65. function($return_count) use ($excerpt_count) {
  66. $return_count = $excerpt_count;
  67. return $return_count;
  68. });
  69.  
  70.  
  71. $excerpt_read_more_text = (!empty($instance["excerpt_read_more_text"]))?$instance["excerpt_read_more_text"]:' ... Continue reading <span class="meta-nav">→</span>';
  72. add_filter('excerpt_more',
  73. function($return_text) use ($excerpt_read_more_text) {
  74. $return_text = $excerpt_read_more_text;
  75. return '<a href="'. get_permalink($post->ID) . '"> '.$return_text.'</a>';
  76. });
  77.  
  78.  
  79. echo $before_widget;
  80.  
  81. // Widget title with class
  82. $title_class = (isset($instance["titleclass"]))?$instance["titleclass"]:'widget-title';
  83.  
  84. echo '<'.$instance["titleheading"].' class="'.$title_class.'">' . $instance["title"] . '</'.$instance["titleheading"].'>';
  85.  
  86. // Loop through the posts
  87. echo "<ul class='".$instance["class"]."'>\n";
  88.  
  89. while ( $cat_posts->have_posts() )
  90. {
  91. $cat_posts->the_post();
  92. ?>
  93.  
  94. <li class="cat-post-item"> <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>">
  95. <?php the_title(); ?>
  96. </a><br />
  97. <?php
  98. if (
  99. function_exists('the_post_thumbnail') &&
  100. current_theme_supports("post-thumbnails") &&
  101. $instance["thumb"] &&
  102. has_post_thumbnail()
  103. ) :
  104. ?>
  105. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
  106. <?php the_post_thumbnail( 'featured_image_thumb_size'.$this->id ); ?>
  107. </a>
  108. <?php endif; ?>
  109. <?php if ( $instance['date'] ) : ?>
  110. <p class="post-date">
  111. <?php the_time("j M Y"); ?>
  112. </p>
  113. <?php endif; ?>
  114. <?php if ( $instance['author'] ) : ?>
  115. <p class="post-author">Posted By:
  116. <?php the_author(); ?>
  117. </p>
  118. <?php endif; ?>
  119. <?php if ( $instance['excerpt'] ) : ?>
  120. <?php the_excerpt(); ?>
  121. <?php endif; ?>
  122. <?php if ( $instance['comment_numbers'] ) : ?>
  123. <p class="comment-num">(
  124. <?php comments_number(); ?>
  125. )</p>
  126. <?php endif; ?>
  127. </li>
  128. <?php
  129. }
  130.  
  131. echo "</ul>\n";
  132.  
  133. echo $after_widget;
  134.  
  135. $post = $post_pre;
  136. }
  137.  
  138.  
  139. // update form values
  140. function update($new_instance, $old_instance) {
  141.  
  142. if ( function_exists('the_post_thumbnail') )
  143. {
  144. $sizes = get_option('featured_image_thumb_sizes');
  145. if ( !$sizes ) $sizes = array();
  146. $sizes[$this->id] = array($new_instance['thumb_width']);
  147. update_option('featured_image_thumb_sizes', $sizes);
  148. }
  149.  
  150. return $new_instance;
  151. }
  152.  
  153. // admin form
  154. function form($instance) {
  155. ?>
  156. <p>
  157. <label for="<?php echo $this->get_field_id("title"); ?>">
  158. <?php _e( 'Title' ); ?>
  159. :
  160. <input class="widefat" 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"]); ?>" />
  161. </label>
  162. </p>
  163. <p>
  164. <label>
  165. <?php _e( 'Select Category/s' ); ?>
  166. : </label>
  167. </p>
  168. <div style="max-height:80px; overflow-y:scroll; border:1px solid #cccccc;">
  169. <?php
  170. $categories= get_categories();
  171. foreach ($categories as $cat) {
  172. $option='<input type="checkbox" id="'. $this->get_field_id( 'cats' ) .'[]" name="'. $this->get_field_name( 'cats' ) .'[]"';
  173. if (is_array($instance['cats'])) {
  174. foreach ($instance['cats'] as $cats) {
  175. if($cats==$cat->term_id) {
  176. $option=$option.' checked="checked"';
  177. }
  178. }
  179. }
  180. $option .= ' value="'.$cat->term_id.'" />';
  181. $option .= $cat->cat_name;
  182. $option .= ' ('.$cat->category_count.')';
  183. $option .= '<br />';
  184. echo $option;
  185. }
  186. ?>
  187. </div>
  188. <p>
  189. <label for="<?php echo $this->get_field_id("class"); ?>">
  190. <?php _e('Widget Class'); ?>
  191. :
  192. <input id="<?php echo $this->get_field_id("class"); ?>" name="<?php echo $this->get_field_name("class"); ?>" type="text" value="<?php echo $instance["class"]; ?>" />
  193. </label>
  194. </p>
  195. <p>
  196. <label for="<?php echo $this->get_field_id("titleheading"); ?>">
  197. <?php _e('Widget Title Heading'); ?>
  198. :
  199. <select id="<?php echo $this->get_field_id("titleheading"); ?>" name="<?php echo $this->get_field_name("titleheading"); ?>">
  200. <option value="H1"<?php selected( $instance["titleheading"], "H1" ); ?>>H1</option>
  201. <option value="H2"<?php selected( $instance["titleheading"], "H2" ); ?>>H2</option>
  202. <option value="H3"<?php selected( $instance["titleheading"], "H3" ); ?>>H3</option>
  203. <option value="H4"<?php selected( $instance["titleheading"], "H4" ); ?>>H4</option>
  204. <option value="H5"<?php selected( $instance["titleheading"], "H5" ); ?>>H5</option>
  205. <option value="H6"<?php selected( $instance["titleheading"], "H6" ); ?>>H6</option>
  206. </select>
  207. </label>
  208. </p>
  209. <p>
  210. <label for="<?php echo $this->get_field_id("titleclass"); ?>">
  211. <?php _e('Title Class'); ?>
  212. :
  213. <input id="<?php echo $this->get_field_id("titleclass"); ?>" name="<?php echo $this->get_field_name("titleclass"); ?>" type="text" value="<?php echo $instance["titleclass"]; ?>" placeholder="widget-title (default class)" />
  214. </label>
  215. </p>
  216. <p>
  217. <label for="<?php echo $this->get_field_id("num"); ?>">
  218. <?php _e('Number of posts to show'); ?>
  219. :
  220. <input style="text-align: center; width:30%;" id="<?php echo $this->get_field_id("num"); ?>" name="<?php echo $this->get_field_name("num"); ?>" type="number" value="<?php echo absint($instance["num"]); ?>" size='3' />
  221. </label>
  222. </p>
  223. <p>
  224. <label for="<?php echo $this->get_field_id("excerpt"); ?>">
  225. <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 ); ?> />
  226. <?php _e( 'Show post excerpt' ); ?>
  227. </label>
  228. </p>
  229. <p>
  230. <label for="<?php echo $this->get_field_id("excerpt_count"); ?>">
  231. <?php _e( 'Excerpt Word Count:' ); ?>
  232. </label>
  233. <input style="text-align: center; width:30%;" type="number" id="<?php echo $this->get_field_id("excerpt_count"); ?>" name="<?php echo $this->get_field_name("excerpt_count"); ?>" value="<?php echo $instance["excerpt_count"]; ?>" size="3" />
  234. </p>
  235. <p>
  236. <label for="<?php echo $this->get_field_id("date"); ?>">
  237. <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 ); ?> />
  238. <?php _e( 'Show post date' ); ?>
  239. </label>
  240. </p>
  241. <p>
  242. <label for="<?php echo $this->get_field_id("author"); ?>">
  243. <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 ); ?> />
  244. <?php _e( 'Show post author' ); ?>
  245. </label>
  246. </p>
  247. <p>
  248. <label for="<?php echo $this->get_field_id("comment_numbers"); ?>">
  249. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("comment_numbers"); ?>" name="<?php echo $this->get_field_name("comment_numbers"); ?>"<?php checked( (bool) $instance["comment_numbers"], true ); ?> />
  250. <?php _e( 'Show number of comments' ); ?>
  251. </label>
  252. </p>
  253. <?php if ( function_exists('the_post_thumbnail') && current_theme_supports("post-thumbnails") ) : ?>
  254. <p>
  255. <label for="<?php echo $this->get_field_id("thumb"); ?>">
  256. <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 ); ?> />
  257. <?php _e( 'Show post featured image' ); ?>
  258. </label>
  259. </p>
  260. <p>
  261. <label>
  262. <?php _e('Featured Image dimensions'); ?>
  263. </label>
  264. :<br />
  265. <label for="<?php echo $this->get_field_id("thumb_width"); ?>"> Width:
  266. <input class="widefat" style="width:30%;" type="number" id="<?php echo $this->get_field_id("thumb_width"); ?>" name="<?php echo $this->get_field_name("thumb_width"); ?>" value="<?php echo $instance["thumb_width"]; ?>" />
  267. </label>
  268. </p>
  269. <p>
  270. <label for="<?php echo $this->get_field_id("sort_by"); ?>">
  271. <?php _e('Post sort by'); ?>
  272. :
  273. <select id="<?php echo $this->get_field_id("sort_by"); ?>" name="<?php echo $this->get_field_name("sort_by"); ?>">
  274. <option value="date"<?php selected( $instance["sort_by"], "date" ); ?>>Date</option>
  275. <option value="title"<?php selected( $instance["sort_by"], "title" ); ?>>Title</option>
  276. <option value="comment_count"<?php selected( $instance["sort_by"], "comment_count" ); ?>>Number of comments</option>
  277. <option value="rand"<?php selected( $instance["sort_by"], "rand" ); ?>>Random</option>
  278. </select>
  279. </label>
  280. </p>
  281. <p>
  282. <label for="<?php echo $this->get_field_id("asc_sort_order"); ?>">
  283. <input type="checkbox" class="checkbox"
  284. id="<?php echo $this->get_field_id("asc_sort_order"); ?>"
  285. name="<?php echo $this->get_field_name("asc_sort_order"); ?>"
  286. <?php checked( (bool) $instance["asc_sort_order"], true ); ?> />
  287. <?php _e( 'Post sort order ASC/DESC' ); ?>
  288. </label>
  289. </p>
  290. <p>
  291. <label for="<?php echo $this->get_field_id("excerpt_read_more_text"); ?>">
  292. <?php _e('Replace "Continue Reading" text'); ?>
  293. :
  294. <input id="<?php echo $this->get_field_id("excerpt_read_more_text"); ?>" name="<?php echo $this->get_field_name("excerpt_read_more_text"); ?>" type="text" value="<?php echo $instance["excerpt_read_more_text"]; ?>" placeholder="...Continue Reading (default text)" class="widefat" />
  295. </label>
  296. </p>
  297. <?php endif; ?>
  298. <?php
  299. }
  300. }
  301. // initialize
  302. add_action( 'widgets_init', create_function('', 'return register_widget("CategoryPostsSidebarWidget");') );
  303.  
  304. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement