/wp-content/themes/paradise/Paradise/widgets/widget-recent.php

https://gitlab.com/billyprice1/website · PHP · 175 lines · 149 code · 23 blank · 3 comment · 8 complexity · 0c787f7f80fb4d9a6251645699907a4d MD5 · raw file

  1. <?php class RecentPostsWidget extends WP_Widget
  2. {
  3. var $excertp_length;
  4. function RecentPostsWidget(){
  5. $widget_opts = array('classname' => 'widget_recent_posts', 'description' => __('The most recent posts on your site with thumbnails', TEMPLATENAME));
  6. parent::WP_Widget(false, __('Recent Posts +', TEMPLATENAME), $widget_opts);
  7. }
  8. /* Displays the Widget in the front-end */
  9. function widget($args, $instance){
  10. extract($args);
  11. $title = strip_tags($instance['title']);
  12. $posts_count = $instance['posts_count'];
  13. $category = $instance['category'];
  14. $show_thumbnail = $instance['show_thumbnail'];
  15. $thumb_size = $instance['thumb_size'];
  16. $show_excerpt = $instance['show_excerpt'];
  17. $excerpt_length = $instance['excerpt_length'];
  18. $show_date = $instance['show_date'];
  19. echo $before_widget;
  20. if ( $title )
  21. echo $before_title . $title . $after_title;
  22. $loop_options = array(
  23. 'posts_per_page' => $posts_count,
  24. 'post_type' => 'post',
  25. 'orderby' => 'post_date',
  26. 'order' => 'DESC'
  27. );
  28. if (!empty($category)) {
  29. $loop_options['cat'] = $category;
  30. }
  31. if ($show_excerpt) {
  32. $this->excertp_length = $excerpt_length;
  33. }
  34. $loop = new WP_Query($loop_options);
  35. if($loop->have_posts()) :
  36. add_filter('excerpt_length', array(&$this, 'recent_excerpt_length'));
  37. ?>
  38. <ul>
  39. <?php while($loop->have_posts()) :
  40. $loop->the_post(); ?>
  41. <li>
  42. <a href="<?php the_permalink() ?>">
  43. <?php if ($show_thumbnail) the_post_thumbnail($thumb_size, array('class' => 'pic alignleft', 'title' => false)); ?>
  44. <h4><?php the_title(); ?></h4>
  45. <?php if ($show_excerpt): ?><div class="short"><?php echo get_the_excerpt(); ?></div><?php endif; ?>
  46. <?php if ($show_date): ?><span class="date"><?php echo get_the_date(); ?></span><?php endif; ?>
  47. </a>
  48. </li>
  49. <?php endwhile; ?>
  50. </ul>
  51. <?php endif;
  52. remove_filter('excerpt_length', array(&$this, 'recent_excerpt_length'));
  53. echo $after_widget;
  54. }
  55. function recent_excerpt_length($length) {
  56. return $this->excertp_length;
  57. }
  58. /*Saves the settings. */
  59. function update($new_instance, $old_instance){
  60. $instance = $old_instance;
  61. $instance['title'] = stripslashes($new_instance['title']);
  62. $instance['posts_count'] = (int) $new_instance['posts_count'];
  63. $categories = get_terms('category');
  64. foreach($categories as &$category) {
  65. $category = $category->term_id;
  66. }
  67. $instance['category'] = (in_array($new_instance['category'], $categories)) ? $new_instance['category'] : '';
  68. $instance['show_thumbnail'] = (bool) $new_instance['show_thumbnail'];
  69. $instance['thumb_size'] = (array_key_exists($new_instance['thumb_size'], $this->get_sizes())) ? $new_instance['thumb_size'] : '';
  70. $instance['show_excerpt'] = (bool) $new_instance['show_excerpt'];
  71. $instance['excerpt_length'] = (int) $new_instance['excerpt_length'];
  72. $instance['show_date'] = (bool) $new_instance['show_date'];
  73. return $instance;
  74. }
  75. /*Creates the form for the widget in the back-end. */
  76. function form($instance){
  77. $instance = wp_parse_args((array) $instance, array(
  78. 'title' => __('Recent Posts', TEMPLATENAME),
  79. 'posts_count' => 3,
  80. 'show_thumbnail' => true,
  81. 'thumb_size' => 'thumbnail',
  82. 'show_excerpt' => true,
  83. 'excerpt_length' => 10,
  84. 'show_date' => true,
  85. ));
  86. $title = strip_tags($instance['title']);
  87. $posts_count = $instance['posts_count'];
  88. $category = $instance['category'];
  89. $show_thumbnail = $instance['show_thumbnail'];
  90. $thumb_size = $instance['thumb_size'];
  91. $show_excerpt = $instance['show_excerpt'];
  92. $excerpt_length = $instance['excerpt_length'];
  93. $show_date = $instance['show_date'];
  94. $cats = get_terms('category');
  95. $sizes = $this->get_sizes();
  96. ?>
  97. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', TEMPLATENAME) ?></label><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; ?>" /></p>
  98. <p><label for="<?php echo $this->get_field_id('posts_count'); ?>"><?php _e('Number of posts to show:', TEMPLATENAME) ?></label><input class="widefat" id="<?php echo $this->get_field_id('posts_count'); ?>" name="<?php echo $this->get_field_name('posts_count'); ?>" type="text" value="<?php echo $posts_count; ?>" /></p>
  99. <p>
  100. <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Select posts category:', TEMPLATENAME); ?></label>
  101. <select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
  102. <option value=""><?php _e('All Categolries', TEMPLATENAME); ?></option>
  103. <?php
  104. foreach ($cats as $cat) {
  105. echo '<option value="' . intval($cat->term_id) . '"'
  106. . ($cat->term_id == $instance['category'] ? ' selected="selected"' : '')
  107. . '>' . $cat->name . "</option>\n";
  108. }
  109. ?>
  110. </select></p>
  111. <p>
  112. <input id="<?php echo $this->get_field_id('show_thumbnail'); ?>" name="<?php echo $this->get_field_name('show_thumbnail'); ?>" type="checkbox" <?php checked($show_thumbnail); ?> />&nbsp;<label for="<?php echo $this->get_field_id('show_thumbnail'); ?>"><?php _e('Show post thumbnail?', TEMPLATENAME); ?></label>
  113. </p>
  114. <p>
  115. <label for="<?php echo $this->get_field_id('thumb_size'); ?>"><?php _e('Select thumbnail size:', TEMPLATENAME); ?></label>
  116. <select class="widefat" id="<?php echo $this->get_field_id('thumb_size'); ?>" name="<?php echo $this->get_field_name('thumb_size'); ?>">
  117. <?php
  118. foreach ( $sizes as $key => $val ) {
  119. echo '<option value="' . $key . '"'
  120. . selected($instance['thumb_size'], $key)
  121. . '>' . $val . "</option>\n";
  122. }
  123. ?>
  124. </select></p>
  125. <p>
  126. <input id="<?php echo $this->get_field_id('show_excerpt'); ?>" name="<?php echo $this->get_field_name('show_excerpt'); ?>" type="checkbox" <?php checked($show_excerpt); ?> />&nbsp;<label for="<?php echo $this->get_field_id('show_excerpt'); ?>"><?php _e('Show post excerpt?', TEMPLATENAME); ?></label>
  127. </p>
  128. <p><label for="<?php echo $this->get_field_id('excerpt_length'); ?>"><?php _e('Excerpt length:', TEMPLATENAME) ?></label><input class="widefat" id="<?php echo $this->get_field_id('excerpt_length'); ?>" name="<?php echo $this->get_field_name('excerpt_length'); ?>" type="text" value="<?php echo $excerpt_length; ?>" /></p>
  129. <p>
  130. <input id="<?php echo $this->get_field_id('show_date'); ?>" name="<?php echo $this->get_field_name('show_date'); ?>" type="checkbox" <?php checked($show_date); ?> />&nbsp;<label for="<?php echo $this->get_field_id('show_date'); ?>"><?php _e('Show post date?', TEMPLATENAME); ?></label>
  131. </p>
  132. <?php
  133. }
  134. function get_sizes() {
  135. global $_wp_additional_image_sizes;
  136. $sizes = array();
  137. $default_sizes = array('large', 'medium', 'thumbnail');
  138. foreach($default_sizes as $size) {
  139. $sizes[$size] = array('width' => get_option("{$size}_size_w"), 'height' => get_option("{$size}_size_h"));
  140. }
  141. $sizes = array_merge($sizes, $_wp_additional_image_sizes);
  142. foreach($sizes as $name => &$data) {
  143. $data = "$name ({$data['width']}x{$data['height']})";
  144. }
  145. return $sizes;
  146. }
  147. }// end RecentPostsWidget class
  148. function RecentPostsWidgetInit() {
  149. register_widget('RecentPostsWidget');
  150. }
  151. add_action('widgets_init', 'RecentPostsWidgetInit');
  152. ?>