| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Third_Party; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\Web_Stories_Conditional; |
| 6 |
use Yoast\WP\SEO\Context\Meta_Tags_Context; |
| 7 |
use Yoast\WP\SEO\Integrations\Front_End_Integration; |
| 8 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 9 |
use Yoast\WP\SEO\Models\Indexable; |
| 10 |
use Yoast\WP\SEO\Presentations\Indexable_Presentation; |
| 11 |
use Yoast\WP\SEO\Presenters\Title_Presenter; |
| 12 |
|
| 13 |
/** |
| 14 |
* Web Stories integration. |
| 15 |
*/ |
| 16 |
class Web_Stories implements Integration_Interface { |
| 17 |
|
| 18 |
/** |
| 19 |
* The front end integration. |
| 20 |
* |
| 21 |
* @var Front_End_Integration |
| 22 |
*/ |
| 23 |
protected $front_end; |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns the conditionals based in which this loadable should be active. |
| 27 |
* |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public static function get_conditionals() { |
| 31 |
return [ Web_Stories_Conditional::class ]; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructs the Web Stories integration |
| 36 |
* |
| 37 |
* @param Front_End_Integration $front_end The front end integration. |
| 38 |
*/ |
| 39 |
public function __construct( Front_End_Integration $front_end ) { |
| 40 |
$this->front_end = $front_end; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Initializes the integration. |
| 45 |
* |
| 46 |
* This is the place to register hooks and filters. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function register_hooks() { |
| 51 |
// Disable default title and meta description output in the Web Stories plugin, |
| 52 |
// and force-add title & meta description presenter, regardless of theme support. |
| 53 |
\add_filter( 'web_stories_enable_document_title', '__return_false' ); |
| 54 |
\add_filter( 'web_stories_enable_metadata', '__return_false' ); |
| 55 |
\add_filter( 'wpseo_frontend_presenters', [ $this, 'filter_frontend_presenters' ], 10, 2 ); |
| 56 |
|
| 57 |
\add_action( 'web_stories_enable_schemaorg_metadata', '__return_false' ); |
| 58 |
\add_action( 'web_stories_enable_open_graph_metadata', '__return_false' ); |
| 59 |
\add_action( 'web_stories_enable_twitter_metadata', '__return_false' ); |
| 60 |
|
| 61 |
\add_action( 'web_stories_story_head', [ $this, 'web_stories_story_head' ], 1 ); |
| 62 |
\add_filter( 'wpseo_schema_article_type', [ $this, 'filter_schema_article_type' ], 10, 2 ); |
| 63 |
\add_filter( 'wpseo_metadesc', [ $this, 'filter_meta_description' ], 10, 2 ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Filter 'wpseo_frontend_presenters' - Allow filtering the presenter instances in or out of the request. |
| 68 |
* |
| 69 |
* @param array $presenters The presenters. |
| 70 |
* @param Meta_Tags_Context $context The meta tags context for the current page. |
| 71 |
* @return array Filtered presenters. |
| 72 |
*/ |
| 73 |
public function filter_frontend_presenters( $presenters, $context ) { |
| 74 |
if ( $context->indexable->object_sub_type !== 'web-story' ) { |
| 75 |
return $presenters; |
| 76 |
} |
| 77 |
|
| 78 |
$has_title_presenter = false; |
| 79 |
|
| 80 |
foreach ( $presenters as $presenter ) { |
| 81 |
if ( $presenter instanceof Title_Presenter ) { |
| 82 |
$has_title_presenter = true; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! $has_title_presenter ) { |
| 87 |
$presenters[] = new Title_Presenter(); |
| 88 |
} |
| 89 |
|
| 90 |
return $presenters; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Hooks into web story <head> generation to modify output. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function web_stories_story_head() { |
| 99 |
\remove_action( 'web_stories_story_head', 'rel_canonical' ); |
| 100 |
\add_action( 'web_stories_story_head', [ $this->front_end, 'call_wpseo_head' ], 9 ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Filters the meta description for stories. |
| 105 |
* |
| 106 |
* @param string $description The description sentence. |
| 107 |
* @param Indexable_Presentation $presentation The presentation of an indexable. |
| 108 |
* @return string The description sentence. |
| 109 |
*/ |
| 110 |
public function filter_meta_description( $description, $presentation ) { |
| 111 |
if ( $description || $presentation->model->object_sub_type !== 'web-story' ) { |
| 112 |
return $description; |
| 113 |
} |
| 114 |
|
| 115 |
return \get_the_excerpt( $presentation->model->object_id ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Filters Article type for Web Stories. |
| 120 |
* |
| 121 |
* @param string|string[] $type The Article type. |
| 122 |
* @param Indexable $indexable The indexable. |
| 123 |
* @return string|string[] Article type. |
| 124 |
*/ |
| 125 |
public function filter_schema_article_type( $type, $indexable ) { |
| 126 |
if ( $indexable->object_sub_type !== 'web-story' ) { |
| 127 |
return $type; |
| 128 |
} |
| 129 |
|
| 130 |
if ( \is_string( $type ) && $type === 'None' ) { |
| 131 |
return 'Article'; |
| 132 |
} |
| 133 |
|
| 134 |
return $type; |
| 135 |
} |
| 136 |
} |
| 137 |
|