/wp-content/plugins/list-category-posts/include/lcp-widget.php

https://github.com/livinglab/openlab · PHP · 148 lines · 120 code · 14 blank · 14 comment · 22 complexity · 34b6988951aeca26d786e3748fea5944 MD5 · raw file

  1. <?php
  2. /**
  3. * List Category Posts sidebar widget.
  4. * @author fernando@picandocodigo.net
  5. */
  6. class ListCategoryPostsWidget extends WP_Widget{
  7. function __construct() {
  8. $opts = array('description' => __('List posts from a specified category','list-category-posts') );
  9. parent::__construct(false, $name = __('List Category Posts','list-category-posts'), $opts);
  10. }
  11. function widget($args, $instance) {
  12. global $post;
  13. extract( $args );
  14. $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
  15. $limit = (is_numeric($instance['limit'])) ? $instance['limit'] : 5;
  16. $orderby = ($instance['orderby']) ? $instance['orderby'] : 'date';
  17. $order = ($instance['order']) ? $instance['order'] : 'desc';
  18. $exclude = ($instance['exclude'] != '') ? $instance['exclude'] : 0;
  19. if($instance['excludeposts'] == 'current')
  20. $excludeposts = $post->ID;
  21. if(!isset($excludeposts))
  22. $excludeposts = ($instance['excludeposts'] != '') ? $instance['excludeposts'] : 0;
  23. $includeposts = isset($instance['includeposts']) ? $instance['includeposts'] : 0;
  24. $offset = (is_numeric($instance['offset'])) ? $instance['offset'] : 0;
  25. $category_id = $instance['categoryid'];
  26. $dateformat = ($instance['dateformat']) ? $instance['dateformat'] : get_option('date_format');
  27. $showdate = ($instance['show_date'] == 'on') ? 'yes' : 'no';
  28. $pagination = ($instance['pagination'] == 'on') ? 'yes' : 'no';
  29. $showmodifieddate = ($instance['show_modified_date'] == 'on') ? 'yes' : 'no';
  30. $showexcerpt = ($instance['show_excerpt'] == 'on') ? 'yes' : 'no';
  31. $excerptsize = (empty($instance['excerpt_size']) ? 55 : $instance['excerpt_size']);
  32. $showauthor = ($instance['show_author'] == 'on') ? 'yes' : 'no';
  33. $showcatlink = ($instance['show_catlink'] == 'on') ? 'yes' : 'no';
  34. $thumbnail = ($instance['thumbnail'] == 'on') ? 'yes' : 'no';
  35. $thumbnail_size = ($instance['thumbnail_size']) ? $instance['thumbnail_size'] : 'thumbnail';
  36. $morelink = empty($instance['morelink']) ? ' ' : $instance['morelink'];
  37. if ( empty( $instance['tags_as_class'] ) ) {
  38. $instance['tags_as_class'] = 'no';
  39. }
  40. $tags_as_class = ($instance['tags_as_class'] == 'yes') ? 'yes' : 'no';
  41. $template = empty($instance['template']) ? 'default' : $instance['template'];
  42. $atts = array(
  43. 'id' => $category_id,
  44. 'orderby' => $orderby,
  45. 'order' => $order,
  46. 'numberposts' => $limit,
  47. 'date' => $showdate,
  48. 'date_modified' => $showmodifieddate,
  49. 'author' => $showauthor,
  50. 'dateformat' => $dateformat,
  51. 'template' => 'default',
  52. 'excerpt' => $showexcerpt,
  53. 'excerpt_size' => $excerptsize,
  54. 'exclude' => $exclude,
  55. 'excludeposts' => $excludeposts,
  56. 'includeposts' => $includeposts,
  57. 'offset' => $offset,
  58. 'catlink' => $showcatlink,
  59. 'thumbnail' => $thumbnail,
  60. 'thumbnail_size' => $thumbnail_size,
  61. 'morelink' => $morelink,
  62. 'tags_as_class' => $tags_as_class,
  63. 'template' => $template,
  64. 'pagination_next' => '>>',
  65. 'pagination_prev' => '<<',
  66. 'pagination' => $pagination,
  67. 'instance' => $this->id
  68. );
  69. $atts = array_merge(ListCategoryPosts::default_params(), $atts);
  70. echo $before_widget;
  71. if ($pagination === 'yes') lcp_pagination_css();
  72. // To make the widget title replacement work with "Current category" we need to
  73. // run the displayer here to determine the current cat id.
  74. // Otherwise the id remains set to "-1".
  75. $catlist_displayer = new CatListDisplayer($atts);
  76. $lcp_display = $catlist_displayer->display();
  77. // Fetch the category id from the Catlist instance.
  78. $category_id = $catlist_displayer->catlist->get_category_id();
  79. if ((is_null($category_id) || [0] === $category_id ) &&
  80. ($title == 'catlink' || $title == 'catname')) {
  81. $title = '';
  82. } elseif ($title == 'catlink') {
  83. // If the user has setup 'catlink' as the title, replace it with
  84. // the category link:
  85. $lcp_category = get_category($category_id);
  86. $title = '<a href="' . get_category_link($lcp_category->cat_ID) . '">' .
  87. $lcp_category->name . '</a>';
  88. } elseif ($title == 'catname') {
  89. // If the user has setup 'catname' as the title, replace it with
  90. // the category link:
  91. $lcp_category = get_category($category_id);
  92. $title = $lcp_category->name;
  93. }
  94. echo $before_title . $title . $after_title;
  95. echo $lcp_display;
  96. echo $after_widget;
  97. }
  98. /** @see WP_Widget::update */
  99. function update($new_instance, $old_instance) {
  100. $instance = $old_instance;
  101. $instance['title'] = strip_tags($new_instance['title']);
  102. $instance['limit'] = strip_tags($new_instance['limit']);
  103. $instance['orderby'] = strip_tags($new_instance['orderby']);
  104. $instance['order'] = strip_tags($new_instance['order']);
  105. $instance['exclude'] = strip_tags($new_instance['exclude']);
  106. $instance['excludeposts'] = strip_tags($new_instance['excludeposts']);
  107. $instance['includeposts'] = strip_tags($new_instance['includeposts']);
  108. $instance['offset'] = strip_tags($new_instance['offset']);
  109. $instance['categoryid'] = strip_tags($new_instance['categoryid']);
  110. $instance['dateformat'] = strip_tags($new_instance['dateformat']);
  111. $instance['show_date'] = strip_tags($new_instance['show_date']);
  112. $instance['show_modified_date'] = strip_tags($new_instance['show_modified_date']);
  113. $instance['show_excerpt'] = strip_tags($new_instance['show_excerpt']);
  114. $instance['excerpt_size'] = strip_tags($new_instance['excerpt_size']);
  115. $instance['show_author'] = strip_tags($new_instance['show_author']);
  116. $instance['show_catlink'] = strip_tags($new_instance['show_catlink']);
  117. $instance['show_catlink'] = strip_tags($new_instance['show_catlink']);
  118. $instance['thumbnail'] = strip_tags($new_instance['thumbnail']);
  119. $instance['thumbnail_size'] = strip_tags($new_instance['thumbnail_size']);
  120. $instance['morelink'] = strip_tags($new_instance['morelink']);
  121. $instance['tags_as_class'] = strip_tags($new_instance['tags_as_class']);
  122. $instance['template'] = strip_tags($new_instance['template']);
  123. $instance['pagination'] = strip_tags($new_instance['pagination']);
  124. return $instance;
  125. }
  126. /** @see WP_Widget::form */
  127. function form($instance) {
  128. include('lcp-widget-form.php');
  129. }
  130. }
  131. function lcp_register_widget() {
  132. return register_widget("listCategoryPostsWidget");
  133. }
  134. add_action('widgets_init', 'lcp_register_widget');