/class.bcn_breadcrumb.php

  1. <?php 
  2. /**  
  3. Copyright 2007-2016 John Havlik (email : john.havlik@mtekk.us) 
  4.   
  5. This program is free software; you can redistribute it and/or modify 
  6. it under the terms of the GNU General Public License as published by 
  7. the Free Software Foundation; either version 2 of the License, or 
  8. (at your option) any later version. 
  9.   
  10. This program is distributed in the hope that it will be useful,  
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  13. GNU General Public License for more details. 
  14.   
  15. You should have received a copy of the GNU General Public License 
  16. along with this program; if not, write to the Free Software 
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
  18. */ 
  19. require_once(dirname(__FILE__) . '/includes/block_direct_access.php'); 
  20. //The breadcrumb class 
  21. class bcn_breadcrumb 
  22. //Our member variables 
  23. const version = '5.6.0'; 
  24. //The main text that will be shown 
  25. protected $title; 
  26. //The breadcrumb's template, used durring assembly 
  27. protected $template; 
  28. //The breadcrumb's no anchor template, used durring assembly when there won't be an anchor 
  29. protected $template_no_anchor; 
  30. //Boolean, is this element linked 
  31. protected $linked = false; 
  32. //The link the breadcrumb leads to, null if $linked == false 
  33. protected $url; 
  34. //The corresponding resource ID 
  35. protected $id = NULL; 
  36. private $_title = NULL; 
  37. //The type of this breadcrumb 
  38. public $type; 
  39. protected $allowed_html = array(); 
  40. const default_template_no_anchor = '<span property="itemListElement" typeof="ListItem"><span property="name">%htitle%</span><meta property="position" content="%position%"></span>'; 
  41. /** 
  42. * The enhanced default constructor, ends up setting all parameters via the set_ functions 
  43. *  
  44. * @param string $title (optional) The title of the breadcrumb 
  45. * @param string $template (optional) The html template for the breadcrumb 
  46. * @param string $type (optional) The breadcrumb type 
  47. * @param string $url (optional) The url the breadcrumb links to 
  48. */ 
  49. public function __construct($title = '', $template = '', array $type = array(), $url = '', $id = NULL) 
  50. //Filter allowed_html array to allow others to add acceptable tags 
  51. $this->allowed_html = apply_filters('bcn_allowed_html', wp_kses_allowed_html('post')); 
  52. //The breadcrumb type 
  53. $this->type = $type; 
  54. //Set the resource id 
  55. $this->set_id($id); 
  56. //Set the title 
  57. $this->set_title($title); 
  58. //Set the default anchorless templates value 
  59. $this->template_no_anchor = bcn_breadcrumb::default_template_no_anchor; 
  60. //If we didn't get a good template, use a default template 
  61. if($template == NULL) 
  62. $this->set_template(bcn_breadcrumb::get_default_template()); 
  63. //If something was passed in template wise, update the appropriate internal template 
  64. else 
  65. //Loose comparison, evaluates to true if URL is '' or NULL 
  66. if($url == NULL) 
  67. $this->template_no_anchor = wp_kses(apply_filters('bcn_breadcrumb_template_no_anchor', $template, $this->type, $this->id), $this->allowed_html); 
  68. $this->set_template(bcn_breadcrumb::get_default_template()); 
  69. else 
  70. $this->set_template($template); 
  71. //Always NULL if unlinked 
  72. $this->set_url($url); 
  73. /** 
  74. * Function to return the translated default template 
  75. *  
  76. * @return string The default breadcrumb template  
  77. */ 
  78. static public function get_default_template() 
  79. return __('<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Go to %title%." href="%link%" class="%type%"><span property="name">%htitle%</span></a><meta property="position" content="%position%"></span>', 'breadcrumb-navxt'); 
  80. /** 
  81. * Function to set the protected title member 
  82. *  
  83. * @param string $title The title of the breadcrumb 
  84. */ 
  85. public function set_title($title) 
  86. //Set the title 
  87. $this->title = apply_filters('bcn_breadcrumb_title', $title, $this->type, $this->id); 
  88. $this->_title = $this->title; 
  89. /** 
  90. * Function to get the protected title member 
  91. *  
  92. * @return $this->title 
  93. */ 
  94. public function get_title() 
  95. //Return the title 
  96. return $this->title; 
  97. /** 
  98. * Function to set the internal URL variable 
  99. *  
  100. * @param string $url the url to link to 
  101. */ 
  102. public function set_url($url) 
  103. $this->url = apply_filters('bcn_breadcrumb_url', $url, $this->type, $this->id); 
  104. //If the URL seemed nullish, we are not linked 
  105. if($this->url == NULL) 
  106. $this->linked = false; 
  107. else 
  108. $this->linked = true; 
  109. /** 
  110. * Function to set the internal breadcrumb template 
  111. *  
  112. * @param string $template the template to use durring assebly 
  113. */ 
  114. public function set_template($template) 
  115. //Assign the breadcrumb template 
  116. $this->template = wp_kses(apply_filters('bcn_breadcrumb_template', $template, $this->type, $this->id), $this->allowed_html); 
  117. /** 
  118. * Function to set the internal breadcrumb ID 
  119. * 
  120. * @param int $id the id of the resource this breadcrumb represents 
  121. */ 
  122. public function set_id($id) 
  123. $this->id = $id; 
  124. /** 
  125. * Function to get the internal breadcrumb ID 
  126. * 
  127. * @return int the id of the resource this breadcrumb represents 
  128. */ 
  129. public function get_id() 
  130. return $this->id; 
  131. /** 
  132. * Append a type entry to the type array 
  133. *  
  134. * @param string $type the type to append 
  135. */ 
  136. public function add_type($type) 
  137. $this->type[] = $type; 
  138. /** 
  139. * This function will intelligently trim the title to the value passed in through $max_length. This function is deprecated, do not call. 
  140. *  
  141. * @param int $max_length of the title. 
  142. * @deprecated since 5.2.0 
  143. */ 
  144. public function title_trim($max_length) 
  145. _deprecated_function(__FUNCTION__, '5.2.0'); 
  146. //To preserve HTML entities, must decode before splitting 
  147. $this->title = html_entity_decode($this->title, ENT_COMPAT, 'UTF-8'); 
  148. $title_length = mb_strlen($this->title); 
  149. //Make sure that we are not making it longer with that ellipse 
  150. if($title_length > $max_length && ($title_length + 2) > $max_length) 
  151. //Trim the title 
  152. $this->title = mb_substr($this->title, 0, $max_length - 1); 
  153. //Make sure we can split a, but we want to limmit to cutting at max an additional 25% 
  154. if(mb_strpos($this->title, ' ', .75 * $max_length) > 0) 
  155. //Don't split mid word 
  156. while(mb_substr($this->title, -1) != ' ') 
  157. $this->title = mb_substr($this->title, 0, -1); 
  158. //Remove the whitespace at the end and add the hellip 
  159. $this->title = rtrim($this->title) . html_entity_decode('…', ENT_COMPAT, 'UTF-8'); 
  160. //Return to the encoded version of all HTML entities (keep standards complance) 
  161. $this->title = htmlentities($this->title, ENT_COMPAT, 'UTF-8'); 
  162. /** 
  163. * Assembles the parts of the breadcrumb into a html string 
  164. *  
  165. * @param bool $linked Allow the output to contain anchors? 
  166. * @param int $position The position of the breadcrumb in the trail (between 1 and n when there are n breadcrumbs in the trail) 
  167. *  
  168. * @return string The compiled breadcrumb string 
  169. */ 
  170. public function assemble($linked, $position) 
  171. //Build our replacements array 
  172. $replacements = array( 
  173. '%title%' => esc_attr(strip_tags($this->title)),  
  174. '%link%' => esc_url($this->url),  
  175. '%htitle%' => $this->title,  
  176. '%type%' => apply_filters('bcn_breadcrumb_types', $this->type, $this->id),  
  177. '%ftitle%' => esc_attr(strip_tags($this->_title)),  
  178. '%fhtitle%' => $this->_title,  
  179. '%position%' => $position 
  180. ); 
  181. //The type may be an array, implode it if that is the case 
  182. if(is_array($replacements['%type%'])) 
  183. $replacements['%type%'] = implode(' ', $replacements['%type%']); 
  184. $replacements = apply_filters('bcn_template_tags', $replacements, $this->type, $this->id); 
  185. //If we are linked we'll need to use the normal template 
  186. if($this->linked && $linked) 
  187. //Return the assembled breadcrumb string 
  188. return str_replace(array_keys($replacements), $replacements, $this->template); 
  189. //Otherwise we use the no anchor template 
  190. else 
  191. //Return the assembled breadcrumb string 
  192. return str_replace(array_keys($replacements), $replacements, $this->template_no_anchor); 
.