Skip to content

Commit d78a84e

Browse files
authored
Merge pull request #465 from BeAPI/fix-breadcrumb-a11y
fix a11y breadcrumb
2 parents 0711b14 + 2c7c82c commit d78a84e

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

inc/Framework.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BEA\Theme\Framework\Services\Acf;
66
use BEA\Theme\Framework\Services\Assets;
7+
use BEA\Theme\Framework\Services\Breadcrumb;
78
use BEA\Theme\Framework\Services\Performance;
89
use BEA\Theme\Framework\Services\Editor;
910
use BEA\Theme\Framework\Services\Editor_Patterns;
@@ -39,6 +40,7 @@ class Framework {
3940
Svg::class,
4041
Acf::class,
4142
Menu::class,
43+
Breadcrumb::class,
4244

4345
// Services as Tools
4446
Body_Class::class,

inc/Services/Breadcrumb.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* Breadcrumb a11y service
5+
* https://www.wpexplorer.com/accessible-yoast-seo-breadcrumbs/
6+
* https://developer.yoast.com/features/blocks/breadcrumbs/
7+
* https://gist.github.com/doubleedesign/7224a5e990b8506ddb8ec66de8348b2b
8+
*/
9+
10+
namespace BEA\Theme\Framework\Services;
11+
12+
use BEA\Theme\Framework\Service;
13+
use BEA\Theme\Framework\Service_Container;
14+
15+
class Breadcrumb implements Service {
16+
17+
public function register( Service_Container $container ): void {
18+
}
19+
20+
public function boot( Service_Container $container ): void {
21+
add_filter( 'wpseo_breadcrumb_output_wrapper', [ $this, 'replace_breadcrumb_wrapper' ]);
22+
add_filter( 'wpseo_breadcrumb_single_link_wrapper', [ $this, 'replace_breadcrumb_link_wrapper' ]);
23+
add_filter( 'wpseo_breadcrumb_single_link', [ $this, 'replace_breadcrumb_link' ] );
24+
add_filter( 'wpseo_breadcrumb_separator', [ $this, 'remove_breadcrumb_separator' ] );
25+
}
26+
27+
/**
28+
* Get service name
29+
*
30+
* @return string
31+
*/
32+
public function get_service_name(): string {
33+
return 'breadcrumb';
34+
}
35+
36+
/**
37+
* Replace the wrapper around the breadcrumbs
38+
*
39+
* @return string
40+
*/
41+
public function replace_breadcrumb_wrapper(): string {
42+
return 'ol';
43+
}
44+
45+
/**
46+
* Replace the wrappers around the links.
47+
*
48+
* @return string
49+
*/
50+
public function replace_breadcrumb_link_wrapper(): string {
51+
return 'li';
52+
}
53+
54+
/**
55+
* Replace the links and add the separator inside with aria-hidden="true".
56+
*
57+
* @param string $link_output The link output.
58+
* @return string
59+
*/
60+
public function replace_breadcrumb_link( $link_output ): string {
61+
if ( ! str_contains( $link_output, 'breadcrumb_last' ) ) {
62+
return str_replace( '</li>', '<span class="breadcrumb__separator" aria-hidden="true">></span></li>', $link_output );
63+
}
64+
65+
return $link_output;
66+
}
67+
68+
/**
69+
* Remove the breadcrumbs separator outside the links.
70+
*
71+
* @return string
72+
*/
73+
public function remove_breadcrumb_separator(): string {
74+
return '';
75+
}
76+
}

0 commit comments

Comments
 (0)