B then B->C becomes A->C), spec section 10. */ class LoopDetector { const MAX_HOPS = 20; /** * Check whether creating source -> destination would create a loop. * * Follows enabled redirects starting at the destination; reaching the source again is a loop. * * @param string $source_path Source path or URL. * @param string $destination Destination path or URL (internal). * @return array{is_loop: bool, path: array} */ public static function check_loop( $source_path, $destination ) { global $wpdb; $norm_source = Normalizer::normalize_path( $source_path ); $current = Normalizer::normalize_path( $destination ); $trail = array( $norm_source, $current ); if ( $norm_source === $current ) { return array( 'is_loop' => true, 'path' => $trail, ); } $table = Schema::get_table_name( 'slugh_redirects' ); $visited = array( $current => true ); for ( $i = 0; $i < self::MAX_HOPS; $i++ ) { // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $next = $wpdb->get_row( $wpdb->prepare( 'SELECT destination, destination_type FROM %i WHERE source_hash = %s AND source_path = %s AND enabled = 1 LIMIT 1', $table, Normalizer::compute_hash( $current ), $current ) ); // phpcs:enable if ( ! $next || 'internal' !== $next->destination_type ) { break; } $current = Normalizer::normalize_path( $next->destination ); $trail[] = $current; if ( $current === $norm_source ) { return array( 'is_loop' => true, 'path' => $trail, ); } if ( isset( $visited[ $current ] ) ) { // An existing loop that does not involve the new source; not caused by this change. break; } $visited[ $current ] = true; } return array( 'is_loop' => false, 'path' => $trail, ); } /** * Flatten chains on creation: every enabled redirect pointing at $source is pointed at $destination. * * Each update is written to the audit log under the job ID, and returned so a rollback can restore it. * * @param string $source New redirect's canonical source path. * @param string $destination New redirect's stored destination. * @param int $exclude_id Redirect ID to leave untouched (the new redirect itself). * @param int|null $job_id Job ID for the audit log. * @return array Flattened redirects with their previous destinations. */ public static function flatten_chains( $source, $destination, $exclude_id = 0, $job_id = null ) { global $wpdb; $norm_source = Normalizer::normalize_path( $source ); $table = Schema::get_table_name( 'slugh_redirects' ); // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, source_hash, destination FROM %i WHERE destination_hash = %s AND destination_type = 'internal' AND enabled = 1 AND id != %d", $table, Normalizer::compute_hash( $norm_source ), absint( $exclude_id ) ) ); // phpcs:enable $flattened = array(); $dest_hash = Normalizer::compute_hash( Normalizer::normalize_path( $destination ) ); foreach ( (array) $rows as $row ) { if ( Normalizer::normalize_path( $row->destination ) !== $norm_source ) { continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->update( $table, array( 'destination' => $destination, 'destination_hash' => $dest_hash, 'updated_at' => current_time( 'mysql', true ), ), array( 'id' => $row->id ), array( '%s', '%s', '%s' ), array( '%d' ) ); RedirectRepository::clear_cache( $row->source_hash ); AuditLogger::log( 'redirect_update', 'redirect', (int) $row->id, array( 'destination' => $row->destination ), array( 'destination' => $destination, 'reason' => 'chain_flattened', ), $job_id ); $flattened[] = array( 'id' => (int) $row->id, 'previous' => $row->destination, ); } return $flattened; } /** * Report chains and loops among existing enabled internal redirects (for imported rules). * * @param int $limit Maximum entries returned per list. * @return array{chains: array, loops: array, checked: int} */ public static function audit_existing( $limit = 200 ) { global $wpdb; $table = Schema::get_table_name( 'slugh_redirects' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, source_path, destination FROM %i WHERE enabled = 1 AND destination_type = 'internal'", $table ) ); $map = array(); $ids = array(); foreach ( (array) $rows as $row ) { $map[ $row->source_path ] = Normalizer::normalize_path( $row->destination ); $ids[ $row->source_path ] = (int) $row->id; } $chains = array(); $loops = array(); $seen = array(); foreach ( $map as $source => $dest ) { if ( ! isset( $map[ $dest ] ) ) { continue; } $trail = array( $source, $dest ); $current = $dest; $is_loop = false; $visited = array( $source => true, $dest => true, ); $hops = 2; while ( isset( $map[ $current ] ) && $hops <= self::MAX_HOPS ) { $current = $map[ $current ]; $trail[] = $current; ++$hops; if ( isset( $visited[ $current ] ) ) { $is_loop = true; break; } $visited[ $current ] = true; } $entry = array( 'id' => $ids[ $source ], 'path' => $trail, 'final' => end( $trail ), ); if ( $is_loop ) { $key = implode( '|', array_unique( $trail ) ); if ( ! isset( $seen[ $key ] ) && count( $loops ) < $limit ) { $seen[ $key ] = true; $loops[] = $entry; } } elseif ( count( $chains ) < $limit ) { $chains[] = $entry; } } return array( 'chains' => $chains, 'loops' => $loops, 'checked' => count( $map ), ); } }