|
| 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