Advertisement
marjwyatt

WSK Featured Post Widget

Sep 25th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.55 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Adds the WSK Featured Post Widget.
  4.  *
  5.  * @category Genesis
  6.  * @package  Widgets
  7.  * @author   StudioPress
  8.  * @license  http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
  9.  * @link     http://www.studiopress.com/themes/genesis
  10.  */
  11.  
  12. /**
  13.  * Genesis Featured Post widget class.
  14.  *
  15.  * @category Genesis
  16.  * @package Widgets
  17.  *
  18.  * @since 0.1.8
  19.  */
  20. class WSK_Recent_Post extends WP_Widget {
  21.  
  22.     /**
  23.      * Holds widget settings defaults, populated in constructor.
  24.      *
  25.      * @var array
  26.      */
  27.     protected $defaults;
  28.  
  29.     /**
  30.      * Constructor. Set the default widget options and create widget.
  31.      *
  32.      * @since 0.1.8
  33.      */
  34.     function __construct() {
  35.  
  36.         $this->defaults = array(
  37.             'title'                   => '',
  38.             'posts_cat'               => '',
  39.             'posts_num'               => 1,
  40.             'posts_offset'            => 0,
  41.             'orderby'                 => '',
  42.             'order'                   => '',
  43.             'show_image'              => 0,
  44.             'image_alignment'         => '',
  45.             'image_size'              => '',
  46.             'show_title'              => 0,
  47.             'more_text'               => __( '[Read More...]', 'genesis' ),
  48.             'extra_num'               => '',
  49.             'extra_title'             => '',
  50.             'more_from_category'      => '',
  51.             'more_from_category_text' => __( 'More Posts from this Category', 'genesis' ),
  52.         );
  53.  
  54.         $widget_ops = array(
  55.             'classname'   => 'recentpost',
  56.             'description' => __( 'Displays recent posts with thumbnails', 'genesis' ),
  57.         );
  58.  
  59.         $control_ops = array(
  60.             'id_base' => 'recent-post',
  61.             'width'   => 100,
  62.             'height'  => 100,
  63.         );
  64.  
  65.         $this->WP_Widget( 'recent-post', __( 'WSK - Recent Posts', 'genesis' ), $widget_ops, $control_ops );
  66.  
  67.     }
  68.  
  69.     /**
  70.      * Echo the widget content.
  71.      *
  72.      * @since 0.1.8
  73.      *
  74.      * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
  75.      * @param array $instance The settings for the particular instance of the widget
  76.      */
  77.     function widget( $args, $instance ) {
  78.  
  79.         extract( $args );
  80.  
  81.         /** Merge with defaults */
  82.         $instance = wp_parse_args( (array) $instance, $this->defaults );
  83.  
  84.         echo $before_widget;
  85.  
  86.         /** Set up the author bio */
  87.         if ( ! empty( $instance['title'] ) )
  88.             echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
  89.  
  90.         $query_args = array(
  91.             'post_type' => 'post',
  92.             'cat'       => $instance['posts_cat'],
  93.             'showposts' => $instance['posts_num'],
  94.             'offset'    => $instance['posts_offset'],
  95.             'orderby'   => $instance['orderby'],
  96.             'order'     => $instance['order'],
  97.         );
  98.  
  99.         $featured_posts = new WP_Query( $query_args );
  100.  
  101.         if ( $featured_posts->have_posts() ) : while ( $featured_posts->have_posts() ) : $featured_posts->the_post();
  102.             echo '<div class="' . implode( ' ', get_post_class() ) . '">';
  103.  
  104.             if ( ! empty( $instance['show_image'] ) )
  105.                 printf(
  106.                     '<a href="%s" title="%s" class="%s">%s</a>',
  107.                     get_permalink(),
  108.                     the_title_attribute( 'echo=0' ),
  109.                     esc_attr( $instance['image_alignment'] ),
  110.                     genesis_get_image( array( 'format' => 'html', 'size' => $instance['image_size'], ) )
  111.                 );
  112.  
  113.             if ( ! empty( $instance['show_title'] ) )
  114.                 printf( '<h4><a href="%s" title="%s">%s</a></h4>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );
  115.  
  116.             echo '</div><!--end post_class()-->'."\n\n";
  117.  
  118.         endwhile; endif;
  119.  
  120.         /** The EXTRA Posts (list) */
  121.         if ( ! empty( $instance['extra_num'] ) ) {
  122.             if ( ! empty( $instance['extra_title'] ) )
  123.                 echo $before_title . esc_html( $instance['extra_title'] ) . $after_title;
  124.  
  125.             $offset = intval( $instance['posts_num'] ) + intval( $instance['posts_offset'] );
  126.  
  127.             $query_args = array(
  128.                 'cat'       => $instance['posts_cat'],
  129.                 'showposts' => $instance['extra_num'],
  130.                 'offset'    => $offset,
  131.             );
  132.             $extra_posts = new WP_Query( $query_args );
  133.  
  134.             $listitems = '';
  135.  
  136.             if ( $extra_posts->have_posts() ) {
  137.                 while ( $extra_posts->have_posts() ) {
  138.                     $extra_posts->the_post();
  139.                     $listitems .= sprintf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );
  140.                 }
  141.  
  142.                 if ( strlen( $listitems ) > 0 )
  143.                     printf( '<ul>%s</ul>', $listitems );
  144.             }
  145.         }
  146.  
  147.         if ( ! empty( $instance['more_from_category'] ) && ! empty( $instance['posts_cat'] ) )
  148.             printf(
  149.                 '<p class="more-from-category"><a href="%1$s" title="%2$s">%3$s</a></p>',
  150.                 esc_url( get_category_link( $instance['posts_cat'] ) ),
  151.                 esc_attr( get_cat_name( $instance['posts_cat'] ) ),
  152.                 esc_html( $instance['more_from_category_text'] )
  153.             );
  154.  
  155.         echo $after_widget;
  156.         wp_reset_query();
  157.  
  158.     }
  159.  
  160.     /**
  161.      * Update a particular instance.
  162.      *
  163.      * This function should check that $new_instance is set correctly.
  164.      * The newly calculated value of $instance should be returned.
  165.      * If "false" is returned, the instance won't be saved/updated.
  166.      *
  167.      * @since 0.1.8
  168.      *
  169.      * @param array $new_instance New settings for this instance as input by the user via form()
  170.      * @param array $old_instance Old settings for this instance
  171.      * @return array Settings to save or bool false to cancel saving
  172.      */
  173.     function update( $new_instance, $old_instance ) {
  174.  
  175.         $new_instance['title']     = strip_tags( $new_instance['title'] );
  176.         $new_instance['more_text'] = strip_tags( $new_instance['more_text'] );
  177.         $new_instance['post_info'] = wp_kses_post( $new_instance['post_info'] );
  178.         return $new_instance;
  179.  
  180.     }
  181.  
  182.     /**
  183.      * Echo the settings update form.
  184.      *
  185.      * @since 0.1.8
  186.      *
  187.      * @param array $instance Current settings
  188.      */
  189.     function form( $instance ) {
  190.  
  191.         /** Merge with defaults */
  192.         $instance = wp_parse_args( (array) $instance, $this->defaults );
  193.  
  194.         ?>
  195.         <p>
  196.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'genesis' ); ?>:</label>
  197.             <input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" />
  198.         </p>
  199.  
  200.         <div class="genesis-widget-column">
  201.  
  202.             <div class="genesis-widget-column-box genesis-widget-column-box-top">
  203.  
  204.                 <p>
  205.                     <label for="<?php echo $this->get_field_id( 'posts_cat' ); ?>"><?php _e( 'Category', 'genesis' ); ?>:</label>
  206.                     <?php
  207.                     $categories_args = array(
  208.                         'name'            => $this->get_field_name( 'posts_cat' ),
  209.                         'selected'        => $instance['posts_cat'],
  210.                         'orderby'         => 'Name',
  211.                         'hierarchical'    => 1,
  212.                         'show_option_all' => __( 'All Categories', 'genesis' ),
  213.                         'hide_empty'      => '0',
  214.                     );
  215.                     wp_dropdown_categories( $categories_args ); ?>
  216.                 </p>
  217.  
  218.                 <p>
  219.                     <label for="<?php echo $this->get_field_id( 'posts_num' ); ?>"><?php _e( 'Number of Posts to Show', 'genesis' ); ?>:</label>
  220.                     <input type="text" id="<?php echo $this->get_field_id( 'posts_num' ); ?>" name="<?php echo $this->get_field_name( 'posts_num' ); ?>" value="<?php echo esc_attr( $instance['posts_num'] ); ?>" size="2" />
  221.                 </p>
  222.  
  223.                 <p>
  224.                     <label for="<?php echo $this->get_field_id( 'posts_offset' ); ?>"><?php _e( 'Number of Posts to Offset', 'genesis' ); ?>:</label>
  225.                     <input type="text" id="<?php echo $this->get_field_id( 'posts_offset' ); ?>" name="<?php echo $this->get_field_name( 'posts_offset' ); ?>" value="<?php echo esc_attr( $instance['posts_offset'] ); ?>" size="2" />
  226.                 </p>
  227.  
  228.                 <p>
  229.                     <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e( 'Order By', 'genesis' ); ?>:</label>
  230.                     <select id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>">
  231.                         <option value="date" <?php selected( 'date', $instance['orderby'] ); ?>><?php _e( 'Date', 'genesis' ); ?></option>
  232.                         <option value="title" <?php selected( 'title', $instance['orderby'] ); ?>><?php _e( 'Title', 'genesis' ); ?></option>
  233.                         <option value="parent" <?php selected( 'parent', $instance['orderby'] ); ?>><?php _e( 'Parent', 'genesis' ); ?></option>
  234.                         <option value="ID" <?php selected( 'ID', $instance['orderby'] ); ?>><?php _e( 'ID', 'genesis' ); ?></option>
  235.                         <option value="comment_count" <?php selected( 'comment_count', $instance['orderby'] ); ?>><?php _e( 'Comment Count', 'genesis' ); ?></option>
  236.                         <option value="rand" <?php selected( 'rand', $instance['orderby'] ); ?>><?php _e( 'Random', 'genesis' ); ?></option>
  237.                     </select>
  238.                 </p>
  239.  
  240.                 <p>
  241.                     <label for="<?php echo $this->get_field_id( 'order' ); ?>"><?php _e( 'Sort Order', 'genesis' ); ?>:</label>
  242.                     <select id="<?php echo $this->get_field_id( 'order' ); ?>" name="<?php echo $this->get_field_name( 'order' ); ?>">
  243.                         <option value="DESC" <?php selected( 'DESC', $instance['order'] ); ?>><?php _e( 'Descending (3, 2, 1)', 'genesis' ); ?></option>
  244.                         <option value="ASC" <?php selected( 'ASC', $instance['order'] ); ?>><?php _e( 'Ascending (1, 2, 3)', 'genesis' ); ?></option>
  245.                     </select>
  246.                 </p>
  247.  
  248.             </div>
  249.  
  250.             <div class="genesis-widget-column-box">
  251.  
  252.                 <p>
  253.                     <input id="<?php echo $this->get_field_id( 'show_image' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_image' ); ?>" value="1" <?php checked( $instance['show_image'] ); ?>/>
  254.                     <label for="<?php echo $this->get_field_id( 'show_image' ); ?>"><?php _e( 'Show Featured Image', 'genesis' ); ?></label>
  255.                 </p>
  256.  
  257.                 <p>
  258.                     <label for="<?php echo $this->get_field_id( 'image_size' ); ?>"><?php _e( 'Image Size', 'genesis' ); ?>:</label>
  259.                     <select id="<?php echo $this->get_field_id( 'image_size' ); ?>" name="<?php echo $this->get_field_name( 'image_size' ); ?>">
  260.                         <option value="thumbnail">thumbnail (<?php echo get_option( 'thumbnail_size_w' ); ?>x<?php echo get_option( 'thumbnail_size_h' ); ?>)</option>
  261.                         <?php
  262.                         $sizes = genesis_get_additional_image_sizes();
  263.                         foreach( (array) $sizes as $name => $size )
  264.                             echo '<option value="'.esc_attr( $name ).'" '.selected( $name, $instance['image_size'], FALSE ).'>'.esc_html( $name ).' ( '.$size['width'].'x'.$size['height'].' )</option>';
  265.                         ?>
  266.                     </select>
  267.                 </p>
  268.  
  269.                 <p>
  270.                     <label for="<?php echo $this->get_field_id( 'image_alignment' ); ?>"><?php _e( 'Image Alignment', 'genesis' ); ?>:</label>
  271.                     <select id="<?php echo $this->get_field_id( 'image_alignment' ); ?>" name="<?php echo $this->get_field_name( 'image_alignment' ); ?>">
  272.                         <option value="alignnone">- <?php _e( 'None', 'genesis' ); ?> -</option>
  273.                         <option value="alignleft" <?php selected( 'alignleft', $instance['image_alignment'] ); ?>><?php _e( 'Left', 'genesis' ); ?></option>
  274.                         <option value="alignright" <?php selected( 'alignright', $instance['image_alignment'] ); ?>><?php _e( 'Right', 'genesis' ); ?></option>
  275.                     </select>
  276.                 </p>
  277.  
  278.             </div>
  279.  
  280.         </div>
  281.  
  282.         <div class="genesis-widget-column genesis-widget-column-right">
  283.  
  284.             <div class="genesis-widget-column-box genesis-widget-column-box-top">
  285.  
  286.                 <p>
  287.                     <input id="<?php echo $this->get_field_id( 'show_title' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_title' ); ?>" value="1" <?php checked( $instance['show_title'] ); ?>/>
  288.                     <label for="<?php echo $this->get_field_id( 'show_title' ); ?>"><?php _e( 'Show Post Title', 'genesis' ); ?></label>
  289.                 </p>
  290.  
  291.                 <p>
  292.                     <label for="<?php echo $this->get_field_id( 'show_content' ); ?>"><?php _e( 'Content Type', 'genesis' ); ?>:</label>
  293.                     <select id="<?php echo $this->get_field_id( 'show_content' ); ?>" name="<?php echo $this->get_field_name( 'show_content' ); ?>">
  294.                         <option value="content" <?php selected( 'content' , $instance['show_content'] ); ?>><?php _e( 'Show Content', 'genesis' ); ?></option>
  295.                         <option value="excerpt" <?php selected( 'excerpt' , $instance['show_content'] ); ?>><?php _e( 'Show Excerpt', 'genesis' ); ?></option>
  296.                         <option value="content-limit" <?php selected( 'content-limit' , $instance['show_content'] ); ?>><?php _e( 'Show Content Limit', 'genesis' ); ?></option>
  297.                         <option value="" <?php selected( '' , $instance['show_content'] ); ?>><?php _e( 'No Content', 'genesis' ); ?></option>
  298.                     </select>
  299.                     <br />
  300.                     <label for="<?php echo $this->get_field_id( 'content_limit' ); ?>"><?php _e( 'Limit content to', 'genesis' ); ?>
  301.                         <input type="text" id="<?php echo $this->get_field_id( 'image_alignment' ); ?>" name="<?php echo $this->get_field_name( 'content_limit' ); ?>" value="<?php echo esc_attr( intval( $instance['content_limit'] ) ); ?>" size="3" />
  302.                         <?php _e( 'characters', 'genesis' ); ?>
  303.                     </label>
  304.                 </p>
  305.  
  306.                 <p>
  307.                     <label for="<?php echo $this->get_field_id( 'more_text' ); ?>"><?php _e( 'More Text (if applicable)', 'genesis' ); ?>:</label>
  308.                     <input type="text" id="<?php echo $this->get_field_id( 'more_text' ); ?>" name="<?php echo $this->get_field_name( 'more_text' ); ?>" value="<?php echo esc_attr( $instance['more_text'] ); ?>" />
  309.                 </p>
  310.  
  311.             </div>
  312.  
  313.             <div class="genesis-widget-column-box">
  314.  
  315.                 <p><?php _e( 'To display an unordered list of more posts from this category, please fill out the information below', 'genesis' ); ?>:</p>
  316.  
  317.                 <p>
  318.                     <label for="<?php echo $this->get_field_id( 'extra_title' ); ?>"><?php _e( 'Title', 'genesis' ); ?>:</label>
  319.                     <input type="text" id="<?php echo $this->get_field_id( 'extra_title' ); ?>" name="<?php echo $this->get_field_name( 'extra_title' ); ?>" value="<?php echo esc_attr( $instance['extra_title'] ); ?>" class="widefat" />
  320.                 </p>
  321.  
  322.                 <p>
  323.                     <label for="<?php echo $this->get_field_id( 'extra_num' ); ?>"><?php _e( 'Number of Posts to Show', 'genesis' ); ?>:</label>
  324.                     <input type="text" id="<?php echo $this->get_field_id( 'extra_num' ); ?>" name="<?php echo $this->get_field_name( 'extra_num' ); ?>" value="<?php echo esc_attr( $instance['extra_num'] ); ?>" size="2" />
  325.                 </p>
  326.  
  327.             </div>
  328.  
  329.             <div class="genesis-widget-column-box">
  330.  
  331.                 <p>
  332.                     <input id="<?php echo $this->get_field_id( 'more_from_category' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'more_from_category' ); ?>" value="1" <?php checked( $instance['more_from_category'] ); ?>/>
  333.                     <label for="<?php echo $this->get_field_id( 'more_from_category' ); ?>"><?php _e( 'Show Category Archive Link', 'genesis' ); ?></label>
  334.                 </p>
  335.  
  336.                 <p>
  337.                     <label for="<?php echo $this->get_field_id( 'more_from_category_text' ); ?>"><?php _e( 'Link Text', 'genesis' ); ?>:</label>
  338.                     <input type="text" id="<?php echo $this->get_field_id( 'more_from_category_text' ); ?>" name="<?php echo $this->get_field_name( 'more_from_category_text' ); ?>" value="<?php echo esc_attr( $instance['more_from_category_text'] ); ?>" class="widefat" />
  339.                 </p>
  340.  
  341.             </div>
  342.  
  343.         </div>
  344.         <?php
  345.  
  346.     }
  347.  
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement