Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TravisBallard/4961924 to your computer and use it in GitHub Desktop.
Save TravisBallard/4961924 to your computer and use it in GitHub Desktop.
Slimmed down single testimonial widget for TBTestimonials.
<?php
/**
* plugin name: TBTestimonials Single Testimonial Widget
* author: Travis Ballard
* author uri: http://travisballard.com
* version: 1.0
* description: Adds a slimmed down TBTestimonials widget that allows you to select a single testimonial
*/
class TBTestimonialsSingleTestimonialWidget extends WP_Widget
{
var $post_type = null;
/**
* widget construct
*
*/
function TBTestimonialsSingleTestimonialWidget()
{
global $tbtestimonials;
parent::WP_Widget( false, $name = 'TB Testimonials Widget - Single Testimonial' );
$this->post_type = apply_filters( 'tbtestimonials_post_type', $tbtestimonials->post_type );
}
/**
* update settings
*
* @param mixed $new_instance
* @param mixed $old_instance
* @return array
*/
function update( $new_instance, $old_instance )
{
global $tbtestimonials;
$instance = $old_instance;
$instance['title'] = strip_tags( esc_html( $new_instance['title'] ) );
$instance['testimonial_id'] = intval( $new_instance['testimonial_id'] );
$instance['template'] = esc_html( $new_instance['template'] );
return $instance;
}
/**
* widget form
*
* @param mixed $instance
*/
function form( $instance )
{
global $tbtestimonials;
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo isset( $instance['title'] ) ? $instance['title'] : ''; ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'testimonial_id' ); ?>"><?php _e( 'Testimonial:' ); ?></label>
<select name="<?php echo $this->get_field_name( 'testimonial_id' ); ?>" id="<?php echo $this->get_field_id(); ?>" class="widefat">
<?php
$q = new wp_query( array( 'post_type' => $this->post_type, 'post_status' => 'publish', 'posts_per_page' => -1 ) );
if( $q->have_posts() ){
while( $q->have_posts() ) {
$q->the_post();
printf( '<option value="%d" title="%s"%s>ID: %d -- %s -- %s&hellip;</option>', get_the_ID(), esc_attr( get_the_content() ), $instance['testimonial_id'] == get_the_ID() ? selected( 1, 1 ) : '', get_the_ID(), get_the_title(), substr( get_the_excerpt(), 0, 20 ) );
}
}
?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'template' ); ?>"><?php _e( 'Output Template:' ); ?></label>
<select name="<?php echo $this->get_field_name( 'template' ); ?>" id="<?php echo $this->get_field_id; ?>" class="widefat">
<?php foreach( $tbtestimonials->templates as $template_id => $template ) : ?>
<option value="<?php echo $template_id; ?>" <?php selected( $template_id, $instance['template'] ); ?> ><?php echo $template->name(); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php
}
/**
* widget
*
* @param mixed $args
* @param mixed $instance
*/
function widget( $args, $instance )
{
extract( $args );
$tbtestimonials_settings = get_option( 'tbtestimonials_settings' );
echo $before_widget;
if( isset( $instance['title'] ) )
printf( '%s%s%s', $before_title, esc_attr( $instance['title'] ), $after_title );
if( ! isset( $instance['template'] ) || empty( $instance['template'] ) )
$instance['template'] = 'widget';
$testimonial_args = array(
'post_type' => $this->post_type,
'p' => $instance['testimonial_id']
);
$testimonials = new WP_Query( $testimonial_args );
if( $testimonials->have_posts() )
{
$tbtestimonials = new TBTestimonials(); ?>
<ul id="tbtestimonials-widget">
<?php
$x = 0;
$t = array();
while( $testimonials->have_posts() )
{
$testimonials->the_post();
$t[] = $tbtestimonials->prepare_testimonial( $instance['template'] );
}
$pages = $t;
$p = 0;
foreach( $pages as $page )
{
$class = ++$p > 1 ? 'testimonial-slide hidden-testimonial' : 'testimonial-slide';
printf( '<li class="%s"><ul>', $class );
print $page;
printf( '</ul></li>' );
}
?></ul><?php
}
wp_reset_query();
echo $after_widget;
}
}
# init widget
add_action( 'widgets_init', create_function( '', 'return register_widget( "TBTestimonialsSingleTestimonialWidget" );' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment