Author URI: http://codigoweb.co
*/
//error_reporting(E_ALL);
//admin menu
//add_action('admin_menu', array('Ajax_Post_Carousel', 'admin_actions'));
//js and css
add_action('template_redirect', array('Ajax_Post_Carousel', 'add_scripts'));
//register widget
add_action("widgets_init", array('Ajax_Post_Carousel', 'register'));
//register shortcode
add_shortcode('apc-carousel', array('Ajax_Post_Carousel', 'shortcode'));
//AJAX
add_action( 'wp_ajax_nopriv_ajax_apc_get_posts', array('Ajax_Post_Carousel', 'ajax_apc_get_posts') );
add_action( 'wp_ajax_ajax_apc_get_posts', array('Ajax_Post_Carousel', 'ajax_apc_get_posts') );
class Ajax_Post_Carousel extends WP_Widget{
function Ajax_Post_Carousel() {
$widget_ops = array('classname' => 'apc_widget', 'description' => 'Widget that displays posts as a carousel using jQuery for animations.');
$control_ops = array( 'id_base' => 'ajax-post-carousel' );
$this->WP_Widget( 'ajax-post-carousel', 'Ajax Post Carousel', $widget_ops, $control_ops );
}
function show_carousel($random=0, $visible_posts=3, $init_posts=9, $show_title=0, $show_excerpt=0, $loop=0, $post_type='post', $category='all', $tax_filter=''){
$output =
'
←
';
//array for get_posts function
$get_posts_args = array();
//array of requested taxonomies, to be used in count function
$taxonomies_for_count = array();
if ($tax_filter){
//get the taxonomies requested by the user
$tax_filter_array = explode("&", $tax_filter);
foreach ($tax_filter_array as $tax){
$tax_slug = strtok($tax, '=');
$term = strtok('=');
$get_posts_args[$tax_slug] = $term;
$taxonomies_for_count[] = $term;
}
}
//if post type and category, then added to the args of get_post function
if ($post_type == 'all'){
$get_posts_args['post_type'] = 'any';
}else{
$get_posts_args['post_type'] = $post_type;
}
if ($category != 'all'){
$get_posts_args['category_name'] = $category;
}
//before random, have to get total posts
$total_posts = self::__apc_count_posts($post_type, $category, $taxonomies_for_count);
if ( $random ){
$offset = mt_rand(0, $total_posts - 1);
}else{
$offset = 0;
}
$get_posts_args['numberposts'] = $init_posts;
$get_posts_args['offset'] = $offset;
//print_r($get_posts_args);
$posts = get_posts($get_posts_args);
$output .= self::__display_items($posts, $show_title, $show_excerpt);
//when the offset is to high and only a few posts (less than init_posts) are displayed, we have to get some more
$new_offset = $offset + $init_posts;
if ( $new_offset > $total_posts && $offset > 0 ){
//shows all the posts needed to complete the init_posts number, but shouldn't repeat the ones already shown
$get_posts_args['numberposts'] = min($new_offset - $total_posts, $offset);
$get_posts_args['offset'] = 0;
$posts = get_posts($get_posts_args);
$output .= self::__display_items($posts, $show_title, $show_excerpt);
}
$output .=
'
→
';
return $output;
}
function shortcode($args){
extract(shortcode_atts(array(
'random' => 0,
'visible_posts' => 3,
'init_posts' => 9,
'show_title' => 0,
'show_excerpt' => 0,
'loop' => 0,
'post_type' => 'post',
'category' => 'all',
), $args));
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
$tax_filter = '';
foreach ($taxonomies as $tax){
if (isset($args[$tax->query_var])){
$tax_filter .= '&'.$tax->query_var.'='.$args[$tax->query_var];
}
}
$tax_filter = substr($tax_filter, 1);
return self::show_carousel($random, $visible_posts, $init_posts, $show_title, $show_excerpt, $loop, $post_type, $category, $tax_filter);
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title'] );
$random = isset($instance['random']) ? $instance['random'] : false;
$visible_posts = $instance['visible_posts'];
$init_posts = $instance['init_posts'];
$show_title = isset($instance['show_title']) ? $instance['show_title'] : false;
$show_excerpt = isset($instance['show_excerpt']) ? $instance['show_excerpt'] : false;
$loop = isset($instance['loop']) ? $instance['loop'] : false;
$post_type = $instance['post_type'];
$category = $instance['category'];
echo $before_widget;
echo $before_title . $title . $after_title;
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
$tax_filter = '';
foreach ($taxonomies as $tax){
if ($instance[$tax->query_var] != 'all'){
$tax_filter .= '&'.$tax->query_var.'='.$instance[$tax->query_var];
}
}
$tax_filter = substr($tax_filter, 1);
echo $this->show_carousel($random, $visible_posts, $init_posts, $show_title, $show_excerpt, $loop, $post_type, $category, $tax_filter);
echo $after_widget;
}
function __apc_count_posts($post_type, $category, $taxonomies_for_count){
//categoy is treated as any other taxonomy
if ($category != 'all'){
$taxonomies_for_count[] = $category;
}
global $wpdb;
$SQL = "SELECT COUNT(DISTINCT p.ID) FROM $wpdb->posts AS p";
foreach($taxonomies_for_count as $key => $termslug){
$SQL .= ", $wpdb->term_relationships AS rel$key, $wpdb->term_taxonomy AS tax$key, $wpdb->terms AS term$key";
}
if ( ! empty($taxonomies_for_count)){
$SQL .= " WHERE p.ID=rel0.object_id";
}
foreach($taxonomies_for_count as $key => $termslug){
if ($key > 0){
$SQL .= " AND p.ID=rel$key.object_id";
}
$SQL .= " AND rel$key.term_taxonomy_id=tax$key.term_taxonomy_id AND tax$key.term_id=term$key.term_id AND term$key.slug='$termslug'";
}
if ( ! empty($taxonomies_for_count)){
$SQL .= " AND p.post_status='publish'";
}else{
$SQL .= " WHERE p.post_status='publish'";
}
if ($post_type != 'all'){
$SQL .= " AND p.post_type='$post_type'";
}else{
$SQL .= " AND (p.post_type='page' OR p.post_type='post'";
$custom_types = get_post_types(array('_builtin' => false));
foreach ($custom_types as $type ) {
$SQL .= " OR p.post_type='$type'";
}
$SQL .= ")";
}
//echo $SQL.' ';
return $wpdb->get_var($SQL);
}
function __display_items($posts, $show_title, $show_excerpt){
$output = '';
foreach ($posts as $post){
$output .= '