<?php

/**
 * Returns a calculator from the CDN
 *
 * This function returns the calculator from the
 * CDN based on the parameter set in the
 * calculator shortcode.
 *
 * @function get_calculator()
 *
 * @param (string) $title The calculator title
 *
 * @return (string) The specified calculator HTML and jQuery
 *
 * @since 1.0.0 
 *
 */
function get_calculator( $title ) {
	
	ob_start();
	
	require_once 'calculators/' . $title . '.php';
	
	return ob_get_clean();	
}



/**
 * Returns a list of published calculators
 *
 * This function returns a list of published
 * calculators based on the current page child
 * pages.
 *
 * @function list_calculators()
 *
 * @uses wp_list_pages(), get_the_ID()
 *
 * @param N/A
 *
 * @return (string) HTML list of calculators
 *
 * @since 1.0.0 
 *
 */
function list_calculators() {
	return '<ul id="calculators-list">' . wp_list_pages( array( 'child_of' => get_the_ID(), 'title_li' => '', 'echo' => false ) ) . '</ul>';
}



/**
 * Returns the calculator shortcode output
 *
 * This function returns either the calculator
 * list or an individual calculator depending
 * on if the shortcode contains a parameter.
 *
 * @function add_calculators()
 *
 * @uses get_calculator(), list_calculators()
 *
 * @param (array) $var[ (string) type ]
 *
 * @return (string) Either a list of, or one calculator
 *
 * @since 1.0.0 
 *
 */
function add_calculators( $var ) {
	
	$var = shortcode_atts( array( 'type' => '' ), $var, 'calculators' );
	$type = $var['type'];

	if ( $type != '' ) {
		
		return get_calculator( $type );
	}
	return list_calculators();
}
add_shortcode( 'calculator', 'add_calculators' );