should_intercept() ) { return; } try { $this->process_redirect(); } catch ( \Throwable $e ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( 'SlugH RedirectHandler error: ' . $e->getMessage() ); } } /** * Skip admin, AJAX, REST, cron and CLI requests, and any method other than GET or HEAD. * * @return bool */ private function should_intercept() { if ( is_admin() || wp_doing_ajax() || wp_doing_cron() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { return false; } if ( defined( 'WP_CLI' ) && WP_CLI ) { return false; } $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : ''; return in_array( $method, array( 'GET', 'HEAD' ), true ); } /** * Resolve and serve a redirect for the current request. * * @return void */ private function process_redirect() { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by Sanitizer::path_input(), which keeps multibyte and percent-encoded characters (stored sources use the same sanitizer). $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? \SlugH\Support\Sanitizer::path_input( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; if ( '' === $request_uri ) { return; } $canonical_path = Normalizer::normalize_path( $request_uri ); $source_hash = Normalizer::compute_hash( $canonical_path ); $is_404 = is_404(); // Normal (non-404) requests only need the cached set of "always" hashes; no query. if ( ! $is_404 && ! in_array( $source_hash, RedirectRepository::get_always_hashes(), true ) ) { return; } $redirect = RedirectRepository::find_by_hash( $source_hash, $canonical_path ); if ( ! $redirect ) { return; } if ( '404_only' === $redirect->match_mode && ! $is_404 ) { return; } /** * Filter the matched redirect before it is served. Return false to skip it. * * @param object $redirect Matched redirect row. * @param string $canonical_path Canonical requested path. */ $redirect = apply_filters( 'slugh_redirect_match', $redirect, $canonical_path ); if ( ! $redirect ) { return; } RedirectRepository::record_hit( $redirect->id ); $status_code = absint( $redirect->status_code ); if ( ! in_array( $status_code, RedirectRepository::STATUS_CODES, true ) ) { $status_code = 301; } if ( 410 === $status_code ) { status_header( 410 ); nocache_headers(); exit; } $destination_url = $this->build_destination_url( $redirect->destination, $redirect->destination_type, $request_uri ); if ( 'external' === $redirect->destination_type ) { wp_redirect( $destination_url, $status_code, 'SlugH' ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect } else { wp_safe_redirect( $destination_url, $status_code, 'SlugH' ); } exit; } /** * Build the destination URL: home_url() + user_trailingslashit() for internal paths, * per-segment encoding, and the incoming query merged in (destination keys win). * * @param string $destination Stored destination. * @param string $destination_type internal or external. * @param string $request_uri Incoming request URI. * @return string */ public function build_destination_url( $destination, $destination_type, $request_uri ) { $incoming_query = Normalizer::extract_query( $request_uri ); $dest_query = Normalizer::extract_query( $destination ); $dest_path = preg_replace( '/[?#].*$/s', '', $destination ); $params = array(); if ( '' !== $incoming_query ) { wp_parse_str( $incoming_query, $incoming_params ); $params = $incoming_params; } if ( '' !== $dest_query ) { wp_parse_str( $dest_query, $dest_params ); $params = array_merge( $params, $dest_params ); } if ( 'external' === $destination_type ) { $parts = wp_parse_url( $dest_path ); $base = ( isset( $parts['scheme'] ) ? $parts['scheme'] : 'https' ) . '://' . ( isset( $parts['host'] ) ? $parts['host'] : '' ) . ( isset( $parts['port'] ) ? ':' . $parts['port'] : '' ); $full_url = $base . $this->encode_path_segments( isset( $parts['path'] ) ? $parts['path'] : '/' ); } else { $path = $this->encode_path_segments( '/' . ltrim( $dest_path, '/' ) ); $last_part = basename( $path ); if ( '/' !== $path && false === strpos( $last_part, '.' ) ) { $path = user_trailingslashit( untrailingslashit( $path ) ); } $full_url = home_url( $path ); } if ( ! empty( $params ) ) { $full_url .= '?' . http_build_query( $params, '', '&', PHP_QUERY_RFC3986 ); } return $full_url; } /** * Encode non-ASCII path segments with rawurlencode(), one segment at a time. * A segment that is already percent-encoded is never encoded again. * * @param string $path URL path. * @return string */ public function encode_path_segments( $path ) { $segments = explode( '/', $path ); foreach ( $segments as $i => $segment ) { if ( '' === $segment ) { continue; } if ( preg_match( '/%[0-9a-fA-F]{2}/', $segment ) ) { // Already encoded: only encode stray non-ASCII bytes, leave existing escapes intact. $segments[ $i ] = preg_replace_callback( '/[^\x00-\x7F]+/', function ( $matches ) { return rawurlencode( $matches[0] ); }, $segment ); } else { $segments[ $i ] = rawurlencode( $segment ); } } return implode( '/', $segments ); } }