<?php
/**
 * Yeah Shortcode function
 *
 * @author		Yeahthemes
 * @copyright	GPL & GNU 2.0 +
 * @link		http://yeahthemes.com
 * @since		Version 1.0
 * @package 	Yeah shortcodes
 */

/*
 * Exit if accessed directly
 */
if ( ! defined( 'ABSPATH' ) ) exit; 

/**
 * Returns current plugin version.
 * 
 * @return string Plugin version
 */

if ( ! function_exists( 'yt_shortcode_unautop' ) ) {
	function yt_shortcode_unautop( $content ) {
		$content = do_shortcode( shortcode_unautop( $content ) );
		$content = preg_replace( '#^<\/p>|^<br \/>|<p>$#', '', $content );
		return $content;
	} 
}

add_shortcode( 'button', 'yt_shortcode_button' );
add_shortcode( 'alert', 'yt_shortcode_alert' );
add_shortcode( 'label', 'yt_shortcode_label' );
add_shortcode( 'progress_bars', 'yt_shortcode_progress_bars' );
add_shortcode( 'progress_bar', 'yt_shortcode_progress_bar' );
add_shortcode( 'list_group', 'yt_shortcode_list_group' );
add_shortcode( 'list_item', 'yt_shortcode_list_item' );
add_shortcode( 'panel', 'yt_shortcode_panel' );
add_shortcode( 'row', 'yt_shortcode_row' );
add_shortcode( 'column', 'yt_shortcode_column' );
/**
 * Button
 */
function yt_shortcode_button( $atts, $content = null ) {

	extract( shortcode_atts( array(
		'title'		=> $content,
		'url' 		=> '#',
		'postid' 	=> '',

		'style' 	=> 'btn-default',
		'size' 		=> '',
		'fullwidth' => 'false',
		'class'		=> '',

		'target' 	=> '_self',
		'rel'		=> '',
		'css'		=> ''
		
	), $atts));

	//print_r( $atts);
	
	// Class
	$classes = array();

	$classes[] = 'btn';
	$classes[] = $style;

	if( 'true' == $fullwidth )
		$classes[] = 'btn-block';

	if( $size )
		$classes[] = $size;

	if( $class )
		$classes = array_merge( $classes, explode( ' ', $class ) );

	// Attributes
	$attributes = '';
	$attributes .= $title ? ' title="' . esc_attr( $title ) . '"' : '';
	$attributes .= $target ? ' target="' . esc_attr( $target ) . '"' : '';
	$attributes .= $rel ? ' rel="' . esc_attr( $rel ) . '"' : '';
	$attributes .= $css ? ' style="' . esc_attr( $css ) . '"' : '';

	// URL
	if( is_numeric( $postid ) && get_permalink( $postid ) )
		$url = get_permalink( $postid );
	
	$output = sprintf('<a href="%s" class="%s"%s>%s</a>', 
		esc_url( $url ),
		join( ' ', array_unique( $classes ) ),
		$attributes,
		$content );

   	return apply_filters( 'yt_shortcode_button_html', $output, $atts );
}
/**
 * Alerts
 */
function yt_shortcode_alert( $atts, $content = null ) {

	extract( shortcode_atts( array(

		'style' 	=> 'alert-default',
		'close' => 'false',
		
	), $atts));

	$output = '<div class="yt-shortcode-alert alert ' . esc_attr( $style ) . '">';
	$output .= 'true' == $close ? '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' : '';
		$output .= yt_shortcode_unautop( $content );
	$output .= '</div>';

	return apply_filters( 'yt_shortcode_alert_html', $output, $atts );
}	
/**
 * Labels
 */
function yt_shortcode_label( $atts, $content = null ) {

	extract( shortcode_atts( array(

		'style' 	=> 'label-default',
		'close' => 'false',
		
	), $atts));

	$output = '<span class="yt-shortcode-label label ' . esc_attr( $style ) . '">';
		$output .= strip_tags( $content );
	$output .= '</span>';

	return apply_filters( 'yt_shortcode_label_html', $output, $atts );
}		
/**
 * Progress bars
 * 
 * Container
 * Bars
 */
function yt_shortcode_progress_bars( $atts, $content = null ) {

	extract( shortcode_atts( array(

		'striped' => 'false',
		'active' => 'false',
		
	), $atts));

	$output = sprintf( '<div class="yt-shortcode-progress progress %s%s">', ( 'true' == $striped ? ' progress-striped' : ''), ( 'true' == $active ? ' active' : '' ) );
		$output .= yt_shortcode_unautop( $content );
	$output .= '</div>';

	return apply_filters( 'yt_shortcode_progress_bars_html', $output, $atts );
}	

function yt_shortcode_progress_bar( $atts, $content = null ) {

	extract( shortcode_atts( array(

		'title' => '',
		'width' => '50',
		'style' => 'progress-bar-primary',
		
	), $atts));

	$style = 'progress-bar-primary' == $style ? '' : $style;
	$width = '' == $width ? 10 : $width;
	$output = '<div class="progress-bar ' . esc_attr( $style ) . '" style="width:' . esc_attr( intval( $width ) ) .'%;">';
		$output .= $title ? $title : '';
	$output .= '</div>';

	return apply_filters( 'yt_shortcode_progress_bar_html', $output, $atts );
}
		
/**
 * List group
 * 
 * Container
 * Items
 */
function yt_shortcode_list_group( $atts, $content = null ) {
	
	if( !$content )
		return '';

	$output = '<div class="yt-shortcode-list-group list-group">';
		$output .= yt_shortcode_unautop( $content );
	$output .= '</div>';

	return apply_filters( 'yt_shortcode_list_group_html', $output, $atts );
}

function yt_shortcode_list_item( $atts, $content = null ) {

	extract( shortcode_atts( array(

		'title' => '',
		'type' => 'simple',
		'url' => '50',
		
	), $atts));

	$item_tag = '' !== $url ? 'a' : 'span';
	$link = '' !== $url ? sprintf( ' href="%s"', esc_url( $url ) ) : '';

	$output = '<div class="list-group">';
		$output .= yt_shortcode_unautop( $content );
	$output .= '</div>';

	$output = "<$item_tag class=\"list-group-item\"$link>";
	$output .= $title ? "<h4 class=\"list-group-item-heading\">$title</h4>" : '';
	$output .= $title ? wpautop( $content ) : $content;
	$output .= "</$item_tag>";

	return apply_filters( 'yt_shortcode_list_item_html', $output, $atts );
}
/**
 * Panel
 */
function yt_shortcode_panel( $atts, $content = null ) {

	extract( shortcode_atts( array(

		'title' => '',
		'style' => 'panel-default',
		'footer' => '',
		
	), $atts));

	$output = '<div class="yt-shortcode-panel panel ' . esc_attr( $style ) . '">';
		$output .= $title ? '<div class="panel-heading"><h3 class="panel-title">' . $title . '</h3></div>' : '';
		// Body
		$output .= '<div class="panel-body">';
			$output .= yt_shortcode_unautop( ( false !== strpos( $content, PHP_EOL ) ? wpautop( $content ) : $content ) );
		$output .= '</div>';

		$output .= $footer ? '<div class="panel-footer">' . $footer . '</div>' : '';

	$output .= '</div>';

	return apply_filters( 'yt_shortcode_panel_html', $output, $atts );
}
/**
 * Column
 */
function yt_shortcode_row( $atts, $content = null ) {

	extract( shortcode_atts( array(
		
	), $atts));

	$output = '<div class="yt-shortcode-row row">';
		$output .= yt_shortcode_unautop( $content );

	$output .= '</div>';

	return apply_filters( 'yt_shortcode_row_html', $output, $atts );
}

function yt_shortcode_column( $atts, $content = null ) {

	extract( shortcode_atts( array(
		'class' => 'col-md-4',
	), $atts));

	$class = '' == $class ? 'col-md-4' : $class;

	$output = sprintf( '<div class="yt-shortcode-column %s">', esc_attr( $class ) );

		$output .= yt_shortcode_unautop( $content );

	$output .= '</div>';

	return apply_filters( 'yt_shortcode_column_html', $output, $atts );
}