compat = $compat ?? new ABDS_SEO_Compat_Detector(); } /** * Registers the hooks. * * @param ABDS_SEO_Loader $loader Hook loader. */ public function register_hooks( ABDS_SEO_Loader $loader ): void { $loader->add_action( 'template_redirect', $this, 'maybe_redirect', 5 ); $loader->add_action( 'template_redirect', $this, 'log_not_found', 99 ); } /** * The request's path relative to the site root, normalized, and its query. * * @return array{path:string, query:string} */ protected function request(): array { $uri = isset( $_SERVER['REQUEST_URI'] ) ? (string) wp_unslash( $_SERVER['REQUEST_URI'] ) : '/'; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- compared as plain text only; never output or stored unnormalized. $path = ABDS_SEO_Redirect_Matcher::normalize_path( $uri ); $base = ABDS_SEO_Redirect_Matcher::normalize_path( (string) wp_parse_url( home_url(), PHP_URL_PATH ) ); if ( '/' !== $base && ( $path === $base || 0 === strpos( $path, $base . '/' ) ) ) { $path = ABDS_SEO_Redirect_Matcher::normalize_path( substr( $path, strlen( $base ) ) ); } return array( 'path' => $path, 'query' => ABDS_SEO_Redirect_Matcher::query_of( $uri ), ); } /** * Redirects (or answers 410) when a rule covers this request. */ public function maybe_redirect(): void { if ( $this->compat->has_active_competitor() || is_admin() || is_embed() ) { return; } $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET'; if ( ! in_array( $method, array( 'GET', 'HEAD' ), true ) ) { return; } $repository = new ABDS_SEO_Redirect_Repository(); if ( ! $repository->has_rules() ) { return; } $request = $this->request(); $result = ABDS_SEO_Redirect_Matcher::resolve( $repository->finder(), $request['path'] ); if ( 'ok' !== $result['status'] ) { return; // No rule, or a loop / too long a chain: serve the request as WordPress would. } if ( is_array( $result['first'] ) ) { $repository->record_hit( (int) $result['first']['id'] ); } if ( 410 === $result['type'] ) { $this->gone(); return; } $destination = $this->destination( (string) $result['target'], $request['query'] ); if ( $this->same_as_current( $destination, $request ) ) { return; } // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- redirecting to an external address is what a redirect rule is for; the target was validated when the rule was saved. wp_redirect( $destination, (int) $result['type'], 'ABDS SEO' ); exit; } /** * The absolute URL to send the visitor to. * * @param string $target Target path or URL from the rule. * @param string $query Request query string. * @return string */ protected function destination( string $target, string $query ): string { $url = ABDS_SEO_Redirect_Matcher::carry_query( $target, $query ); return 0 === strpos( $url, '/' ) ? home_url( $url ) : $url; } /** * Whether the destination is the page already being requested. * * @param string $destination Absolute URL. * @param array $request { path, query }. * @return bool */ protected function same_as_current( string $destination, array $request ): bool { return ABDS_SEO_Redirect_Matcher::normalize_path( $destination ) === $request['path'] && ABDS_SEO_Redirect_Matcher::query_of( $destination ) === $request['query'] && (string) wp_parse_url( $destination, PHP_URL_HOST ) === (string) wp_parse_url( home_url(), PHP_URL_HOST ); } /** * Answers 410 Gone with the theme's 404 page. */ protected function gone(): void { global $wp_query; $wp_query->set_404(); status_header( 410 ); nocache_headers(); $template = get_404_template(); if ( $template ) { include $template; exit; } wp_die( esc_html__( 'This page has been removed.', 'abds-seo' ), '', array( 'response' => 410 ) ); } /** * Records a 404 that no rule handled. */ public function log_not_found(): void { if ( ! is_404() || $this->compat->has_active_competitor() || is_admin() ) { return; } $request = $this->request(); $referer = isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : ''; $agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; ( new ABDS_SEO_Not_Found_Log() )->record( $request['path'], $referer, $agent ); } }