term_meta = $term_meta; $this->archive_settings = $archive_settings; $this->features = $features; } public function register(): void { add_filter( 'pre_get_document_title', array( $this, 'filter_title' ) ); add_filter( 'wp_robots', array( $this, 'filter_robots' ) ); // WP core's `get_canonical_url` filter only fires for singular posts // (rel_canonical() checks is_singular()), so term archives never get a // canonical link from core — print one directly instead of relying on it. add_action( 'wp_head', array( $this, 'print_meta_description' ), 5 ); } public function filter_title( string $title ): string { if ( ! $this->should_handle() || ! $this->features->enabled( 'meta_title' ) ) { return $title; } $term = $this->current_term(); if ( null === $term ) { return $title; } $custom = (string) $this->term_meta->get( $term->term_id, TermMetaFields::TITLE ); return '' !== $custom ? $custom : $title; } /** * @param array $robots * * @return array */ public function filter_robots( array $robots ): array { if ( ! $this->should_handle() || ! $this->features->enabled( 'meta_robots' ) ) { return $robots; } $term = $this->current_term(); if ( null === $term ) { return $robots; } $settings = $this->archive_settings->get( $term->taxonomy ); if ( ! $settings['robots_index'] ) { $robots['noindex'] = true; } if ( ! $settings['robots_follow'] ) { $robots['nofollow'] = true; } return $robots; } public function print_meta_description(): void { if ( ! $this->should_handle() ) { return; } $term = $this->current_term(); if ( null === $term ) { return; } $description = (string) $this->term_meta->get( $term->term_id, TermMetaFields::DESCRIPTION ); if ( '' === $description ) { $description = wp_strip_all_tags( term_description( $term->term_id ) ); } if ( '' !== $description && $this->features->enabled( 'meta_description' ) ) { printf( '' . "\n", esc_attr( $description ) ); } if ( $this->features->enabled( 'canonical' ) ) { printf( '' . "\n", esc_url( (string) get_term_link( $term ) ) ); } } private function should_handle(): bool { return (bool) apply_filters( 'acez_seo_should_handle_meta', ! SeoPluginDetector::has_conflict() ); } private function current_term(): ?\WP_Term { return CurrentArchiveTerm::get(); } }