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