/topspin_shortcodes.php

https://github.com/ezmiller/topspin-wordpress · PHP · 176 lines · 116 code · 9 blank · 51 comment · 13 complexity · e09cdf19788f423bcf398a8f10e50601 MD5 · raw file

  1. <?php
  2. /*
  3. * Last Modified: October 16, 2011
  4. *
  5. * ----------------------------------
  6. * Change Log
  7. * ----------------------------------
  8. * 2011-09-10 (eThan)
  9. - Added shortcode function for nav menu.
  10. * 2011-09-19
  11. - Fixed shortcode content positioning - [@bryanlanders - https://github.com/topspin/topspin-wordpress/issues/19]
  12. * 2011-08-01
  13. - Updated topspin_shortcode_featured_item() to display multiple featured images
  14. - Fixed the pagination to work with and without permalinks
  15. - Added new short code [topspin_store_item]
  16. * 2011-04-05
  17. * - updated topspin_shortcode_buy_buttons()
  18. update template file path orders for new template modes:
  19. 3.1: <current-theme>/topspin-mode, <parent-theme>/topspin-mode
  20. 3.0.0: <current-theme>/topspin-template, <parent-theme>/topspin-template
  21. Default: <plugin-dir>/topspin-mode
  22. * - update topspin_shortcode_featured_item()
  23. update template file path orders for new template modes: (see top)
  24. */
  25. ### Short Codes
  26. add_shortcode('topspin_store_item','topspin_shortcode_store_item');
  27. add_shortcode('topspin_buy_buttons','topspin_shortcode_buy_buttons');
  28. add_shortcode('topspin_featured_item','topspin_shortcode_featured_item');
  29. add_shortcode('topspin_store_nav_menu','topspin_shortcode_store_nav_menu');
  30. ### [topspin_store_item]
  31. ### @id The item ID
  32. function topspin_shortcode_store_item($atts) {
  33. global $store;
  34. $defaults = array('id'=>0);
  35. $a = shortcode_atts($defaults,$atts);
  36. if($a['id']) {
  37. $featureditem = topspin_get_item($a['id']);
  38. if($featureditem) {
  39. ob_start();
  40. ## Template File
  41. $templateMode = $store->getSetting('topspin_template_mode');
  42. $templatefile = 'templates/topspin-'.$templateMode.'/featured-item.php';
  43. ## 3.1
  44. if(file_exists(TOPSPIN_CURRENT_THEME_PATH.'/topspin-'.$templateMode.'/featured-item.php')) { $templatefile = TOPSPIN_CURRENT_THEME_PATH.'/topspin-'.$templateMode.'/featured-item.php'; }
  45. elseif(file_exists(TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-'.$templateMode.'/featured-item.php')) { $templatefile = TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-'.$templateMode.'/featured-item.php'; }
  46. ## 3.0.0
  47. if(file_exists(TOPSPIN_CURRENT_THEME_PATH.'/topspin-templates/featured-item.php')) { $templatefile = TOPSPIN_CURRENT_THEME_PATH.'/topspin-templates/featured-item.php'; }
  48. elseif(file_exists(TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-templates/featured-item.php')) { $templatefile = TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-templates/featured-item.php'; }
  49. include($templatefile);
  50. $html = ob_get_contents();
  51. ob_end_clean();
  52. return $html;
  53. }
  54. }
  55. }
  56. ### [topspin_buy_buttons]
  57. ### @id Default: the current store's ID
  58. function topspin_shortcode_buy_buttons($atts) {
  59. ## Outputs the current store grid
  60. global $store;
  61. global $post;
  62. //Query string append sign
  63. $permalinkEnabled = (get_option('permalink_structure')!='') ? true : false;
  64. $queryAppendSign = ($permalinkEnabled) ? '?' : '&';
  65. $defaults = array(
  66. 'id' => (isset($atts['id'])) ? $atts['id'] : $store->getStoreId($post->ID)
  67. );
  68. $a = shortcode_atts($defaults,$atts);
  69. $storeID = $a['id'];
  70. $storedata = $store->getStore($storeID);
  71. $storedata['grid_item_width'] = floor(100/$storedata['grid_columns']);
  72. ## Set Page
  73. $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
  74. ## Get Items
  75. $allitems = $store->getStoreItems($storeID,0);
  76. ## Get Paged Items
  77. $storeitems = ($storedata['show_all_items']) ? $allitems : $store->getStoreItemsPage($allitems,$storedata['items_per_page'],$page);
  78. ## Set Additional Store Data
  79. if($storedata['show_all_items']) {
  80. $storedata['total_items'] = count($allitems);
  81. $storedata['total_pages'] = 1;
  82. $storedata['curr_page'] = $page;
  83. $storedata['prev_page'] = '';
  84. $storedata['next_page'] = '';
  85. }
  86. else {
  87. $storedata['total_items'] = count($allitems);
  88. $storedata['total_pages'] = ceil($storedata['total_items']/$storedata['items_per_page']);
  89. $storedata['curr_page'] = $page;
  90. $storedata['prev_page'] = ($page==1) ? '' : get_permalink($post->ID).$queryAppendSign.'page='.($page-1);
  91. $storedata['next_page'] = ($storedata['curr_page']<$storedata['total_pages']) ? get_permalink($post->ID).$queryAppendSign.'page='.($page+1) : '';
  92. }
  93. ob_start();
  94. ## Template File
  95. $templateMode = $store->getSetting('topspin_template_mode');
  96. $templatefile = 'templates/topspin-'.$templateMode.'/item-listings.php';
  97. ## 3.1
  98. if(file_exists(TOPSPIN_CURRENT_THEME_PATH.'/topspin-'.$templateMode.'/item-listings.php')) { $templatefile = TOPSPIN_CURRENT_THEME_PATH.'/topspin-'.$templateMode.'/item-listings.php'; }
  99. elseif(file_exists(TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-'.$templateMode.'/item-listings.php')) { $templatefile = TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-'.$templateMode.'/item-listings.php'; }
  100. ## 3.0.0
  101. if(file_exists(TOPSPIN_CURRENT_THEME_PATH.'/topspin-templates/item-listings.php')) { $templatefile = TOPSPIN_CURRENT_THEME_PATH.'/topspin-templates/item-listings.php'; }
  102. elseif(file_exists(TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-templates/item-listings.php')) { $templatefile = TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-templates/item-listings.php'; }
  103. include($templatefile);
  104. $html = ob_get_contents();
  105. ob_end_clean();
  106. return $html;
  107. }
  108. ### [topspin_featured_item]
  109. ### @id Default: the current store's ID
  110. function topspin_shortcode_featured_item($atts) {
  111. ## Outputs the current store's featured item
  112. global $store;
  113. global $post;
  114. $defaults = array(
  115. 'id' => (isset($atts['id'])) ? $atts['id'] : $store->getStoreId($post->ID)
  116. );
  117. $a = shortcode_atts($defaults,$atts);
  118. $storeID = $a['id'];
  119. $featuredItems = $store->getStoreFeaturedItems($storeID);
  120. if(count($featuredItems)) {
  121. ob_start();
  122. ## Template File
  123. $templateMode = $store->getSetting('topspin_template_mode');
  124. $templatefile = 'templates/topspin-'.$templateMode.'/featured-item.php';
  125. ## 3.1
  126. if(file_exists(TOPSPIN_CURRENT_THEME_PATH.'/topspin-'.$templateMode.'/featured-item.php')) { $templatefile = TOPSPIN_CURRENT_THEME_PATH.'/topspin-'.$templateMode.'/featured-item.php'; }
  127. elseif(file_exists(TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-'.$templateMode.'/featured-item.php')) { $templatefile = TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-'.$templateMode.'/featured-item.php'; }
  128. ## 3.0.0
  129. if(file_exists(TOPSPIN_CURRENT_THEME_PATH.'/topspin-templates/featured-item.php')) { $templatefile = TOPSPIN_CURRENT_THEME_PATH.'/topspin-templates/featured-item.php'; }
  130. elseif(file_exists(TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-templates/featured-item.php')) { $templatefile = TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-templates/featured-item.php'; }
  131. $html = '';
  132. foreach($featuredItems as $featureditem) {
  133. include($templatefile);
  134. ob_flush();
  135. $html .= ob_get_contents();
  136. }
  137. ob_end_clean();
  138. return $html;
  139. }
  140. }
  141. ### [topspin_store_nav_menu]
  142. ### @id Default: the current store's ID
  143. function topspin_shortcode_store_nav_menu($atts) {
  144. global $store;
  145. global $post;
  146. $defaults = array(
  147. 'id' => (isset($atts['id'])) ? $atts['id'] : $store->getStoreId($post->ID)
  148. );
  149. $a = shortcode_atts($defaults,$atts);
  150. $storeID = $a['id'];
  151. $storesList = $store->stores_get_nested_list();
  152. ob_start();
  153. ## Template File
  154. $templateMode = $store->getSetting('topspin_template_mode');
  155. $templatefile = 'templates/topspin-'.$templateMode.'/nav-menu.php';
  156. ## 3.1
  157. if(file_exists(TOPSPIN_CURRENT_THEME_PATH.'/topspin-'.$templateMode.'/nav-menu.php')) { $templatefile = TOPSPIN_CURRENT_THEME_PATH.'/topspin-'.$templateMode.'/nav-menu.php'; }
  158. elseif(file_exists(TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-'.$templateMode.'/nav-menu.php')) { $templatefile = TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-templates/nav-menu.php'; }
  159. ## 3.0.0
  160. if(file_exists(TOPSPIN_CURRENT_THEME_PATH.'/topspin-templates/nav-menu.php')) { $templatefile = TOPSPIN_CURRENT_THEME_PATH.'/topspin-templates/nav-menu.php'; }
  161. elseif(file_exists(TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-templates/nav-menu.php')) { $templatefile = TOPSPIN_CURRENT_THEMEPARENT_PATH.'/topspin-templates/nav-menu.php'; }
  162. include($templatefile);
  163. $html = ob_get_contents();
  164. ob_end_clean();
  165. echo $html;
  166. }
  167. ?>