PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/web xuanthuc.com php/web xuanthuc.com/wp-content/plugins/breadcrumb-navxt/class.bcn_breadcrumb.php

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