Created
April 20, 2018 04:00
-
-
Save Steeru/3d42cd19cef987e25c79205851438475 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Testimonial Widget Class | |
* | |
* @since WEN Business 1.0 | |
* | |
*/ | |
class WEN_Business_Testimonial_Widget extends WP_Widget { | |
function __construct() { | |
$opts = array( | |
'classname' => 'wen_business_widget_testimonial', | |
'description' => __( 'Testimonial Widget', 'wen-business' ) | |
); | |
parent::__construct('wen-business-testimonial', __( 'Testimonial Widget', 'wen-business'), $opts); | |
} | |
function widget( $args, $instance ) { | |
extract( $args ); | |
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base ); | |
$subtitle = apply_filters('widget_subtitle', empty($instance['subtitle']) ? '' : $instance['subtitle'], $instance, $this->id_base ); | |
$post_category = ! empty( $instance['post_category'] ) ? $instance['post_category'] : 0; | |
$featured_image = ! empty( $instance['featured_image'] ) ? $instance['featured_image'] : 'thumbnail'; | |
$post_number = ! empty( $instance['post_number'] ) ? $instance['post_number'] : 4; | |
$excerpt_length = ! empty( $instance['excerpt_length'] ) ? $instance['excerpt_length'] : 40; | |
$post_order_by = ! empty( $instance['post_order_by'] ) ? $instance['post_order_by'] : 'date'; | |
$post_order = ! empty( $instance['post_order'] ) ? $instance['post_order'] : 'desc'; | |
$transition_delay = ! empty( $instance['transition_delay'] ) ? $instance['transition_delay'] : 3; | |
$transition_duration = ! empty( $instance['transition_duration'] ) ? $instance['transition_duration'] : 1; | |
$disable_pager = ! empty( $instance['disable_pager'] ) ? $instance['disable_pager'] : false ; | |
$custom_class = apply_filters( 'widget_custom_class', empty( $instance['custom_class'] ) ? '' : $instance['custom_class'], $instance, $this->id_base ); | |
// Validation | |
// Order | |
if ( in_array( $post_order, array( 'asc', 'desc' ) ) ) { | |
$post_order = strtoupper( $post_order ); | |
} | |
else{ | |
$post_order = 'DESC'; | |
} | |
// Order By | |
switch ( $post_order_by ) { | |
case 'date': | |
$post_order_by = 'date'; | |
break; | |
case 'title': | |
$post_order_by = 'title'; | |
break; | |
case 'random': | |
$post_order_by = 'rand'; | |
break; | |
case 'menu-order': | |
$post_order_by = 'menu_order'; | |
break; | |
default: | |
$post_order_by = 'date'; | |
break; | |
} | |
// Add Custom class | |
if ( $custom_class ) { | |
$before_widget = str_replace( 'class="', 'class="'. $custom_class . ' ', $before_widget ); | |
} | |
echo $before_widget; | |
// Title | |
if ( $title ) echo $before_title . $title . $after_title; | |
// Sub Title | |
if ( $subtitle ) printf( '%s%s%s', '<h4 class="widget-subtitle">', $subtitle, '</h4>' ); | |
// | |
?> | |
<?php | |
$qargs = array( | |
'posts_per_page' => $post_number, | |
'no_found_rows' => true, | |
'orderby' => $post_order_by, | |
'order' => $post_order, | |
); | |
if ( absint( $post_category ) > 0 ) { | |
$qargs['cat'] = $post_category; | |
} | |
$all_posts = get_posts( $qargs ); | |
?> | |
<?php if ( ! empty( $all_posts ) ): ?> | |
<?php global $post; ?> | |
<?php | |
// Cycle data | |
$slide_data = array( | |
'fx' => 'fade', | |
'speed' => $transition_duration * 1000, | |
'pause-on-hover' => 'true', | |
'log' => 'false', | |
'swipe' => 'true', | |
'auto-height' => 'container', | |
'slides' => '> article', | |
); | |
$slide_data['timeout'] = $transition_delay * 1000; | |
$slide_attributes_text = ''; | |
foreach ($slide_data as $key => $item) { | |
$slide_attributes_text .= ' '; | |
$slide_attributes_text .= ' data-cycle-'.esc_attr( $key ); | |
$slide_attributes_text .= '="'.esc_attr( $item ).'"'; | |
} | |
?> | |
<div class="testimonial-widget"> | |
<div class="cycle-slideshow" <?php echo $slide_attributes_text; ?> > | |
<?php foreach ( $all_posts as $key => $post ): ?> | |
<?php setup_postdata( $post ); ?> | |
<article class="testimonial-item"> | |
<?php if ( 'disable' != $featured_image ): ?> | |
<div class="testimonial-thumb"> | |
<a href="<?php the_permalink(); ?>"><?php | |
the_post_thumbnail( $featured_image ); | |
?> | |
</a> | |
</div><!-- .testimonial-thumb --> | |
<?php endif ?> | |
<div class="testimonial-text-wrap"> | |
<div class="testimonial-summary"> | |
<?php | |
$excerpt = $this->get_the_excerpt( $post, $excerpt_length ); | |
echo wp_kses_post( $excerpt ); | |
?> | |
</div><!-- .testimonial-summary --> | |
<h3 class="testimonial-title"><?php the_title(); ?></h3><!-- .testimonial-title --> | |
</div><!-- .testimonial-text-wrap --> | |
</article><!-- .testimonial-item --> | |
<?php endforeach ?> | |
<?php if ( false == $disable_pager ): ?> | |
<div class="cycle-pager"></div> | |
<?php endif ?> | |
</div><!-- .cycle-slideshow --> | |
</div><!-- .testimonial-widget --> | |
<?php wp_reset_postdata(); // Reset ?> | |
<?php endif; ?> | |
<?php | |
// | |
echo $after_widget; | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); | |
$instance['subtitle'] = sanitize_text_field( $new_instance['subtitle'] ); | |
$instance['post_category'] = absint( $new_instance['post_category'] ); | |
$instance['featured_image'] = esc_attr( $new_instance['featured_image'] ); | |
$instance['post_number'] = absint( $new_instance['post_number'] ); | |
$instance['excerpt_length'] = absint( $new_instance['excerpt_length'] ); | |
$instance['post_order_by'] = esc_attr( $new_instance['post_order_by'] ); | |
$instance['post_order'] = esc_attr( $new_instance['post_order'] ); | |
$instance['transition_delay'] = absint( $new_instance['transition_delay'] ); | |
$instance['transition_duration'] = absint( $new_instance['transition_duration'] ); | |
$instance['disable_pager'] = isset( $new_instance['disable_pager'] ); | |
$instance['custom_class'] = esc_attr( $new_instance['custom_class'] ); | |
return $instance; | |
} | |
function form( $instance ) { | |
//Defaults | |
$instance = wp_parse_args( (array) $instance, array( | |
'title' => '', | |
'subtitle' => '', | |
'post_category' => '', | |
'featured_image' => 'thumbnail', | |
'post_number' => 4, | |
'excerpt_length' => 20, | |
'post_order_by' => 'date', | |
'post_order' => 'desc', | |
'transition_delay' => 3, | |
'transition_duration' => 1, | |
'disable_pager' => 0, | |
'custom_class' => '', | |
) ); | |
$title = strip_tags( $instance['title'] ); | |
$subtitle = esc_textarea( $instance['subtitle'] ); | |
$post_category = absint( $instance['post_category'] ); | |
$featured_image = esc_attr( $instance['featured_image'] ); | |
$post_number = absint( $instance['post_number'] ); | |
$excerpt_length = absint( $instance['excerpt_length'] ); | |
$post_order_by = esc_attr( $instance['post_order_by'] ); | |
$post_order = esc_attr( $instance['post_order'] ); | |
$transition_delay = absint( $instance['transition_delay'] ); | |
$transition_duration = absint( $instance['transition_duration'] ); | |
$disable_pager = esc_attr( $instance['disable_pager'] ); | |
$custom_class = esc_attr( $instance['custom_class'] ); | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wen-business' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'subtitle' ); ?>"><?php _e( 'Sub Title:', 'wen-business' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'subtitle' ); ?>" name="<?php echo $this->get_field_name( 'subtitle' ); ?>" type="text" value="<?php echo esc_attr( $subtitle ); ?>" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'post_category' ); ?>"><?php _e( 'Category:', 'wen-business' ); ?></label> | |
<?php | |
$cat_args = array( | |
'orderby' => 'name', | |
'hide_empty' => 0, | |
'taxonomy' => 'category', | |
'name' => $this->get_field_name('post_category'), | |
'id' => $this->get_field_id('post_category'), | |
'selected' => $post_category, | |
'show_option_all' => __( 'All Categories','wen-business' ), | |
); | |
wp_dropdown_categories( $cat_args ); | |
?> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'featured_image' ); ?>"><?php _e( 'Featured Image:', 'wen-business' ); ?></label> | |
<?php | |
$this->dropdown_image_sizes( array( | |
'id' => $this->get_field_id( 'featured_image' ), | |
'name' => $this->get_field_name( 'featured_image' ), | |
'selected' => $featured_image, | |
) | |
); | |
?> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'post_number' ); ?>"><?php _e('Number of Posts:', 'wen-business' ); ?></label> | |
<input class="widefat1" id="<?php echo $this->get_field_id( 'post_number' ); ?>" name="<?php echo $this->get_field_name( 'post_number' ); ?>" type="number" value="<?php echo esc_attr( $post_number ); ?>" min="1" style="max-width:50px;" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'excerpt_length' ); ?>"><?php _e('Excerpt Length:', 'wen-business' ); ?></label> | |
<input class="widefat1" id="<?php echo $this->get_field_id( 'excerpt_length' ); ?>" name="<?php echo $this->get_field_name( 'excerpt_length' ); ?>" type="number" value="<?php echo esc_attr( $excerpt_length ); ?>" min="1" style="max-width:50px;" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'post_order_by' ); ?>"><?php _e( 'Order By:', 'wen-business' ); ?></label> | |
<?php | |
$this->dropdown_post_order_by( array( | |
'id' => $this->get_field_id( 'post_order_by' ), | |
'name' => $this->get_field_name( 'post_order_by' ), | |
'selected' => $post_order_by, | |
) | |
); | |
?> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'post_order' ); ?>"><?php _e( 'Order:', 'wen-business' ); ?></label> | |
<select id="<?php echo $this->get_field_id( 'post_order' ); ?>" name="<?php echo $this->get_field_name( 'post_order' ); ?>"> | |
<option value="asc" <?php selected( $post_order, 'asc' ) ?>><?php _e( 'Ascending', 'wen-business' ) ?></option> | |
<option value="desc" <?php selected( $post_order, 'desc' ) ?>><?php _e( 'Descending', 'wen-business' ) ?></option> | |
</select> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'transition_delay' ); ?>"><?php _e( 'Transition Delay:', 'wen-business' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'transition_delay' ); ?>" name="<?php echo $this->get_field_name( 'transition_delay' ); ?>" type="text" value="<?php echo esc_attr( $transition_delay ); ?>" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'transition_duration' ); ?>"><?php _e( 'Transition Duration:', 'wen-business' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'transition_duration' ); ?>" name="<?php echo $this->get_field_name( 'transition_duration' ); ?>" type="text" value="<?php echo esc_attr( $transition_duration ); ?>" /> | |
</p> | |
<p><input id="<?php echo $this->get_field_id( 'disable_pager' ); ?>" name="<?php echo $this->get_field_name( 'disable_pager' ); ?>" type="checkbox" <?php checked(isset($instance['disable_pager']) ? $instance['disable_pager'] : 0); ?> /> <label for="<?php echo $this->get_field_id( 'disable_pager' ); ?>"><?php _e( 'Disable Pager', 'wen-business' ); ?> | |
</label> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'custom_class' ); ?>"><?php _e( 'Custom Class:', 'wen-business' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'custom_class'); ?>" name="<?php echo $this->get_field_name( 'custom_class' ); ?>" type="text" value="<?php echo esc_attr( $custom_class ); ?>" /> | |
</p> | |
<?php | |
} | |
function get_the_excerpt( $post_obj, $length = 40 ){ | |
if ( is_null( $post_obj ) ) { | |
return; | |
} | |
$length = absint( $length ); | |
if ( $length < 1 ) { | |
$length = 40; | |
} | |
$source_content = $post_obj->post_content; | |
if ( ! empty( $post_obj->post_excerpt ) ) { | |
$source_content = $post_obj->post_excerpt; | |
} | |
$trimmed_content = wp_trim_words( $source_content, $length, '...' ); | |
return $trimmed_content; | |
} | |
function dropdown_post_order_by( $args ){ | |
$defaults = array( | |
'id' => '', | |
'name' => '', | |
'selected' => 0, | |
'echo' => 1, | |
); | |
$r = wp_parse_args( $args, $defaults ); | |
$output = ''; | |
$choices = array( | |
'date' => __( 'Date','wen-business' ), | |
'title' => __( 'Title','wen-business' ), | |
'menu-order' => __( 'Menu Order','wen-business' ), | |
'random' => __( 'Random','wen-business' ), | |
); | |
if ( ! empty( $choices ) ) { | |
$output = "<select name='" . esc_attr( $r['name'] ) . "' id='" . esc_attr( $r['id'] ) . "'>\n"; | |
foreach ( $choices as $key => $choice ) { | |
$output .= '<option value="' . esc_attr( $key ) . '" '; | |
$output .= selected( $r['selected'], $key, false ); | |
$output .= '>' . esc_html( $choice ) . '</option>\n'; | |
} | |
$output .= "</select>\n"; | |
} | |
if ( $r['echo'] ) { | |
echo $output; | |
} | |
return $output; | |
} | |
function dropdown_image_sizes( $args ){ | |
$defaults = array( | |
'id' => '', | |
'name' => '', | |
'selected' => 0, | |
'echo' => 1, | |
); | |
$r = wp_parse_args( $args, $defaults ); | |
$output = ''; | |
$choices = $this->get_image_sizes_options(); | |
if ( ! empty( $choices ) ) { | |
$output = "<select name='" . esc_attr( $r['name'] ) . "' id='" . esc_attr( $r['id'] ) . "'>\n"; | |
foreach ( $choices as $key => $choice ) { | |
$output .= '<option value="' . esc_attr( $key ) . '" '; | |
$output .= selected( $r['selected'], $key, false ); | |
$output .= '>' . esc_html( $choice ) . '</option>\n'; | |
} | |
$output .= "</select>\n"; | |
} | |
if ( $r['echo'] ) { | |
echo $output; | |
} | |
return $output; | |
} | |
private function get_image_sizes_options(){ | |
global $_wp_additional_image_sizes; | |
$get_intermediate_image_sizes = get_intermediate_image_sizes(); | |
$choices = array(); | |
$choices['disable'] = __( 'No Image', 'wen-business' ); | |
foreach ( array( 'thumbnail', 'medium', 'large' ) as $key => $_size ) { | |
$choices[ $_size ] = $_size . ' ('. get_option( $_size . '_size_w' ) . 'x' . get_option( $_size . '_size_h' ) . ')'; | |
} | |
$choices['full'] = __( 'full (original)', 'wen-business' ); | |
if ( ! empty( $_wp_additional_image_sizes ) && is_array( $_wp_additional_image_sizes ) ) { | |
foreach ($_wp_additional_image_sizes as $key => $size ) { | |
$choices[ $key ] = $key . ' ('. $size['width'] . 'x' . $size['height'] . ')'; | |
} | |
} | |
return $choices; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment