<?php
/*
Plugin Name: WP Bootstrap Carousel
Plugin URI: http://peterherrel.com/wordpress/plugins/wp-bootstrap-carousel
Description: A simple, straightforward implementation of the Twitter Bootstrap Carousel in WordPress. This plugin allows you to display images attached to a post or page as a Bootstrap Carousel using the [carousel] shortcode. As a bonus, this plugin can transform post listings generated by the <a href="http://wordpress.org/extend/plugins/display-posts-shortcode/">Display Posts Shortcode</a> plugin into a Bootstrap Carousel slideshow.
Version: 0.1.1
Author: Peter J. Herrel
Author URI: http://peterherrel.com/
License: GPL3
Text Domain: wp_bootstrap_carousel
Domain Path: /lang
*/

/**
 * @author Peter J. Herrel  (email : peterherrel [at] gmail.com)
 * 
 * @link http://wordpress.org/extend/plugins/wp-bootstrap-carousel/
 * @link https://github.com/diggy/wp-bootstrap-carousel/wiki/
 * @link http://peterherrel.com/wordpress/plugins/wp-bootstrap-carousel
 **/

/**
 * LICENSE
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 3, as 
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

if ( ! defined( 'ABSPATH' ) ) exit; 
 
if ( ! class_exists( 'WP_Bootstrap_Carousel' ) ) {

/**
 * WP_Bootstrap_Carousel class
 */
class WP_Bootstrap_Carousel
{
    var $version = '0.1.1';
    var $plugin_dir_url = '';
    
    function WP_Bootstrap_Carousel()
    {
        $this->__construct();
    }
    function __construct()
    {
        $this->plugin_dir_url = trailingslashit( plugins_url( dirname( plugin_basename( __FILE__ ) ) ) );
        
        if ( ! is_admin() || defined( 'DOING_AJAX' ) ) :
        
            add_action( 'init', array( &$this, 'init' ), 10 );
            
            if( function_exists( 'be_display_posts_shortcode' ) )
                $this->be_display_posts_plugin();
            
        endif;

        if ( is_admin() ) :
        
            add_filter( 'plugin_row_meta', array( &$this, 'plugin_row_meta' ), 10, 2 );
            
        endif;
    }
    function init()
    {
        add_shortcode( 'carousel', array( &$this, 'shortcode' ) );
        
        do_action( 'wp_bootstrap_carousel_init' );
    }
    function shortcode( $atts )
    {
        $data       = (array) $this->get_data( $atts );
        $items      = (array) $data['query'];
        $vars       = (array) $data['vars'];
        $parent     = isset( $vars['id'] ) && (int) $vars['id'] ? (int) $vars['id'] : '';
        $width_int  = str_replace( array( '%', 'px' ), '', $vars['width'] );
        $style      = $this->style();

        global $post;
        
        if( ! $parent || ! $items )
            return apply_filters( 'wp_bootstrap_carousel_no_results', false );

        if( is_feed() )
            return apply_filters( 
                'wp_bootstrap_carousel_feed',
                '<a href="' . get_permalink( $post ) . '#wp-bootstrap-carousel-' . $vars['id'] . '">' . __( 'Click here to view the embedded slideshow.', 'wp_bootstrap_carousel' ) . '</a>',
                $vars
            );

       $this->enqueue( $vars['interval'], $vars['pause'], $vars['thickbox'] );
        
        $carousel = '';

        $carousel .= $style;
        
        $carousel .= '<div id="wp-bootstrap-carousel-' . $vars['id'] . '" class="carousel' . ( ( $vars['slide'] ) ? " slide" : "" ) . '" style="width:' . $width_int . 'px;">
  <!-- Carousel items -->
  <div class="carousel-inner">';

        $i = 0;
        foreach ( $items as $item_id => $item )
        {
            $full   = wp_get_attachment_image_src( $item_id, 'full', false );               // full size
            $thumb  = wp_get_attachment_image_src( $item_id, $vars['image_size'], false );  // thumbnail size
            $link   = ( $vars['file'] ) ? $full[0] : get_attachment_link( $item_id );
            $text   = wpautop( wptexturize( $item->post_content ) );
			
		// banner link code 
		$link = 'warranty';

            $carousel .= '<div id="item-' . $item_id . '" class="' . ( ( $i == 0 ) ? "active" : "" ) . ' item">
               <a class="' . ( ( $vars['file'] && $vars['thickbox'] ) ? "thickbox " : "" ) . '" rel="' . $vars['rel'] . '" href="' . $link . '"><img src="' . $thumb[0] . '" width="' . $vars['width'] . '" alt="' . $item->post_title . '"/></a>';
                
            $carousel .= '<div class="carousel-caption">';
            
            $carousel .= '<h4>' . $item->post_title;
            
            if( $vars['comments'] && comments_open( $item_id ) ) :
            
                $comments = ( get_comments_number( $item_id ) != 0 ) ? sprintf( _n( '1 Comment', '%1$s Comments', get_comments_number( $item_id ), 'wp_bootstrap_carousel' ), number_format_i18n( get_comments_number( $item_id ) ) ) : __( '0 Comments', 'wp_bootstrap_carousel' );
                
                $carousel .= '<span class="carousel-comments-link">
                    <a href="' . get_comments_link( $item_id ) . '" rel="bookmark">' . $comments . '</a>
                </span>';
            
            endif;
            
            $carousel .= '</h4>';
            
            $carousel .= apply_filters( 'wp_bootstrap_carousel_caption_text', $text, $item_id );
            
            $carousel .= '</div>';
                
            $carousel .= '</div>';
            
            $i++;
        }
        
        $carousel .= '</div><!-- .carousel-inner -->
  <!-- Carousel nav -->
  <a class="carousel-control left" href="#wp-bootstrap-carousel-' . $vars['id'] . '" data-slide="prev">' . __( '&lsaquo;', 'wp_bootstrap_carousel' ) . '</a>
  <a class="carousel-control right" href="#wp-bootstrap-carousel-' . $vars['id'] . '" data-slide="next">' . __( '&rsaquo;', 'wp_bootstrap_carousel' ) . '</a>
</div>';
        
        return $carousel;
    }
    public function get_data( $atts )
    {
        global $post, $content_width;

        $id = intval( $post->ID );
        $thumbnail_id = get_post_meta( $id, '_thumbnail_id', true );
        
        $atts = shortcode_atts( apply_filters( 'wp_bootstrap_carousel_shortcode_atts', array(
        
            'post_parent'       => $id,
            'post_status'       => 'inherit',
            'post_type'         => 'attachment',
            'post_mime_type'    => 'image',
            'exclude'           => $thumbnail_id,
            'order'             => 'ASC',
            'orderby'           => 'ID',
            
            'width'             => ( isset( $content_width ) ) ? $content_width : '100%',
            'image_size'        => 'large',
            'rel'               => '',
            'file'              => 1,
            'comments'          => 1,
            'slide'             => 1,
            
            'interval'          => 5000,
            'pause'             => 'hover',
            'thickbox'          => 1,
            
        ) ), $atts);

        // query vars
        $post_parent    = intval( $atts['post_parent'] );
        $post_status    = $atts['post_status'];
        $post_type      = sanitize_text_field( $atts['post_type'] );
        $post_mime_type = sanitize_text_field( $atts['post_mime_type'] );
        $exclude        = array_map( 'intval', explode( ',', $atts['exclude'] ) );
        $order          = sanitize_key( $atts['order'] );
        $orderby        = sanitize_key( $atts['orderby'] );

        if ( 'RAND' == $order )
            $orderby = 'none';
        
        // display vars
        $width          = intval( $atts['width'] );
        $image_size     = sanitize_text_field( $atts['image_size'] );
        $rel            = sanitize_text_field( $atts['rel'] );
        $file           = (bool)$atts['file'];
        $comments       = (bool)$atts['comments'];
        $slide          = (bool)$atts['slide'];
        
        // js vars
        $interval       = intval( $atts['interval'] );
        $pause          = sanitize_text_field( $atts['pause'] );
        $thickbox       = (bool)$atts['thickbox'];
        
        // query vars array
        $args = array(
            'post_parent'       => $post_parent,
            'post_status'       => $post_status,
            'post_type'         => $post_type,
            'post_mime_type'    => $post_mime_type,
            'exclude'           => $exclude,
            'order'             => $order,
            'orderby'           => $orderby
        );
        
        // display vars array
        $vars = array(
            'id'                => $post_parent,
            'width'             => $width,
            'image_size'        => $image_size,
            'rel'               => $rel,
            'file'              => $file,
            'comments'          => $comments,
            'slide'             => $slide,

            'interval'          => $interval,
            'pause'             => $pause,
            'thickbox'          => $thickbox,
        );
        
        // data array
        $data = array( 
            'query' => get_children( $args ),
            'vars' => $vars
        );
        
        return $data;
    }
    public function enqueue( $interval, $pause, $thickbox )
    {
        $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
        
        // enqueue jQuery
        wp_enqueue_script( 'jquery' );
        
        // bootstrap styles & scripts
        wp_enqueue_style( 'wp-bootstrap-carousel', $this->plugin_dir_url . 'css/carousel' . $min . '.css', array(), NULL, 'screen' );
        wp_enqueue_script( 'wp-bootstrap-carousel', $this->plugin_dir_url . 'js/carousel' . $min . '.js', array( 'jquery' ), NULL, true );
        wp_enqueue_script( 'wp-bootstrap-carousel-init', $this->plugin_dir_url . 'js/carousel-init.js', array( 'jquery', 'wp-bootstrap-carousel' ), NULL, true );

        // thickbox styles & script
        if( $thickbox ) :
        
            wp_enqueue_style( 'thickbox' );
            // wp_enqueue_script( 'thickbox' );
        
        endif;
        
        // JS variables
        $wp_bootstrap_carousel_js_vars = array(
            'interval' 	=> $interval,
            'pause' 	=> $pause
        );
        wp_localize_script( 'wp-bootstrap-carousel-init', 'wp_bootstrap_carousel_js_vars', apply_filters( 'wp_bootstrap_carousel_js_vars', $wp_bootstrap_carousel_js_vars ) );

        // action hook
        do_action( 'wp_bootstrap_carousel_enqueue' );
    }
    function style()
    {
        $rtl = ( ( is_rtl() ) ? "left" : "right" );
        
        $style = '';
        $style .= apply_filters( 
            'wp_bootstrap_carousel_extra_style', 
            '<style type="text/css">.carousel,.carousel a:link,.carousel a:hover,.carousel a:visited{color:#fff !important}
a.carousel-control{text-decoration:none;font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;font-weight:100;}
a.carousel-control:focus{outline:none}
.carousel-comments-link{float:' . $rtl . ';clear:' . $rtl . '}
.carousel-comments-link a:link,.carousel-comments-link a:visited,.carousel-comments-link a:hover{color:#fff;font-weight:normal;text-decoration:none}
.carousel-caption h4, .carousel-caption h4 a:link {color:#fff !important;text-decoration:none !important}</style>', $rtl
        );
        
        return $style;
    }
    public function be_display_posts_plugin()
    {
        if ( ! class_exists( 'WP_Bootstrap_Carousel_DPS' ) )
            include( 'inc/wp-bootstrap-carousel-dps.php' );
            
        return new WP_Bootstrap_Carousel_DPS();
    }
    public function plugin_row_meta( $links, $file )
    {
        $plugin = plugin_basename( __FILE__ );
        if ( $plugin === $file ) {
            $url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=JMEK9E362ALYJ';
            $links[] = sprintf( __( '<a href="%1$s">Donate</a>', 'wp_bootstrap_carousel' ), esc_url( $url ) );
        }
        return $links;
    }
} // class

/**
 * Init WP_Bootstrap_Carousel class
 *
 * Initializes the main plugin class
 *
 * @since WP Bootstrap Carousel 0.1
 */

$GLOBALS['wp_bootstrap_carousel'] = new WP_Bootstrap_Carousel();

} // class_exists check