prefix . 'awb_redirects'; $source = self::current_path(); $row = $wpdb->get_row( $wpdb->prepare( "SELECT target, type FROM {$table} WHERE source = %s LIMIT 1", $source ) ); if ( ! $row ) return; // Increment hit counter. $wpdb->query( $wpdb->prepare( "UPDATE {$table} SET hits = hits + 1 WHERE source = %s", $source ) ); wp_redirect( $row->target, (int) $row->type ); exit; } private static function current_path() { $path = parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ), PHP_URL_PATH ); return rtrim( $path, '/' ) ?: '/'; } // ── CRUD helpers (used by admin) ───────────────────────────────────────── public static function add( $source, $target, $type = 301 ) { global $wpdb; return $wpdb->replace( $wpdb->prefix . 'awb_redirects', [ 'source' => rtrim( $source, '/' ) ?: '/', 'target' => $target, 'type' => (int) $type, ], [ '%s', '%s', '%d' ] ); } public static function delete( $id ) { global $wpdb; return $wpdb->delete( $wpdb->prefix . 'awb_redirects', [ 'id' => (int) $id ], [ '%d' ] ); } public static function get_all( $limit = 200, $offset = 0 ) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}awb_redirects ORDER BY created_at DESC LIMIT %d OFFSET %d", $limit, $offset ) ); } public static function count() { global $wpdb; return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}awb_redirects" ); } public static function import_csv( $csv_text ) { $lines = explode( "\n", trim( $csv_text ) ); $imported = 0; foreach ( $lines as $line ) { $parts = str_getcsv( trim( $line ) ); if ( count( $parts ) < 2 ) continue; [$source, $target] = $parts; $type = isset( $parts[2] ) ? (int) $parts[2] : 301; if ( self::add( trim( $source ), trim( $target ), $type ) ) { $imported++; } } return $imported; } }