*/ public static function defaults(): array { return array( 'enabled' => true, 'title_template' => '%title% %sep% %sitename%', 'separator' => '–', 'social_tags' => true, 'force_output' => false, // Content scrapers steal via RSS — a source credit in every feed // item makes stolen copies link back to the original. 'rss_before' => '', 'rss_after' => 'This post first appeared on %sitename%: %post_link%', // Per-post-type overrides. Empty means "use the global template", // which keeps an upgrade from silently rewriting existing titles. 'templates' => array(), ) + Services\Archives::defaults(); } /** * Variables a template may use, with a human label. One list, used by the * resolver, the REST payload and the admin hint — so they cannot drift. * * @return array */ public static function variables(): array { return array( '%title%' => __( 'Page title', 'ai-rank-suite' ), '%sitename%' => __( 'Site name', 'ai-rank-suite' ), '%sep%' => __( 'Separator', 'ai-rank-suite' ), '%tagline%' => __( 'Site tagline', 'ai-rank-suite' ), '%excerpt%' => __( 'Excerpt', 'ai-rank-suite' ), '%category%' => __( 'Primary category', 'ai-rank-suite' ), '%author%' => __( 'Author name', 'ai-rank-suite' ), '%date%' => __( 'Publish date', 'ai-rank-suite' ), '%modified%' => __( 'Last modified date', 'ai-rank-suite' ), '%currentyear%' => __( 'Current year', 'ai-rank-suite' ), '%currentdate%' => __( 'Today’s date', 'ai-rank-suite' ), '%page%' => __( 'Page 2 of 7 (empty on page 1)', 'ai-rank-suite' ), '%tags%' => __( 'Tags, comma separated', 'ai-rank-suite' ), ); } /** * The template that applies to a post: its post type's override if one is * set, otherwise the site-wide template. * * @param array $settings Module settings. */ private static function template_for( array $settings, string $post_type, string $kind ): string { $templates = (array) ( $settings['templates'] ?? array() ); $specific = trim( (string) ( $templates[ $post_type ][ $kind ] ?? '' ) ); if ( '' !== $specific ) { return $specific; } return 'title' === $kind ? (string) $settings['title_template'] : ''; } /** * Replace template variables against a post. * * @param array $settings Module settings. */ private static function fill( string $template, \WP_Post $post, array $settings ): string { $out = strtr( $template, array( '%title%' => get_the_title( $post ), '%sitename%' => get_bloginfo( 'name' ), '%sep%' => (string) $settings['separator'], '%tagline%' => get_bloginfo( 'description' ), '%excerpt%' => wp_strip_all_tags( (string) get_the_excerpt( $post ) ), '%category%' => self::primary_category( $post ), '%author%' => (string) get_the_author_meta( 'display_name', (int) $post->post_author ), '%date%' => (string) get_the_date( '', $post ), '%modified%' => (string) get_the_modified_date( '', $post ), '%currentyear%' => (string) wp_date( 'Y' ), '%currentdate%' => (string) wp_date( (string) get_option( 'date_format', 'F j, Y' ) ), '%page%' => self::page_label(), '%tags%' => implode( ', ', array_map( static fn( $t ) => (string) $t->name, (array) ( get_the_terms( $post, 'post_tag' ) ?: array() ) ) ), ) ); // Drop leftovers from other plugins' syntax rather than shipping them live. $out = (string) preg_replace( '/%%?[a-z_0-9\-]+%%?/i', '', $out ); return wp_strip_all_tags( trim( (string) preg_replace( '/\s{2,}/', ' ', $out ) ) ); } /** * "Page 2 of 7" on a paginated post or archive, empty on page one — the * shape Rank Math and Yoast both use, so an imported "%title% %page%" reads * the same here as it did there. */ private static function page_label(): string { $page = max( (int) get_query_var( 'paged' ), (int) get_query_var( 'page' ) ); if ( $page < 2 ) { return ''; } global $wp_query; $max = $wp_query instanceof \WP_Query ? (int) $wp_query->max_num_pages : 0; /* translators: 1: current page number, 2: total pages. */ return $max > 1 ? sprintf( __( 'Page %1$d of %2$d', 'ai-rank-suite' ), $page, $max ) : sprintf( __( 'Page %d', 'ai-rank-suite' ), $page ); } /** * Another SEO plugin already emitting meta? */ public static function conflict(): ?string { if ( defined( 'WPSEO_VERSION' ) ) { return 'Yoast SEO'; } if ( class_exists( 'RankMath' ) ) { return 'Rank Math'; } if ( defined( 'AIOSEO_VERSION' ) ) { return 'All in One SEO'; } return null; } public function register( Container $container ): void {} public function boot( Container $container ): void { add_action( 'rest_api_init', array( $this, 'routes' ) ); $options = $container->get( Options::class ); $settings = $options->get( 'seo', self::defaults() ); if ( empty( $settings['enabled'] ) ) { return; } // With another SEO plugin installed, Coexistence decides surface by // surface who writes what. Legacy force_output still wins outright. $coexist = null !== self::conflict() && empty( $settings['force_output'] ); $owns = static function ( string $surface ) use ( $coexist ): bool { if ( ! $coexist ) { return true; } return \AiRankSuite\Modules\Coexistence\CoexistenceModule::owns( $surface ); }; if ( $owns( 'title' ) ) { Guard::filter( 'pre_get_document_title', array( $this, 'title' ), 20 ); } Guard::action( 'wp_head', array( $this, 'head_tags' ), 1 ); Guard::action( 'wp_head', array( $this, 'archive_head' ), 1 ); Guard::action( 'template_redirect', array( $this, 'redirect_dead_archives' ), 5 ); add_filter( 'the_content_feed', array( $this, 'rss_content' ) ); add_filter( 'the_excerpt_rss', array( $this, 'rss_content' ) ); // We emit our own canonical (with per-post overrides), so core's must go — // two canonical tags on one page make Google discard both. When the other // plugin owns the canonical, core's removal is its job, not ours. if ( $owns( 'canonical' ) ) { remove_action( 'wp_head', 'rel_canonical' ); } } /** * Resolve the effective SEO title for a post. * * A per-post override may itself contain template variables (imports from * Yoast and Rank Math routinely do), so both paths go through the same * variable resolution — otherwise a templated title would be published, * and analyzed, literally. */ public static function resolve_title( \WP_Post $post ): string { $settings = \AiRankSuite\Kernel::service( Options::class )->get( 'seo', self::defaults() ); $custom = (string) get_post_meta( $post->ID, '_airanksuite_seo_title', true ); $template = '' !== trim( $custom ) ? $custom : self::template_for( $settings, $post->post_type, 'title' ); return self::fill( $template, $post, $settings ); } /** * Resolve the meta description for a post. * * Precedence: the page's own description, then an AI summary, then a * post-type template, then the excerpt. A template must never outrank * something a human wrote for this specific page. */ public static function resolve_description( \WP_Post $post ): string { $settings = \AiRankSuite\Kernel::service( Options::class )->get( 'seo', self::defaults() ); // Whatever wins, it goes through fill(). The per-post field is // sanitised with the token-preserving sanitiser precisely so a variable // survives being saved — leaving it unresolved here meant %sitename% // worked in the title and shipped verbatim in the description, which // is the sort of inconsistency people find on their live site. $desc = (string) get_post_meta( $post->ID, '_airanksuite_seo_desc', true ); if ( '' === trim( $desc ) ) { $desc = (string) get_post_meta( $post->ID, '_airanksuite_ai_summary', true ); } if ( '' === trim( $desc ) ) { $desc = self::template_for( $settings, $post->post_type, 'desc' ); } if ( '' !== trim( $desc ) ) { $desc = self::fill( $desc, $post, $settings ); } if ( '' === trim( $desc ) ) { $desc = (string) get_the_excerpt( $post ); } $desc = wp_strip_all_tags( $desc ); if ( function_exists( 'mb_strlen' ) && mb_strlen( $desc ) > 160 ) { $desc = mb_substr( $desc, 0, 157 ) . '…'; } return $desc; } private static function primary_category( \WP_Post $post ): string { $terms = get_the_terms( $post, 'category' ); return is_array( $terms ) && ! empty( $terms ) ? (string) $terms[0]->name : ''; } /** * Custom document title from template / per-post override. */ public function title( string $title ): string { if ( is_singular() ) { $post = get_queried_object(); if ( ! $post instanceof \WP_Post ) { return $title; } $resolved = self::resolve_title( $post ); return '' !== $resolved ? $resolved : $title; } // Archives used to fall straight through to the theme, which is how a // site ends up publishing "Archiv für Kategorie 'Behandlungen' - Seite // 3" as a search result. On a large site these pages are the majority // of the URLs. $settings = $this->options()->get( 'seo', self::defaults() ); $resolved = Services\Archives::title( $settings ); return '' !== $resolved ? $resolved : $title; } /** * Description, robots and canonical for archive pages. * * Kept separate from head_tags() rather than folded into it: that method is * built entirely around a WP_Post and reads it a dozen times. */ public function archive_head(): void { if ( is_singular() ) { return; } $settings = $this->options()->get( 'seo', self::defaults() ); $coexist = null !== self::conflict() && empty( $settings['force_output'] ); $owns = static function ( string $surface ) use ( $coexist ): bool { return ! $coexist || \AiRankSuite\Modules\Coexistence\CoexistenceModule::owns( $surface ); }; /* * The markers, which this half of the head never carried. * * HeadArbiter finds our block by them and returns the head untouched * when they are absent — so on every non-singular page the whole * duplicate-tag firewall was inert: the home page, every category, * tag, author and date archive, and every page 2..n of each, which on * a real site is most of its URLs. Two canonicals shipped and the live * conflict scan, which defaults to the home page, counted our own tags * as the other plugin's and reported the site clean. * * Written unconditionally, before the first tag, because an empty * marker pair is what tells the arbiter "we are here and we emitted * nothing" — which is a different statement from "we never ran". */ echo "\n\n"; $robots = Services\Archives::robots( $settings ); if ( ! empty( $robots ) && $owns( 'robots' ) ) { printf( '' . "\n", esc_attr( implode( ', ', array_merge( $robots, array( 'follow' ) ) ) ) ); } $desc = Services\Archives::description( $settings ); if ( '' !== $desc && $owns( 'description' ) ) { $desc = mb_strlen( $desc ) > 160 ? mb_substr( $desc, 0, 157 ) . '…' : $desc; printf( '' . "\n", esc_attr( $desc ) ); } // Canonical. Archives had none, and on a large site they are most of // the URLs — every term page, every author, the blog index, and every // page 2..n of each. Without one, the same listing is reachable at // several addresses and Google picks the winner. $canonical = Services\Archives::canonical(); if ( '' !== $canonical && $owns( 'canonical' ) ) { printf( '' . "\n", esc_url( $canonical ) ); } // Open Graph. A category page shared into Slack or LinkedIn used to // render as a bare grey box with the URL as its title, because there // was nothing for the unfurler to read. Archives are shared more often // than their owners expect — they are what people link when they mean // "everything we have written about X". if ( ! empty( $settings['social_tags'] ) && $owns( 'open_graph' ) && '' !== $canonical ) { $title = wp_get_document_title(); printf( '' . "\n" ); printf( '' . "\n", esc_attr( $title ) ); if ( '' !== $desc ) { printf( '' . "\n", esc_attr( $desc ) ); } printf( '' . "\n", esc_url( $canonical ) ); printf( '' . "\n", esc_attr( get_bloginfo( 'name' ) ) ); $image = (string) ( $settings['og_default_image'] ?? '' ); if ( '' !== $image ) { printf( '' . "\n", esc_url( $image ) ); } $card = (string) ( $settings['twitter_card'] ?? 'summary_large_image' ); printf( '' . "\n", esc_attr( $card ) ); printf( '' . "\n", esc_attr( $title ) ); if ( '' !== $desc ) { printf( '' . "\n", esc_attr( $desc ) ); } if ( '' !== $image ) { printf( '' . "\n", esc_url( $image ) ); } $site_handle = (string) ( $settings['twitter_site'] ?? '' ); if ( '' !== $site_handle ) { printf( '' . "\n", esc_attr( $site_handle ) ); } } echo "\n"; } /** * Archives the site has switched off, and attachment pages. * * An attachment page is a near-empty page wrapping one image; it exists on * every WordPress site, is almost never linked, and is exactly the kind of * thin page an assistant quotes when it cannot find anything better. */ public function redirect_dead_archives(): void { if ( is_admin() || wp_doing_ajax() ) { return; } $settings = $this->options()->get( 'seo', self::defaults() ); if ( is_attachment() && ! empty( $settings['attachment_redirect'] ) ) { $attachment = get_queried_object(); if ( $attachment instanceof \WP_Post ) { $parent = (int) $attachment->post_parent; $target = $parent > 0 ? (string) get_permalink( $parent ) : (string) wp_get_attachment_url( $attachment->ID ); if ( '' !== $target ) { wp_safe_redirect( $target, 301 ); exit; } } } if ( Services\Archives::disabled( $settings ) ) { wp_safe_redirect( home_url( '/' ), 301 ); exit; } } /** * Prepend/append credit text to feed items, with variables resolved. */ public function rss_content( string $content ): string { $settings = $this->options()->get( 'seo', self::defaults() ); $post = get_post(); if ( ! $post instanceof \WP_Post ) { return $content; } $vars = array( '%post_link%' => '' . esc_html( get_the_title( $post ) ) . '', '%post_url%' => esc_url( (string) get_permalink( $post ) ), '%sitename%' => esc_html( get_bloginfo( 'name' ) ), '%site_link%' => '' . esc_html( get_bloginfo( 'name' ) ) . '', '%author%' => esc_html( get_the_author_meta( 'display_name', (int) $post->post_author ) ), ); $before = trim( (string) $settings['rss_before'] ); $after = trim( (string) $settings['rss_after'] ); if ( '' !== $before ) { $content = '

' . strtr( wp_kses_post( $before ), $vars ) . '

' . $content; } if ( '' !== $after ) { $content .= '

' . strtr( wp_kses_post( $after ), $vars ) . '

'; } return $content; } /** * Meta description, canonical, robots, OpenGraph/Twitter. */ public function head_tags(): void { if ( ! is_singular() ) { return; } $post = get_queried_object(); if ( ! $post instanceof \WP_Post ) { return; } $settings = $this->options()->get( 'seo', self::defaults() ); $desc = self::resolve_description( $post ); // Per-post canonical override, validated to http(s) in PostSeo. $url = \AiRankSuite\Support\PostSeo::canonical( $post ); $title = wp_get_document_title(); $coexist = null !== self::conflict() && empty( $settings['force_output'] ); $owns = static function ( string $surface ) use ( $coexist ): bool { return ! $coexist || \AiRankSuite\Modules\Coexistence\CoexistenceModule::owns( $surface ); }; echo "\n\n"; if ( '' !== $desc && $owns( 'description' ) ) { printf( '' . "\n", esc_attr( $desc ) ); } if ( $owns( 'canonical' ) ) { printf( '' . "\n", esc_url( $url ) ); } if ( ! empty( $settings['social_tags'] ) && $owns( 'open_graph' ) ) { $card = \AiRankSuite\Support\PostSeo::social_card( $post, $title, $desc ); printf( '' . "\n", esc_attr( is_page() ? 'website' : 'article' ) ); printf( '' . "\n", esc_attr( $card['og_title'] ) ); if ( '' !== $card['og_desc'] ) { printf( '' . "\n", esc_attr( $card['og_desc'] ) ); } printf( '' . "\n", esc_url( $url ) ); printf( '' . "\n", esc_attr( get_bloginfo( 'name' ) ) ); if ( '' !== $card['og_image'] ) { printf( '' . "\n", esc_url( $card['og_image'] ) ); // Width/height let crawlers render the preview before fetching // the image; alt is required for accessible link previews. if ( is_array( $card['image_meta'] ?? null ) ) { printf( '' . "\n", (int) $card['image_meta']['width'] ); printf( '' . "\n", (int) $card['image_meta']['height'] ); if ( '' !== $card['image_meta']['alt'] ) { printf( '' . "\n", esc_attr( $card['image_meta']['alt'] ) ); } } } printf( '' . "\n", esc_attr( $card['tw_card'] ) ); printf( '' . "\n", esc_attr( $card['tw_title'] ) ); if ( '' !== $card['tw_desc'] ) { printf( '' . "\n", esc_attr( $card['tw_desc'] ) ); } if ( '' !== $card['tw_image'] ) { printf( '' . "\n", esc_url( $card['tw_image'] ) ); } } echo "\n"; } public function routes(): void { register_rest_route( self::NAMESPACE, '/settings/seo', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => array( $this, 'update_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'enabled' => array( 'type' => 'boolean' ), 'title_template' => array( 'type' => 'string', // Not sanitize_text_field: it eats %category% and %date%. 'sanitize_callback' => array( \AiRankSuite\Support\Sanitize::class, 'template' ), ), 'separator' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'social_tags' => array( 'type' => 'boolean' ), 'force_output' => array( 'type' => 'boolean' ), 'rss_before' => array( 'type' => 'string', 'sanitize_callback' => 'wp_kses_post', ), 'rss_after' => array( 'type' => 'string', 'sanitize_callback' => 'wp_kses_post', ), 'templates' => array( 'type' => 'object' ), // Archives. Every template goes through Sanitize::template // for the same reason the post ones do: sanitize_text_field // eats %term% and %search_query%. 'home_title' => array( 'type' => 'string', 'sanitize_callback' => array( \AiRankSuite\Support\Sanitize::class, 'template' ) ), 'home_desc' => array( 'type' => 'string', 'sanitize_callback' => array( \AiRankSuite\Support\Sanitize::class, 'template_multiline' ) ), 'taxonomy_templates' => array( 'type' => 'object' ), 'noindex_taxonomies' => array( 'type' => 'array' ), 'noindex_empty_terms' => array( 'type' => 'boolean' ), 'author_enabled' => array( 'type' => 'boolean' ), 'author_title' => array( 'type' => 'string', 'sanitize_callback' => array( \AiRankSuite\Support\Sanitize::class, 'template' ) ), 'author_desc' => array( 'type' => 'string', 'sanitize_callback' => array( \AiRankSuite\Support\Sanitize::class, 'template_multiline' ) ), 'author_noindex' => array( 'type' => 'boolean' ), 'date_enabled' => array( 'type' => 'boolean' ), 'date_title' => array( 'type' => 'string', 'sanitize_callback' => array( \AiRankSuite\Support\Sanitize::class, 'template' ) ), 'date_noindex' => array( 'type' => 'boolean' ), 'search_title' => array( 'type' => 'string', 'sanitize_callback' => array( \AiRankSuite\Support\Sanitize::class, 'template' ) ), 'notfound_title' => array( 'type' => 'string', 'sanitize_callback' => array( \AiRankSuite\Support\Sanitize::class, 'template' ) ), 'paginated_noindex' => array( 'type' => 'boolean' ), 'noindex_types' => array( 'type' => 'array' ), 'attachment_redirect' => array( 'type' => 'boolean' ), 'og_default_image' => array( 'type' => 'string', 'sanitize_callback' => 'esc_url_raw' ), 'twitter_card' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field' ), 'twitter_site' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field' ), 'facebook_app_id' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field' ), ), ), ) ); } public function get_settings(): WP_REST_Response { $post_types = array(); foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $type ) { if ( 'attachment' === $type->name ) { continue; } $post_types[] = array( 'name' => $type->name, 'label' => $type->labels->name ?? $type->name, ); } return new WP_REST_Response( array( 'settings' => $this->options()->get( 'seo', self::defaults() ), 'conflict' => self::conflict(), 'post_types' => $post_types, 'variables' => self::variables(), 'variable_groups' => Services\Archives::variable_groups(), 'taxonomies' => array_values( array_map( static fn( $tax ) => array( 'name' => $tax->name, 'label' => (string) ( $tax->labels->name ?? $tax->name ), ), array_filter( get_taxonomies( array( 'public' => true ), 'objects' ), static fn( $tax ) => ! in_array( $tax->name, array( 'post_format' ), true ) ) ) ), ) ); } public function update_settings( WP_REST_Request $request ): WP_REST_Response { $defaults = self::defaults(); $values = array(); foreach ( array_keys( $defaults ) as $key ) { if ( ! $request->has_param( $key ) ) { continue; } if ( 'templates' === $key ) { // An 'object' arg reaches us as-is, so the shape is enforced // here: real post types only, two known keys, plain text. $clean = array(); foreach ( (array) $request->get_param( $key ) as $type => $pair ) { $type = sanitize_key( (string) $type ); if ( ! post_type_exists( $type ) || ! is_array( $pair ) ) { continue; } $title = \AiRankSuite\Support\Sanitize::template( (string) ( $pair['title'] ?? '' ) ); $desc = \AiRankSuite\Support\Sanitize::template_multiline( (string) ( $pair['desc'] ?? '' ) ); if ( '' === $title && '' === $desc ) { continue; } $clean[ $type ] = array( 'title' => mb_substr( $title, 0, 200 ), 'desc' => mb_substr( $desc, 0, 400 ), ); } $values[ $key ] = $clean; continue; } $values[ $key ] = $request->get_param( $key ); } $this->options()->set( 'seo', $values, $defaults ); return $this->get_settings(); } private function options(): Options { return \AiRankSuite\Kernel::service( Options::class ); } }