admin
3 years ago
debug
5 years ago
open-graph
3 years ago
slack
3 years ago
twitter
3 years ago
webmaster
4 years ago
abstract-indexable-presenter.php
4 years ago
abstract-indexable-tag-presenter.php
3 years ago
abstract-presenter.php
5 years ago
breadcrumbs-presenter.php
3 years ago
canonical-presenter.php
4 years ago
meta-author-presenter.php
3 years ago
meta-description-presenter.php
3 years ago
rel-next-presenter.php
4 years ago
rel-prev-presenter.php
4 years ago
robots-presenter.php
4 years ago
robots-txt-presenter.php
3 years ago
schema-presenter.php
4 years ago
score-icon-presenter.php
3 years ago
title-presenter.php
4 years ago
url-list-presenter.php
5 years ago
breadcrumbs-presenter.php
262 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Yoast\WP\SEO\Presenters; |
| 4 | |
| 5 | use Yoast\WP\SEO\Presentations\Indexable_Presentation; |
| 6 | |
| 7 | /** |
| 8 | * Presenter class for the breadcrumbs. |
| 9 | */ |
| 10 | class Breadcrumbs_Presenter extends Abstract_Indexable_Presenter { |
| 11 | |
| 12 | /** |
| 13 | * The id attribute. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | private $id; |
| 18 | |
| 19 | /** |
| 20 | * The class name attribute. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | private $class; |
| 25 | |
| 26 | /** |
| 27 | * The wrapper element name. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | private $wrapper; |
| 32 | |
| 33 | /** |
| 34 | * Separator to use. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | private $separator; |
| 39 | |
| 40 | /** |
| 41 | * The element. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | private $element; |
| 46 | |
| 47 | /** |
| 48 | * Presents the breadcrumbs. |
| 49 | * |
| 50 | * @return string The breadcrumbs HTML. |
| 51 | */ |
| 52 | public function present() { |
| 53 | $breadcrumbs = $this->get(); |
| 54 | if ( ! \is_array( $breadcrumbs ) || empty( $breadcrumbs ) ) { |
| 55 | return ''; |
| 56 | } |
| 57 | |
| 58 | $links = []; |
| 59 | $total = \count( $breadcrumbs ); |
| 60 | foreach ( $breadcrumbs as $index => $breadcrumb ) { |
| 61 | $links[ $index ] = $this->crumb_to_link( $breadcrumb, $index, $total ); |
| 62 | } |
| 63 | |
| 64 | // Removes any effectively empty links. |
| 65 | $links = \array_map( 'trim', $links ); |
| 66 | $links = \array_filter( $links ); |
| 67 | $output = \implode( $this->get_separator(), $links ); |
| 68 | |
| 69 | if ( empty( $output ) ) { |
| 70 | return ''; |
| 71 | } |
| 72 | |
| 73 | $output = '<' . $this->get_wrapper() . $this->get_id() . $this->get_class() . '>' . $output . '</' . $this->get_wrapper() . '>'; |
| 74 | $output = $this->filter( $output ); |
| 75 | |
| 76 | $prefix = $this->helpers->options->get( 'breadcrumbs-prefix' ); |
| 77 | if ( $prefix !== '' ) { |
| 78 | $output = "\t" . $prefix . "\n" . $output; |
| 79 | } |
| 80 | |
| 81 | return $output; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets the raw value of a presentation. |
| 86 | * |
| 87 | * @return array The raw value. |
| 88 | */ |
| 89 | public function get() { |
| 90 | return $this->presentation->breadcrumbs; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Filters the output. |
| 95 | * |
| 96 | * @param string $output The HTML output. |
| 97 | * |
| 98 | * @return string The filtered output. |
| 99 | */ |
| 100 | protected function filter( $output ) { |
| 101 | /** |
| 102 | * Filter: 'wpseo_breadcrumb_output' - Allow changing the HTML output of the Yoast SEO breadcrumbs class. |
| 103 | * |
| 104 | * @param Indexable_Presentation $presentation The presentation of an indexable. |
| 105 | * |
| 106 | * @api string $output The HTML output. |
| 107 | */ |
| 108 | return \apply_filters( 'wpseo_breadcrumb_output', $output, $this->presentation ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Create a breadcrumb element string. |
| 113 | * |
| 114 | * @param array $breadcrumb Link info array containing the keys: |
| 115 | * 'text' => (string) link text. |
| 116 | * 'url' => (string) link url. |
| 117 | * (optional) 'title' => (string) link title attribute text. |
| 118 | * @param int $index Index for the current breadcrumb. |
| 119 | * @param int $total The total number of breadcrumbs. |
| 120 | * |
| 121 | * @return string The breadcrumb link. |
| 122 | */ |
| 123 | protected function crumb_to_link( $breadcrumb, $index, $total ) { |
| 124 | $link = ''; |
| 125 | |
| 126 | if ( ! isset( $breadcrumb['text'] ) || ! \is_string( $breadcrumb['text'] ) || empty( $breadcrumb['text'] ) ) { |
| 127 | return $link; |
| 128 | } |
| 129 | |
| 130 | $text = \trim( $breadcrumb['text'] ); |
| 131 | |
| 132 | if ( |
| 133 | $index < ( $total - 1 ) |
| 134 | && isset( $breadcrumb['url'] ) |
| 135 | && \is_string( $breadcrumb['url'] ) |
| 136 | && ! empty( $breadcrumb['url'] ) |
| 137 | ) { |
| 138 | // If it's not the last element and we have a url. |
| 139 | $link .= '<' . $this->get_element() . '>'; |
| 140 | $title_attr = isset( $breadcrumb['title'] ) ? ' title="' . \esc_attr( $breadcrumb['title'] ) . '"' : ''; |
| 141 | $link .= '<a href="' . \esc_url( $breadcrumb['url'] ) . '"' . $title_attr . '>' . $text . '</a>'; |
| 142 | $link .= '</' . $this->get_element() . '>'; |
| 143 | } |
| 144 | elseif ( $index === ( $total - 1 ) ) { |
| 145 | // If it's the last element. |
| 146 | |
| 147 | if ( $this->helpers->options->get( 'breadcrumbs-boldlast' ) === true ) { |
| 148 | $text = '<strong>' . $text . '</strong>'; |
| 149 | } |
| 150 | |
| 151 | $link .= '<' . $this->get_element() . ' class="breadcrumb_last" aria-current="page">' . $text . '</' . $this->get_element() . '>'; |
| 152 | } |
| 153 | else { |
| 154 | // It's not the last element and has no url. |
| 155 | $link .= '<' . $this->get_element() . '>' . $text . '</' . $this->get_element() . '>'; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Filter: 'wpseo_breadcrumb_single_link' - Allow changing of each link being put out by the Yoast SEO breadcrumbs class. |
| 160 | * |
| 161 | * @param array $link The link array. |
| 162 | * |
| 163 | * @api string $link_output The output string. |
| 164 | */ |
| 165 | return \apply_filters( 'wpseo_breadcrumb_single_link', $link, $breadcrumb ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Retrieves HTML ID attribute. |
| 170 | * |
| 171 | * @return string The id attribute. |
| 172 | */ |
| 173 | protected function get_id() { |
| 174 | if ( ! $this->id ) { |
| 175 | /** |
| 176 | * Filter: 'wpseo_breadcrumb_output_id' - Allow changing the HTML ID on the Yoast SEO breadcrumbs wrapper element. |
| 177 | * |
| 178 | * @api string $unsigned ID to add to the wrapper element. |
| 179 | */ |
| 180 | $this->id = \apply_filters( 'wpseo_breadcrumb_output_id', '' ); |
| 181 | if ( ! \is_string( $this->id ) ) { |
| 182 | return ''; |
| 183 | } |
| 184 | |
| 185 | if ( $this->id !== '' ) { |
| 186 | $this->id = ' id="' . \esc_attr( $this->id ) . '"'; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return $this->id; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Retrieves HTML Class attribute. |
| 195 | * |
| 196 | * @return string The class attribute. |
| 197 | */ |
| 198 | protected function get_class() { |
| 199 | if ( ! $this->class ) { |
| 200 | /** |
| 201 | * Filter: 'wpseo_breadcrumb_output_class' - Allow changing the HTML class on the Yoast SEO breadcrumbs wrapper element. |
| 202 | * |
| 203 | * @api string $unsigned Class to add to the wrapper element. |
| 204 | */ |
| 205 | $this->class = \apply_filters( 'wpseo_breadcrumb_output_class', '' ); |
| 206 | if ( ! \is_string( $this->class ) ) { |
| 207 | return ''; |
| 208 | } |
| 209 | |
| 210 | if ( $this->class !== '' ) { |
| 211 | $this->class = ' class="' . \esc_attr( $this->class ) . '"'; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return $this->class; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Retrieves the wrapper element name. |
| 220 | * |
| 221 | * @return string The wrapper element name. |
| 222 | */ |
| 223 | protected function get_wrapper() { |
| 224 | if ( ! $this->wrapper ) { |
| 225 | $this->wrapper = \apply_filters( 'wpseo_breadcrumb_output_wrapper', 'span' ); |
| 226 | $this->wrapper = \tag_escape( $this->wrapper ); |
| 227 | if ( ! \is_string( $this->wrapper ) || $this->wrapper === '' ) { |
| 228 | $this->wrapper = 'span'; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return $this->wrapper; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Retrieves the separator. |
| 237 | * |
| 238 | * @return string The separator. |
| 239 | */ |
| 240 | protected function get_separator() { |
| 241 | if ( ! $this->separator ) { |
| 242 | $this->separator = \apply_filters( 'wpseo_breadcrumb_separator', $this->helpers->options->get( 'breadcrumbs-sep' ) ); |
| 243 | $this->separator = ' ' . $this->separator . ' '; |
| 244 | } |
| 245 | |
| 246 | return $this->separator; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Retrieves the crumb element name. |
| 251 | * |
| 252 | * @return string The element to use. |
| 253 | */ |
| 254 | protected function get_element() { |
| 255 | if ( ! $this->element ) { |
| 256 | $this->element = \esc_attr( \apply_filters( 'wpseo_breadcrumb_single_link_wrapper', 'span' ) ); |
| 257 | } |
| 258 | |
| 259 | return $this->element; |
| 260 | } |
| 261 | } |
| 262 |