<?php
class Hbbot_Testimonials_Widget extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'hbbot_testimonial_widget',
'description' => 'Display groups of testimonials in your sidebar or other widgetized areas'
);
parent::__construct( 'hbbot_testy_widget', 'HBBOT Testimonials Wiget', $widget_ops );
}
function form( $instance ) {
$title = esc_attr( $instance[ 'title' ] );
$num_posts = esc_attr( $instance[ 'num_posts' ] );
$use_thumb = esc_attr( $instance[ 'use_thumb' ] );
if( isset( $instance['use_groups'] ) ) {
$use_groups = $instance[ 'use_groups' ];
} else {
$use_groups = array('all');
}
$groups = get_terms('hbbot_group', array('hide_empty' => false));
$tcount = wp_count_posts( 'hbbot_testimonial' );
$gcount = wp_count_terms( 'hbbot_group', array( 'hide_empty' => false ) );
if( $tcount > 0 ):
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title: (optional)
<input class="widefat"
id="<?php echo $this->get_field_id( 'title' ); ?>"
name="<?php echo $this->get_field_name( 'title' ); ?>"
type="text"
value="<?php echo attribute_escape( $title ); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'num_posts' ); ?>">Testimonials to display:
<input size="3"
id="<?php echo $this->get_field_id( 'num_posts' ); ?>"
name="<?php echo $this->get_field_name( 'num_posts' ); ?>"
type="text"
value="<?php echo attribute_escape( $num_posts ); ?>" />
</label>
</p>
<?php if( $gcount > 0 ) : ?>
<p>Limit to these Groups:<br />
<select
class="widefat"
size="5"
style="height: auto;"
id="<?php echo $this->get_field_id( 'use_groups' );?>"
name="<?php echo $this->get_field_name( 'use_groups' );?>"
multiple="multiple">
<option value="all"<?php if( in_array( 'all', $use_groups ) !== FALSE ): ?> selected="selected"<?php endif;?> >All Groups</option>
<?php foreach( $groups as $g ): ?>
<option value="<?php echo $g->term_id; ?>" <?php if( in_array( $g->term_id, $use_groups ) !== FALSE ) { ?>selected="selected"<?php } ?> ><?php echo $g->name . " (". $g->count . ")"; ?></option>
<?php endforeach; ?>
</select>
</p>
<?php else: ?>
<p>You haven't defined any Groups for your Testimonials. Once you <a href="<?php get_bloginfo('url'); ?>/wp-admin/edit-tags.php?taxonomy=hbbot_group&post_type=hbbot_testimonial">create some</a> you can use this widget to display specific groups of testimonials.</p>
<?php endif; ?>
<p>
<label for="<?php echo $this->get_field_id( 'use_thumb' ); ?>">
<input size="3"
id="<?php echo $this->get_field_id( 'use_thumb' ); ?>"
name="<?php echo $this->get_field_name( 'use_thumb' ); ?>"
type="checkbox"
<?php echo ( $use_thumb ) ? 'checked="checked"' : ''; ?> /> Show Thumbnails?
</label>
</p>
<?php
else :
?>
<p>Oops! You haven't created any Testimonials, yet. <a href="<?php get_bloginfo('url'); ?>/wp-admin/post-new.php?post_type=hbbot_testimonial">Create some now.</a></p>
<?php
endif;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance[ 'title' ] );
$instance['num_posts'] = strip_tags( $new_instance[ 'num_posts' ] );
$instance['use_thumb'] = ( isset( $new_instance[ 'use_thumb' ] ) ) ? true : false;
$instance['use_groups'] = $new_instance[ 'use_groups' ] ;
return $instance;
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$tlimit = (int)$instance[ 'num_posts' ];
$use_thumb = (bool)$instance[ 'use_thumb' ];
}
} // end class
add_action( 'widgets_init', 'hbbot_register_widget' );
function hbbot_register_widget() { return register_widget( 'Hbbot_Testimonials_Widget' ); }