: 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 ); // Also priority 1: unlike render_head() (singular-only), this must // fire on every request type — archives/search/404 included — since // F2.5's whole point is making the Policy Engine's resolved index/ // follow real everywhere it applies, not just on single posts. $loader->add_action( 'wp_head', $this, 'print_meta_robots', 1 ); $loader->add_action( 'template_redirect', $this, 'dequeue_core_head_tags' ); // Priority 5: WordPress core's own redirect_canonical() also runs on // template_redirect at the default priority (10) and, for an // attachment on a "plain" permalink structure, sends its own 301 to // the raw upload URL — same-priority hooks fire in registration // order, and core's is always registered first, so without an // earlier priority this redirect would never get a chance to run. $loader->add_action( 'template_redirect', $this, 'maybe_redirect_attachment', 5 ); } /** * 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' ); } /** * Redirects an attachment's own page to its parent post (R2, F2.4) when * the site owner has opted in via the Archives/System tab's toggle. * Deliberately NOT a Policy Engine concern — a 301 redirect isn't an * index/follow/canonical/sitemap/schema question, it's "does this URL * even serve its own page", so it stays outside resolve_current_request(). * Hooked to template_redirect (before any output starts). */ public function maybe_redirect_attachment(): void { if ( $this->is_suppressed() || ! is_attachment() ) { return; } $settings = (array) ( get_option( 'abds_seo_settings', array() )['archive_settings'] ?? array() ); if ( true !== ( $settings['attachment_redirect'] ?? false ) ) { return; } $parent_id = wp_get_post_parent_id( get_queried_object_id() ); if ( ! $parent_id ) { return; } $parent_url = get_permalink( $parent_id ); if ( false === $parent_url ) { return; } wp_safe_redirect( $parent_url, 301 ); exit; } /** * Prints the meta robots tag for the current request (R2, F2.5) — the * first real consumer of ABDS_SEO_Policy_Engine::resolve_current_request(). * This is where the R1-migrated-but-previously-inert _abds_seo_noindex/ * _abds_seo_nofollow post meta starts actually affecting the live site * for the first time (see that class's own docblock, and the * conscious-activation mechanism in ABDS_SEO_Noindex_Activation that * ships in the same release). Unlike render_head(), NOT gated on * is_singular() — archive/search/404 pages get a real answer too, now * that the Policy Engine models them via its pseudo-keys. Always prints * both directives explicitly (never omits one), since the underlying * values can come from long-unaudited migrated data. Hooked to wp_head. */ public function print_meta_robots(): void { if ( $this->is_suppressed() ) { return; } $policy = ABDS_SEO_Policy_Engine::resolve_current_request(); printf( '<meta name="robots" content="%s,%s" />' . "\n", esc_attr( $policy['index'] ? 'index' : 'noindex' ), esc_attr( $policy['follow'] ? 'follow' : 'nofollow' ) ); } /** * 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; otherwise, when a * Global title template is configured (R2, F2.2), resolves through it; * otherwise the untouched WordPress-default title (post title + * separator + site name — the same shape ABDS_SEO_Meta_Builder's own * hardcoded fallback produces, so leaving WP's own pipeline in charge * when there's nothing to override is deliberate, not a gap). 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; } $post_id = get_queried_object_id(); $seo_title = trim( (string) get_post_meta( $post_id, '_abds_seo_title', true ) ); if ( '' !== $seo_title ) { return $seo_title; } $global = (array) ( get_option( 'abds_seo_settings', array() )['global'] ?? array() ); $title_template = (string) ( $global['title_template'] ?? '' ); if ( '' === trim( $title_template ) ) { return $title; } $post = get_queried_object(); if ( ! $post instanceof WP_Post ) { return $title; } return ABDS_SEO_Meta_Builder::resolve_title( '', get_the_title( $post ), (string) get_bloginfo( 'name' ), (string) ( $global['title_separator'] ?? '-' ), $title_template ); } /** * Prints description, canonical, OG and Twitter Card tags on a singular * view, and the page's ONE JSON-LD graph on every front-end view (what * the graph contains for a given view is decided in * ABDS_SEO_Schema_Graph). Does nothing when suppressed. Hooked to wp_head. */ public function render_head(): void { if ( $this->is_suppressed() || is_embed() ) { return; } $context = null; if ( is_singular() ) { $post = get_queried_object(); if ( ! $post instanceof WP_Post ) { return; } $context = $this->build_context( $post ); } echo "\n<!-- ABDS SEO -->\n"; if ( null !== $context ) { $this->print_description( $context['description'] ); $this->print_canonical( $context['url'] ); $this->print_open_graph( $context ); $this->print_twitter_card( $context ); } $this->print_graph( ABDS_SEO_Schema_Context::build_for_current_request( $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 ); $site_name = (string) get_bloginfo( 'name' ); // R2 (F2.2): the Global Search Appearance tab's separator/templates, // seeded with defaults by ABDS_SEO_Settings_Installer::backfill_v2() // on every install (new or upgraded) — this array key is always // present in practice, but array-access defaults stay defensive in // case this ever runs before that backfill has. This plugin's own // stored separator now supersedes WordPress core's own // document_title_separator filter for this plugin's own output // (deliberate, R2: the whole point of the new Global tab is that the // separator becomes something the site owner configures here, not // something a theme filter passively supplies). $global = (array) ( get_option( 'abds_seo_settings', array() )['global'] ?? array() ); $separator = (string) ( $global['title_separator'] ?? '-' ); $title_template = (string) ( $global['title_template'] ?? '' ); $description_template = (string) ( $global['description_template'] ?? '' ); $default_og_image_id = (int) ( $global['default_og_image'] ?? 0 ); $title = ABDS_SEO_Meta_Builder::resolve_title( $seo_title, get_the_title( $post ), $site_name, $separator, $title_template ); $description = ABDS_SEO_Meta_Builder::resolve_description( $seo_description, (string) $post->post_excerpt, wp_strip_all_tags( (string) $post->post_content ), $description_template, get_the_title( $post ), $site_name, $separator ); // R2 (F2.5): canonical is now resolved through the Policy Engine // directly (§95) instead of this class keeping its own second, // drifting copy of the same seo_canonical-over-wp_canonical logic — // resolve() already does exactly this cascade. $canonical = (string) ABDS_SEO_Policy_Engine::resolve( $post )['canonical']; $image = (string) get_the_post_thumbnail_url( $post, 'full' ); if ( '' === $image && $default_og_image_id > 0 ) { $image = (string) wp_get_attachment_image_url( $default_og_image_id, '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 page's single JSON-LD script. The encoding escapes < > and & * (JSON_HEX_*), so a post title containing "</script>" cannot close the * script element, while slashes and non-ASCII text stay readable. * * @param array $graph Array with '@context' and '@graph'. */ protected function print_graph( array $graph ): void { $json = ABDS_SEO_Schema_Graph::to_json( $graph ); if ( '' === $json ) { return; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- ABDS_SEO_Schema_Graph::to_json() escapes < > & (JSON_HEX_*), the correct escaping for a JSON-LD script body; the content is the plugin's own structured data. echo '<script type="application/ld+json">' . $json . "</script>\n"; } }