<?php

/**
 * WP_Bootstrap_Carousel_DPS class
 *
 * This class is an addon that optionally transforms post listings generated by Bill Erickson's
 * "Display Posts Shortcode" plugin in a Bootstrap Carousel slideshow.
 *
 * @author Peter J. Herrel  (email : peterherrel [at] gmail.com)
 * @link http://peterherrel.com/wordpress/plugins/wp-bootstrap-carousel
 *
 * To download the "Display Posts Shortcode" plugin, visit:
 * http://wordpress.org/extend/plugins/display-posts-shortcode/
 * Note: make sure you download version 2.2.1 or higher
 *
 * For more info on how to customize the output of the shortcode, visit:
 * https://github.com/billerickson/display-posts-shortcode/wiki
 */
 
/**
 * 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_DPS' ) ) {

/**
 * WP_Bootstrap_Carousel_DPS class
 */
class WP_Bootstrap_Carousel_DPS
{
    function WP_Bootstrap_Carousel_DPS()
    {
        $this->__construct();
    }
    function __construct()
    {
        if ( ! is_admin() || defined( 'DOING_AJAX' ) ) :
        
            add_action( 'init', array( &$this, 'init' ) );
            
        endif;
    }
    function init()
    {
        add_filter( 'display_posts_shortcode_args',             array( &$this, 'display_posts_shortcode_args' ),            10, 1 );
        add_filter( 'display_posts_shortcode_wrapper_open',     array( &$this, 'display_posts_shortcode_wrapper_open' ),    11, 2 );
        add_filter( 'display_posts_shortcode_output',           array( &$this, 'display_posts_shortcode_output' ),          12, 9 );
        add_filter( 'display_posts_shortcode_wrapper_close',    array( &$this, 'display_posts_shortcode_wrapper_close' ),   13, 2 );
    }
    function display_posts_shortcode_args( $original_atts )
    {
        $bootstrap = array(
            'bootstrap' => 0,
            'interval'  => 5000,
            'pause'     => 'hover',
            'thickbox'  => 0
        );
        $original_atts = array_merge( $bootstrap, $original_atts );
        
        return $original_atts;
    }
    function display_posts_shortcode_output( $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class )
    {
        if ( $original_atts['bootstrap'] != 1 || is_feed() )
            return $output;

        global $post;

        if( ! post_type_supports( get_post_type( $post ), 'thumbnail' ) )
            continue;
        
        $image_size     = ( isset( $original_atts['image_size'] ) ? sanitize_text_field( $original_atts['image_size'] ) : 'large' );
        $thickbox       = ( isset( $original_atts['thickbox'] ) ? (bool)$original_atts['thickbox'] : 0 );
        $src            = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), $image_size );
        $link           = ( $thickbox ) ? $src[0] : get_permalink();
        
        $inner_wrapper  = 'div';
        $image          = '<a class="image' . ( ( $thickbox == 1 ) ? " thickbox" : "" ) . '" href="' . $link . '">' . get_the_post_thumbnail( $post->ID, $image_size ) . '</a> ';
        
        //$title = '<a class="title' . ( ( $thickbox == 1 ) ? " thickbox" : "" ) . '" href="' . $link . '">' . get_the_title() . '</a>';
        //$comments_link = '<span class="carousel-comments-link">...</span>';

        $output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . ' item active">' . $image . '<div class="carousel-caption"><h4>' . $title . '</h4><p>' . $date . $excerpt . '</p></div></' . $inner_wrapper . '>';

        return $output;
    }
    function display_posts_shortcode_wrapper_open( $output, $original_atts )
    {
        if ( $original_atts['bootstrap'] != 1 || is_feed() )
            return $output;
            
        global $wp_bootstrap_carousel;

        $interval   = ( isset( $original_atts['interval'] ) ? intval( $original_atts['interval'] ) : 5000 );
        $pause      = ( isset( $original_atts['pause'] )    ? sanitize_text_field( $original_atts['pause'] ) : 'hover' );
        $thickbox   = ( isset( $original_atts['thickbox'] ) ? (bool)$original_atts['thickbox'] : 0 );

        // generate an ID for the carousel
        $md5 = substr( md5( rand() ), 0, 7);
        update_option( 'wp_bc_dps_md5', $md5 );
        
        $output = '';
        $output .= $wp_bootstrap_carousel->enqueue( $interval, $pause, $thickbox );
        $output .= $wp_bootstrap_carousel->style();

        $output .= '<div id="wp-bootstrap-carousel-dps-' . get_option( 'wp_bc_dps_md5' ) . '" class="carousel carousel-dps">
            <div class="carousel-inner carousel-inner-dps">';
            
        return $output;
    }
    function display_posts_shortcode_wrapper_close( $output, $original_atts )
    {
        if ( $original_atts['bootstrap'] != 1 || is_feed() )
            return $output;
            
        $output = '';
        $output .= '</div>
            <a class="carousel-control carousel-control-dps left" data-slide="prev" href="#wp-bootstrap-carousel-dps-' . get_option( 'wp_bc_dps_md5' ) . '">' . __( '&lsaquo;', 'wp_bootstrap_carousel' ) . '</a>
            <a class="carousel-control carousel-control-dps right" data-slide="next" href="#wp-bootstrap-carousel-dps-' . get_option( 'wp_bc_dps_md5' ) . '">' . __( '&rsaquo;', 'wp_bootstrap_carousel' ) . '</a>
        </div>';

        // delete option that contains carousel ID
        delete_option( 'wp_bc_dps_md5' );
        
        return $output;
    }
} // class

} // class_exists check