`). Note: Google has * officially ignored these since 2019, but Bing and other crawlers still * consume them — cheap to emit, occasionally useful. */ final class RelLinksOutput { private FeaturesRepositoryInterface $features; public function __construct( FeaturesRepositoryInterface $features ) { $this->features = $features; } public function register(): void { add_action( 'wp_head', array( $this, 'output' ), 3 ); } public function output(): void { if ( ! $this->features->enabled( 'rel_links' ) ) { return; } if ( is_singular() ) { $this->output_singular(); return; } $this->output_archive(); } private function output_archive(): void { global $wp_query; $max = isset( $wp_query->max_num_pages ) ? (int) $wp_query->max_num_pages : 0; if ( $max < 2 ) { return; } $paged = max( 1, (int) get_query_var( 'paged' ) ); if ( $paged > 1 ) { $this->print_link( 'prev', get_pagenum_link( $paged - 1 ) ); } if ( $paged < $max ) { $this->print_link( 'next', get_pagenum_link( $paged + 1 ) ); } } private function output_singular(): void { global $page, $numpages; $total = (int) $numpages; $current = max( 1, (int) $page ); if ( $total < 2 ) { return; } if ( $current > 1 ) { $this->print_link( 'prev', $this->post_page_url( $current - 1 ) ); } if ( $current < $total ) { $this->print_link( 'next', $this->post_page_url( $current + 1 ) ); } } /** * URL of page N of a ``-paginated post — mirrors core's * own `_wp_link_page()` URL construction (which is marked private API). */ private function post_page_url( int $page_number ): string { global $wp_rewrite; $post = get_post(); if ( ! $post instanceof \WP_Post ) { return ''; } $permalink = (string) get_permalink( $post ); if ( 1 === $page_number ) { return $permalink; } if ( ! $wp_rewrite->using_permalinks() || in_array( $post->post_status, array( 'draft', 'pending' ), true ) ) { return (string) add_query_arg( 'page', $page_number, $permalink ); } return trailingslashit( $permalink ) . user_trailingslashit( (string) $page_number, 'single_paged' ); } private function print_link( string $rel, string $url ): void { if ( '' === $url ) { return; } printf( '' . "\n", esc_attr( $rel ), esc_url( $url ) ); } }