bcn_breadcrumb

The Breadcrumb NavXT bcn breadcrumb class.

Defined (1)

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

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