breadcrumb-navxt
Last commit date
includes
6 months ago
languages
9 years ago
breadcrumb-navxt.php
6 months ago
class.bcn_admin.php
6 months ago
class.bcn_breadcrumb.php
6 months ago
class.bcn_breadcrumb_trail.php
6 months ago
class.bcn_network_admin.php
6 months ago
class.bcn_rest_controller.php
6 months ago
class.bcn_widget.php
6 months ago
options_upgrade.php
1 year ago
readme.txt
6 months ago
uninstall.php
6 months ago
class.bcn_breadcrumb.php
276 lines
| 1 | <?php |
| 2 | /* |
| 3 | Copyright 2007-2025 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 | { |
| 23 | //Our member variables |
| 24 | const version = breadcrumb_navxt::version; |
| 25 | //The main text that will be shown |
| 26 | protected $title; |
| 27 | //The breadcrumb's template, used during assembly |
| 28 | protected $template; |
| 29 | //The breadcrumb's no anchor template, used during assembly when there won't be an anchor |
| 30 | protected $template_no_anchor; |
| 31 | //Boolean, is this element linked |
| 32 | protected $linked = false; |
| 33 | //The link the breadcrumb leads to, null if $linked == false |
| 34 | protected $url; |
| 35 | //The corresponding resource ID |
| 36 | protected $id = null; |
| 37 | private $_title = null; |
| 38 | //The type of this breadcrumb |
| 39 | protected $type; |
| 40 | const default_template_no_anchor = '<span property="itemListElement" typeof="ListItem"><span property="name" class="%type%">%htitle%</span><meta property="url" content="%link%"><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 | * @param bool $linked (optional) Whether or not the breadcrumb uses the linked or unlinked template |
| 49 | */ |
| 50 | public function __construct($title = '', $template = '', array $type = array(), $url = '', $id = null, $linked = false) |
| 51 | { |
| 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 | { |
| 63 | $this->set_template(bcn_breadcrumb::get_default_template()); |
| 64 | } |
| 65 | //If something was passed in template wise, update the appropriate internal template |
| 66 | else |
| 67 | { |
| 68 | if($linked) |
| 69 | { |
| 70 | $this->set_template($template); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | $this->template_no_anchor = $this->run_template_kses(apply_filters('bcn_breadcrumb_template_no_anchor', $template, $this->type, $this->id)); |
| 75 | $this->set_template(bcn_breadcrumb::get_default_template()); |
| 76 | } |
| 77 | } |
| 78 | $this->set_url($url); |
| 79 | $this->set_linked($linked); |
| 80 | } |
| 81 | /** |
| 82 | * Function to return the translated default template |
| 83 | * |
| 84 | * @return string The default breadcrumb template |
| 85 | */ |
| 86 | static public function get_default_template() |
| 87 | { |
| 88 | return sprintf('<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="%1$s" href="%%link%%" class="%%type%%" bcn-aria-current><span property="name">%%htitle%%</span></a><meta property="position" content="%%position%%"></span>', esc_attr__('Go to %title%.','breadcrumb-navxt')); |
| 89 | } |
| 90 | /** |
| 91 | * Function to set the protected title member |
| 92 | * |
| 93 | * @param string $title The title of the breadcrumb |
| 94 | */ |
| 95 | public function set_title($title) |
| 96 | { |
| 97 | //Set the title |
| 98 | $this->title = apply_filters('bcn_breadcrumb_title', $title, $this->type, $this->id); |
| 99 | $this->_title = $this->title; |
| 100 | } |
| 101 | /** |
| 102 | * Function to get the protected title member |
| 103 | * |
| 104 | * @return $this->title |
| 105 | */ |
| 106 | public function get_title() |
| 107 | { |
| 108 | //Return the title |
| 109 | return $this->title; |
| 110 | } |
| 111 | /** |
| 112 | * Function to set the internal URL variable |
| 113 | * |
| 114 | * @param string $url the URL to link to |
| 115 | */ |
| 116 | public function set_url($url) |
| 117 | { |
| 118 | $url = trim($url); |
| 119 | $this->url = apply_filters('bcn_breadcrumb_url', $url, $this->type, $this->id); |
| 120 | } |
| 121 | /** |
| 122 | * Function to get the internal URL variable |
| 123 | * |
| 124 | * @return string the URL that the breadcrumb links to |
| 125 | */ |
| 126 | public function get_url() |
| 127 | { |
| 128 | return $this->url; |
| 129 | } |
| 130 | /** |
| 131 | * Function to set the internal breadcrumb linked status |
| 132 | * |
| 133 | * @param bool $linked whether or not the breadcrumb uses the linked or unlinked template |
| 134 | */ |
| 135 | public function set_linked($linked) |
| 136 | { |
| 137 | $this->linked = apply_filters('bcn_breadcrumb_linked', $linked, $this->type, $this->id); |
| 138 | } |
| 139 | /** |
| 140 | * Function to check if this breadcrumb will be linked |
| 141 | * |
| 142 | * @return boolean whether or not this breadcrumb is linked |
| 143 | */ |
| 144 | public function is_linked() |
| 145 | { |
| 146 | return $this->linked; |
| 147 | } |
| 148 | /** |
| 149 | * A wrapper for wp_kses which handles getting the allowed html |
| 150 | * |
| 151 | * @param string $template_str The template string to run through kses |
| 152 | * @return string The template string post cleaning |
| 153 | */ |
| 154 | protected function run_template_kses($template_str) |
| 155 | { |
| 156 | return wp_kses($template_str, apply_filters('bcn_allowed_html', wp_kses_allowed_html('post'))); |
| 157 | } |
| 158 | /** |
| 159 | * Function to set the internal breadcrumb template |
| 160 | * |
| 161 | * @param string $template the template to use during assembly |
| 162 | */ |
| 163 | public function set_template($template) |
| 164 | { |
| 165 | //Assign the breadcrumb template |
| 166 | $this->template = $this->run_template_kses(apply_filters('bcn_breadcrumb_template', $template, $this->type, $this->id)); |
| 167 | } |
| 168 | /** |
| 169 | * Function to set the internal breadcrumb ID |
| 170 | * |
| 171 | * @param int $id the id of the resource this breadcrumb represents |
| 172 | */ |
| 173 | public function set_id($id) |
| 174 | { |
| 175 | $this->id = $id; |
| 176 | } |
| 177 | /** |
| 178 | * Function to get the internal breadcrumb ID |
| 179 | * |
| 180 | * @return int the id of the resource this breadcrumb represents |
| 181 | */ |
| 182 | public function get_id() |
| 183 | { |
| 184 | return $this->id; |
| 185 | } |
| 186 | /** |
| 187 | * Append a type entry to the type array |
| 188 | * |
| 189 | * @param string $type the type to append |
| 190 | */ |
| 191 | public function add_type($type) |
| 192 | { |
| 193 | $this->type[] = $type; |
| 194 | } |
| 195 | /** |
| 196 | * Return the type array |
| 197 | * |
| 198 | * @return array The type array |
| 199 | */ |
| 200 | public function get_types() |
| 201 | { |
| 202 | return $this->type; |
| 203 | } |
| 204 | /** |
| 205 | * Assembles the parts of the breadcrumb into a html string |
| 206 | * |
| 207 | * @param bool $linked Allow the output to contain anchors? |
| 208 | * @param int $position The position of the breadcrumb in the trail (between 1 and n when there are n breadcrumbs in the trail) |
| 209 | * @param bool $is_current_item Whether or not this breadcrumb represents the current item |
| 210 | * |
| 211 | * @return string The compiled breadcrumb string |
| 212 | */ |
| 213 | public function assemble($linked, $position, $is_current_item = false) |
| 214 | { |
| 215 | if($is_current_item) |
| 216 | { |
| 217 | $aria_current_str = 'aria-current="page"'; |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | $aria_current_str = ''; |
| 222 | } |
| 223 | //Build our replacements array |
| 224 | $replacements = array( |
| 225 | '%title%' => esc_attr(strip_tags($this->title)), |
| 226 | '%link%' => esc_url($this->url), |
| 227 | '%htitle%' => $this->title, |
| 228 | '%type%' => apply_filters('bcn_breadcrumb_types', $this->type, $this->id), |
| 229 | '%ftitle%' => esc_attr(strip_tags($this->_title)), |
| 230 | '%fhtitle%' => $this->_title, |
| 231 | '%position%' => $position, |
| 232 | 'bcn-aria-current' => $aria_current_str |
| 233 | ); |
| 234 | //The type may be an array, implode it if that is the case |
| 235 | if(is_array($replacements['%type%'])) |
| 236 | { |
| 237 | array_walk($replacements['%type%'], 'sanitize_html_class'); |
| 238 | $replacements['%type%'] = esc_attr(implode(' ', $replacements['%type%'])); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | _doing_it_wrong(__CLASS__ . '::' . __FUNCTION__, __('bcn_breadcrumb::type must be an array', 'breadcrumb-navxt'), '6.0.2'); |
| 243 | } |
| 244 | $replacements = apply_filters('bcn_template_tags', $replacements, $this->type, $this->id); |
| 245 | //If we are linked we'll need to use the normal template |
| 246 | if($this->linked && $linked) |
| 247 | { |
| 248 | //Return the assembled breadcrumb string |
| 249 | return str_replace(array_keys($replacements), $replacements, $this->template); |
| 250 | } |
| 251 | //Otherwise we use the no anchor template |
| 252 | else |
| 253 | { |
| 254 | //Return the assembled breadcrumb string |
| 255 | return str_replace(array_keys($replacements), $replacements, $this->template_no_anchor); |
| 256 | } |
| 257 | } |
| 258 | /** |
| 259 | * Assembles the parts of the breadcrumb into a JSON-LD ready object-array |
| 260 | * |
| 261 | * @param int $position The position of the breadcrumb in the trail (between 1 and n when there are n breadcrumbs in the trail) |
| 262 | * |
| 263 | * @return array(object) The prepared array object ready to pass into json_encode |
| 264 | */ |
| 265 | public function assemble_json_ld($position) |
| 266 | { |
| 267 | return (object) apply_filters('bcn_breadcrumb_assembled_json_ld_array', array( |
| 268 | '@type' => 'ListItem', |
| 269 | 'position' => $position, |
| 270 | 'item' => (object)array( |
| 271 | '@id' => esc_url($this->url), |
| 272 | 'name' => esc_attr($this->title)) |
| 273 | ), $this->type, $this->id); |
| 274 | } |
| 275 | } |
| 276 |