Skip to content

Instantly share code, notes, and snippets.

@woogist
Created April 29, 2013 08:00
Show Gist options
  • Select an option

  • Save woogist/5480279 to your computer and use it in GitHub Desktop.

Select an option

Save woogist/5480279 to your computer and use it in GitHub Desktop.
Related Posts Slider Widget
<?php
/*
Plugin Name: WooSlider - Related Posts Slider
Plugin URI: http://woothemes.com/
Description: Related Posts WooSlider slideshow.
Version: 1.0.0
Author: WooThemes
Author URI: http://woothemes.com/
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
if ( ! is_file( $dir = WPMU_PLUGIN_DIR . '/wooslider/widgets/widget-wooslider-base.php' ) ) {
if ( ! is_file( $dir = WP_PLUGIN_DIR . '/wooslider/widgets/widget-wooslider-base.php' ) )
$dir = null;
}
if ($dir !== null) {
require_once( $dir );
add_action( 'widgets_init', create_function('', 'return register_widget("WooSlider_Widget_RelatedPosts");'), 1 );
add_filter( 'widget_update_callback', array( 'WooSlider_Widget_RelatedPosts', 'save_widget_form' ), 10, 2 );
add_action( 'in_widget_form', array( 'WooSlider_Widget_RelatedPosts', 'widget_form_html' ), 10, 3 );
}
class WooSlider_Widget_RelatedPosts extends WooSlider_Widget_Base {
public function __construct () {
$this->slider_type = 'posts';
$this->woo_widget_cssclass = 'widget_wooslider_slideshow_relatedposts';
$this->woo_widget_description = __( 'A slideshow of recent posts on your site', 'wooslider_widget_relatedposts' );
$this->woo_widget_idbase = 'wooslider_slideshow_relatedposts';
$this->woo_widget_title = __('Related Posts Slideshow (WooSlider)', 'wooslider_widget_relatedposts' );
$this->init();
$this->defaults = array('title' => __( 'Related Posts', 'wooslider_widget_relatedposts' ));
} // End Constructor
function remove_current_post( $query ) {
global $post;
if ( is_single() ) {
$query->set('post__not_in', array($post->ID));
return;
}
}
protected function generate_slideshow ( $instance ) {
global $wooslider, $post;
$settings = $wooslider->settings->get_settings();
$settings['slider_type'] = $this->slider_type;
$extra_args = array();
foreach ( $instance as $k => $v ) {
if ( ! in_array( $k, array_keys( $settings ) ) ) {
$extra_args[$k] = esc_attr( $v );
}
}
// Make sure the various settings are applied.
if ( isset( $instance['show_advanced_settings'] ) && ( $instance['show_advanced_settings'] == true ) ) {
foreach ( $settings as $k => $v ) {
if ( isset( $instance[$k] ) && ( $instance[$k] != $settings[$k] ) ) {
$settings[$k] = esc_attr( $instance[$k] );
}
}
}
if( is_single() ) {
if( $instance['tags'] == true ) {
$terms_tags = get_the_terms( $post->ID, 'post_tag' );
$count = 0;
if ( !empty( $terms_tags ) ) {
$extra_args['tag'] = '';
foreach( $terms_tags as $k => $v ) {
$count++;
if ( $count > 1 ) {
$extra_args['tag'] .= '+';
}
$extra_args['tag'] .= esc_attr( $v->slug );
}
}
}
if( $instance['cats'] == true ) {
$terms_categories = get_the_terms( $post->ID, 'category' );
$count = 0;
if ( !empty( $terms_categories ) ) {
$extra_args['category'] = '';
foreach ( $terms_categories as $k => $v ) {
$count++;
if ( $count > 1 ) {
$extra_args['category'] .= ',';
}
$extra_args['category'] .= esc_attr( $v->slug );
}
}
}
}
else {
if ( isset( $instance['tag'] ) && is_array( $instance['tag'] ) ) {
$count = 0;
$extra_args['tag'] = '';
foreach ( $instance['tag'] as $k => $v ) {
$count++;
if ( $count > 1 ) {
$extra_args['tag'] .= '+';
}
$extra_args['tag'] .= esc_attr( $v );
}
}
if ( isset( $instance['category'] ) && is_array( $instance['category'] ) ) {
$count = 0;
$extra_args['category']='';
foreach ( $instance['category'] as $k => $v ) {
$count++;
if ( $count > 1 ) {
$extra_args['category'] .= ',';
}
$extra_args['category'] .= esc_attr( $v );
}
}
}
add_action( 'pre_get_posts', array( 'WooSlider_Widget_RelatedPosts', 'remove_current_post' ) );
$html = wooslider( array( 'slider_type' => 'posts', 'smoothheight' => 'true', 'randomize' => 'true' ), $extra_args, false );
remove_action( 'pre_get_posts', array( 'WooSlider_Widget_RelatedPosts', 'remove_current_post' ) );
return $html;
} // End generate_slideshow()
public function save_widget_form ( $instance, $new_instance, $old_instance, $obj ) {
if ( isset( $new_instance['tags'] ) ) {
$instance['tags'] = $new_instance['tags'];
} else {
$instance['tags'] = false;
}
if ( isset( $new_instance['cats'] ) ) {
$instance['cats'] = $new_instance['cats'];
} else {
$instance['cats'] = false;
}
return $instance;
} // End save_widget_form()
public function widget_form_html ( $obj, $return, $instance ) {
global $return;
if(isset($obj->woo_widget_idbase) && $obj->woo_widget_idbase == 'wooslider_slideshow_relatedposts' && is_active_widget(false, false, $obj->woo_widget_idbase, true )) {
if ( ! isset( $instance['tags'] ) ) {
$instance['tags'] = 1;
}
if ( ! isset( $instance['cats'] ) ) {
$instance['cats'] = 1;
}
?>
<p>
<input id="<?php echo $obj->get_field_id('tags'); ?>" name="<?php echo $obj->get_field_name('tags'); ?>" type="checkbox" value="1" <?php checked( $instance['tags'], 1 ); ?>/>
<label for="<?php echo $obj->get_field_id('tags'); ?>"><?php _e('Filter by Tags'); ?></label>
</p>
<p>
<input id="<?php echo $obj->get_field_id('cats'); ?>" name="<?php echo $obj->get_field_name('cats'); ?>" type="checkbox" value="1" <?php checked( $instance['cats'], 1 ); ?>/>
<label for="<?php echo $obj->get_field_id('cats'); ?>"><?php _e('Filter by Categories'); ?></label>
</p>
<?php
}
$return = null;
} // End widget_form_html()
} // end class WooSlider_Widget_RelatedPosts
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment