ShortCodeLoader

"Contact Form to Database" Copyright (C) 2011-2012 Michael Simpson (email : michael.d.simpson@gmail.com).

Defined (1)

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

/ShortCodeLoader.php  
  1. abstract class ShortCodeLoader { 
  2.  
  3. /** 
  4. * @param $shortcodeName mixed either string name of the shortcode 
  5. * (as it would appear in a post, e.g. [shortcodeName]) 
  6. * or an array of such names in case you want to have more than one name 
  7. * for the same shortcode 
  8. * @return void 
  9. */ 
  10. public function register($shortcodeName) { 
  11. $this->registerShortcodeToFunction($shortcodeName, 'handleShortcode'); 
  12.  
  13. /** 
  14. * @param $shortcodeName string|array name of the shortcode 
  15. * as it would appear in a post, e.g. [shortcodeName] 
  16. * or an array of such names in case you want to have more than one name 
  17. * for the same shortcode 
  18. * @param $functionName string name of public function in this class to call as the 
  19. * shortcode handler 
  20. * @return void 
  21. */ 
  22. protected function registerShortcodeToFunction($shortcodeName, $functionName) { 
  23. if (is_array($shortcodeName)) { 
  24. foreach ($shortcodeName as $aName) { 
  25. add_shortcode($aName, array($this, $functionName)); 
  26. else { 
  27. add_shortcode($shortcodeName, array($this, $functionName)); 
  28.  
  29. /** 
  30. * @param $atts array 
  31. * @return array 
  32. */ 
  33. public function decodeAttributes($atts) { 
  34. if (is_array($atts)) { 
  35. foreach ($atts as $key => $value) { 
  36. $atts[$key] = $this->decodeString($value); 
  37. $atts = $this->workAround_29658($atts); 
  38. return $atts; 
  39.  
  40. // Work-around for https://core.trac.wordpress.org/ticket/29658 
  41. public function workAround_29658($atts) { 
  42. if (isset($atts[0])) { 
  43. if (isset($atts['filter']) && strpos($atts['filter'], '*') === 0) { 
  44. $atts['filter'] = $atts['filter'] . ' ' . $atts[0]; 
  45. $atts['filter'] = $this->stripCurlyQuotes($atts['filter']); 
  46. unset($atts[0]); 
  47. else if (isset($atts['tfilter']) && strpos($atts['tfilter'], '*') === 0) { 
  48. $atts['tfilter'] = $atts['tfilter'] . ' ' . $atts[0]; 
  49. $atts['tfilter'] = $this->stripCurlyQuotes($atts['tfilter']); 
  50. unset($atts[0]); 
  51. else if (isset($atts['trans']) && strpos($atts['trans'], '*') === 0) { 
  52. $atts['trans'] = $atts['trans'] . ' ' . $atts[0]; 
  53. $atts['trans'] = $this->stripCurlyQuotes($atts['trans']); 
  54. unset($atts[0]); 
  55. return $atts; 
  56.  
  57. /** 
  58. * Deal with WordPress editor or theme replacing quoted short code attributed 
  59. * with different variations of quotes, which would then be included in the attribute 
  60. * string and cause errors 
  61. * @param $text string 
  62. * @return string 
  63. */ 
  64. public function decodeString($text) { 
  65. $text = html_entity_decode($text); 
  66. $text = $this->stripCurlyQuotes($text); 
  67. return $text; 
  68.  
  69. /** 
  70. * Remove leading-ending curly quotes 
  71. * @param $text string 
  72. * @return string 
  73. */ 
  74. public function stripCurlyQuotes($text) { 
  75. $quotes = array('*', '*', '*', '*', '*', '*'); 
  76. $startsWith = false; 
  77. $startQuote = null; 
  78. foreach ($quotes as $startQuote) { 
  79. $quoteLen = strlen($startQuote); 
  80. $startsWith = substr($text, 0, $quoteLen) == $startQuote; 
  81. if ($startsWith) { 
  82. break; 
  83. if ($startsWith) { 
  84. foreach ($quotes as $endQuote) { 
  85. $quoteLen = strlen($endQuote); 
  86. $endsWith = substr($text, -$quoteLen) == $endQuote; 
  87. if ($endsWith) { 
  88. $text = substr($text,  
  89. strlen($startQuote),  
  90. strlen($text) - $quoteLen - strlen($startQuote)); 
  91. break; 
  92. return $text; 
  93.  
  94. /** 
  95. * @abstract Override this function and add actual shortcode handling here 
  96. * @param $atts array (associative) of shortcode inputs 
  97. * @param $content string inner content of short code 
  98. * @return string shortcode content 
  99. */ 
  100. public abstract function handleShortcode($atts, $content = null); 
  101.