Frontend.php
7.64 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
namespace AIOSEO\Plugin\Common\Breadcrumbs;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Frontend.
*
* @since 4.1.1
*/
class Frontend {
/**
* A local 'cached' crumb array.
*
* @since 4.1.1
*
* @var array
*/
public $breadcrumbs = [];
/**
* Gets the current page's breadcrumbs.
*
* @since 4.1.1
*
* @return array
*/
public function getBreadcrumbs() {
if ( ! empty( $this->breadcrumbs ) ) {
return apply_filters( 'aioseo_breadcrumbs_trail', $this->breadcrumbs );
}
$type = '';
$reference = get_queried_object();
// These types need the queried object for reference.
if ( is_object( $reference ) ) {
if ( is_single() ) {
$type = 'single';
}
if ( is_singular( 'post' ) ) {
$type = 'post';
}
if ( is_page() && ! is_front_page() ) {
$type = 'page';
}
if ( is_category() || is_tag() ) {
$type = 'category';
}
if ( is_tax() ) {
$type = 'taxonomy';
}
if ( is_post_type_archive() ) {
$type = 'postTypeArchive';
}
if ( is_author() ) {
$type = 'author';
}
if ( is_home() ) {
$type = 'blog';
}
}
if ( is_date() ) {
$type = 'date';
$reference = [
'year' => get_query_var( 'year' ),
'month' => get_query_var( 'monthnum' ),
'day' => get_query_var( 'day' )
];
}
if ( is_search() ) {
$type = 'search';
$reference = htmlspecialchars( sanitize_text_field( get_search_query() ) );
}
if ( is_404() ) {
$type = 'notFound';
}
$paged = false;
if ( is_paged() || ( is_singular() && 1 < get_query_var( 'page' ) ) ) {
global $wp;
$paged = [
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' ),
'link' => home_url( $wp->request )
];
}
return apply_filters( 'aioseo_breadcrumbs_trail', aioseo()->breadcrumbs->buildBreadcrumbs( $type, $reference, $paged ) );
}
/**
* Helper function to display breadcrumbs for a specific page.
*
* @since 4.1.1
*
* @param bool $echo Print out the breadcrumb.
* @param string $type The type for the breadcrumb.
* @param string $reference A reference to be used for rendering the breadcrumb.
* @return string|void A html breadcrumb.
*/
public function sideDisplay( $echo = true, $type = '', $reference = '' ) {
// Save previously built breadcrumbs.
$previousCrumbs = $this->breadcrumbs;
// Build and run the sideDisplay.
$this->breadcrumbs = aioseo()->breadcrumbs->buildBreadcrumbs( $type, $reference );
$sideDisplay = $this->display( $echo );
// Restore previously built breadcrumbs.
$this->breadcrumbs = $previousCrumbs;
return $sideDisplay;
}
/**
* Display a generic breadcrumb preview.
*
* @since 4.1.5
*
* @param bool $echo Print out the breadcrumb.
* @param string $label The preview crumb label.
* @return string|void A html breadcrumb.
*/
public function preview( $echo = true, $label = '' ) {
// Translators: "Crumb" refers to a part of the breadcrumb trail.
$label = empty( $label ) ? __( 'Sample Crumb', 'all-in-one-seo-pack' ) : $label;
return $this->sideDisplay( $echo, 'preview', $label );
}
/**
* Display the breadcrumb in the frontend.
*
* @since 4.1.1
*
* @param bool $echo Print out the breadcrumb.
* @return string|void A html breadcrumb.
*/
public function display( $echo = true ) {
if ( ! aioseo()->options->breadcrumbs->enable || ! apply_filters( 'aioseo_breadcrumbs_output', true ) ) {
return;
}
// We can only run after this action because we need all post types loaded.
if ( ! did_action( 'init' ) ) {
return;
}
$breadcrumbs = $this->getBreadcrumbs();
if ( empty( $breadcrumbs ) ) {
return;
}
$breadcrumbsCount = count( $breadcrumbs );
$display = '<div class="aioseo-breadcrumbs">';
foreach ( $breadcrumbs as $breadcrumb ) {
--$breadcrumbsCount;
$breadcrumbDisplay = $this->breadcrumbToDisplay( $breadcrumb );
// Strip link from Last crumb.
if (
0 === $breadcrumbsCount &&
aioseo()->breadcrumbs->showCurrentItem() &&
! $this->linkCurrentItem() &&
'default' === $breadcrumbDisplay['templateType']
) {
$breadcrumbDisplay['template'] = $this->stripLink( $breadcrumbDisplay['template'] );
}
$display .= $breadcrumbDisplay['template'];
if ( 0 < $breadcrumbsCount ) {
$display .= $this->getSeparator();
}
}
$display .= '</div>';
$display = wp_kses_post( $display );
if ( $echo ) {
echo $display; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
return $display;
}
/**
* Turns a crumb array into a rendered html crumb.
*
* @since 4.1.1
*
* @param array $item The crumb array.
* @return string|void The crumb html.
*/
protected function breadcrumbToDisplay( $item ) {
$templateItem = $this->getCrumbTemplate( $item );
if ( empty( $templateItem['template'] ) ) {
return;
}
// Do tags.
$templateItem['template'] = aioseo()->breadcrumbs->tags->replaceTags( $templateItem['template'], $item );
// Restore html.
$templateItem['template'] = aioseo()->helpers->decodeHtmlEntities( $templateItem['template'] );
// Remove html link if it comes back from the template but we passed no links to it.
if ( empty( $item['link'] ) ) {
$templateItem['template'] = $this->stripLink( $templateItem['template'] );
}
// Allow shortcodes to run in the final html.
$templateItem['template'] = do_shortcode( $templateItem['template'] );
// Final security cleaning.
$templateItem['template'] = wp_kses_post( $templateItem['template'] );
return $templateItem;
}
/**
* Helper function to get a crumb's template.
*
* @since 4.1.1
*
* @param array $crumb The crumb array.
* @return string The html template.
*/
protected function getTemplate( $crumb ) {
return $this->getDefaultTemplate( $crumb );
}
/**
* Helper function to get a crumb's template.
*
* @since 4.1.1
*
* @param array $crumb The crumb array.
* @return array The template type and html.
*/
protected function getCrumbTemplate( $crumb ) {
return [
'templateType' => 'default',
'template' => $this->getTemplate( $crumb )
];
}
/**
* Default html template.
*
* @since 4.1.1
*
* @param string $type The crumb's type.
* @param mixed $reference The crumb's reference.
* @return string The default crumb template.
*/
public function getDefaultTemplate( $type = '', $reference = '' ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
return <<<TEMPLATE
<span class="aioseo-breadcrumb">
<a href="#breadcrumb_link" title="#breadcrumb_label">#breadcrumb_label</a>
</span>
TEMPLATE;
}
/**
* Helper function to strip a html link from the crumb.
*
* @since 4.1.1
*
* @param string $html The crumb's html.
* @return string A crumb html without links.
*/
public function stripLink( $html ) {
return preg_replace( '/<a\s.*?>|<\/a>/is', '', $html );
}
/**
* Get the breadcrumb configured separator.
*
* @since 4.1.1
*
* @return string The separator html.
*/
public function getSeparator() {
$separator = apply_filters( 'aioseo_breadcrumbs_separator_symbol', aioseo()->options->breadcrumbs->separator );
return apply_filters( 'aioseo_breadcrumbs_separator', '<span class="aioseo-breadcrumb-separator">' . esc_html( $separator ) . '</span>' );
}
/**
* Function to filter the linkCurrentItem option.
*
* @since 4.1.3
*
* @return bool Link current item.
*/
public function linkCurrentItem() {
return apply_filters( 'aioseo_breadcrumbs_link_current_item', aioseo()->options->breadcrumbs->linkCurrentItem );
}
}