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 = '
'; 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 .= '
  • '; if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail($post->ID)) ){ $output .= get_the_post_thumbnail($post->ID, 'thumbnail', array('class' => 'apc_thumb', 'title' => $post->post_title, 'alt' => $post->post_excerpt)); }else{ $w = get_option('thumbnail_size_w'); $h = get_option('thumbnail_size_h'); $output .= ''. $post->post_excerpt.''; } $output .= ''; if ( $show_title ){ $output .= '
    '.$post->post_title.'
    '; } if ( $show_excerpt ){ if ( ! $post->post_excerpt){ $excerpt_text = strip_shortcodes( $post->post_content ); $excerpt_text = apply_filters('the_content', $excerpt_text); $excerpt_text = strip_tags(str_replace(']]>', ']]>', $excerpt_text)); $words = preg_split("/[\n\r\t ]+/", $excerpt_text, 56, PREG_SPLIT_NO_EMPTY); if ( count($words) > 55 ) { array_pop($words); $excerpt_text = implode(' ', $words); $excerpt_text = $excerpt_text . ' ...'; } else { $excerpt_text = implode(' ', $words); } }else{ $excerpt_text = $post->post_excerpt; } $output .= apply_filters('the_excerpt', $excerpt_text.' [+]'); } } return $output; } function update($new_instance, $old_instance){ $val_int_options = array( 'options' => array( 'min_range' => 1 ) ); $instance = $old_instance; $title = filter_var(strip_tags($new_instance['title']), FILTER_SANITIZE_STRING); if ($title !== false){ $instance['title'] = $title; } $instance['random'] = filter_var($new_instance['random'], FILTER_VALIDATE_BOOLEAN); $visible_posts = filter_var(strip_tags($new_instance['visible_posts']), FILTER_VALIDATE_INT, $val_int_options); if ( $visible_posts !== false ){ $instance['visible_posts'] = $visible_posts; } $init_posts = filter_var(strip_tags($new_instance['init_posts']), FILTER_VALIDATE_INT, $val_int_options); if ( $init_posts !== false ){ $instance['init_posts'] = $init_posts; } $instance['show_title'] = filter_var($new_instance['show_title'], FILTER_VALIDATE_BOOLEAN); $instance['show_excerpt'] = filter_var($new_instance['show_excerpt'], FILTER_VALIDATE_BOOLEAN); $instance['loop'] = filter_var($new_instance['loop'], FILTER_VALIDATE_BOOLEAN); $instance['post_type'] = filter_var(strip_tags($new_instance['post_type']), FILTER_SANITIZE_STRING); $instance['category'] = filter_var(strip_tags($new_instance['category']), FILTER_SANITIZE_STRING); $taxonomies = get_taxonomies(array('_builtin' => false), 'objects'); foreach ($taxonomies as $tax ) { $instance[$tax->query_var] = filter_var(strip_tags($new_instance[$tax->query_var]), FILTER_SANITIZE_STRING); } return $instance; } function form($instance){ $defaults = array('title' => 'Previous Posts', 'random' => false, 'visible_posts' => 3, 'init_posts' => 9, 'show_title' => false, 'show_excerpt' => false, 'loop' => false, 'post_type' => 'post', 'category' => 'all'); $instance = wp_parse_args((array) $instance, $defaults); ?>

    false), 'objects'); foreach ($taxonomies as $tax ) { //print_r ($tax); $name = $this->get_field_name($tax->query_var); echo '

    '; } } function register(){ register_widget( 'Ajax_Post_Carousel' ); } function add_scripts(){ wp_enqueue_script('ajax_post_carousel_js', plugins_url('ajax_post_carousel.js', __FILE__), array('jquery')); //load css file from the theme folder or the plugin folder if ( file_exists( get_stylesheet_directory()."/ajax_post_carousel.css" ) ) { wp_enqueue_style( 'ajax_post_carousel_style', get_stylesheet_directory_uri() . '/ajax_post_carousel.css' ); }elseif ( file_exists( get_template_directory()."/ajax_post_carousel.css" ) ) { wp_enqueue_style( 'ajax_post_carousel_style', get_template_directory_uri() . '/ajax_post_carousel' ); }else{ wp_enqueue_style('ajax_post_carousel_style', plugins_url('ajax_post_carousel.css', __FILE__)); } } //admin page /*function admin_actions(){ add_theme_page('Ajax Post Carousel', 'Ajax Post Carousel', 'edit_themes', 'ajax-post-carousel', array('Ajax_Post_Carousel', 'admin_page')); } function admin_page(){ include('apc_admin.php'); }*/ //AJAX function function ajax_apc_get_posts() { $get_posts_args = array( 'numberposts' => $_POST['num'], 'offset' => $_POST['offset'] ); if ($_POST['post_type'] == 'all'){ $get_posts_args['post_type'] = 'any'; }else{ $get_posts_args['post_type'] = $_POST['post_type']; } if ($_POST['category'] != 'all'){ $get_posts_args['category_name'] = $_POST['category']; } foreach ($_POST as $key => $val){ if (substr($key, 0, 4) == 'tax_'){ $get_posts_args[substr($key, 4)] = $val; } } $posts = get_posts($get_posts_args); echo self::__display_items($posts, $_POST['title'], $_POST['excerpt']); // IMPORTANT: don't forget to "exit" exit; } } ?>