Advertisement
marjwyatt

Genesis - Featured Custom Post Type Widget

May 1st, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.25 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Adds the Featured Custom 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.  * Explainafide Featured Custom Post widget class.
  14.  *
  15.  * @category Genesis
  16.  * @package Widgets
  17.  *
  18.  * @since 0.1.8
  19.  */
  20. class Featured_Custom_Posts 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_term'               => '',
  39.             'posts_num'               => 1,
  40.             'posts_offset'            => 0,
  41.             'orderby'                 => '',
  42.             'order'                   => '',
  43.             'show_image'              => 0,
  44.             'image_alignment'         => '',
  45.             'image_size'              => '',
  46.             'show_gravatar'           => 0,
  47.             'gravatar_alignment'      => '',
  48.             'gravatar_size'           => '',
  49.             'show_title'              => 0,
  50.             'show_byline'             => 0,
  51.             'post_info'               => '[post_date] ' . __( 'By', 'genesis' ) . ' [post_author_posts_link] [post_comments]',
  52.             'show_content'            => 'excerpt',
  53.             'content_limit'           => '',
  54.             'more_text'               => __( '[Read More...]', 'genesis' ),
  55.             'extra_num'               => '',
  56.             'extra_title'             => '',
  57.             'more_from_category'      => '',
  58.             'more_from_category_text' => __( 'More Posts from this Category', 'genesis' ),
  59.         );
  60.  
  61.         $widget_ops = array(
  62.             'classname'   => 'featuredpost',
  63.             'description' => __( 'Displays featured posts with thumbnails', 'genesis' ),
  64.         );
  65.  
  66.         $control_ops = array(
  67.             'id_base' => 'featured-custom-post',
  68.             'width'   => 505,
  69.             'height'  => 350,
  70.         );
  71.  
  72.         $this->WP_Widget( 'featured-custom-post', __( 'Featured Custom Posts', 'genesis' ), $widget_ops, $control_ops );
  73.  
  74.     }
  75.  
  76.     /**
  77.      * Echo the widget content.
  78.      *
  79.      * @since 0.1.8
  80.      *
  81.      * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
  82.      * @param array $instance The settings for the particular instance of the widget
  83.      */
  84.     function widget( $args, $instance ) {
  85.  
  86.         extract( $args );
  87.  
  88.         /** Merge with defaults */
  89.         $instance = wp_parse_args( (array) $instance, $this->defaults );
  90.  
  91.         echo $before_widget;
  92.  
  93.         /** Set up the author bio */
  94.         if ( ! empty( $instance['title'] ) )
  95.             echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
  96.  
  97.         $query_args = array(
  98.             'post_type' => 'portfolio',
  99.             'terms'     => $instance['posts_term'],
  100.             'showposts' => $instance['posts_num'],
  101.             'offset'    => $instance['posts_offset'],
  102.             'orderby'   => $instance['orderby'],
  103.             'order'     => $instance['order'],
  104.         );
  105.  
  106.         $featured_posts = new WP_Query( $query_args );
  107.  
  108.         if ( $featured_posts->have_posts() ) : while ( $featured_posts->have_posts() ) : $featured_posts->the_post();
  109.             echo '<div class="' . implode( ' ', get_post_class() ) . '">';
  110.  
  111.             if ( ! empty( $instance['show_image'] ) && $image = genesis_get_image( array( 'format' => 'html', 'size' => $instance['image_size'] ) ) )
  112.                 printf( '<a href="%s" title="%s" class="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), esc_attr( $instance['image_alignment'] ), $image );
  113.  
  114.             if ( ! empty( $instance['show_gravatar'] ) ) {
  115.                 echo '<span class="' . esc_attr( $instance['gravatar_alignment'] ) . '">';
  116.                 echo get_avatar( get_the_author_meta( 'ID' ), $instance['gravatar_size'] );
  117.                 echo '</span>';
  118.             }
  119.  
  120.             if ( ! empty( $instance['show_title'] ) )
  121.                 printf( '<h2><a href="%s" title="%s">%s</a></h2>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );
  122.  
  123.             if ( ! empty( $instance['show_byline'] ) && ! empty( $instance['post_info'] ) )
  124.                 printf( '<p class="byline post-info">%s</p>', do_shortcode( $instance['post_info'] ) );
  125.  
  126.             if ( ! empty( $instance['show_content'] ) ) {
  127.                 if ( 'excerpt' == $instance['show_content'] )
  128.                     the_excerpt();
  129.                 elseif ( 'content-limit' == $instance['show_content'] )
  130.                     the_content_limit( (int) $instance['content_limit'], esc_html( $instance['more_text'] ) );
  131.                 else
  132.                     the_content( esc_html( $instance['more_text'] ) );
  133.             }
  134.  
  135.             echo '</div><!--end post_class()-->'."\n\n";
  136.  
  137.         endwhile; endif;
  138.  
  139.         /** The EXTRA Posts (list) */
  140.         if ( ! empty( $instance['extra_num'] ) ) {
  141.             if ( ! empty( $instance['extra_title'] ) )
  142.                 echo $before_title . esc_html( $instance['extra_title'] ) . $after_title;
  143.  
  144.             $offset = intval( $instance['posts_num'] ) + intval( $instance['posts_offset'] );
  145.  
  146.             $query_args = array(
  147.                 'terms'       => $instance['posts_term'],
  148.                 'showposts' => $instance['extra_num'],
  149.                 'offset'    => $offset,
  150.             );
  151.             $extra_posts = new WP_Query( $query_args );
  152.  
  153.             $listitems = '';
  154.  
  155.             if ( $extra_posts->have_posts() ) {
  156.                 while ( $extra_posts->have_posts() ) {
  157.                     $extra_posts->the_post();
  158.                     $listitems .= sprintf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );
  159.                 }
  160.  
  161.                 if ( strlen( $listitems ) > 0 )
  162.                     printf( '<ul>%s</ul>', $listitems );
  163.             }
  164.         }
  165.  
  166.         if ( ! empty( $instance['more_from_category'] ) && ! empty( $instance['posts_term'] ) )
  167.             printf(
  168.                 '<p class="more-from-category"><a href="%1$s" title="%2$s">%3$s</a></p>',
  169.                 esc_url( get_category_link( $instance['posts_term'] ) ),
  170.                 esc_attr( get_cat_name( $instance['posts_term'] ) ),
  171.                 esc_html( $instance['more_from_category_text'] )
  172.             );
  173.  
  174.         echo $after_widget;
  175.         wp_reset_query();
  176.  
  177.     }
  178.  
  179.     /**
  180.      * Update a particular instance.
  181.      *
  182.      * This function should check that $new_instance is set correctly.
  183.      * The newly calculated value of $instance should be returned.
  184.      * If "false" is returned, the instance won't be saved/updated.
  185.      *
  186.      * @since 0.1.8
  187.      *
  188.      * @param array $new_instance New settings for this instance as input by the user via form()
  189.      * @param array $old_instance Old settings for this instance
  190.      * @return array Settings to save or bool false to cancel saving
  191.      */
  192.     function update( $new_instance, $old_instance ) {
  193.  
  194.         $new_instance['title']     = strip_tags( $new_instance['title'] );
  195.         $new_instance['more_text'] = strip_tags( $new_instance['more_text'] );
  196.         $new_instance['post_info'] = wp_kses_post( $new_instance['post_info'] );
  197.         return $new_instance;
  198.  
  199.     }
  200.  
  201.     /**
  202.      * Echo the settings update form.
  203.      *
  204.      * @since 0.1.8
  205.      *
  206.      * @param array $instance Current settings
  207.      */
  208.     function form( $instance ) {
  209.  
  210.         /** Merge with defaults */
  211.         $instance = wp_parse_args( (array) $instance, $this->defaults );
  212.  
  213.         ?>
  214.         <p>
  215.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'genesis' ); ?>:</label>
  216.             <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" />
  217.         </p>
  218.  
  219.         <div class="genesis-widget-column">
  220.  
  221.             <div class="genesis-widget-column-box genesis-widget-column-box-top">
  222.  
  223.                 <p>
  224.                     <label for="<?php echo $this->get_field_id( 'posts_term' ); ?>"><?php _e( 'Job Types', 'genesis' ); ?>:</label>
  225.                     <?php
  226.                     $categories_args = array(
  227.                         'name'            => $this->get_field_name( 'posts_term' ),
  228.                         'selected'        => $instance['posts_term'],
  229.                         'orderby'         => 'Name',
  230.                         'hierarchical'    => 1,
  231.                         'show_option_all' => __( 'All Types', 'genesis' ),
  232.                         'taxonomy'        => 'portfolio-type',
  233.                         'hide_empty'      => '0',
  234.                     );
  235.                     //
  236.                     wp_dropdown_categories( $categories_args  ); ?>
  237.                 </p>
  238.  
  239.                 <p>
  240.                     <label for="<?php echo $this->get_field_id( 'posts_num' ); ?>"><?php _e( 'Number of Posts to Show', 'genesis' ); ?>:</label>
  241.                     <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" />
  242.                 </p>
  243.  
  244.                 <p>
  245.                     <label for="<?php echo $this->get_field_id( 'posts_offset' ); ?>"><?php _e( 'Number of Posts to Offset', 'genesis' ); ?>:</label>
  246.                     <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" />
  247.                 </p>
  248.  
  249.                 <p>
  250.                     <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e( 'Order By', 'genesis' ); ?>:</label>
  251.                     <select id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>">
  252.                         <option value="date" <?php selected( 'date', $instance['orderby'] ); ?>><?php _e( 'Date', 'genesis' ); ?></option>
  253.                         <option value="title" <?php selected( 'title', $instance['orderby'] ); ?>><?php _e( 'Title', 'genesis' ); ?></option>
  254.                         <option value="parent" <?php selected( 'parent', $instance['orderby'] ); ?>><?php _e( 'Parent', 'genesis' ); ?></option>
  255.                         <option value="ID" <?php selected( 'ID', $instance['orderby'] ); ?>><?php _e( 'ID', 'genesis' ); ?></option>
  256.                         <option value="comment_count" <?php selected( 'comment_count', $instance['orderby'] ); ?>><?php _e( 'Comment Count', 'genesis' ); ?></option>
  257.                         <option value="rand" <?php selected( 'rand', $instance['orderby'] ); ?>><?php _e( 'Random', 'genesis' ); ?></option>
  258.                     </select>
  259.                 </p>
  260.  
  261.                 <p>
  262.                     <label for="<?php echo $this->get_field_id( 'order' ); ?>"><?php _e( 'Sort Order', 'genesis' ); ?>:</label>
  263.                     <select id="<?php echo $this->get_field_id( 'order' ); ?>" name="<?php echo $this->get_field_name( 'order' ); ?>">
  264.                         <option value="DESC" <?php selected( 'DESC', $instance['order'] ); ?>><?php _e( 'Descending (3, 2, 1)', 'genesis' ); ?></option>
  265.                         <option value="ASC" <?php selected( 'ASC', $instance['order'] ); ?>><?php _e( 'Ascending (1, 2, 3)', 'genesis' ); ?></option>
  266.                     </select>
  267.                 </p>
  268.  
  269.             </div>
  270.  
  271.             <div class="genesis-widget-column-box">
  272.  
  273.                 <p>
  274.                     <input id="<?php echo $this->get_field_id( 'show_gravatar' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_gravatar' ); ?>" value="1" <?php checked( $instance['show_gravatar'] ); ?>/>
  275.                     <label for="<?php echo $this->get_field_id( 'show_gravatar' ); ?>"><?php _e( 'Show Author Gravatar', 'genesis' ); ?></label>
  276.                 </p>
  277.  
  278.                 <p>
  279.                     <label for="<?php echo $this->get_field_id( 'gravatar_size' ); ?>"><?php _e( 'Gravatar Size', 'genesis' ); ?>:</label>
  280.                     <select id="<?php echo $this->get_field_id( 'gravatar_size' ); ?>" name="<?php echo $this->get_field_name( 'gravatar_size' ); ?>">
  281.                         <option value="45" <?php selected( 45, $instance['gravatar_size'] ); ?>><?php _e( 'Small (45px)', 'genesis' ); ?></option>
  282.                         <option value="65" <?php selected( 65, $instance['gravatar_size'] ); ?>><?php _e( 'Medium (65px)', 'genesis' ); ?></option>
  283.                         <option value="85" <?php selected( 85, $instance['gravatar_size'] ); ?>><?php _e( 'Large (85px)', 'genesis' ); ?></option>
  284.                         <option value="125" <?php selected( 105, $instance['gravatar_size'] ); ?>><?php _e( 'Extra Large (125px)', 'genesis' ); ?></option>
  285.                     </select>
  286.                 </p>
  287.  
  288.                 <p>
  289.                     <label for="<?php echo $this->get_field_id( 'gravatar_alignment' ); ?>"><?php _e( 'Gravatar Alignment', 'genesis' ); ?>:</label>
  290.                     <select id="<?php echo $this->get_field_id( 'gravatar_alignment' ); ?>" name="<?php echo $this->get_field_name( 'gravatar_alignment' ); ?>">
  291.                         <option value="alignnone">- <?php _e( 'None', 'genesis' ); ?> -</option>
  292.                         <option value="alignleft" <?php selected( 'alignleft', $instance['gravatar_alignment'] ); ?>><?php _e( 'Left', 'genesis' ); ?></option>
  293.                         <option value="alignright" <?php selected( 'alignright', $instance['gravatar_alignment'] ); ?>><?php _e( 'Right', 'genesis' ); ?></option>
  294.                     </select>
  295.                 </p>
  296.  
  297.             </div>
  298.  
  299.             <div class="genesis-widget-column-box">
  300.  
  301.                 <p>
  302.                     <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'] ); ?>/>
  303.                     <label for="<?php echo $this->get_field_id( 'show_image' ); ?>"><?php _e( 'Show Featured Image', 'genesis' ); ?></label>
  304.                 </p>
  305.  
  306.                 <p>
  307.                     <label for="<?php echo $this->get_field_id( 'image_size' ); ?>"><?php _e( 'Image Size', 'genesis' ); ?>:</label>
  308.                     <select id="<?php echo $this->get_field_id( 'image_size' ); ?>" class="genesis-image-size-selector" name="<?php echo $this->get_field_name( 'image_size' ); ?>">
  309.                         <option value="thumbnail">thumbnail (<?php echo get_option( 'thumbnail_size_w' ); ?>x<?php echo get_option( 'thumbnail_size_h' ); ?>)</option>
  310.                         <?php
  311.                         $sizes = genesis_get_additional_image_sizes();
  312.                         foreach( (array) $sizes as $name => $size )
  313.                             echo '<option value="'.esc_attr( $name ).'" '.selected( $name, $instance['image_size'], FALSE ).'>'.esc_html( $name ).' ( '.$size['width'].'x'.$size['height'].' )</option>';
  314.                         ?>
  315.                     </select>
  316.                 </p>
  317.  
  318.                 <p>
  319.                     <label for="<?php echo $this->get_field_id( 'image_alignment' ); ?>"><?php _e( 'Image Alignment', 'genesis' ); ?>:</label>
  320.                     <select id="<?php echo $this->get_field_id( 'image_alignment' ); ?>" name="<?php echo $this->get_field_name( 'image_alignment' ); ?>">
  321.                         <option value="alignnone">- <?php _e( 'None', 'genesis' ); ?> -</option>
  322.                         <option value="alignleft" <?php selected( 'alignleft', $instance['image_alignment'] ); ?>><?php _e( 'Left', 'genesis' ); ?></option>
  323.                         <option value="alignright" <?php selected( 'alignright', $instance['image_alignment'] ); ?>><?php _e( 'Right', 'genesis' ); ?></option>
  324.                     </select>
  325.                 </p>
  326.  
  327.             </div>
  328.  
  329.         </div>
  330.  
  331.         <div class="genesis-widget-column genesis-widget-column-right">
  332.  
  333.             <div class="genesis-widget-column-box genesis-widget-column-box-top">
  334.  
  335.                 <p>
  336.                     <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'] ); ?>/>
  337.                     <label for="<?php echo $this->get_field_id( 'show_title' ); ?>"><?php _e( 'Show Post Title', 'genesis' ); ?></label>
  338.                 </p>
  339.  
  340.                 <p>
  341.                     <input id="<?php echo $this->get_field_id( 'show_byline' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'show_byline' ); ?>" value="1" <?php checked( $instance['show_byline'] ); ?>/>
  342.                     <label for="<?php echo $this->get_field_id( 'show_byline' ); ?>"><?php _e( 'Show Post Info', 'genesis' ); ?></label>
  343.                     <input type="text" id="<?php echo $this->get_field_id( 'post_info' ); ?>" name="<?php echo $this->get_field_name( 'post_info' ); ?>" value="<?php echo esc_attr( $instance['post_info'] ); ?>" class="widefat" />
  344.                 </p>
  345.  
  346.                 <p>
  347.                     <label for="<?php echo $this->get_field_id( 'show_content' ); ?>"><?php _e( 'Content Type', 'genesis' ); ?>:</label>
  348.                     <select id="<?php echo $this->get_field_id( 'show_content' ); ?>" name="<?php echo $this->get_field_name( 'show_content' ); ?>">
  349.                         <option value="content" <?php selected( 'content' , $instance['show_content'] ); ?>><?php _e( 'Show Content', 'genesis' ); ?></option>
  350.                         <option value="excerpt" <?php selected( 'excerpt' , $instance['show_content'] ); ?>><?php _e( 'Show Excerpt', 'genesis' ); ?></option>
  351.                         <option value="content-limit" <?php selected( 'content-limit' , $instance['show_content'] ); ?>><?php _e( 'Show Content Limit', 'genesis' ); ?></option>
  352.                         <option value="" <?php selected( '' , $instance['show_content'] ); ?>><?php _e( 'No Content', 'genesis' ); ?></option>
  353.                     </select>
  354.                     <br />
  355.                     <label for="<?php echo $this->get_field_id( 'content_limit' ); ?>"><?php _e( 'Limit content to', 'genesis' ); ?>
  356.                         <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" />
  357.                         <?php _e( 'characters', 'genesis' ); ?>
  358.                     </label>
  359.                 </p>
  360.  
  361.                 <p>
  362.                     <label for="<?php echo $this->get_field_id( 'more_text' ); ?>"><?php _e( 'More Text (if applicable)', 'genesis' ); ?>:</label>
  363.                     <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'] ); ?>" />
  364.                 </p>
  365.  
  366.             </div>
  367.  
  368.             <div class="genesis-widget-column-box">
  369.  
  370.                 <p><?php _e( 'To display an unordered list of more posts from this category, please fill out the information below', 'genesis' ); ?>:</p>
  371.  
  372.                 <p>
  373.                     <label for="<?php echo $this->get_field_id( 'extra_title' ); ?>"><?php _e( 'Title', 'genesis' ); ?>:</label>
  374.                     <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" />
  375.                 </p>
  376.  
  377.                 <p>
  378.                     <label for="<?php echo $this->get_field_id( 'extra_num' ); ?>"><?php _e( 'Number of Posts to Show', 'genesis' ); ?>:</label>
  379.                     <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" />
  380.                 </p>
  381.  
  382.             </div>
  383.  
  384.             <div class="genesis-widget-column-box">
  385.  
  386.                 <p>
  387.                     <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'] ); ?>/>
  388.                     <label for="<?php echo $this->get_field_id( 'more_from_category' ); ?>"><?php _e( 'Show Category Archive Link', 'genesis' ); ?></label>
  389.                 </p>
  390.  
  391.                 <p>
  392.                     <label for="<?php echo $this->get_field_id( 'more_from_category_text' ); ?>"><?php _e( 'Link Text', 'genesis' ); ?>:</label>
  393.                     <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" />
  394.                 </p>
  395.  
  396.             </div>
  397.  
  398.         </div>
  399.         <?php
  400.  
  401.     }
  402.  
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement