: the document * title (via pre_get_document_title, not a raw echo, so it cooperates * with the theme and other title filters), plus meta description, * canonical, Open Graph, Twitter Card, and JSON-LD on wp_head. * * Compatibility mode is checked on EVERY request, never cached at load: * a competing SEO plugin can be activated or deactivated between our * plugin loading and a given front-end hit, and emitting a duplicate * /canonical is worse for SEO than emitting nothing. When any * competitor is active, this class outputs nothing at all. * * All value RESOLUTION (title/description fallbacks, tag maps, JSON-LD * nodes) lives in the pure ABDS_SEO_Meta_Builder; this class only gathers * WordPress context and prints escaped output. * * @package ABDS_SEO */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * WordPress glue that renders resolved SEO tags into the frontend head. */ class ABDS_SEO_Frontend_Output { /** * Competitor detector; consulted on every hook invocation. * * @var ABDS_SEO_Compat_Detector */ protected ABDS_SEO_Compat_Detector $compat; /** * Constructor. * * @param ABDS_SEO_Compat_Detector|null $compat Optional detector override. */ public function __construct( ?ABDS_SEO_Compat_Detector $compat = null ) { $this->compat = $compat ?? new ABDS_SEO_Compat_Detector(); } /** * Registers this class's hooks with the plugin's hook loader. * * @param ABDS_SEO_Loader $loader Hook loader. */ public function register_hooks( ABDS_SEO_Loader $loader ): void { $loader->add_filter( 'pre_get_document_title', $this, 'filter_document_title', 15 ); // Priority 1: emit canonical/description early, before most themes' // own head output, matching where SEO plugins conventionally sit. $loader->add_action( 'wp_head', $this, 'render_head', 1 ); $loader->add_action( 'template_redirect', $this, 'dequeue_core_head_tags' ); } /** * Removes WordPress core's own canonical output when we will emit ours, * so a page never carries two <link rel="canonical"> tags. Runs before * wp_head; leaves core's tag in place when output is suppressed (a * competitor is active) or off a singular view. */ public function dequeue_core_head_tags(): void { if ( $this->is_suppressed() || ! is_singular() ) { return; } remove_action( 'wp_head', 'rel_canonical' ); } /** * Whether output must be suppressed right now: a competitor is active, * or the abds_seo_disable_frontend_output filter forced it off. * * @return bool */ protected function is_suppressed(): bool { if ( $this->compat->has_active_competitor() ) { return true; } /** * Filters whether ABDS SEO suppresses all of its frontend tag * output. Compatibility mode already suppresses when a competitor * is active; this lets a site force it off for other reasons. * * @since 0.4.6 * * @param bool $suppress Whether to suppress output. Default false. */ return (bool) apply_filters( 'abds_seo_disable_frontend_output', false ); } /** * Returns the explicit SEO title on a singular view, or the untouched * WordPress-default title otherwise (which IS the fallback — post title * + separator + site name). Hooked to pre_get_document_title. * * @param string $title The title WordPress would otherwise use. * @return string */ public function filter_document_title( $title ) { if ( $this->is_suppressed() || ! is_singular() ) { return $title; } $seo_title = trim( (string) get_post_meta( get_queried_object_id(), '_abds_seo_title', true ) ); return '' !== $seo_title ? $seo_title : $title; } /** * Prints description, canonical, OG, Twitter Card, and JSON-LD for a * singular view; does nothing when suppressed or off a singular view. * Hooked to wp_head. */ public function render_head(): void { if ( $this->is_suppressed() || ! is_singular() ) { return; } $post = get_queried_object(); if ( ! $post instanceof WP_Post ) { return; } $context = $this->build_context( $post ); echo "\n<!-- ABDS SEO -->\n"; $this->print_description( $context['description'] ); $this->print_canonical( $context['url'] ); $this->print_open_graph( $context ); $this->print_twitter_card( $context ); $this->print_json_ld( $context ); echo "<!-- /ABDS SEO -->\n"; } /** * Gathers the resolved page context from WordPress + saved meta, * delegating every fallback decision to the pure meta builder. * * @param WP_Post $post Queried post. * @return array<string,string> Resolved context for the printers. */ protected function build_context( WP_Post $post ): array { $post_id = (int) $post->ID; $seo_title = (string) get_post_meta( $post_id, '_abds_seo_title', true ); $seo_description = (string) get_post_meta( $post_id, '_abds_seo_description', true ); $seo_canonical = (string) get_post_meta( $post_id, '_abds_seo_canonical', true ); $site_name = (string) get_bloginfo( 'name' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- reading WordPress core's own document_title_separator value so our social/OG title matches the theme's separator; not defining a plugin hook. $separator = (string) apply_filters( 'document_title_separator', '-' ); $title = ABDS_SEO_Meta_Builder::resolve_title( $seo_title, get_the_title( $post ), $site_name, $separator ); $description = ABDS_SEO_Meta_Builder::resolve_description( $seo_description, (string) $post->post_excerpt, wp_strip_all_tags( (string) $post->post_content ) ); $canonical = '' !== trim( $seo_canonical ) ? $seo_canonical : (string) wp_get_canonical_url( $post ); $image = (string) get_the_post_thumbnail_url( $post, 'full' ); return array( 'title' => $title, 'description' => $description, 'url' => $canonical, 'site_name' => $site_name, 'image' => $image, 'type' => 'page' === $post->post_type ? 'website' : 'article', 'author' => (string) get_the_author_meta( 'display_name', (int) $post->post_author ), 'published' => (string) get_post_time( 'c', true, $post ), 'modified' => (string) get_post_modified_time( 'c', true, $post ), ); } /** * Prints the meta description tag when non-empty. * * @param string $description Resolved description. */ protected function print_description( string $description ): void { if ( '' === $description ) { return; } printf( '<meta name="description" content="%s" />' . "\n", esc_attr( $description ) ); } /** * Prints the canonical link when non-empty. * * @param string $url Canonical URL. */ protected function print_canonical( string $url ): void { if ( '' === $url ) { return; } printf( '<link rel="canonical" href="%s" />' . "\n", esc_url( $url ) ); } /** * Prints the Open Graph tags. * * @param array $context Resolved context. */ protected function print_open_graph( array $context ): void { foreach ( ABDS_SEO_Meta_Builder::build_open_graph( $context ) as $property => $content ) { if ( 'og:image' === $property || 'og:url' === $property ) { printf( '<meta property="%s" content="%s" />' . "\n", esc_attr( $property ), esc_url( $content ) ); } else { printf( '<meta property="%s" content="%s" />' . "\n", esc_attr( $property ), esc_attr( $content ) ); } } } /** * Prints the Twitter Card tags. * * @param array $context Resolved context. */ protected function print_twitter_card( array $context ): void { foreach ( ABDS_SEO_Meta_Builder::build_twitter_card( $context ) as $name => $content ) { if ( 'twitter:image' === $name ) { printf( '<meta name="%s" content="%s" />' . "\n", esc_attr( $name ), esc_url( $content ) ); } else { printf( '<meta name="%s" content="%s" />' . "\n", esc_attr( $name ), esc_attr( $content ) ); } } } /** * Prints the JSON-LD graph: an Article node for the post, plus a * LocalBusiness node on the front page when a reference NAP is set * (reusing the data ABDS_SEO_Local_SEO_Scanner already stores). * * @param array $context Resolved context. */ protected function print_json_ld( array $context ): void { $nodes = array( ABDS_SEO_Meta_Builder::build_article_schema( $context ) ); // LocalBusiness JSON-LD draws its NAP from the Local SEO scanner, which // ships in the Pro add-on; without it there is simply no LocalBusiness // node to emit (the Article schema above is unaffected). if ( is_front_page() && class_exists( 'ABDS_SEO_Local_SEO_Scanner' ) ) { $nap = ( new ABDS_SEO_Local_SEO_Scanner() )->get_reference_nap(); $local_node = ABDS_SEO_Meta_Builder::build_local_business_schema( $nap, home_url( '/' ) ); if ( null !== $local_node ) { $nodes[] = $local_node; } } foreach ( $nodes as $node ) { printf( '<script type="application/ld+json">%s</script>' . "\n", // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_json_encode is the correct escaper for a JSON-LD script body; it escapes forward slashes by default, so a value containing "</script>" cannot break out of the tag. wp_json_encode( $node ) ); } } }