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