*/ public static function defaults(): array { return array( // Home page. Separate from everything else because it is the one // page whose title has no post to draw from. 'home_title' => '%sitename% %sep% %tagline%', 'home_desc' => '', // Per-taxonomy title/description templates, keyed by taxonomy name. 'taxonomy_templates' => array(), // Taxonomies that should never be indexed. 'noindex_taxonomies' => array(), // A term with no posts is an empty page; indexing it is asking to be // judged on it. 'noindex_empty_terms' => true, 'author_enabled' => true, 'author_title' => '%author% %sep% %sitename%', 'author_desc' => '', 'author_noindex' => false, 'date_enabled' => true, 'date_title' => '%date% %sep% %sitename%', 'date_noindex' => true, 'search_title' => '%search_query% %sep% %sitename%', 'notfound_title' => 'Page not found %sep% %sitename%', // Page 2 and beyond of any archive. Off by default: Google prefers // to see the whole set, and noindexing page 2 can strip the only // path to deep posts on a site with weak internal linking. 'paginated_noindex' => false, // Post types excluded from indexing entirely. 'noindex_types' => array(), // An attachment page is a near-empty page wrapping one image. It // exists on every WordPress site and is almost never wanted. 'attachment_redirect' => true, // Social defaults, used when a page has nothing of its own. 'og_default_image' => '', 'twitter_card' => 'summary_large_image', 'twitter_site' => '', 'facebook_app_id' => '', ); } /** * Variables, grouped by where they mean something. * * Grouped rather than one flat list because "%search_query% on a category * archive" is a question the UI should be able to answer before somebody * pastes it into the wrong field. * * @return array> */ public static function variable_groups(): array { return array( 'Everywhere' => array( '%sitename%' => __( 'Site name', 'ai-rank-suite' ), '%tagline%' => __( 'Site tagline', 'ai-rank-suite' ), '%sep%' => __( 'Separator', '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' ), '%pagenumber%' => __( 'Current page number', 'ai-rank-suite' ), ), 'Posts and pages' => array( '%title%' => __( 'Page title', 'ai-rank-suite' ), '%excerpt%' => __( 'Excerpt', 'ai-rank-suite' ), '%category%' => __( 'Primary category', 'ai-rank-suite' ), '%tags%' => __( 'Tags, comma separated', 'ai-rank-suite' ), '%author%' => __( 'Author name', 'ai-rank-suite' ), '%date%' => __( 'Publish date', 'ai-rank-suite' ), '%modified%' => __( 'Last modified date', 'ai-rank-suite' ), '%post_type%' => __( 'Post type label', 'ai-rank-suite' ), '%parent_title%' => __( 'Parent page title', 'ai-rank-suite' ), ), 'Archives' => array( '%term%' => __( 'Term name', 'ai-rank-suite' ), '%term_description%' => __( 'Term description', 'ai-rank-suite' ), '%taxonomy%' => __( 'Taxonomy label', 'ai-rank-suite' ), '%search_query%' => __( 'Search term', 'ai-rank-suite' ), '%count%' => __( 'Number of results', 'ai-rank-suite' ), ), ); } /** * Values that apply on any page, single or archive. * * @param array $settings Module settings. * @return array */ public static function global_values( array $settings ): array { $page = max( 1, (int) get_query_var( 'paged' ), (int) get_query_var( 'page' ) ); global $wp_query; $total = isset( $wp_query->max_num_pages ) ? max( 1, (int) $wp_query->max_num_pages ) : 1; return array( '%sitename%' => (string) get_bloginfo( 'name' ), '%tagline%' => (string) get_bloginfo( 'description' ), '%sep%' => (string) ( $settings['separator'] ?? '–' ), '%currentyear%' => (string) wp_date( 'Y' ), '%currentdate%' => (string) wp_date( (string) get_option( 'date_format' ) ), // Empty on page 1 so a template can carry it unconditionally without // putting "Page 1 of 1" on every single page of the site. '%page%' => $page > 1 /* translators: 1: current page, 2: total pages. */ ? sprintf( __( 'Page %1$d of %2$d', 'ai-rank-suite' ), $page, $total ) : '', '%pagenumber%' => (string) $page, ); } /** * Resolve the document title for whatever archive is being viewed. * * Returns an empty string when this is not an archive we handle, so the * caller can fall through to the theme. * * @param array $settings Module settings. */ public static function title( array $settings ): string { $values = self::global_values( $settings ); $title = self::resolve_title( $settings, $values ); // Page 2..n of an archive otherwise carries the identical title to page // 1 — forty URLs on a large site all called "Behandlungen | Mediflow", // indistinguishable in a result list and in Search Console. Templates // can say %page% explicitly; when they do not, saying it anyway is // better than shipping duplicates, because nobody sets this up until // after the duplicates have already been indexed. $page = max( (int) get_query_var( 'paged' ), (int) get_query_var( 'page' ) ); if ( '' !== $title && $page > 1 && ! str_contains( (string) self::raw_template( $settings ), '%page%' ) ) { $title .= ' ' . (string) ( $settings['separator'] ?? '–' ) . ' ' . sprintf( /* translators: %d: page number. */ __( 'Page %d', 'ai-rank-suite' ), $page ); } return $title; } /** * Which template string the current request will use, before filling. * * Only needed to answer "did the author already put %page% in it", which is * not worth threading back out of resolve_title(). * * @param array $settings Module settings. */ private static function raw_template( array $settings ): string { if ( is_search() ) { return (string) $settings['search_title']; } if ( is_404() ) { return (string) $settings['notfound_title']; } if ( is_front_page() || is_home() ) { return (string) $settings['home_title']; } if ( is_author() ) { return (string) $settings['author_title']; } if ( is_date() ) { return (string) $settings['date_title']; } $term = get_queried_object(); if ( $term instanceof \WP_Term ) { $own = self::term_title( $term ); if ( '' !== $own ) { return $own; } $template = self::taxonomy_template( $settings, $term->taxonomy, 'title' ); return '' !== $template ? $template : '%term% %sep% %sitename%'; } return ''; } /** * A title written for this one term, if there is one. * * Per-term values are what an editor typed on the category edit screen in * their old SEO plugin — the most specific thing the site knows about that * archive, and the first thing an import brings over. They win over the * per-taxonomy template, which is a pattern, which in turn wins over the * built-in fallback. * * @param \WP_Term|int|null $term */ public static function term_title( $term ): string { return self::term_meta( $term, \AiRankSuite\Modules\ImportExport\Services\ImportWriter::TERM_TITLE ); } /** * @param \WP_Term|int|null $term */ public static function term_description( $term ): string { return self::term_meta( $term, \AiRankSuite\Modules\ImportExport\Services\ImportWriter::TERM_DESC ); } /** * @param \WP_Term|int|null $term */ public static function term_canonical( $term ): string { return self::term_meta( $term, \AiRankSuite\Modules\ImportExport\Services\ImportWriter::TERM_CANONICAL ); } /** * @param \WP_Term|int|null $term * @return array */ public static function term_robots( $term ): array { $id = $term instanceof \WP_Term ? (int) $term->term_id : (int) $term; if ( $id <= 0 ) { return array(); } $value = get_term_meta( $id, \AiRankSuite\Modules\ImportExport\Services\ImportWriter::TERM_ROBOTS, true ); return is_array( $value ) ? array_values( array_map( 'strval', $value ) ) : array(); } /** * @param \WP_Term|int|null $term */ private static function term_meta( $term, string $key ): string { $id = $term instanceof \WP_Term ? (int) $term->term_id : (int) $term; if ( $id <= 0 ) { return ''; } $value = get_term_meta( $id, $key, true ); return is_string( $value ) ? trim( $value ) : ''; } /** * The title before the pagination suffix. * * @param array $settings Module settings. * @param array $values Variable values. */ private static function resolve_title( array $settings, array $values ): string { if ( is_search() ) { $values['%search_query%'] = (string) get_search_query(); $values['%count%'] = (string) ( $GLOBALS['wp_query']->found_posts ?? 0 ); return self::fill( (string) $settings['search_title'], $values ); } if ( is_404() ) { return self::fill( (string) $settings['notfound_title'], $values ); } if ( is_front_page() || is_home() ) { // A static front page is a real post and keeps its own title; only // a blog-index home falls back to the site template. if ( is_front_page() && 'page' === get_option( 'show_on_front' ) ) { return ''; } return self::fill( (string) $settings['home_title'], $values ); } if ( is_author() ) { $values['%author%'] = (string) get_the_author_meta( 'display_name', (int) get_query_var( 'author' ) ); return self::fill( (string) $settings['author_title'], $values ); } if ( is_date() ) { $values['%date%'] = self::date_label(); return self::fill( (string) $settings['date_title'], $values ); } $term = get_queried_object(); if ( is_category() || is_tag() || is_tax() ) { if ( ! $term instanceof \WP_Term ) { return ''; } $own = self::term_title( $term ); if ( '' !== $own ) { return self::fill( $own, self::term_values( $values, $term ) ); } $template = self::taxonomy_template( $settings, $term->taxonomy, 'title' ); if ( '' === $template ) { $template = '%term% %sep% %sitename%'; } return self::fill( $template, self::term_values( $values, $term ) ); } return ''; } /** * Meta description for an archive, or an empty string. * * @param array $settings Module settings. */ public static function description( array $settings ): string { $values = self::global_values( $settings ); if ( is_front_page() || is_home() ) { if ( is_front_page() && 'page' === get_option( 'show_on_front' ) ) { return ''; } $template = (string) $settings['home_desc']; // An empty template used to mean the home page shipped with no // description at all — on a blog-index site, the most linked page on // the domain and the one most often shown in a result. The tagline // is what the site owner already wrote to describe the site, so it // is a better default than nothing and a better default than a // generated sentence nobody approved. if ( '' === trim( $template ) ) { return wp_strip_all_tags( (string) get_bloginfo( 'description' ) ); } return self::fill( $template, $values ); } if ( is_author() ) { $id = (int) get_query_var( 'author' ); $values['%author%'] = (string) get_the_author_meta( 'display_name', $id ); $template = (string) $settings['author_desc']; if ( '' === trim( $template ) ) { return wp_strip_all_tags( (string) get_the_author_meta( 'description', $id ) ); } return self::fill( $template, $values ); } $term = get_queried_object(); if ( ( is_category() || is_tag() || is_tax() ) && $term instanceof \WP_Term ) { $own = self::term_description( $term ); if ( '' !== $own ) { return self::fill( $own, self::term_values( $values, $term ) ); } $template = self::taxonomy_template( $settings, $term->taxonomy, 'desc' ); if ( '' === trim( $template ) ) { // The term's own description is what an editor already wrote for // this page; preferring an empty template over it would be // throwing away the only human sentence on the page. return wp_strip_all_tags( (string) term_description( $term ) ); } return self::fill( $template, self::term_values( $values, $term ) ); } return ''; } /** * Robots directives for the current request. * * @param array $settings Module settings. * @return array */ public static function robots( array $settings ): array { $out = array(); $paged = max( (int) get_query_var( 'paged' ), (int) get_query_var( 'page' ) ) > 1; if ( $paged && ! empty( $settings['paginated_noindex'] ) ) { $out[] = 'noindex'; } if ( is_search() || is_404() ) { $out[] = 'noindex'; } if ( is_author() && ! empty( $settings['author_noindex'] ) ) { $out[] = 'noindex'; } if ( is_date() && ! empty( $settings['date_noindex'] ) ) { $out[] = 'noindex'; } $term = get_queried_object(); if ( ( is_category() || is_tag() || is_tax() ) && $term instanceof \WP_Term ) { // A directive set on this one term (imported, or set here) is the // most specific instruction there is. foreach ( self::term_robots( $term ) as $directive ) { $out[] = $directive; } if ( in_array( $term->taxonomy, (array) $settings['noindex_taxonomies'], true ) ) { $out[] = 'noindex'; } if ( ! empty( $settings['noindex_empty_terms'] ) && 0 === (int) $term->count ) { $out[] = 'noindex'; } } if ( is_singular() ) { $post = get_queried_object(); if ( $post instanceof \WP_Post && in_array( $post->post_type, (array) $settings['noindex_types'], true ) ) { $out[] = 'noindex'; } } return array_values( array_unique( $out ) ); } /** * The canonical URL for whatever archive is being viewed. * * Archives had no canonical at all, and on a large site they are most of * the URLs — every category, every tag, every author, the blog index, and * page 2 through page 40 of each of them. Without one, WordPress will * happily serve the same listing at `/category/news/`, * `/category/news/?utm_source=x` and `/category/news/page/1/`, and Google * gets to choose which of them is the real one. * * Paginated pages point at *themselves*, not at page 1. Pointing page 3 at * page 1 tells Google the posts only reachable from page 3 do not have a * home, and it is the mistake that quietly de-indexes the deep half of a * large archive. Google's own guidance changed to this years ago and plenty * of plugins still have not followed. * * Search results and 404s get nothing: they are already noindex, and a * canonical on a noindexed page is a contradiction. */ public static function canonical(): string { if ( is_search() || is_404() ) { return ''; } $base = ''; if ( is_front_page() || is_home() ) { $page_for_posts = (int) get_option( 'page_for_posts' ); $base = is_home() && $page_for_posts > 0 && ! is_front_page() ? (string) get_permalink( $page_for_posts ) : (string) home_url( '/' ); } elseif ( is_author() ) { $base = (string) get_author_posts_url( (int) get_query_var( 'author' ) ); } elseif ( is_post_type_archive() ) { $type = get_query_var( 'post_type' ); $type = is_array( $type ) ? (string) reset( $type ) : (string) $type; $link = '' !== $type ? get_post_type_archive_link( $type ) : false; $base = is_string( $link ) ? $link : ''; } elseif ( is_date() ) { $year = (int) get_query_var( 'year' ); $month = (int) get_query_var( 'monthnum' ); $day = (int) get_query_var( 'day' ); if ( $year > 0 && $month > 0 && $day > 0 ) { $base = (string) get_day_link( $year, $month, $day ); } elseif ( $year > 0 && $month > 0 ) { $base = (string) get_month_link( $year, $month ); } elseif ( $year > 0 ) { $base = (string) get_year_link( $year ); } } else { $term = get_queried_object(); if ( $term instanceof \WP_Term ) { $link = get_term_link( $term ); $base = is_string( $link ) ? $link : ''; } } if ( '' === $base ) { return ''; } $paged = max( (int) get_query_var( 'paged' ), (int) get_query_var( 'page' ) ); if ( $paged > 1 ) { global $wp_rewrite; $base = $wp_rewrite && $wp_rewrite->using_permalinks() ? trailingslashit( $base ) . trailingslashit( $wp_rewrite->pagination_base ) . $paged : add_query_arg( 'paged', $paged, $base ); $base = user_trailingslashit( $base ); } return $base; } /** * Should this archive exist at all? * * A one-author site's author archive is a duplicate of its blog index, and * WordPress will happily serve both. * * @param array $settings Module settings. */ public static function disabled( array $settings ): bool { if ( is_author() && empty( $settings['author_enabled'] ) ) { return true; } if ( is_date() && empty( $settings['date_enabled'] ) ) { return true; } return false; } /** * @param array $settings Module settings. * @param string $taxonomy Taxonomy name. * @param string $kind 'title' or 'desc'. */ private static function taxonomy_template( array $settings, string $taxonomy, string $kind ): string { $templates = (array) ( $settings['taxonomy_templates'] ?? array() ); return trim( (string) ( $templates[ $taxonomy ][ $kind ] ?? '' ) ); } /** * What this plugin would print for one term, resolved. * * The term editor shows this under the fields: a value typed for the term * wins, then the taxonomy template, then the built-in fallback — the same * order the front end uses, filled with this term's own name and count, so * the editor is looking at the sentence rather than at a pattern. * * @param \WP_Term $term Term. * @return array{title: string, desc: string, source: string} */ public static function preview_for_term( \WP_Term $term ): array { $settings = \AiRankSuite\Kernel::service( \AiRankSuite\Support\Options::class )->get( 'seo', \AiRankSuite\Modules\Seo\SeoModule::defaults() ); $values = self::term_values( self::global_values( $settings ), $term ); $own_title = self::term_title( $term ); $template = self::taxonomy_template( $settings, $term->taxonomy, 'title' ); $source = '' !== $own_title ? 'term' : ( '' !== $template ? 'template' : 'default' ); $title = self::fill( '' !== $own_title ? $own_title : ( '' !== $template ? $template : '%term% %sep% %sitename%' ), $values ); $own_desc = self::term_description( $term ); $desc_tpl = self::taxonomy_template( $settings, $term->taxonomy, 'desc' ); $desc = '' !== $own_desc ? self::fill( $own_desc, $values ) : ( '' !== $desc_tpl ? self::fill( $desc_tpl, $values ) : wp_strip_all_tags( (string) term_description( $term ) ) ); return array( 'title' => $title, 'desc' => $desc, 'source' => $source ); } /** * @param array $values Base values. * @return array */ private static function term_values( array $values, \WP_Term $term ): array { $taxonomy = get_taxonomy( $term->taxonomy ); return array_merge( $values, array( '%term%' => $term->name, '%term_description%' => wp_strip_all_tags( (string) term_description( $term ) ), '%taxonomy%' => $taxonomy ? (string) $taxonomy->labels->singular_name : $term->taxonomy, '%count%' => (string) (int) $term->count, ) ); } private static function date_label(): string { if ( is_day() ) { return (string) get_the_date( (string) get_option( 'date_format' ) ); } if ( is_month() ) { return (string) get_the_date( 'F Y' ); } return (string) get_the_date( 'Y' ); } /** * @param array $values Replacements. */ private static function fill( string $template, array $values ): string { if ( '' === trim( $template ) ) { return ''; } $out = strtr( $template, $values ); // Anything left is a variable that means nothing here — a %term% on the // search page. Dropping it beats publishing it. $out = (string) preg_replace( '/%%?[a-z_0-9\-]+%%?/i', '', $out ); // Collapse the gap the dropped variable left, and any separator now // dangling at either end. $out = (string) preg_replace( '/\s{2,}/', ' ', $out ); return trim( $out, " \t\n\r\0\x0B-–—|·»" ); } }