Advertisement
marjwyatt

Genesis Framework - Grid Featured Post Widget

Sep 25th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.01 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_Grid 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'   => 'recent-post-grid',
  56.             'description' => __( 'Displays recent posts with thumbnails', 'genesis' ),
  57.         );
  58.  
  59.         $control_ops = array(
  60.             'id_base' => 'recent-post-grid',
  61.             'width'   => 100,
  62.             'height'  => 100,
  63.         );
  64.  
  65.         $this->WP_Widget( 'recent-post-grid', __( 'WSK - Right Grid Featured', '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() ) :
  102.             $i=1; // counter
  103.             while ( $featured_posts->have_posts() ) : $featured_posts->the_post();
  104.                 if($i%2==0) { // if counter is multiple of 2, use grid even
  105.                     echo '<div class="' . implode( ' ', get_post_class(array('recent-post-grid-even')) ) . '">';}
  106.                 elseif ($i%2<>0) { // if counter is not multiple of 2, use grid odd
  107.                     echo '<div class="' . implode( ' ', get_post_class(array('recent-post-grid-odd')) ) . '">';}
  108.  
  109.                 if ( ! empty( $instance['show_image'] ) )
  110.                     printf(
  111.                         '<a href="%s" title="%s" class="%s">%s</a>',
  112.                         get_permalink(),
  113.                         the_title_attribute( 'echo=0' ),
  114.                         esc_attr( $instance['image_alignment'] ),
  115.                         genesis_get_image( array( 'format' => 'html', 'size' => $instance['image_size'], ) )
  116.                     );
  117.  
  118.                 if ( ! empty( $instance['show_title'] ) )
  119.                     printf( '<h3><a class="grid-title" href="%s" title="%s">%s</a></h3>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );
  120.  
  121.                 echo '</div><!--end post_class()-->'."\n\n";
  122.                 $i++;
  123.  
  124.             endwhile; endif;
  125.  
  126.         /** The EXTRA Posts (list) */
  127.         if ( ! empty( $instance['extra_num'] ) ) {
  128.             if ( ! empty( $instance['extra_title'] ) )
  129.                 echo $before_title . esc_html( $instance['extra_title'] ) . $after_title;
  130.  
  131.             $offset = intval( $instance['posts_num'] ) + intval( $instance['posts_offset'] );
  132.  
  133.             $query_args = array(
  134.                 'cat'       => $instance['posts_cat'],
  135.                 'showposts' => $instance['extra_num'],
  136.                 'offset'    => $offset,
  137.             );
  138.             $extra_posts = new WP_Query( $query_args );
  139.  
  140.             $listitems = '';
  141.  
  142.             if ( $extra_posts->have_posts() ) {
  143.                 while ( $extra_posts->have_posts() ) {
  144.                     $extra_posts->the_post();
  145.                     $listitems .= sprintf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );
  146.                 }
  147.  
  148.                 if ( strlen( $listitems ) > 0 )
  149.                     printf( '<ul>%s</ul>', $listitems );
  150.             }
  151.         }
  152.  
  153.         if ( ! empty( $instance['more_from_category'] ) && ! empty( $instance['posts_cat'] ) )
  154.             printf(
  155.                 '<p class="more-from-category"><a href="%1$s" title="%2$s">%3$s</a></p>',
  156.                 esc_url( get_category_link( $instance['posts_cat'] ) ),
  157.                 esc_attr( get_cat_name( $instance['posts_cat'] ) ),
  158.                 esc_html( $instance['more_from_category_text'] )
  159.             );
  160.  
  161.         echo $after_widget;
  162.         wp_reset_query();
  163.  
  164.     }
  165.  
  166.     /**
  167.      * Update a particular instance.
  168.      *
  169.      * This function should check that $new_instance is set correctly.
  170.      * The newly calculated value of $instance should be returned.
  171.      * If "false" is returned, the instance won't be saved/updated.
  172.      *
  173.      * @since 0.1.8
  174.      *
  175.      * @param array $new_instance New settings for this instance as input by the user via form()
  176.      * @param array $old_instance Old settings for this instance
  177.      * @return array Settings to save or bool false to cancel saving
  178.      */
  179.     function update( $new_instance, $old_instance ) {
  180.  
  181.         $new_instance['title']     = strip_tags( $new_instance['title'] );
  182.         $new_instance['more_text'] = strip_tags( $new_instance['more_text'] );
  183.         $new_instance['post_info'] = wp_kses_post( $new_instance['post_info'] );
  184.         return $new_instance;
  185.  
  186.     }
  187.  
  188.     /**
  189.      * Echo the settings update form.
  190.      *
  191.      * @since 0.1.8
  192.      *
  193.      * @param array $instance Current settings
  194.      */
  195.     function form( $instance ) {
  196.  
  197.         /** Merge with defaults */
  198.         $instance = wp_parse_args( (array) $instance, $this->defaults );
  199.  
  200.         ?>
  201.         <p>
  202.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'genesis' ); ?>:</label>
  203.             <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" />
  204.         </p>
  205.  
  206.         <div class="genesis-widget-column">
  207.  
  208.             <div class="genesis-widget-column-box genesis-widget-column-box-top">
  209.  
  210.                 <p>
  211.                     <label for="<?php echo $this->get_field_id( 'posts_cat' ); ?>"><?php _e( 'Category', 'genesis' ); ?>:</label>
  212.                     <?php
  213.                     $categories_args = array(
  214.                         'name'            => $this->get_field_name( 'posts_cat' ),
  215.                         'selected'        => $instance['posts_cat'],
  216.                         'orderby'         => 'Name',
  217.                         'hierarchical'    => 1,
  218.                         'show_option_all' => __( 'All Categories', 'genesis' ),
  219.                         'hide_empty'      => '0',
  220.                     );
  221.                     wp_dropdown_categories( $categories_args ); ?>
  222.                 </p>
  223.  
  224.                 <p>
  225.                     <label for="<?php echo $this->get_field_id( 'posts_num' ); ?>"><?php _e( 'Number of Posts to Show', 'genesis' ); ?>:</label>
  226.                     <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" />
  227.                 </p>
  228.  
  229.                 <p>
  230.                     <label for="<?php echo $this->get_field_id( 'posts_offset' ); ?>"><?php _e( 'Number of Posts to Offset', 'genesis' ); ?>:</label>
  231.                     <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" />
  232.                 </p>
  233.  
  234.                 <p>
  235.                     <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e( 'Order By', 'genesis' ); ?>:</label>
  236.                     <select id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>">
  237.                         <option value="date" <?php selected( 'date', $instance['orderby'] ); ?>><?php _e( 'Date', 'genesis' ); ?></option>
  238.                         <option value="title" <?php selected( 'title', $instance['orderby'] ); ?>><?php _e( 'Title', 'genesis' ); ?></option>
  239.                         <option value="parent" <?php selected( 'parent', $instance['orderby'] ); ?>><?php _e( 'Parent', 'genesis' ); ?></option>
  240.                         <option value="ID" <?php selected( 'ID', $instance['orderby'] ); ?>><?php _e( 'ID', 'genesis' ); ?></option>
  241.                         <option value="comment_count" <?php selected( 'comment_count', $instance['orderby'] ); ?>><?php _e( 'Comment Count', 'genesis' ); ?></option>
  242.                         <option value="rand" <?php selected( 'rand', $instance['orderby'] ); ?>><?php _e( 'Random', 'genesis' ); ?></option>
  243.                     </select>
  244.                 </p>
  245.  
  246.                 <p>
  247.                     <label for="<?php echo $this->get_field_id( 'order' ); ?>"><?php _e( 'Sort Order', 'genesis' ); ?>:</label>
  248.                     <select id="<?php echo $this->get_field_id( 'order' ); ?>" name="<?php echo $this->get_field_name( 'order' ); ?>">
  249.                         <option value="DESC" <?php selected( 'DESC', $instance['order'] ); ?>><?php _e( 'Descending (3, 2, 1)', 'genesis' ); ?></option>
  250.                         <option value="ASC" <?php selected( 'ASC', $instance['order'] ); ?>><?php _e( 'Ascending (1, 2, 3)', 'genesis' ); ?></option>
  251.                     </select>
  252.                 </p>
  253.  
  254.             </div>
  255.  
  256.             <div class="genesis-widget-column-box">
  257.  
  258.                 <p>
  259.                     <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'] ); ?>/>
  260.                     <label for="<?php echo $this->get_field_id( 'show_image' ); ?>"><?php _e( 'Show Featured Image', 'genesis' ); ?></label>
  261.                 </p>
  262.  
  263.                 <p>
  264.                     <label for="<?php echo $this->get_field_id( 'image_size' ); ?>"><?php _e( 'Image Size', 'genesis' ); ?>:</label>
  265.                     <select id="<?php echo $this->get_field_id( 'image_size' ); ?>" name="<?php echo $this->get_field_name( 'image_size' ); ?>">
  266.                         <option value="thumbnail">thumbnail (<?php echo get_option( 'thumbnail_size_w' ); ?>x<?php echo get_option( 'thumbnail_size_h' ); ?>)</option>
  267.                         <?php
  268.                         $sizes = genesis_get_additional_image_sizes();
  269.                         foreach( (array) $sizes as $name => $size )
  270.                             echo '<option value="'.esc_attr( $name ).'" '.selected( $name, $instance['image_size'], FALSE ).'>'.esc_html( $name ).' ( '.$size['width'].'x'.$size['height'].' )</option>';
  271.                         ?>
  272.                     </select>
  273.                 </p>
  274.  
  275.                 <p>
  276.                     <label for="<?php echo $this->get_field_id( 'image_alignment' ); ?>"><?php _e( 'Image Alignment', 'genesis' ); ?>:</label>
  277.                     <select id="<?php echo $this->get_field_id( 'image_alignment' ); ?>" name="<?php echo $this->get_field_name( 'image_alignment' ); ?>">
  278.                         <option value="alignnone">- <?php _e( 'None', 'genesis' ); ?> -</option>
  279.                         <option value="alignleft" <?php selected( 'alignleft', $instance['image_alignment'] ); ?>><?php _e( 'Left', 'genesis' ); ?></option>
  280.                         <option value="alignright" <?php selected( 'alignright', $instance['image_alignment'] ); ?>><?php _e( 'Right', 'genesis' ); ?></option>
  281.                     </select>
  282.                 </p>
  283.  
  284.             </div>
  285.  
  286.         </div>
  287.  
  288.         <div class="genesis-widget-column genesis-widget-column-right">
  289.  
  290.             <div class="genesis-widget-column-box genesis-widget-column-box-top">
  291.  
  292.                 <p>
  293.                     <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'] ); ?>/>
  294.                     <label for="<?php echo $this->get_field_id( 'show_title' ); ?>"><?php _e( 'Show Post Title', 'genesis' ); ?></label>
  295.                 </p>
  296.  
  297.                 <p>
  298.                     <label for="<?php echo $this->get_field_id( 'show_content' ); ?>"><?php _e( 'Content Type', 'genesis' ); ?>:</label>
  299.                     <select id="<?php echo $this->get_field_id( 'show_content' ); ?>" name="<?php echo $this->get_field_name( 'show_content' ); ?>">
  300.                         <option value="content" <?php selected( 'content' , $instance['show_content'] ); ?>><?php _e( 'Show Content', 'genesis' ); ?></option>
  301.                         <option value="excerpt" <?php selected( 'excerpt' , $instance['show_content'] ); ?>><?php _e( 'Show Excerpt', 'genesis' ); ?></option>
  302.                         <option value="content-limit" <?php selected( 'content-limit' , $instance['show_content'] ); ?>><?php _e( 'Show Content Limit', 'genesis' ); ?></option>
  303.                         <option value="" <?php selected( '' , $instance['show_content'] ); ?>><?php _e( 'No Content', 'genesis' ); ?></option>
  304.                     </select>
  305.                     <br />
  306.                     <label for="<?php echo $this->get_field_id( 'content_limit' ); ?>"><?php _e( 'Limit content to', 'genesis' ); ?>
  307.                         <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" />
  308.                         <?php _e( 'characters', 'genesis' ); ?>
  309.                     </label>
  310.                 </p>
  311.  
  312.                 <p>
  313.                     <label for="<?php echo $this->get_field_id( 'more_text' ); ?>"><?php _e( 'More Text (if applicable)', 'genesis' ); ?>:</label>
  314.                     <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'] ); ?>" />
  315.                 </p>
  316.  
  317.             </div>
  318.  
  319.             <div class="genesis-widget-column-box">
  320.  
  321.                 <p><?php _e( 'To display an unordered list of more posts from this category, please fill out the information below', 'genesis' ); ?>:</p>
  322.  
  323.                 <p>
  324.                     <label for="<?php echo $this->get_field_id( 'extra_title' ); ?>"><?php _e( 'Title', 'genesis' ); ?>:</label>
  325.                     <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" />
  326.                 </p>
  327.  
  328.                 <p>
  329.                     <label for="<?php echo $this->get_field_id( 'extra_num' ); ?>"><?php _e( 'Number of Posts to Show', 'genesis' ); ?>:</label>
  330.                     <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" />
  331.                 </p>
  332.  
  333.             </div>
  334.  
  335.             <div class="genesis-widget-column-box">
  336.  
  337.                 <p>
  338.                     <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'] ); ?>/>
  339.                     <label for="<?php echo $this->get_field_id( 'more_from_category' ); ?>"><?php _e( 'Show Category Archive Link', 'genesis' ); ?></label>
  340.                 </p>
  341.  
  342.                 <p>
  343.                     <label for="<?php echo $this->get_field_id( 'more_from_category_text' ); ?>"><?php _e( 'Link Text', 'genesis' ); ?>:</label>
  344.                     <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" />
  345.                 </p>
  346.  
  347.             </div>
  348.  
  349.         </div>
  350.         <?php
  351.  
  352.     }
  353.  
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement