resolver = $resolver; $this->features = $features; $this->archive_graph = $archive_graph; } public function register(): void { add_action( 'wp_head', array( $this, 'print_schema' ), 10 ); } public function print_schema(): void { if ( ! $this->should_handle() || ! $this->features->enabled( 'schema' ) || ! $this->resolver->applies_to_schema() ) { return; } $json = ArchiveGraph::encode( $this->archive_graph->build( $this->collection_page() ) ); if ( ! is_string( $json ) ) { return; } // Same printer and reasoning as SchemaOutput::print_schema(): // wp_print_inline_script_tag() refuses text that could close the // element early and does not html-escape the JSON body. wp_print_inline_script_tag( $json, array( 'type' => 'application/ld+json' ) ); } /** * @return array */ private function collection_page(): array { $url = $this->resolver->canonical(); $node = array( '@type' => 'CollectionPage', '@id' => $url . '#collectionpage', 'url' => $url, 'name' => $this->resolver->name(), 'description' => $this->resolver->description(), 'isPartOf' => array( '@id' => SchemaIds::website() ), ); /** * Filters the CollectionPage schema node of the blog index. * * @param array $node * @param \WP_Post|null $posts_page The "Posts page", or null on a "latest posts" front page. */ return (array) apply_filters( 'acez_seo_blog_index_collection_page', $node, $this->resolver->posts_page() ); } private function should_handle(): bool { return (bool) apply_filters( 'acez_seo_should_handle_schema', ! SeoPluginDetector::has_conflict() ); } }