mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-06 15:12:30 +09:00
Changing a WordPress site's URL is not a single UPDATE: the URL is embedded across wp_options, post content, and PHP-serialized blobs whose byte-length prefixes break under a naive REPLACE. This adds a first-class swap tool built on WP-CLI search-replace (serialization-safe), with the rails to make it routine: - preview_url_change: dry-run that reports per-pair replacement counts, no writes - change_site_url: backs the database up first, rewrites with --skip-columns=guid (full-URL + host-only pairs so scheme/protocol-relative refs are covered), updates home/siteurl, flushes the object cache + permalinks, then re-points the primary Domain row and nginx vhost. Rolls the DB back from the backup on failure. keep_old_redirect keeps the old host resolving so WordPress 301s it. - POST /sites/<id>/url/preview and /sites/<id>/url (admin-gated) - "Change URL" modal on the site detail page: live dry-run preview, backup notice, keep-old-address toggle, then apply. _write_app_vhost is factored out of _provision_routing and shared by the re-point path. The pure-SQL search-replace in db_sync_service remains the offline-dump fallback only. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
152 lines
7.4 KiB
React
152 lines
7.4 KiB
React
import { useState, useEffect } from 'react';
|
|
import { Globe, AlertTriangle, ArrowRight } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Label } from '@/components/ui/label';
|
|
import { Switch } from '@/components/ui/switch';
|
|
import wordpressApi from '../../services/wordpress';
|
|
import { useToast } from '../../contexts/ToastContext';
|
|
|
|
// Change a WordPress site's URL safely: a debounced dry-run preview of the DB
|
|
// rewrite, then a backed-up, serialization-safe apply (WP-CLI search-replace)
|
|
// on confirm. The backend re-points the Domain row + nginx vhost afterwards.
|
|
export default function ChangeUrlModal({ site, onClose, onChanged }) {
|
|
const toast = useToast();
|
|
const currentUrl = site.url || '';
|
|
const [newUrl, setNewUrl] = useState(currentUrl);
|
|
const [keepOld, setKeepOld] = useState(true);
|
|
const [preview, setPreview] = useState(null);
|
|
const [previewing, setPreviewing] = useState(false);
|
|
const [applying, setApplying] = useState(false);
|
|
|
|
const target = newUrl.trim();
|
|
const dirty = target && target !== currentUrl;
|
|
|
|
// Debounced dry-run preview whenever the target URL settles.
|
|
useEffect(() => {
|
|
if (!dirty) { setPreview(null); return undefined; }
|
|
let cancelled = false;
|
|
setPreviewing(true);
|
|
const timer = setTimeout(async () => {
|
|
try {
|
|
const res = await wordpressApi.previewUrlChange(site.id, target);
|
|
if (!cancelled) setPreview(res);
|
|
} catch (err) {
|
|
if (!cancelled) setPreview({ success: false, error: err.message || 'Preview failed' });
|
|
} finally {
|
|
if (!cancelled) setPreviewing(false);
|
|
}
|
|
}, 500);
|
|
return () => { cancelled = true; clearTimeout(timer); };
|
|
}, [target, dirty, site.id]);
|
|
|
|
async function handleApply(e) {
|
|
e?.preventDefault();
|
|
if (!dirty || applying) return;
|
|
setApplying(true);
|
|
toast.info('Changing site URL — backing up and rewriting the database…', { duration: 5000 });
|
|
try {
|
|
const res = await wordpressApi.changeUrl(site.id, target, keepOld);
|
|
if (res.success) {
|
|
toast.success(`Site URL changed to ${res.new_url} (${res.replacements} replacement${res.replacements === 1 ? '' : 's'})`);
|
|
if (res.warning) toast.warning(res.warning, { duration: 7000 });
|
|
onChanged?.();
|
|
onClose();
|
|
} else {
|
|
toast.error(res.error || 'Failed to change URL');
|
|
}
|
|
} catch (err) {
|
|
toast.error(err.message || 'Failed to change URL');
|
|
} finally {
|
|
setApplying(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="modal-overlay" onClick={() => !applying && onClose()}>
|
|
<div className="modal" onClick={(e) => e.stopPropagation()}>
|
|
<div className="modal-header">
|
|
<h2><Globe size={18} /> Change Site URL</h2>
|
|
<button className="modal-close" onClick={() => !applying && onClose()}>×</button>
|
|
</div>
|
|
<form onSubmit={handleApply}>
|
|
<div className="wp-url-swap">
|
|
<div className="wp-url-swap__warning">
|
|
<AlertTriangle size={18} aria-hidden="true" />
|
|
<span>
|
|
This rewrites every reference to the site URL across the database
|
|
(posts, options, serialized data) with a serialization-safe
|
|
search-replace. A database backup is taken first and restored
|
|
automatically if anything fails.
|
|
</span>
|
|
</div>
|
|
|
|
<div className="wp-url-swap__fromto">
|
|
<code>{currentUrl || '—'}</code>
|
|
<ArrowRight size={15} aria-hidden="true" />
|
|
<code className={dirty ? 'is-new' : ''}>{target || '—'}</code>
|
|
</div>
|
|
|
|
<div className="form-group">
|
|
<Label htmlFor="wp-new-url">New URL</Label>
|
|
<Input
|
|
id="wp-new-url"
|
|
type="text"
|
|
value={newUrl}
|
|
onChange={(e) => setNewUrl(e.target.value)}
|
|
placeholder="https://example.com"
|
|
disabled={applying}
|
|
autoFocus
|
|
/>
|
|
<span className="form-hint">
|
|
Include http:// or https://. Point this hostname's DNS at the server first.
|
|
</span>
|
|
</div>
|
|
|
|
<label className="wp-url-swap__keep">
|
|
<Switch checked={keepOld} onCheckedChange={setKeepOld} disabled={applying} />
|
|
<span>
|
|
Keep the old address working
|
|
<small>The previous host keeps resolving; WordPress 301-redirects it to the new URL.</small>
|
|
</span>
|
|
</label>
|
|
|
|
{dirty && (
|
|
<div className="wp-url-swap__preview">
|
|
{previewing && <span className="form-hint">Previewing changes…</span>}
|
|
{!previewing && preview?.success && (
|
|
<>
|
|
<div className="wp-url-swap__preview-total">
|
|
{preview.total} database replacement{preview.total === 1 ? '' : 's'} will be made
|
|
</div>
|
|
<ul className="wp-url-swap__preview-pairs">
|
|
{preview.pairs.map((p, i) => (
|
|
<li key={i}>
|
|
<code>{p.search}</code>
|
|
<ArrowRight size={12} aria-hidden="true" />
|
|
<code>{p.replace}</code>
|
|
<span className="wp-url-swap__count">{p.replacements}</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</>
|
|
)}
|
|
{!previewing && preview && !preview.success && (
|
|
<div className="wp-url-swap__preview-error">{preview.error}</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="modal-actions">
|
|
<Button type="button" variant="outline" onClick={onClose} disabled={applying}>Cancel</Button>
|
|
<Button type="submit" disabled={!dirty || applying}>
|
|
{applying ? 'Changing…' : 'Change URL'}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|