bradtrivers

better-recent-posts-widget-pro.php with Default Thumbnail

Feb 16th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.28 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Better Recent Posts Widget Pro
  4. Plugin URI: http://pippinsplugins.com/better-recent-posts-widget
  5. Description: Provides a better recent posts widget, including thumbnails, taxonomy, post type, and number options
  6. Version: B2.0.8
  7. Author: Pippin Williamson - Brad Trivers added in default Thumbnail image functionality and counter to ensure number of posts limited
  8. Author URI: http://pippinsplugins.com   (http://sunriseweb.ca)
  9. */
  10.  
  11. /*******************************************
  12. * plugin text domain for translations
  13. *******************************************/
  14.  
  15. load_plugin_textdomain( 'better-recent-posts-pro', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
  16.  
  17. /**
  18.  * Recent Posts Widget Class
  19.  */
  20. class brpwp_wrapper extends WP_Widget {
  21.  
  22.  
  23.     /** constructor */
  24.     function brpwp_wrapper() {
  25.         parent::WP_Widget(false, $name = __('Better Recent Posts Pro', 'better-recent-posts-pro'));
  26.     }
  27.  
  28.     /** @see WP_Widget::widget */
  29.     function widget($args, $instance) {
  30.         extract( $args );
  31.         $title          = apply_filters('widget_title', $instance['title']);
  32.         $post_title     = strip_tags($instance['post_title']);
  33.         $author         = strip_tags($instance['author']);
  34.         $time           = strip_tags($instance['time']);
  35.         $comments       = strip_tags($instance['comments']);
  36.         $excerpt        = strip_tags($instance['excerpt']);
  37.         $excerpt_length = strip_tags($instance['excerpt_length']);
  38.         $order          = strip_tags($instance['order']);
  39.         $sortby         = strip_tags($instance['sortby']);
  40.         $number         = intval(strip_tags($instance['number']));
  41.         $offset         = strip_tags($instance['offset']);
  42.         $cat            = strip_tags($instance['cat']);
  43.         $cat_ids        = strip_tags($instance['cat_ids']);
  44.         $thumbnail_size = strip_tags($instance['thumbnail_size']);
  45.         $thumbnail      = $instance['thumbnail'];
  46.         $posttype       = $instance['posttype'];
  47.         $defaultThumbnail = $instance['defaultThumbnail'];
  48.         ?>
  49.               <?php echo $before_widget; ?>
  50.                   <?php if ( $title )
  51.                         echo $before_title . $title . $after_title; ?>
  52.                             <ul class="no-bullets">
  53.                             <?php
  54.                                 global $post;
  55.                                 $tmp_post = $post;
  56.                                 $args = array( 'numberposts' => $number, 'offset'=> $offset, 'post_type' => $posttype, 'orderby' => $sortby, 'order' => $order );
  57.                                 if($cat != 'All') {
  58.                                     $cats = explode(',', $cat_ids);
  59.                                     if(is_numeric($cats[0])) { $field = 'id'; } else { $field = 'slug'; }
  60.                                     $args['tax_query'] = array(
  61.                                         array(
  62.                                             'taxonomy'  => $cat,
  63.                                             'field'     => $field,
  64.                                             'terms'     => $cats,
  65.                                             'operator'  => 'IN'
  66.                                         )
  67.                                     );
  68.                                 }
  69.                                 $myposts = get_posts( $args );
  70.                                 $postCount = 0;
  71.                                 foreach( $myposts as $post ) : setup_postdata($post);
  72.                                   $postCount++;
  73.                                   if($postCount > $number) { break; }
  74.                                     ?><?php $post_class = get_post_class('', $post); $post_class = implode(' ', $post_class); ?>
  75.                                     <li <?php if(!empty($thumbnail_size) && (has_post_thumbnail() || $defaultThumbnail != "") ) { $size = $thumbnail_size + 8; echo 'style="min-height: ' . $size . 'px; height: auto!important; height: 100%;"'; } ?> id="brpwp_<?php echo $post->ID; ?>" class="<?php echo $post_class; ?>">
  76.                                         <?php if($thumbnail == true) { ?>
  77.                                           <?php
  78.                         if(has_post_thumbnail()) {
  79.                           ?><a href="<?php the_permalink(); ?>" style="float: left; margin: 0 5px 0 0;" class="brpwp-thumb-link"><?php the_post_thumbnail(array($thumbnail_size),array('title'=>$post->post_title, 'alt'=>$post->post_title)); ?></a> <?php
  80.                         } else {
  81.                           ?><a href="<?php the_permalink(); ?>" style="float: left; margin: 0 5px 0 0;" class="brpwp-thumb-link">
  82.                               <img width="<?php echo $thumbnail_size; ?>" height="<?php echo $thumbnail_size; ?>" src="<?php echo $defaultThumbnail; ?>" class="attachment-56 wp-post-image" alt="<?php echo $post->post_title; ?>" title="<?php echo $post->post_title; ?>">
  83.                              </a><?php
  84.                         }
  85.                       ?>
  86.                                            
  87.                                         <?php } ?>
  88.                                         <?php if($post_title == true) { ?>
  89.                                             <span class="brpwp-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
  90.                                         <?php } ?>
  91.                                         <?php if($comments == true) { ?> (<a href="<?php comments_link(); ?>" title="<?php _e('View comments on:', 'better-recent-posts-pro'); echo ' ' . $post->post_title; ?>"><?php comments_number('0', '1', '%'); ?></a>)<?php } ?>
  92.                                         <?php if($author == true) { _e('by' , 'better-recent-posts-pro'); echo ' '; the_author(); ?> - <?php } ?>
  93.                                         <?php if($time == true) { ?>
  94.                                             <span class="time"><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ' . __('ago', 'better-recent-posts-pro'); ?></span>
  95.                                         <?php } ?>
  96.                                         <?php if($excerpt == true) {
  97.                                             $content_excerpt = $post->post_excerpt;
  98.                                             if($content_excerpt == '') {
  99.                                                 $content_excerpt = $post->post_content;
  100.                                             }
  101.                                             $content_excerpt = strip_shortcodes(strip_tags(stripslashes($content_excerpt), '<a><em><strong>'));
  102.                                             if (!isset($excerpt_length)) { $excerpt_length = 10; }
  103.                                             $content_excerpt = preg_split('/\b/', $content_excerpt, $excerpt_length*2+1);
  104.                                             $body_excerpt_waste = array_pop($content_excerpt);
  105.                                             $content_excerpt = implode($content_excerpt);
  106.                                             $content_excerpt .= '...';
  107.                                             echo wpautop($content_excerpt);
  108.                                            
  109.                                         } ?>
  110.                                        
  111.                                     </li>
  112.                                 <?php endforeach; ?>
  113.                                 <?php $post = $tmp_post; ?>
  114.                             </ul>
  115.               <?php echo $after_widget; ?>
  116.         <?php
  117.     }
  118.  
  119.     /** @see WP_Widget::update */
  120.     function update($new_instance, $old_instance) {    
  121.         $instance = $old_instance;
  122.         $instance['title']          = strip_tags($new_instance['title']);
  123.         $instance['post_title']     = strip_tags($new_instance['post_title']);
  124.         $instance['author']         = strip_tags($new_instance['author']);
  125.         $instance['time']           = strip_tags($new_instance['time']);
  126.         $instance['comments']       = strip_tags($new_instance['comments']);
  127.         $instance['excerpt']        = strip_tags($new_instance['excerpt']);
  128.         $instance['excerpt_length'] = strip_tags($new_instance['excerpt_length']);
  129.         $instance['order']          = strip_tags($new_instance['order']);
  130.         $instance['sortby']         = strip_tags($new_instance['sortby']);
  131.         $instance['number']         = strip_tags($new_instance['number']);
  132.         $instance['offset']         = strip_tags($new_instance['offset']);
  133.         $instance['cat']            = strip_tags($new_instance['cat']);
  134.         $instance['cat_ids']        = strip_tags($new_instance['cat_ids']);
  135.         $instance['thumbnail_size'] = strip_tags($new_instance['thumbnail_size']);
  136.         $instance['thumbnail']      = $new_instance['thumbnail'];
  137.         $instance['posttype']       = $new_instance['posttype'];
  138.         $instance['defaultThumbnail']       = $new_instance['defaultThumbnail'];
  139.         return $instance;
  140.     }
  141.  
  142.     /** @see WP_Widget::form */
  143.     function form($instance) { 
  144.  
  145.         $posttypes = get_post_types('', 'objects');
  146.    
  147.         $title          = esc_attr($instance['title']);
  148.         $post_title     = esc_attr($instance['post_title']);
  149.         $author         = esc_attr($instance['author']);
  150.         $time           = esc_attr($instance['time']);
  151.         $excerpt        = esc_attr($instance['excerpt']);
  152.         $excerpt_length = esc_attr($instance['excerpt_length']);
  153.         $comments       = esc_attr($instance['comments']);
  154.         $order          = esc_attr($instance['order']);
  155.         $sortby         = esc_attr($instance['sortby']);
  156.         $number         = esc_attr($instance['number']);
  157.         $offset         = esc_attr($instance['offset']);
  158.         $cat            = esc_attr($instance['cat']);
  159.         $cat_ids        = esc_attr($instance['cat_ids']);
  160.         $thumbnail_size = esc_attr($instance['thumbnail_size']);
  161.         $thumbnail      = esc_attr($instance['thumbnail']);
  162.             $posttype       = esc_attr($instance['posttype']);
  163.             $defaultThumbnail = esc_attr($instance['defaultThumbnail']);
  164.         ?>
  165.  
  166.         <p>
  167.           <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:', 'better-recent-posts-pro'); ?></label>
  168.           <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; ?>" />
  169.         </p>
  170.         <p>
  171.           <input id="<?php echo $this->get_field_id('post_title'); ?>" name="<?php echo $this->get_field_name('post_title'); ?>" type="checkbox" value="1" <?php checked( '1', $post_title ); ?>/>
  172.           <label for="<?php echo $this->get_field_id('post_title'); ?>"><?php _e('Display Post Title?', 'better-recent-posts-pro'); ?></label>
  173.         </p>
  174.         <p>
  175.           <input id="<?php echo $this->get_field_id('author'); ?>" name="<?php echo $this->get_field_name('author'); ?>" type="checkbox" value="1" <?php checked( '1', $author ); ?>/>
  176.           <label for="<?php echo $this->get_field_id('author'); ?>"><?php _e('Display Post Author?', 'better-recent-posts-pro'); ?></label>
  177.         </p>
  178.         <p>
  179.           <input id="<?php echo $this->get_field_id('time'); ?>" name="<?php echo $this->get_field_name('time'); ?>" type="checkbox" value="1" <?php checked( '1', $time ); ?>/>
  180.           <label for="<?php echo $this->get_field_id('time'); ?>"><?php _e('Display Post Date?', 'better-recent-posts-pro'); ?></label>
  181.         </p>
  182.         <p>
  183.           <input id="<?php echo $this->get_field_id('comments'); ?>" name="<?php echo $this->get_field_name('comments'); ?>" type="checkbox" value="1" <?php checked( '1', $comments ); ?>/>
  184.           <label for="<?php echo $this->get_field_id('comments'); ?>"><?php _e('Display Comment Count?', 'better-recent-posts-pro'); ?></label>
  185.         </p>
  186.         <p>
  187.           <input id="<?php echo $this->get_field_id('excerpt'); ?>" name="<?php echo $this->get_field_name('excerpt'); ?>" type="checkbox" value="1" <?php checked( '1', $excerpt ); ?>/>
  188.           <label for="<?php echo $this->get_field_id('excerpt'); ?>"><?php _e('Display Post Excerpt?', 'better-recent-posts-pro'); ?></label>
  189.         </p>
  190.         <p>
  191.           <label for="<?php echo $this->get_field_id('excerpt_length'); ?>"><?php _e('Excerpt length in words', 'better-recent-posts-pro'); ?></label>
  192.           <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; ?>" />
  193.         </p>
  194.         <p>
  195.             <label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Choose the Order to Display Posts', 'better-recent-posts-pro'); ?></label>
  196.             <select name="<?php echo $this->get_field_name('order'); ?>" id="<?php echo $this->get_field_id('order'); ?>" class="widefat">
  197.                 <?php
  198.                 $orders = array('ASC', 'DESC');
  199.                 foreach ($orders as $post_order) {
  200.                     echo '<option value="' . $post_order . '" id="' . $post_order . '"', $order == $post_order ? ' selected="selected"' : '', '>', $post_order, '</option>';
  201.                 }
  202.                 ?>
  203.             </select>  
  204.         </p>
  205.         <p>
  206.             <label for="<?php echo $this->get_field_id('sortby'); ?>"><?php _e('Choose the Post Sorting Method', 'better-recent-posts-pro'); ?></label>
  207.             <select name="<?php echo $this->get_field_name('sortby'); ?>" id="<?php echo $this->get_field_id('sortby'); ?>" class="widefat">
  208.                 <?php
  209.                 $sort_options = array('title', 'post_date', 'rand');
  210.                 foreach ($sort_options as $sort) {
  211.                     echo '<option id="' . $sort . '"', $sortby == $sort ? ' selected="selected"' : '', '>', $sort, '</option>';
  212.                 }
  213.                 ?>
  214.             </select>  
  215.         </p>
  216.         <p>
  217.           <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number to Show:', 'better-recent-posts-pro'); ?></label>
  218.           <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
  219.         </p>
  220.         <p>
  221.           <label for="<?php echo $this->get_field_id('offset'); ?>"><?php _e('Offset (the number of posts to skip):', 'better-recent-posts-pro'); ?></label>
  222.           <input class="widefat" id="<?php echo $this->get_field_id('offset'); ?>" name="<?php echo $this->get_field_name('offset'); ?>" type="text" value="<?php echo $offset; ?>" />
  223.         </p>
  224.         <p>
  225.           <input id="<?php echo $this->get_field_id('thumbnail'); ?>" name="<?php echo $this->get_field_name('thumbnail'); ?>" type="checkbox" value="1" <?php checked( '1', $thumbnail ); ?> class="extra-options-thumb"/>
  226.           <label for="<?php echo $this->get_field_id('thumbnail'); ?>"><?php _e('Display Thumbnails?', 'better-recent-posts-pro'); ?></label>
  227.         </p>
  228.     <p>
  229.           <label for="<?php echo $this->get_field_id('defaultThumbnail'); ?>"><?php _e('Default Thumbnail Image URL', 'better-recent-posts-pro'); ?></label>
  230.           <input class="widefat" id="<?php echo $this->get_field_id('defaultThumbnail'); ?>" name="<?php echo $this->get_field_name('defaultThumbnail'); ?>" type="text" value="<?php echo $defaultThumbnail; ?>" />
  231.         </p>
  232.         <p>
  233.           <label for="<?php echo $this->get_field_id('thumbnail_size'); ?>" class="disabled-thumb-fields" ><?php _e('Size of the thumbnails, e.g.', 'better-recent-posts-pro'); ?> <em>80</em> = 80px x 80px</label>
  234.           <input class="widefat disabled-thumb-fields" id="<?php echo $this->get_field_id('thumbnail_size'); ?>" name="<?php echo $this->get_field_name('thumbnail_size'); ?>" type="text" value="<?php echo $thumbnail_size; ?>"  />
  235.         </p>
  236.         <p>
  237.             <label for="<?php echo $this->get_field_id('cat'); ?>"><?php _e('Choose the Taxonomy to Display Posts From', 'better-recent-posts-pro'); ?></label>
  238.             <select name="<?php echo $this->get_field_name('cat'); ?>" id="<?php echo $this->get_field_id('cat'); ?>" class="widefat extra-options-select">
  239.                 <?php
  240.                 $cats = get_taxonomies('','names');
  241.                 echo '<option id="cats-all"', $cat == 'cats-all' ? ' selected="selected"' : '', '>All</option>';
  242.                 foreach ($cats as $tax) {
  243.                     echo '<option id="' . $tax . '"', $cat == $tax ? ' selected="selected"' : '', '>', $tax, '</option>';
  244.                 }
  245.                 ?>
  246.             </select>  
  247.         </p>
  248.         <p>
  249.           <label for="<?php echo $this->get_field_id('cat_ids'); ?>" class="disabled-select-fields" ><?php _e('Taxonomy Term IDs to Display Posts From', 'better-recent-posts-pro'); ?></label>
  250.           <input class="widefat disabled-select-fields" id="<?php echo $this->get_field_id('cat_ids'); ?>" name="<?php echo $this->get_field_name('cat_ids'); ?>" type="text" value="<?php echo $cat_ids; ?>" />
  251.         </p>
  252.         <p>
  253.             <label for="<?php echo $this->get_field_id('posttype'); ?>"><?php _e('Choose the Post Type to Display', 'better-recent-posts-pro'); ?></label>
  254.             <select name="<?php echo $this->get_field_name('posttype'); ?>" id="<?php echo $this->get_field_id('posttype'); ?>" class="widefat">
  255.                 <?php
  256.                 foreach ($posttypes as $option) {
  257.                     echo '<option id="' . $option->name . '"', $posttype == $option->name ? ' selected="selected"' : '', '>', $option->name, '</option>';
  258.                 }
  259.                 ?>
  260.             </select>      
  261.         </p>
  262.         <?php
  263.     }
  264.  
  265.  
  266. } // class brpwp_wrapper
  267. // register Recent Posts widget
  268. add_action('widgets_init', create_function('', 'return register_widget("brpwp_wrapper");'));
  269.  
  270. ?>
Advertisement
Add Comment
Please, Sign In to add comment