<?php
/**
 * Shortcode Title: Pricing table
 * Shortcode: pricing_table
 * Usage: [pricing_table][pricing_table_column]...[/pricing_table_column][/pricing_table]
 */
add_shortcode('pricing_table', 'ts_pricing_table_func');

function ts_pricing_table_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) {
				
				if ($column['empty'] == 'yes') {
					$row['value'] == 'text';
				}
				
				switch ($row['value']) {
					case 'checked':
						$features .= '<li data-feature="'.$row['tooltip'].'"><span class="check">✓</span></li>';
						break;
					case 'not_checked':
						$features .= '<li data-feature="'.$row['tooltip'].'"><span class="uncheck">—</span></li>';
						break;
					case 'text':
						if ($column['empty'] == 'yes') {
							$features .= '<li data-tooltip="'.$row['tooltip'].'">'.$row['text'].'</li>';
						} else {
							$features .= '<li data-feature="'.$row['tooltip'].'">'.$row['text'].'</li>';
						}
						break;
				}
			}
		}
			
		if ($column['empty'] == 'yes') {
			$columns .= '
				<div class="column features-list ">
					<div class="header"></div>
					<ul class="features">
						'.$features.'
					</ul>
				</div>';
		} else {
			
			$button = '';
			if (!empty($column['buttontext'])) {
				$button = '<div class="footer"><a class="button" href="'.$column['url'].'">'.$column['buttontext'].'</a></div>';
			}
			
			$columns .= '
				<div class="column '.($column['featured'] == 'yes' ? 'featured' : '').'">
					<div class="header">
						<h2 class="title">'.$column['title'].'</h2>
						<h3 class="price"><span class="price-value">'.$column['price'].'</span><span class="price-period">'.$column['subtitle'].'</span></h3>
					</div>
					<ul class="features">
						'.$features.'
					</ul>
					'.$button.'
				</div>';
			
			
			
		}
	}
    $pricing_table_columns = array();

	return '<section class="extended-pricing-table col'.(count($pricing_table_columns) - 1).' featured clearfix">'.$columns.'</section>';
}

/**
 * Shortcode Title: Pricing Table Column - can be used only with pricing_table shortcode
 * Shortcode: pricing_table_columns
 * Usage: [pricing_table_column empty="no" featured="no" title="Medium plan" price="$25" subtitle="per month" discount="" buttontext="Signup" url="..."][/pricing_table_column]
 */
add_shortcode('pricing_table_column', 'ts_pricing_table_column_func');
function ts_pricing_table_column_func( $atts, $content = null ) {
    extract(shortcode_atts(array(
	    'empty' => 'no',
	    'featured' => 'no',
	    'title' => '',
	    'price' => '',
	    'subtitle' => '',
	    '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,
	    'price' => $price,
	    'subtitle' => $subtitle,
	    '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 shortcode
 * Shortcode: pricing_table_columns
 * Usage: [pricing_table_item value="{value}" text="{text}" tooltip="{tooltip}"]
 */
add_shortcode('pricing_table_item', 'ts_pricing_table_item_func');
function ts_pricing_table_item_func( $atts, $content = null ) {
    extract(shortcode_atts(array(
	    'value' => 'checked',
	    'text' => '',
	    'tooltip' => ''
    ), $atts));
    global $pricing_table_items;
    $pricing_table_items[] = array(
		'value' => $value,
	    'text' => $text,
	    'tooltip' => $tooltip
	);
	return $pricing_table_items;
}