PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4.1
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / front-end / schema-accessibility-feature.php

schema-accessibility-feature.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.4.1, at src/integrations/front-end/schema-accessibility-feature.php

81 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Integrations\Front_End;
4
5 use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
6 use Yoast\WP\SEO\Context\Meta_Tags_Context;
7 use Yoast\WP\SEO\Generators\Schema\Abstract_Schema_Piece;
8 use Yoast\WP\SEO\Generators\Schema\Article;
9 use Yoast\WP\SEO\Integrations\Integration_Interface;
10
11 /**
12 * Adds the table of contents accessibility feature to the article piece with a fallback to the webpage piece.
13 */
14 class Schema_Accessibility_Feature implements Integration_Interface {
15
16 /**
17 * Returns the conditionals based in which this loadable should be active.
18 *
19 * @return array
20 */
21 public static function get_conditionals() {
22 return [ Front_End_Conditional::class ];
23 }
24
25 /**
26 * Initializes the integration.
27 *
28 * This is the place to register hooks and filters.
29 *
30 * @return void
31 */
32 public function register_hooks() {
33 \add_filter( 'wpseo_schema_webpage', [ $this, 'maybe_add_accessibility_feature' ], 10, 4 );
34 \add_filter( 'wpseo_schema_article', [ $this, 'add_accessibility_feature' ], 10, 2 );
35 }
36
37 /**
38 * Adds the accessibility feature to the webpage if there is no article.
39 *
40 * @param array $piece The graph piece.
41 * @param Meta_Tags_Context $context The context.
42 * @param Abstract_Schema_Piece $the_generator The current schema generator.
43 * @param Abstract_Schema_Piece[] $generators The schema generators.
44 *
45 * @return array The graph piece.
46 */
47 public function maybe_add_accessibility_feature( $piece, $context, $the_generator, $generators ) {
48 foreach ( $generators as $generator ) {
49 if ( \is_a( $generator, Article::class ) && $generator->is_needed() ) {
50 return $piece;
51 }
52 }
53
54 return $this->add_accessibility_feature( $piece, $context );
55 }
56
57 /**
58 * Adds the accessibility feature to a schema graph piece.
59 *
60 * @param array $piece The schema piece.
61 * @param Meta_Tags_Context $context The context.
62 *
63 * @return array The graph piece.
64 */
65 public function add_accessibility_feature( $piece, $context ) {
66 if ( empty( $context->blocks['yoast-seo/table-of-contents'] ) ) {
67 return $piece;
68 }
69
70 if ( isset( $piece['accessibilityFeature'] ) ) {
71 $piece['accessibilityFeature'][] = 'tableOfContents';
72 }
73 else {
74 $piece['accessibilityFeature'] = [
75 'tableOfContents',
76 ];
77 }
78 return $piece;
79 }
80 }
81