Advertisement
teastudio

carousel-generator.class.php

Jul 29th, 2015
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.36 KB | None | 0 0
  1. <?php
  2. /*
  3. Author: Marcin Gierada
  4. Author URI: http://www.teastudio.pl/
  5. Author Email: [email protected]
  6. License: GPLv2 or later
  7. License URI: http://www.gnu.org/licenses/gpl-2.0.html
  8. */
  9. class WpPostsCarouselGenerator {  
  10.  
  11.    
  12.         public static function generateId() {
  13.                 return rand();
  14.         }
  15.    
  16.         public static function getDefaults() {
  17.                 return array(
  18.                             'id'                      => self::generateId(),
  19.                             'template'                => 'default.css',
  20.                             'post_type'               => 'post',
  21.                             'ordering'                => 'asc',
  22.                             'categories'              => '',
  23.                             'tags'                    => '',
  24.                             'all_items'               => 10,
  25.                             'show_only'               => 'id',
  26.  
  27.                             'show_title'              => 'true',
  28.                             'show_created_date'       => 'true',
  29.                             'show_description'        => 'excerpt',
  30.                             'show_category'           => 'true',
  31.                             'show_tags'               => 'false',
  32.                             'show_more_button'        => 'true',    
  33.                             'show_featured_image'     => 'true',
  34.                             'image_source'            => 'thumbnail',            
  35.                             'image_width'             => 100,
  36.                             'image_height'            => 100,  
  37.  
  38.                             'items_to_show'           => 4,
  39.                             'loop'                    => 'true',
  40.                             'auto_play'               => 'true',
  41.                             'stop_on_hover'           => 'true',
  42.                             'auto_play_timeout'       => 1200,
  43.                             'nav'                     => 'true',
  44.                             'nav_speed'               => 800,  
  45.                             'dots'                    => 'true',
  46.                             'dots_speed'              => 800,
  47.                             'margin'                  => 5,
  48.                             'lazy_load'               => 'false',
  49.                             'mouse_drag'              => 'true',
  50.                             'mouse_wheel'             => 'true',
  51.                             'touch_drag'              => 'true',
  52.                             'slide_by'                => 1,
  53.                             'easing'                  => "linear"
  54.                            );        
  55.         }
  56.    
  57.    
  58.         public static function generate($atts) {
  59.                 global $post;
  60.                
  61.                
  62.                 /*
  63.                  * default parameters
  64.                  */
  65.                 $params = self::prepareSettings($atts);
  66.  
  67.                 /*
  68.                  * fix to previous versions
  69.                  */
  70.                 if ( array_key_exists('show_description', $params) && in_array( $params['show_description'], array('true', 'false') ) ) {
  71.                         $params['show_description'] = $params['show_description'] == 'true' ? 'excerpt' : 'false';
  72.                 }
  73.                
  74.                 /*
  75.                  * post type
  76.                  */
  77.                 $post_type = $params['post_type'] ? $params['post_type'] : 'post';
  78.                 $post_type_category = $params['post_type'].'_category';  
  79.                 $post_type_tag = $params['post_type'].'_tag';  
  80.                
  81.                 if ($post_type === 'post') {
  82.                         $post_type_category = 'category';
  83.                 }
  84.                        
  85.                 /*
  86.                  * print styles
  87.                  */
  88.                 //wp_print_scripts('owl.carousel');
  89.                 //wp_print_styles('owl.carousel.style');
  90.        
  91.                 /*
  92.                  * theme
  93.                  */
  94.                 $theme =  $params['template'];
  95.                 $theme_name = str_replace('.css', '', $theme);
  96.  
  97.                 /*
  98.                  * check if template css file exists
  99.                  */
  100.                 $plugin_theme_url = plugins_url( dirname(plugin_basename(__FILE__)) ) . '/templates/' . $theme;
  101.                 $plugin_theme_file = plugin_dir_path( __FILE__ ) . '/templates/'. $theme;
  102.                
  103.                 $site_theme_url = get_template_directory_uri() . '/css/wp-posts-carousel/' . $theme;
  104.                 $site_theme_file = get_template_directory() . '/css/wp-posts-carousel/' . $theme;                
  105.  
  106.                 if ( @file_exists($plugin_theme_file) ) {
  107.                         wp_enqueue_style( 'wp_posts_carousel-carousel-style-'. $theme_name, $plugin_theme_url, true );
  108.                 } else if ( @file_exists($site_theme_file) ) {
  109.                         wp_enqueue_style( 'wp_posts_carousel-carousel-style-'. $theme_name, $site_theme_url, true );                        
  110.                 } else {
  111.                         return '<div class="error"><p>'. sprintf( __('Theme - %s.css stylesheet is missing.', 'wp-posts-carousel'), $theme_name ) .'</p></div>';
  112.                 }        
  113.        
  114.                 /*
  115.                  * prepare html and loop
  116.                  */
  117.                 $out = '<div id="wp-posts-carousel-'. $params['id'] .'" class="'. $theme_name .'-theme wp-posts-carousel owl-carousel">';
  118.  
  119.                 /*
  120.                  * prepare sql query
  121.                  */
  122.                 $sql_array = array('post_type'      =>  $post_type,              
  123.                                    'post_status'    =>  'publish',                                  
  124.                                    'posts_per_page' =>  $params['all_items'],
  125.                                    'no_found_rows'  =>  1,
  126.                                    //'post__not_in' =>  array($post->ID) //exclude current post
  127.                                    );
  128.  
  129.                 $sql_i = 0;
  130.                 if ($params['categories'] != "" || $params['tags'] != "") {
  131.                         $sql_array['tax_query'] = array('relation' => 'AND', array());
  132.                 }
  133.                
  134.                 if ($params['categories'] != "") {
  135.                         $sql_array['tax_query'][$sql_i++] = array('taxonomy'  =>  $post_type_category,
  136.                                                                   'field'     =>  'id',
  137.                                                                   'terms'     =>  explode(',', $params['categories']),
  138.                                                                   'operator'  => 'IN'
  139.                                                            );
  140.                 }
  141.                
  142.                 if ($params['tags'] != "") {
  143.                         $sql_array['tax_query'][$sql_i++] = array('taxonomy'  =>  $post_type_tag,
  144.                                                                   'field'     =>  'name',
  145.                                                                   'terms'     =>  explode(',', $params['tags']),
  146.                                                                   'operator'  => 'IN'
  147.                                                             );
  148.                 }                
  149.  
  150.                 switch($params['show_only']) {
  151.                         case "id":
  152.                                 $sql_array['orderby'] = 'ID';
  153.                                 break;  
  154.                        
  155.                         case "newest":
  156.                                 $sql_array['orderby'] = 'post_date';
  157.                                 break;  
  158.                         case "title":
  159.                         default:
  160.                                 $sql_array['orderby'] = 'post_title';
  161.                                 break;
  162.                 }
  163.  
  164.                 if( in_array($params['ordering'], array('asc', 'desc')) ) {
  165.                         $sql_array['order'] = $params['ordering'];
  166.                 }else {                    
  167.                         $sql_array['order'] = 'desc';
  168.                 }
  169.                 /*
  170.                  * end sql query
  171.                  */
  172.                
  173.                 /*
  174.                  * display popular posts from Wordrpess Popular Posts
  175.                  * period: 1 MONTH from now
  176.                  */
  177.                 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  178.                 if( $params['show_only'] === "popular" && is_plugin_active( 'wordpress-popular-posts/wordpress-popular-posts.php' ) ) {
  179.                         /*
  180.                          * include custom queries
  181.                          */
  182.                         require_once("includes/wp-posts-carousel-popular-posts-query.class.php");
  183.                         $loop = new WP_Posts_Carousel_Popular_Posts_Query($sql_array);
  184.                 } else {
  185.                         $loop = new WP_Query($sql_array);
  186.                 }                
  187.  
  188.                 /*
  189.                  * if random, we shuffle array
  190.                  */
  191.                 if($params['ordering'] === "random") {
  192.                         shuffle($loop->posts);
  193.                 }
  194.                
  195.                 /*
  196.                  * check if there are more then one item
  197.                  */
  198.                 if(!$loop->post_count > 1) {
  199.                     return false;
  200.                 }
  201.                
  202.                 /*
  203.                  * products loop
  204.                  */
  205.                 while($loop->have_posts()) {
  206.                         $loop->the_post();            
  207.    
  208.                         $post_url = get_permalink($post->ID);
  209.                         $title = '';
  210.                         $featured_image = '';
  211.                         $description = '';
  212.                         $tags = '';
  213.                         $created_date = '';
  214.                         $category = '';
  215.                         $buttons = '';
  216.  
  217.                         $image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),$params['image_source']);
  218.  
  219.                         /*
  220.                          * if no featured image for the product
  221.                          */
  222.                         if ($image[0] == '' || $image[0] == '/') {
  223.                                 $image[0] = plugin_dir_url( __FILE__ ).'images/placeholder.png';  
  224.                         }
  225.  
  226.                         /*
  227.                          * show title
  228.                          */
  229.                         if ($params['show_title'] === 'true') {
  230.                                 $title = '<h3 class="wp-posts-carousel-title">';
  231.                                         $title .= '<a href="'. $post_url .'" title="'.$post->post_title.'">'.$post->post_title.'</a>';
  232.                                 $title .= '</h3>';                
  233.                         }
  234.                        
  235.                         /*
  236.                          * show title
  237.                          */
  238.                         if ($params['show_category'] === 'true') {
  239.                                 $categories = get_the_terms($post->ID, $post_type_category);
  240.                                
  241.                                 if ($categories) {
  242.                                         $category = '<p class="wp-posts-carousel-categories">';
  243.                                                 foreach ($categories as $cat) {
  244.                                                         $category .= '<a href="'.  get_category_link( $cat->term_id ).'" title="' . esc_attr( sprintf( __( "View all items in %s" ), $cat->name ) ) . '">'.$cat->name.'</a> ';
  245.                                                 }
  246.                                         $category .= '</p>';  
  247.                                 }
  248.                         }                        
  249.                        
  250.                         /*
  251.                          * show tags
  252.                          */                        
  253.                         if ($params['show_tags'] == 'true') {        
  254.                                 $tags = '<p class="wp-posts-carousel-tags">';
  255.                                         $tags .= get_the_term_list(get_the_ID(), $post_type_tag, '', ' ', '' );
  256.                                 $tags .= '</p>';                                  
  257.                         }                          
  258.                        
  259.                         /*
  260.                          * show created date
  261.                          */
  262.                         if ($params['show_created_date'] === 'true') {
  263.                                 $created_date = '<p class="wp-posts-carousel-created-date">';
  264.                                         $created_date .= get_the_date();
  265.                                 $created_date .= '</p>';                
  266.                         }    
  267.                        
  268.                         if ($params['show_featured_image'] === 'true') {
  269.                                 $featured_image = '<div class="wp-posts-carousel-image">';
  270.                                         $featured_image .= '<a href="'. $post_url .'" title="'. __('Read more', 'wp-posts-carousel') .' '. $post->post_title .'">';
  271.                                                 $featured_image .= '<img src="'. $image[0] .'" alt="'. $post->post_title .'" style="max-width:'. $params['image_width'] .'%;max-height:'. $params['image_height'] .'%">';
  272.                                         $featured_image .= '</a>';
  273.                                 $featured_image .= '</div>';
  274.                         }
  275.                        
  276.  
  277.                         /*
  278.                          * show excerpt or full content
  279.                          */
  280.                         if ($params['show_description'] === 'excerpt') {
  281.                                 $description = '<div class="wp-posts-carousel-desc">'. $post->post_excerpt .'</div>';
  282.                         } else if ($params['show_description'] === 'content') {
  283.                                 $description = '<div class="wp-posts-carousel-desc">'. get_the_content() .'</div>';
  284.                         }
  285.                        
  286.                        
  287.                         /*
  288.                          * show button
  289.                          */
  290.                         if ($params['show_more_button'] === 'true') {    
  291.                                 $buttons = '<p class="wp-posts-carousel-buttons">';
  292.                                         $buttons .= '<a href="'. $post_url .'" class="wp-posts-carousel-more-button button" title="'. __('Read more', 'wp-posts-carousel') .' '.$post->post_title.'">'. __('read more', 'wp-posts-carousel') .'</a>';
  293.                                 $buttons .= '<p>';
  294.                         }
  295.            
  296.  
  297.                         /*
  298.                          * list products
  299.                          */
  300.                         $out .= '<div class="wp-posts-carousel-slide slides-'. $params['items_to_show'] .'">';
  301.                                 $out .= '<div class="wp-posts-carousel-container">';
  302.                                        
  303.                                         $out .= $featured_image;
  304.  
  305.                                         $out .= '<div class="wp-posts-carousel-details">';
  306.                                                 $out .= $title;                                                
  307.                                                 $out .= $created_date;
  308.                                                 $out .= $category;
  309.                                                 $out .= $description;  
  310.                                                 $out .= $tags;                                                                                              
  311.                                                 $out .= $buttons;              
  312.                                         $out .= '</div>';
  313.                                 $out .= '</div>';
  314.                         $out .= '</div>';
  315.                 }
  316.                 $out .= '</div>';        
  317.        
  318.                 /*
  319.                  * generate jQuery script for FlexCarousel        
  320.                  */
  321.                 $out .= self::carousel($params);  
  322.                 return $out;
  323.         }
  324.    
  325.         static function carousel($params = array()) {
  326.                 if (empty($params)) {
  327.                         return false;
  328.                 }
  329.                 $mouse_wheel = null;
  330.  
  331.                 if ($params['mouse_wheel'] == 'true') {
  332.                         $mouse_wheel = 'wpPostsCarousel'. $params['id'] .'.on("mousewheel", ".owl-stage", function(e) {
  333.                                        if (e.deltaY > 0) {
  334.                                            wpPostsCarousel'. $params['id'] .'.trigger("next.owl");
  335.                                        } else {
  336.                                            wpPostsCarousel'. $params['id'] .'.trigger("prev.owl");
  337.                                        }
  338.                                        e.preventDefault();
  339.                                        });';  
  340.                 }
  341.  
  342.                 $out = '<script type="text/javascript">        
  343.                    jQuery(document).ready(function(e) {            
  344.                        var wpPostsCarousel'. $params['id'] .' = jQuery("#wp-posts-carousel-'.$params['id'].'");
  345.                        wpPostsCarousel'. $params['id'] .'.owlCarousel({
  346.                            loop: '. $params['loop'] .',
  347.                            nav: '. $params['nav'] .',
  348.                            navSpeed: '. $params['nav_speed'] .',
  349.                            dots: '. $params['dots'] .',
  350.                            dotsSpeed: '. $params['dots_speed'] .',
  351.                            lazyLoad: '. $params['lazy_load'] .',
  352.                            autoplay: '. $params['auto_play'] .',
  353.                            autoplayHoverPause: '. $params['stop_on_hover'] .',
  354.                            autoplayTimeout: '. $params['auto_play_timeout'] .',
  355.                            autoplaySpeed:  '. $params['auto_play_timeout'] .',
  356.                            margin: '. $params['margin'] .',
  357.                            stagePadding: 0,
  358.                            freeDrag: false,      
  359.                            mouseDrag: '. $params['mouse_drag'] .',
  360.                            touchDrag: '. $params['touch_drag'] .',
  361.                            slideBy: '. $params['slide_by'] .',
  362.                            fallbackEasing: "'. $params['easing'] .'",
  363.                            responsiveClass: true,                    
  364.                            navText: [ "'. __('previous', 'wp-posts-carousel') .'", "'. __('next', 'wp-posts-carousel') .'" ],
  365.                            responsive:{
  366.                                0:{
  367.                                    items: 1
  368.                                },
  369.                                600:{
  370.                                    items: '. ceil($params['items_to_show']/2) .',
  371.  
  372.                                },
  373.                                1000:{
  374.                                    items: '. $params['items_to_show'] .'
  375.                                }
  376.                            },
  377.                            autoHeight: true
  378.                        });
  379.                        '. $mouse_wheel .'
  380.                    });  
  381.                </script>';
  382.  
  383.                 return $out;
  384.         }    
  385.    
  386.    
  387.         public static function prepareSettings($settings) {                
  388.                 $checkboxes = array(
  389.                                     'show_title'              => 'true',
  390.                                     'show_created_date'       => 'true',
  391.                                     'show_category'           => 'true',
  392.                                     'show_tags'               => 'false',
  393.                                     'show_more_button'        => 'true',
  394.                                     'show_featured_image'     => 'true',        
  395.  
  396.                                     'loop'                    => 'true',
  397.                                     'auto_play'               => 'true',
  398.                                     'stop_on_hover'           => 'true',
  399.                                     'nav'                     => 'true',
  400.                                     'dots'                    => 'true',
  401.                                     'lazy_load'               => 'false',
  402.                                     'mouse_drag'              => 'true',
  403.                                     'mouse_wheel'             => 'true',
  404.                                     'touch_drag'              => 'true'
  405.                                     );
  406.  
  407.                 foreach($checkboxes as $k => $v) {
  408.                         if (!array_key_exists($k, $settings)) {
  409.                                 $settings[$k] = 'false';
  410.                         } else {
  411.                                 $settings[$k] = ($settings[$k] == 1 || $settings[$k] == 'true') ? 'true' : 'false';
  412.                         }
  413.                 }
  414.  
  415.                 $settings['id'] = self::generateId();
  416.                
  417.                 /*
  418.                  * if there are no all settings
  419.                  */
  420.                 $defaults = self::getDefaults();
  421.                 foreach($defaults as $k => $v) {
  422.                     if (!array_key_exists($k, $settings)) {
  423.                         $settings[$k] = $defaults[$k];
  424.                     }
  425.                 }
  426.                 return $settings;
  427.         }
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement