<?php
/**
* eshop shortcode functions.
* @todo TODO: Class, list class_methods and add shortcode by prefix, ...
*/
// Constants: TODO Class propierties.
defined('DSEP')
|| define('DSEP', DIRECTORY_SEPARATOR); /* Alias DSEP */ define('DS', DSEP);
defined('ESHOP_DIR')
|| define('ESHOP_DIR', realpath(dirname(__FILE__)));
// TODO Check ESHOP_DIR
defined('ESHOP_TPL_DIR')
|| define('ESHOP_TPL_DIR', realpath(ESHOP_DIR . DS . 'templates'));
defined('ESHOP_TPL_DEFAULT')
|| define('ESHOP_TPL_DEFAULT', ESHOP_TPL_DIR . DS . 'eshop_default.phtml');
/**
* ShortCodes:
* Auto Templates by name of shortcode.
*/
$shortCodes = array(
'eshop_list_featured',
'eshop_sale',
'eshop_best_sellers',
'eshop_list_new',
'eshop_list_subpages',
'eshop_list_cat_tags',
'eshop_random_products',
'eshop_show_cancel',
'eshop_show_cart',
'eshop_show_checkout',
'eshop_show_discounts',
'eshop_show_downloads',
'eshop_show_payments',
'eshop_show_product',
'eshop_show_shipping',
'eshop_show_success',
'eshop_empty_cart',
'eshop_list_alpha',
'eshop_cart_items',
'eshop_addtocart',
'eshop_welcome',
'eshop_details'
);
foreach ( $shortCodes as $shortCode )
add_shortcode($shortCode, $shortCode);
/**
* Function to show items in cart.
*
* @param (string) shortcode_atts $atts
*/
function eshop_cart_items(array $atts) {
// FIXME Class propierties.
global $blog_id, $eshopoptions;
// # Base and defaults params:
$linkFormat = '<a href="%1$s" title="%3$s">%2$s</a>';
$tplDefaultMsg = '<p class="error"><strong class="italic">eshop</strong> Template system fails.</p>';
// FIXME: Before and after, or not, with template system¿?
$defaults = array(
'before' => '',
'after' => '',
'hide' => 'no',
'showwhat' => 'both',
'format' => 'ul'
);
$eshopOptDef = array('cart' => 0, 'checkout' => 0);
$eshopTplName = __FUNCTION__; // TODO Default for all shortcodes¿?
// # Checks:
$atts = !empty($atts) && is_array($atts) ? $atts : array();
$eshopOptions = !empty($eshopoptions) && is_array($eshopoptions) ? $eshopoptions : $eshopOptDef;
$blog_id = is_numeric($blog_id) && strlen($blog_id) < 4 ? $blog_id : 1;
// # User / ShortCode params:
extract(shortcode_atts($defaults, $atts));
// TPL Format, it's simple but can be extend to other formats:
switch ( $format ) {
default :
case 'ul' :
break;
case 'ol' :
$eshopTplName .= '_ol';
break;
case 'dl' :
$eshopTplName .= '_dl';
break;
}
$eshopTemplate = ESHOP_TPL_DIR . DS . $eshopTplName . '.phtml';
$eshopTemplate = @is_file($eshopTemplate) ? $eshopTemplate : ESHOP_TPL_DEFAULT;
// eshop defaults:
$eshopCartSes = $_SESSION['eshopcart' . (int) $blog_id];
$eshopSize = 0;
$eshopQty = 0;
// Links and Trans:
$eshopCart = get_permalink($eshopOptions['cart']);
$eshopCheck = get_permalink($eshopOptions['checkout']);
$eshopCartLink = sprintf($linkFormat, $eshopCart, __('View Cart', 'eshop'), __('View Cart', 'eshop'));
$eshopCheckLink = sprintf($linkFormat, $eshopCheck, __('Checkout', 'eshop'), __('Checkout', 'eshop'));
// TPL Values:
$tplItems = array();
$tplProducts = array();
$tplOptions = array($eshopCartLink, $eshopCheckLink);
// # XXX Run...
// # Count items and qty:
if ( isset($eshopCartSes) && !empty($eshopCartSes) ) {
$eshopSize = sizeof($eshopCartSes);
foreach ( $eshopCartSes as $key => $value )
$eshopQty += $value['qty'];
}
// # Build output html:
if ( $hide == 'no' ) {
// Qty & both:
if ( $showwhat == 'qty' || $showwhat == 'both' ) {
$tplItems[] = sprintf(_n('<span>%d</span> product in cart.', '<span>%d</span> products in cart.',$eshopSize, 'eshop'), $eshopSize);
}
// Items & both:
if ( $showwhat == 'items' || $showwhat == 'both' ) {
$tplProducts[] = sprintf(_n('<span>%d</span> item in cart.', '<span>%d</span> items in cart.', $eshopQty, 'eshop'), $eshopQty);
}
// # TPL output:
include_once("{$eshopTemplate}");
}
}