<?php
/**
 * Shortcode Title: Price Table 2
 * Shortcode: pricing_table_2
 * Usage: [pricing_table_2][pricing_table_column_2]...[/pricing_table_column_2][/pricing_table_2]
 */
add_shortcode('pricing_table_2', 'ts_pricing_table_2_func');

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

	global $pricing_table_columns;
    $pricing_table_columns = array(); // clear the array
	
	do_shortcode($content); // execute the '[pricing_table_columns]' shortcode first to get columns
	
	$columns = '';
	
	if (!is_array($pricing_table_columns) || count($pricing_table_columns) == 0) {
		return '';
	}
	
	foreach ($pricing_table_columns as $column) {
		
		$features = '';
		if (is_array($column)) {
			foreach ($column['rows'] as $row) {
				
				switch ($row['value']) {
					case 'checked':
						$features .= '<li><span class="check">✓</span>'.$row['text'].'</li>';
						break;
					case 'not_checked':
						$features .= '<li><span class="uncheck">—</span>'.$row['text'].'</li>';
						break;
					default:
						$features .= '<li>'.$row['text'].'</li>';
						break;
				}
			}
		}
			
		$button = '';
		if (!empty($column['buttontext'])) {
			$button = '<div class="footer"><a class="button" href="'.$column['url'].'">'.$column['buttontext'].'</a></div>';
		}
		$price = '';
		$price_arr = explode('.',$column['price']);
		if (is_array($price_arr)) {
			if (isset($price_arr[0])) {
				$price .= $price_arr[0];
			}
			if (isset($price_arr[1])) {
				$price .= '<span>'.$price_arr[1].'</span>';
			}
		}
		if (empty($price)) {
			$price = $column['price'];
		}
		$symbol_before = '';
		$symbol_after = '';
		if ($column['symbol_position'] == 'before') {
			$symbol_before = '<span>'.$column['symbol'].'</span>';
		} else {
			$symbol_after = '<span>'.$column['symbol'].'</span>';
		}

		$discount = '';
		if (!empty($column['discount'])) {
			$discount = '<p><span class="discount">'.$column['discount'].'</span></p>';
		}
		
		$columns .= '
			<div class="column '.($column['featured'] == 'yes' ? 'featured' : '').'">
				'.$discount.'
				<div class="header">
					<h2 class="title">'.$column['title'].'</h2>
					<h3 class="price"><span class="price-value">'.$symbol_before.$price.$symbol_after.'<span class="price-period">'.$column['subtitle'].'</span></h3>
					<h5 class="description">'.$column['description'].'</h5>
				</div>
				<ul class="features">
					'.$features.'
				</ul>
				'.$button.'
			</div>';
	
	}
    $pricing_table_columns = array();

	return '<section class="simple-pricing-table col clearfix">'.$columns.'</section>';
}

/**
 * Shortcode Title: Pricing Table Column - can be used only with pricing_table_2 shortcode
 * Shortcode: pricing_table_columns_2
 * Usage: [pricing_table_column_2 empty="no" featured="no" title="Medium plan" price="$25" subtitle="per month" discount="" buttontext="Signup" url="..."][/pricing_table_column_2]
 */
add_shortcode('pricing_table_column_2', 'ts_pricing_table_column_2_func');
function ts_pricing_table_column_2_func( $atts, $content = null ) {
    extract(shortcode_atts(array(
	    'empty' => 'no',
	    'featured' => 'no',
	    'title' => '',
		'symbol_position' => '',
		'symbol' => '',
	    'price' => '',
	    'subtitle' => '',
	    'description' => '',
	    'discount' => '',
	    'buttontext' => '',
	    'url' => ''
    ), $atts));
    global $pricing_table_columns, $pricing_table_items;
	$pricing_table_items = array();
	do_shortcode($content);
	
    $pricing_table_columns[] = array(
		'empty' => $empty,
	    'featured' => $featured,
	    'title' => $title,
	    'symbol_position' => $symbol_position,
	    'symbol' => $symbol,
	    'price' => $price,
	    'subtitle' => $subtitle,
	    'description' => $description,
	    'discount' => $discount, 
		'buttontext' => $buttontext,
	    'url' => $url,
		'rows' => $pricing_table_items
	);
	
	$pricing_table_items = array();
	
	return $pricing_table_columns;
}

/**
 * Shortcode Title: Pricing Table Item - can be used only with pricing_table_column_2 shortcode
 * Shortcode: pricing_table_columns_2
 * Usage: [pricing_table_item_2 value="{value}" text="{text}" tooltip="{tooltip}"]
 */
add_shortcode('pricing_table_item_2', 'ts_pricing_table_item_2_func');
function ts_pricing_table_item_2_func( $atts, $content = null ) {
    extract(shortcode_atts(array(
	    'value' => 'checked',
	    'text' => ''
    ), $atts));
    global $pricing_table_items;
    $pricing_table_items[] = array(
		'value' => $value,
	    'text' => $text
	);
	return $pricing_table_items;
}