HLT_BootstrapShortcodes

Copyright (c) 2011 Host Like Toast All rights reserved.

Defined (1)

The class is defined in the following location(s).

/functions/shortcodes.php  
  1. class HLT_BootstrapShortcodes { 
  2.  
  3. protected $sTwitterBootstrapVersion; 
  4.  
  5. public function __construct( $sVersion = '2' ) { 
  6. $aMethods = get_class_methods( $this ); 
  7. $aExclude = array( 'idHtml', 'def', 'noEmptyHtml' ); 
  8. foreach ( $aMethods as $sMethod ) { 
  9. if ( !in_array( $sMethod, $aExclude ) ) { 
  10. add_shortcode( 'TBS_'.strtoupper( $sMethod ), array( $this, $sMethod ) ); 
  11.  
  12. $this->sTwitterBootstrapVersion = $sVersion; 
  13.  
  14. add_filter( 'the_content', array( $this, 'filterTheContent' ), 10 );  
  15. add_filter( 'the_content', array( $this, 'filterTheContentToFixNamedAnchors' ), 99 ); 
  16.  
  17. /** 
  18. * Move the wpautop until after the shortcodes have been run! 
  19. * remove_filter( 'the_content', 'wpautop' ); 
  20. * add_filter( 'the_content', 'wpautop' , 99 ); 
  21. * add_filter( 'the_content', 'shortcode_unautop', 100 ); 
  22. */ 
  23.  
  24. /** 
  25. * Disable wpautop globally! 
  26. * remove_filter( 'the_content', 'wpautop' ); 
  27. * remove_filter( 'comment_text', 'wpautop' ); 
  28. */ 
  29.  
  30. /** 
  31. * Prints the necessary HTML for Twitter Bootstrap Labels 
  32. *  
  33. * Class may be one of: Default Primary Info Success Danger 
  34. *  
  35. * @param $inaAtts 
  36. * @param $insContent 
  37. */ 
  38. public function button( $inaAtts = array(), $insContent = '' ) { 
  39.  
  40. $sElementType = 'a';  
  41. if ( !isset( $inaAtts['link'] ) ) { 
  42. $sElementType = 'button'; 
  43.  
  44. $this->def( $inaAtts, 'id' ); 
  45. $this->def( $inaAtts, 'class', '' ); 
  46. $this->def( $inaAtts, 'style' ); 
  47. $this->def( $inaAtts, 'link_title' ); 
  48. $this->def( $inaAtts, 'value', '0' ); 
  49.  
  50. if ( $this->sTwitterBootstrapVersion == '2' && !preg_match( '/^btn-/', $inaAtts['class'] ) ) { 
  51. $inaAtts['class'] = 'btn-'.$inaAtts['class']; 
  52.  
  53. $sReturn = '<'.$sElementType.' '.$this->noEmptyHtml( $inaAtts['style'], 'style' ).' class="btn '.$inaAtts['class']. '"'.$this->idHtml( $inaAtts['id'] ); 
  54.  
  55. if ( $sElementType == 'a' ) { 
  56. $sReturn .= ' href="'.$inaAtts['link'].'" title="' .$inaAtts['link_title']. '"'; 
  57. else { 
  58. $sReturn .= ' type="button" value="'.$inaAtts['value']. '"'; 
  59.  
  60. $sReturn .= '>'.$this->doShortcode( $insContent ).'</'.$sElementType.'>'; 
  61.  
  62. return $sReturn; 
  63.  
  64.  
  65.  
  66. /** 
  67. * Prints the necessary HTML for Twitter Bootstrap Labels 
  68. *  
  69. * class may be one of: default, success, warning, important, notice 
  70. *  
  71. * @param $inaAtts 
  72. * @param $insContent 
  73. */ 
  74. public function label( $inaAtts = array(), $insContent = '' ) { 
  75.  
  76. $this->def( $inaAtts, 'style' ); 
  77. $this->def( $inaAtts, 'id' ); 
  78. $this->def( $inaAtts, 'class' ); 
  79.  
  80. if ( $this->sTwitterBootstrapVersion == '2' && !preg_match( '/^label-/', $inaAtts['class'] ) ) { 
  81. $inaAtts['class'] = 'label-'.$inaAtts['class']; 
  82.  
  83. $sReturn = '<span '.$this->noEmptyHtml( $inaAtts['style'], 'style' ).' class="label '.$inaAtts['class'].'"'.$this->idHtml( $inaAtts['id'] ).'>'.$this->doShortcode( $insContent ).'</span>'; 
  84.  
  85. return $sReturn; 
  86.  
  87. public function blockquote( $inaAtts = array(), $insContent = '' ) { 
  88.  
  89. $this->def( $inaAtts, 'style' ); 
  90. $this->def( $inaAtts, 'id' ); 
  91. $this->def( $inaAtts, 'class' ); 
  92. $this->def( $inaAtts, 'source' ); 
  93.  
  94. if ($inaAtts['source'] != '') { 
  95. $inaAtts['source'] = '<small>'.$inaAtts['source'].'</small>'; 
  96.  
  97. $sReturn = '<blockquote '.$this->noEmptyHtml( $inaAtts['style'], 'style' ).' '.$this->noEmptyHtml( $inaAtts['class'], 'class' ).' '.$this->idHtml( $inaAtts['id'] ).'><p>'.$this->doShortcode( $insContent ).'</p>'.$inaAtts['source'].'</blockquote>'; 
  98.  
  99. return $sReturn; 
  100.  
  101. /** 
  102. * class may be one of: error, warning, success, info 
  103. *  
  104. * @param $inaAtts 
  105. * @param $insContent 
  106. */ 
  107. public function alert( $inaAtts = array(), $insContent = '' ) { 
  108.  
  109. $this->def( $inaAtts, 'style' ); 
  110. $this->def( $inaAtts, 'id' ); 
  111. $this->def( $inaAtts, 'class' ); 
  112. $this->def( $inaAtts, 'type', 'alert' ); 
  113.  
  114. //Twitter 1.4.0 only supports this one variation 
  115. if ( $this->sTwitterBootstrapVersion == '1' ) { 
  116. $inaAtts['type'] ='alert-message'; 
  117.  
  118. if ( $this->sTwitterBootstrapVersion == '2' && !preg_match( '/^alert-/', $inaAtts['class'] ) ) { 
  119. $inaAtts['class'] = 'alert-'.$inaAtts['class']; 
  120.  
  121. $sReturn = '<div '.$this->noEmptyHtml( $inaAtts['style'], 'style' ) 
  122. .' class="'.$inaAtts['type'].' '.$inaAtts['class'].'" ' 
  123. .$this->noEmptyHtml( $inaAtts['id'], 'id' ).'>'.$this->doShortcode($insContent).'</div>'; 
  124.  
  125. return $sReturn ; 
  126.  
  127. /** 
  128. * DEPRECATED: To BE EVENTUALLY REMOVED AS UNSUPPORTED IN Twitter Bootstrap 2+ 
  129. *  
  130. * Uses alert() function but just adds the class "block-message" 
  131. *  
  132. * class may be one of: error, warning, success, info 
  133. *  
  134. * @param $inaAtts 
  135. * @param $insContent 
  136. */ 
  137. public function block( $inaAtts = array(), $insContent = '' ) { 
  138.  
  139. $this->def( $inaAtts, 'style' ); 
  140. $this->def( $inaAtts, 'id' ); 
  141. $this->def( $inaAtts, 'class' ); 
  142.  
  143. return $this->doShortcode( '[TBS_ALERT '.'class="block-message ' 
  144. .$inaAtts['class'].'" ' 
  145. .$this->noEmptyHtml( $inaAtts['id'], 'id' ).' ' 
  146. .$this->noEmptyHtml( $inaAtts['style'], 'style' ).']'.$insContent.'[/TBS_ALERT]' ); 
  147.  
  148. public function code( $inaAtts = array(), $insContent = '' ) { 
  149.  
  150. $this->def( $inaAtts, 'style' ); 
  151. $this->def( $inaAtts, 'id' ); 
  152.  
  153. $sReturn = '<pre class="prettyprint linenums" '.$this->idHtml( $inaAtts['id'] ).' '.$this->noEmptyHtml( $inaAtts['style'], 'style' ).'>'.$insContent.'</pre>'; 
  154.  
  155. return $sReturn; 
  156.  
  157. /** 
  158. * DEPRECATED: To BE EVENTUALLY REMOVED AS UNSUPPORTED IN Twitter Bootstrap 2+ 
  159. *  
  160. * Options for 'placement' are above | below | left | right 
  161. *  
  162. * @param $inaAtts 
  163. * @param $insContent 
  164. */ 
  165. public function twipsy( $inaAtts = array(), $insContent = '' ) { 
  166.  
  167. return $this->tooltip($inaAtts, $insContent); 
  168.  
  169. /** 
  170. * Options for 'placement' are top | bottom | left | right 
  171. */ 
  172. public function tooltip( $inaAtts = array(), $insContent = '' ) { 
  173.  
  174. $this->def( $inaAtts, 'style' ); 
  175. $this->def( $inaAtts, 'id' ); 
  176. $this->def( $inaAtts, 'class' ); 
  177. $this->def( $inaAtts, 'placement', 'top' ); 
  178. $this->def( $inaAtts, 'title' ); 
  179. $this->def( $inaAtts, 'rel', 'tooltip' ); //could set to 'twipsy' for bootstrap 1.4.0 
  180.  
  181. //backward comnpatibility with Twitter Bootstrap v1.0 
  182. if ( $this->sTwitterBootstrapVersion == '1' ) { 
  183. $inaAtts['rel'] = 'twipsy'; 
  184. if ( $inaAtts['placement'] == 'top' ) { 
  185. $inaAtts['placement'] = 'above'; 
  186. if ( $inaAtts['placement'] == 'bottom' ) { 
  187. $inaAtts['placement'] = 'below'; 
  188. } else { //Twitter Bootstrap v2.0 changed position names 
  189. if ( $inaAtts['placement'] == 'above' ) { 
  190. $inaAtts['placement'] = 'top'; 
  191. if ( $inaAtts['placement'] == 'below' ) { 
  192. $inaAtts['placement'] = 'bottom'; 
  193.  
  194. $sReturn = $insContent; 
  195. if ( $inaAtts['title'] != '' ) { 
  196. $sReturn = '<span' 
  197. .' rel="'.$inaAtts['rel'].'" data-placement="'.$inaAtts['placement'].'" data-original-title="'.$inaAtts['title'].'"' 
  198. .$this->noEmptyHtml( $inaAtts['id'], 'id' ) 
  199. .$this->noEmptyHtml( $inaAtts['class'], 'class' ) 
  200. .$this->noEmptyHtml( $inaAtts['style'], 'style' ).'>'.$this->doShortcode($insContent).'</span>'; 
  201.  
  202. return $sReturn; 
  203.  
  204. /** 
  205. * Options for 'placement' are top | bottom | left | right 
  206. */ 
  207. public function popover( $inaAtts = array(), $insContent = '' ) { 
  208.  
  209. $this->def( $inaAtts, 'style' ); 
  210. $this->def( $inaAtts, 'id' ); 
  211. $this->def( $inaAtts, 'class' ); 
  212. $this->def( $inaAtts, 'placement', 'right' ); 
  213. $this->def( $inaAtts, 'title' ); 
  214. $this->def( $inaAtts, 'content' ); 
  215.  
  216. $sReturn = '<span' 
  217. .' rel="popover" data-placement="'.$inaAtts['placement'].'" title="'.$inaAtts['title'].'"' 
  218. .' data-content="'.$inaAtts['content'].'"' 
  219. .$this->noEmptyHtml( $inaAtts['id'], 'id' ) 
  220. .$this->noEmptyHtml( $inaAtts['class'], 'class' ) 
  221. .$this->noEmptyHtml( $inaAtts['style'], 'style' ).'>'.$this->doShortcode( $insContent ).'</span>'; 
  222.  
  223. return $sReturn; 
  224.  
  225. public function dropdown( $inaAtts = array(), $insContent = '' ) { 
  226. $this->def( $inaAtts, 'name', 'Undefined' ); 
  227.  
  228. $insContent = ' 
  229. <ul class="tabs"> 
  230. <li class="dropdown" data-dropdown="dropdown"> 
  231. <a class="dropdown-toggle" href="#">'.$inaAtts['name'].'</a> 
  232. <ul class="dropdown-menu"> 
  233. '.$insContent.' 
  234. </ul> 
  235. </li> 
  236. </ul> 
  237. '; 
  238.  
  239. return $this->doShortcode( $insContent ); 
  240.  
  241. /** 
  242. * This is used by both dropdown and tabgroup/tab 
  243. */ 
  244. public function dropdown_option( $inaAtts = array(), $insContent = '' ) { 
  245. $this->def( $inaAtts, 'name', 'Undefined' ); 
  246. $this->def( $inaAtts, 'link', '#' ); 
  247.  
  248. $insContent = '<li><a href="'.$inaAtts['link'].'">'.$inaAtts['name'].'</a></li>'; 
  249.  
  250. return $this->doShortcode( $insContent ); 
  251.  
  252. public function tabgroup( $inaAtts = array(), $insContent ) { 
  253.  
  254. $aTabs = array(); 
  255. $aMatches = array(); 
  256. $nOffsetAdjustment = 0; 
  257. $i = 0; 
  258.  
  259. /** 
  260. * Because there are 2 separate sections of HTML for the tabs to work, we need to 
  261. * look for the TBS_TAB shortcodes now, to create the buttons. The $insContent is 
  262. * passed onwards and will be used to create the tab content panes. 
  263. *  
  264. * PREG_OFFSET_CAPTURE requires PHP 4.3.0 
  265. */ 
  266. if ( preg_match_all( '/\[TBS_TAB([^\]]*)\]/', $insContent, $aMatches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE ) ) { 
  267. foreach ( $aMatches as $aMatch ) { 
  268. //aMatch = Array ( [0] => Array ( [0] => [TBS_TAB page_id="53" name="test1"] [1] => 1 ) [1] => Array ( [0] => page_id="53" name="test1" [1] => 9 ) ) 
  269.  
  270. if ( !isset( $aMatch[1] ) ) { 
  271. continue; 
  272.  
  273. $sName = "Undefined"; 
  274. if ( preg_match( '/name\s*=\s*("|\')(.+)\g{-2}+/i', $aMatch[1][0], $aSubMatches ) ) { 
  275. $sName = $aSubMatches[2]; 
  276.  
  277. $sType = "page"; 
  278. if ( preg_match( '/type\s*=\s*("|\')(page|dropdown)\g{-2}+/i', $aMatch[1][0], $aSubMatches ) ) { 
  279. $sType = $aSubMatches[2]; 
  280.  
  281. if ( $sType == "page" ) { 
  282. $aTabs[] = '<li class="'.($i == 0? 'active': '').'"><a href="#TbsTabId'.$i.'">'.$sName.'</a></li>'; 
  283. else { 
  284. /** 
  285. * Handle the dropdowns as the tab() shortcode handles the tab contents only 
  286. */ 
  287. $nOffsetTemp = $aMatch[0][1] + $nOffsetAdjustment; 
  288.  
  289. $sRemainder = substr( $insContent, $nOffsetTemp + strlen( $aMatch[0][0] ) );  
  290. $nPos = strpos( $sRemainder, '[/TBS_TAB]' ); 
  291. $sRemainder = substr( $sRemainder, 0, $nPos ); 
  292.  
  293. // match all dropdowns until [/TBS_TAB] 
  294. if ( !preg_match_all( '/\[TBS_DROPDOWN_OPTION([^\]]*)\]/', $sRemainder, $aSubMatches, PREG_SET_ORDER ) ) { 
  295. continue; 
  296.  
  297. $aOptions = array(); 
  298. foreach ( $aSubMatches as $aSubMatch ) { 
  299. $sLink = '#'; 
  300. if ( preg_match( '/link\s*=\s*("|\')(.*)\g{-2}+/i', $aSubMatch[1][0], $aSubMatches ) ) { 
  301. $sLink = $aSubMatches[2]; 
  302.  
  303. $sName = 'Undefined'; 
  304. if ( preg_match( '/name\s*=\s*("|\')(.*)\g{-2}+/i', $aSubMatch[1][0], $aSubMatches ) ) { 
  305. $sName = $aSubMatches[2]; 
  306.  
  307. $aOptions[] = '<li><a href="'.$sLink.'">'.$sName.'</a></li>'; 
  308.  
  309. $aTabs[] = ' 
  310. <li class="dropdown" data-dropdown="dropdown"> 
  311. <a class="dropdown-toggle" href=" #">'.$sName.'</a> 
  312. <ul class="dropdown-menu"> 
  313. '.implode( '', $aOptions ).' 
  314. </ul> 
  315. </li> 
  316. '; 
  317.  
  318. $nOffset = $aMatch[0][1] + $nOffsetAdjustment; 
  319. $nLength = strlen( $aMatch[0][0] ); 
  320. $sAddition = ' id="TbsTabId'.$i.'"'; 
  321. $insContent = substr_replace( $insContent, '[TBS_TAB'.($aMatch[1][0]).$sAddition.']', $nOffset, $nLength ); 
  322.  
  323. $nOffsetAdjustment += strlen( $sAddition ); 
  324.  
  325. $i++; 
  326.  
  327. $insContent = ' 
  328. <ul class="tabs" data-tabs="tabs"> 
  329. '.implode( "\n", $aTabs ).' 
  330. </ul> 
  331. <div id="my-tab-content" class="tab-content"> 
  332. '.$insContent.' 
  333. </div> 
  334. '; 
  335.  
  336. return $this->doShortcode( $insContent ); 
  337.  
  338. /** 
  339. * Reference: http://codex.wordpress.org/Function_Reference/get_page 
  340. */ 
  341. public function tab( $inaAtts = array(), $insContent = '' ) { 
  342. $this->def( $inaAtts, 'page_id', 0 ); 
  343. $this->def( $inaAtts, 'type', 'page' ); // can be either page or dropdown 
  344.  
  345. // If this value is never not set, then the tabgroup method didn't do it's job! 
  346. $this->def( $inaAtts, 'id', 'TbsTabId_' ); 
  347.  
  348. // Actually not used as the tab name is used by the TabGroup 
  349. $this->def( $inaAtts, 'name', 'Undefined' ); 
  350.  
  351. if ( $inaAtts['page_id'] > 0 ) { 
  352. $oPage = get_page( $inaAtts['page_id'] ); 
  353. if ( !is_null( $oPage ) ) { 
  354. $insContent = $oPage->post_content; 
  355.  
  356. $nIndex = intval( str_replace( 'TbsTabId', '', $inaAtts['id'] ) ); 
  357.  
  358. $insContent = '<div id="'.$inaAtts['id'].'" class="tab-pane'.($nIndex == 0?' active':'').'">'.$insContent.'</div>'; 
  359.  
  360. return $this->doShortcode( $insContent ); 
  361.  
  362. public function row( $inaAtts = array(), $insContent = '' ) { 
  363.  
  364. $sReturn = '<div class="container"> <div class="row">'; 
  365. $sReturn .= $this->doShortcode( $insContent ); 
  366. $sReturn .= '</div></div>'; 
  367.  
  368. return $sReturn; 
  369. }//row 
  370.  
  371. public function column( $inaAtts = array(), $insContent = '' ) { 
  372.  
  373. $this->def( $inaAtts, 'size', 1 ); 
  374. $this->def( $inaAtts, 'style' ); 
  375. $this->def( $inaAtts, 'id' ); 
  376. $this->def( $inaAtts, 'class' ); 
  377.  
  378. $sReturn = '<div class="span'.$inaAtts['size'].' '.$inaAtts['class']. '"' 
  379. .$this->noEmptyHtml( $inaAtts['id'], 'id' ) 
  380. .$this->noEmptyHtml( $inaAtts['style'], 'style' ).'>'; 
  381. $sReturn .= $this->doShortcode( $insContent ); 
  382. $sReturn .= '</div>'; 
  383.  
  384. return $sReturn; 
  385. }//row 
  386.  
  387. /** 
  388. * Public, but should never be directly accessed other than by the WP add_filter method.  
  389. * @param $insContent 
  390. */ 
  391. public function filterTheContent( $insContent = "" ) {  
  392. // Remove <p>'s that get added to [TBS...] by wpautop. 
  393. $insContent = preg_replace( '|(<p>\s*)?(\[/?TBS[^\]]+\])(\s*</p>)?|', "$2", $insContent ); 
  394.  
  395. return $insContent; 
  396.  
  397. public function filterTheContentToFixNamedAnchors( $insContent = "" ) {  
  398. $sPattern = '/(<a\s+href=")(.*)(#TbsTabId[0-9]+">(.*)<\/a>)/'; 
  399. $insContent = preg_replace( $sPattern, '$1$3', $insContent ); 
  400.  
  401. return $insContent; 
  402.  
  403. /** 
  404. * name collision on "default" 
  405. */ 
  406. protected function def( $aSrc, $insKey, $insValue = '' ) { 
  407. if ( !isset( $aSrc[$insKey] ) ) { 
  408. $aSrc[$insKey] = $insValue; 
  409.  
  410. protected function idHtml( $insId ) { 
  411. return (($insId != '')? ' id="'.$insId.'" ' : '' );  
  412. protected function noEmptyHtml( $insContent, $insAttr ) { 
  413. return (($insContent != '')? ' '.$insAttr.'="'.$insContent.'" ' : '' );  
  414.  
  415. /** 
  416. * Only implemented for possible future customisation 
  417. * @param unknown_type $insContent 
  418. */ 
  419. protected function doShortcode( $insContent ) { 
  420. return do_shortcode( $insContent ); 
  421.