Advertisement
Guest User

eshop_shortcodes.php

a guest
Dec 8th, 2010
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | None | 0 0
  1. <?php
  2. /**
  3.  * eshop shortcode functions.
  4.  * @todo TODO: Class, list class_methods and add shortcode by prefix, ...
  5.  */
  6. // Constants: TODO Class propierties.
  7. defined('DSEP')
  8.     || define('DSEP', DIRECTORY_SEPARATOR); /* Alias DSEP */ define('DS', DSEP);
  9. defined('ESHOP_DIR')
  10.     || define('ESHOP_DIR', realpath(dirname(__FILE__)));
  11. // TODO Check ESHOP_DIR
  12. defined('ESHOP_TPL_DIR')
  13.     || define('ESHOP_TPL_DIR', realpath(ESHOP_DIR . DS . 'templates'));
  14. defined('ESHOP_TPL_DEFAULT')
  15.     || define('ESHOP_TPL_DEFAULT', ESHOP_TPL_DIR . DS . 'eshop_default.phtml');
  16.  
  17. /**
  18.  * ShortCodes:
  19.  * Auto Templates by name of shortcode.
  20.  */
  21. $shortCodes = array(
  22.     'eshop_list_featured',
  23.     'eshop_sale',
  24.     'eshop_best_sellers',
  25.     'eshop_list_new',
  26.     'eshop_list_subpages',
  27.     'eshop_list_cat_tags',
  28.     'eshop_random_products',
  29.     'eshop_show_cancel',
  30.     'eshop_show_cart',
  31.     'eshop_show_checkout',
  32.     'eshop_show_discounts',
  33.     'eshop_show_downloads',
  34.     'eshop_show_payments',
  35.     'eshop_show_product',
  36.     'eshop_show_shipping',
  37.     'eshop_show_success',
  38.     'eshop_empty_cart',
  39.     'eshop_list_alpha',
  40.     'eshop_cart_items',
  41.     'eshop_addtocart',
  42.     'eshop_welcome',
  43.     'eshop_details'
  44. );
  45. foreach ( $shortCodes as $shortCode )
  46.     add_shortcode($shortCode, $shortCode);
  47.  
  48. /**
  49.  * Function to show items in cart.
  50.  *
  51.  * @param (string) shortcode_atts $atts
  52.  */
  53. function eshop_cart_items(array $atts) {
  54.     // FIXME Class propierties.
  55.     global $blog_id, $eshopoptions;
  56.  
  57.     // # Base and defaults params:
  58.     $linkFormat     = '<a href="%1$s" title="%3$s">%2$s</a>';
  59.     $tplDefaultMsg  = '<p class="error"><strong class="italic">eshop</strong> Template system fails.</p>';
  60.  
  61.     // FIXME: Before and after, or not, with template system¿?
  62.     $defaults       = array(
  63.         'before'    => '',
  64.         'after'     => '',
  65.         'hide'      => 'no',
  66.         'showwhat'  => 'both',
  67.         'format'    => 'ul'
  68.     );
  69.     $eshopOptDef    = array('cart' => 0, 'checkout' => 0);
  70.     $eshopTplName   = __FUNCTION__; // TODO Default for all shortcodes¿?
  71.  
  72.     // # Checks:
  73.     $atts           = !empty($atts)         && is_array($atts)          ? $atts         : array();
  74.     $eshopOptions   = !empty($eshopoptions) && is_array($eshopoptions)  ? $eshopoptions : $eshopOptDef;
  75.     $blog_id        = is_numeric($blog_id)  && strlen($blog_id) < 4     ? $blog_id      : 1;
  76.  
  77.     // # User / ShortCode params:
  78.     extract(shortcode_atts($defaults, $atts));
  79.  
  80.     // TPL Format, it's simple but can be extend to other formats:
  81.     switch ( $format ) {
  82.         default :
  83.         case 'ul' :
  84.             break;
  85.         case 'ol' :
  86.             $eshopTplName .= '_ol';
  87.             break;
  88.         case 'dl' :
  89.             $eshopTplName .= '_dl';
  90.             break;
  91.     }
  92.     $eshopTemplate  = ESHOP_TPL_DIR . DS . $eshopTplName . '.phtml';
  93.     $eshopTemplate  = @is_file($eshopTemplate) ? $eshopTemplate : ESHOP_TPL_DEFAULT;
  94.  
  95.     // eshop defaults:
  96.     $eshopCartSes   = $_SESSION['eshopcart' . (int) $blog_id];
  97.     $eshopSize      = 0;
  98.     $eshopQty       = 0;
  99.  
  100.     // Links and Trans:
  101.     $eshopCart      = get_permalink($eshopOptions['cart']);
  102.     $eshopCheck     = get_permalink($eshopOptions['checkout']);
  103.     $eshopCartLink  = sprintf($linkFormat, $eshopCart,  __('View Cart', 'eshop'),   __('View Cart', 'eshop'));
  104.     $eshopCheckLink = sprintf($linkFormat, $eshopCheck, __('Checkout', 'eshop'),    __('Checkout', 'eshop'));
  105.  
  106.     // TPL Values:
  107.     $tplItems       = array();
  108.     $tplProducts    = array();
  109.     $tplOptions     = array($eshopCartLink, $eshopCheckLink);
  110.  
  111.     // # XXX Run...
  112.     // # Count items and qty:
  113.     if ( isset($eshopCartSes) && !empty($eshopCartSes) ) {
  114.         $eshopSize = sizeof($eshopCartSes);
  115.         foreach ( $eshopCartSes as $key => $value )
  116.             $eshopQty += $value['qty'];
  117.     }
  118.  
  119.     // # Build output html:
  120.     if ( $hide == 'no' ) {
  121.         // Qty & both:
  122.         if ( $showwhat == 'qty' || $showwhat == 'both' ) {
  123.             $tplItems[] = sprintf(_n('<span>%d</span> product in cart.', '<span>%d</span> products in cart.',$eshopSize, 'eshop'), $eshopSize);
  124.         }
  125.  
  126.         // Items & both:
  127.         if ( $showwhat == 'items' || $showwhat == 'both' ) {
  128.             $tplProducts[] = sprintf(_n('<span>%d</span> item in cart.', '<span>%d</span> items in cart.', $eshopQty, 'eshop'), $eshopQty);
  129.         }
  130.  
  131.         // # TPL output:
  132.         include_once("{$eshopTemplate}");
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement