-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWordPress-simple-URL-based-breadcrumb.php
More file actions
412 lines (289 loc) · 11.9 KB
/
WordPress-simple-URL-based-breadcrumb.php
File metadata and controls
412 lines (289 loc) · 11.9 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
};
/**
* Plugin Name: WordPress simple URL based breadcrumb
* Text Domain: wordpress-simple-url-based-breadcrumb
* Plugin URI: https://github.com/amarinediary/WordPress-simple-URL-based-breadcrumb
* Description: 🍞 A non-invasive WordPress unofficial plugin, minimalist and SEO friendly. both lightweight and lightning fast, adding URL based breadcrumb support. Plug-and-play, with no required configuration.
* Version: 1.2.4
* Requires at least: 5.0.0
* Requires PHP: 7.0.0
* Tested up to: 6.0.2
* Author: amarinediary
* Author URI: https://github.com/amarinediary
* License: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
* License URI: https://github.com/amarinediary/WordPress-simple-URL-based-breadcrumb/blob/main/LICENSE
* GitHub Plugin URI: https://github.com/amarinediary/WordPress-simple-URL-based-breadcrumb
* GitHub Branch: main
*/
/**
* Fallback implementation for str_ends_with function for PHP < 8.0.0 versions.
*
* @param string $haystack The string to search within.
* @param string $needle The string to search for.
*
* @return Bool True if $haystack ends with $needle, false otherwise.
*
* @since 1.0.0
*/
function backward_compatibility_str_ends_with( $haystack, $needle ) {
return substr( $haystack, -strlen( $needle ) ) === $needle;
}
/**
* Fallback implementation for str_contains function for PHP < 8.0.0 versions.
*
* @param string $haystack The string to search within.
* @param string $needle The string to search for.
*
* @return Bool True if $haystack contains $needle, false otherwise.
*
* @since 1.0.0
*/
function backward_compatibility_str_contains( $haystack, $needle ) {
return strpos( $haystack, $needle ) !== false;
}
/**
* Wrapper function that safely uses the str_ends_with function
* It uses the native PHP > 8.0.0 function when available, or falls back to the custom implementation.
*
* @param string $haystack The string to search within.
* @param string $needle The string to search for.
*
* @return Bool True if $haystack ends with $needle, false otherwise.
*
* @since 1.2.4
*/
function safe_str_ends_with( $haystack, $needle ) {
if ( function_exists( 'str_ends_with' ) ) {
return str_ends_with( $haystack, $needle );
}
return backward_compatibility_str_ends_with( $haystack, $needle );
}
/**
* Wrapper function that safely uses the str_contains function
* It uses the native PHP > 8.0.0 function when available, or falls back to the custom implementation.
*
* @param string $haystack The string to search within.
* @param string $needle The string to search for.
*
* @return Bool True if $haystack contains $needle, false otherwise.
*
* @since 1.2.4
*/
function safe_str_contains( $haystack, $needle ) {
if ( function_exists( 'str_contains' ) ) {
return str_contains( $haystack, $needle );
}
return backward_compatibility_str_contains( $haystack, $needle );
}
/**
* Attempts to determine the server scheme (http or https) of the current request based on various server variables.
*
* This function checks multiple server variables like $_SERVER['HTTPS'], $_SERVER['SERVER_PORT'],
* $_SERVER['HTTP_X_FORWARDED_PROTO'], and $_SERVER['HTTP_X_FORWARDED_SSL'] to accurately
* determine whether the current request is made over https or http.
*
* $_SERVER["REQUEST_SCHEME"] seems to be UNRELIABLE.
*
* Article "Is $_SERVER['REQUEST_SCHEME'] reliable?".
* @see https://stackoverflow.com/a/18008178/3645650
*
* $_SERVER['REQUEST_SCHEME'] is a native variable of Apache web server since its version 2.4.
* Naturally, if a variable is not set by the server, PHP will not include it in its global array $_SERVER.
*
* An alternative to $_SERVER['REQUEST_SCHEME'] is $_SERVER['HTTPS'] which set to a non-empty value if the script was queried through the HTTPS protocol.
*
* Article "How to find out if you're using HTTPS without $_SERVER['HTTPS']".
* @see https://stackoverflow.com/a/16076965/3645650
*
* @return String $server_scheme The server scheme, either 'http' or 'https'.
*
* @since 1.0.0
*/
if ( ! function_exists( 'attempt_to_retrieve_server_scheme' ) ) {
function attempt_to_retrieve_server_scheme() {
if ( isset( $_SERVER['HTTPS'] ) && safe_str_contains( $_SERVER['HTTPS'], 'on' ) ) {
$server_scheme = 'https';
} elseif ( isset($_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] == '443' ) {
$server_scheme = 'https';
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && safe_str_contains( $_SERVER['HTTP_X_FORWARDED_PROTO'], 'https' ) ) {
$server_scheme = 'https';
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_SSL'] ) && safe_str_contains( $_SERVER['HTTP_X_FORWARDED_SSL'], 'on' ) ) {
$server_scheme = 'https';
} else {
$server_scheme = 'http';
}
}
}
/**
* Retrieve the crumbs.
*
* @return Array Crumbs array.
*
* @since 1.0.0
*/
if ( ! function_exists( 'get_the_crumbs' ) ) {
function get_the_crumbs() {
// Retrieve the server scheme ('http' or 'https')
$server_scheme = attempt_to_retrieve_server_scheme();
/**
* $_SERVER["REQUEST_URI"] seems to be RELIABLE.
* $_SERVER['REQUEST_URI'] will not be empty in WordPress, because it is filled in wp_fix_server_vars() (file wp-includes/load.php).
*
* Article "Is it safe to use $_SERVER['REQUEST_URI']?".
* @see https://wordpress.stackexchange.com/a/110541/190376
*/
$server_uri = $_SERVER['REQUEST_URI'];
/**
* $_SERVER["HTTP_HOST"] seems to be RELIABLE.
*
* Article "How reliable is HTTP_HOST?".
* @see https://stackoverflow.com/a/4096246/3645650
*/
$server_host = $_SERVER["HTTP_HOST"];
// Remove query string if present
if ( safe_str_contains( $server_uri, '?' ) ) {
$server_uri = substr( $server_uri, 0, strpos( $server_uri, '?' ) );
}
// Remove trailing slash if present
if ( safe_str_ends_with( $server_uri, '/' ) ) {
$server_uri = explode( '/', substr( $server_uri, 1, -1 ) );
} else {
$server_uri = explode( '/', substr( $server_uri, 1 ) );
}
// Initialize crumbs array
$crumbs = array();
// Populate crumbs array
foreach ( $server_uri as $crumb ) {
$slug = esc_html( urldecode( $crumb ) );
$taxonomies = get_taxonomies(
array(
'public' => true,
),
'objects'
);
// Iterate through all the taxonomies.
foreach ( $taxonomies as $taxonomy ) {
// Check if there's a term with the given slug in the current taxonomy.
if ( $term = get_term_by( 'slug', $crumb, $taxonomy->name ) ) {
// If a matching term is found, update the slug with the actual term name.
$slug = $term->name;
// Break the loop since a match has been found.
break;
}
}
$url = esc_url( $server_scheme . '://' . $server_host . '/' . substr( implode( '/', $server_uri ), 0, strpos( implode( '/', $server_uri ), $crumb ) ) . $crumb. '/' );
array_push( $crumbs,
array(
'slug' => $slug,
'url' => $url,
)
);
};
/**
* WordPress, by default, doesn't generate a taxonomy index, meaning https://.../taxonomy will redirect to a 404.
* Any request needs to be made against a term. eg: https://.../taxonomy/term will redirect to taxonomy.php.
* Therefore we need to remove the taxonomy slug from the crumbs array to avoid displaying a link to a 404.
*
* We round up all taxonomies through get_taxonomies().
* @see https://developer.wordpress.org/reference/functions/get_taxonomies/
*
* Through array_filter we filter-out any matching crumbs.
* @see https://www.php.net/manual/en/function.array-filter.php
*/
$banned_slugs = array();
$taxonomies = get_taxonomies(
array(
'public' => true,
),
'objects'
);
foreach ( $taxonomies as $taxonomy ) {
array_push( $banned_slugs, $taxonomy->name );
if ( isset( $taxonomy->rewrite['slug'] ) ) {
array_push( $banned_slugs, $taxonomy->rewrite['slug'] );
};
};
$banned_crumbs = array();
foreach ( $banned_slugs as $banned_slug ) {
$slug = esc_html( $banned_slug );
$url = esc_url( $server_scheme . '://' . $server_host . '/' . substr( implode( '/', $server_uri ), 0, strpos( implode( '/', $server_uri ), $banned_slug ) ) . $banned_slug. '/' );
array_push( $banned_crumbs,
array(
'slug' => $slug,
'url' => $url,
)
);
};
$crumbs = array_filter( $crumbs, function( $crumb ) use ( $banned_slugs ) {
if ( ! in_array( $crumb['slug'], $banned_slugs ) && ! in_array( $crumb['url'], $banned_slugs ) ) {
return ! in_array( $crumb['slug'], $banned_slugs );
};
} );
return $crumbs;
};
};
/**
* Display the breadcrumb, a formatted crumbs list.
*
* @param Array $ingredients The bread arguments.
*
* @return Void
*
* @since 1.0.0
*/
if ( ! function_exists( 'the_bread' ) ) {
function the_bread( $ingredients = array() ) {
// Default values using array destructuring
$defaults = [
'crumbs' => get_the_crumbs(),
'root' => null,
'separator' => '',
'offset' => 0,
'length' => null
];
// Merge provided ingredients with defaults
$ingredients = array_merge( $defaults, $ingredients );
// Extract variables from the ingredients array
extract( $ingredients );
// Handle the root crumb case
if ( $root ) {
array_unshift( $crumbs, $root );
}
// Handle the length case
$crumbs = array_slice( $crumbs, $offset, $length );
if ( $crumbs ) {
echo '<nav aria-label="breadcrumb">';
echo '<ol class="🍞 bread" itemscope itemtype="https://schema.org/BreadcrumbList">';
$i = 0;
foreach ( $crumbs as $crumb ) {
$i++;
// Unparsing the slug
if ( url_to_postid( $crumb['url'] ) ) {
$title = get_the_title( url_to_postid( $crumb['url'] ) );
} elseif ( get_page_by_path( $crumb['slug'] ) ) {
$title = get_the_title( get_page_by_path( $crumb['slug'] ) );
} else {
$title = $crumb['slug'];
};
echo '<li class="crumb" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
<a itemprop="item" href="' . $crumb['url'] . '"';
if ( $i === sizeof( $crumbs ) ) {
echo ' aria-current="page"';
}
echo '>
<span itemprop="name">' . $title . '</span>
</a>
<meta itemprop="position" content="' . $i . '">';
if ( $i !== sizeof( $crumbs ) && ! empty( $ingredients['separator'] ) ) {
echo $ingredients['separator'];
};
echo '</li>';
};
echo '</ol>';
echo '</nav>';
}
}
}