id ); } if ( (int) $row->code === 410 ) { self::gone(); return; } $to = self::with_query( $match['to'], $uri ); // never into a loop: a redirect to the address being asked for does nothing if ( Url::normalize( $to ) === Url::normalize( $uri ) && self::is_local( $to ) ) { return; } self::send( $to, (int) $row->code ); return; } if ( ! is_404() ) { return; } if ( Settings::get( 'log404' ) ) { NotFoundLog::record( $uri ); } $fallback = (string) Settings::get( 'fallback404' ); if ( $fallback === 'home' ) { self::send( home_url( '/' ), (int) Settings::get( 'fallback404Code' ) ); } elseif ( $fallback === 'url' && Settings::get( 'fallback404Url' ) ) { $to = Url::absolute( (string) Settings::get( 'fallback404Url' ) ); if ( Url::normalize( $to ) !== Url::normalize( $uri ) ) { self::send( $to, (int) Settings::get( 'fallback404Code' ) ); } } } /** the visitor's query string goes along, unless the target has its own */ private static function with_query( string $to, string $uri ): string { if ( ! Settings::get( 'keepQuery' ) || strpos( $to, '?' ) !== false ) { return $to; } $query = (string) wp_parse_url( $uri, PHP_URL_QUERY ); return $query === '' ? $to : $to . '?' . $query; } private static function is_local( string $url ): bool { $host = wp_parse_url( $url, PHP_URL_HOST ); return ! $host || strcasecmp( (string) $host, (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) === 0; } private static function send( string $to, int $code ): void { /** * The redirect is about to be sent. */ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- rb_redirection_ is this plugin's prefix (constants, options, tables, functions and hooks); Plugin Check only guesses rbRedirection from the namespace do_action( 'rb_redirection_before_redirect', $to, $code ); nocache_headers(); header( 'X-Redirect-By: RB Links Redirection' ); // wp_redirect, not wp_safe_redirect: a redirect may go to another site on purpose wp_redirect( $to, $code, 'RB Links Redirection' ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } /** 410: the page is gone for good; the theme's 404 template shows */ private static function gone(): void { global $wp_query; $wp_query->set_404(); status_header( 410 ); nocache_headers(); } }