domma-cms 1.7.0 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/CLAUDE.md +30 -0
  3. package/admin/js/app.js +2 -2
  4. package/admin/js/lib/mail-compose.js +1 -0
  5. package/admin/js/lib/wordpress-import-arrange.js +1 -0
  6. package/admin/js/templates/call-new.html +1 -1
  7. package/admin/js/templates/docs/usage-tools.html +13 -1
  8. package/admin/js/templates/wordpress-import.html +223 -0
  9. package/admin/js/views/call-new.js +1 -1
  10. package/admin/js/views/contacts.js +15 -15
  11. package/admin/js/views/form-submissions.js +2 -2
  12. package/admin/js/views/index.js +1 -1
  13. package/admin/js/views/wordpress-import.js +38 -0
  14. package/package.json +1 -1
  15. package/plugins/_lib/admin/mail/compose-window.js +3 -1
  16. package/plugins/_lib/admin/mail/identity.js +18 -3
  17. package/plugins/_lib/admin/mail/reader-view.js +32 -3
  18. package/plugins/blog/CLAUDE.md +4 -0
  19. package/plugins/blog/admin/lib/email.js +191 -0
  20. package/plugins/blog/admin/views/comments.js +3 -1
  21. package/plugins/blog/plugin.json +2 -2
  22. package/plugins/blog/plugin.public.js +1 -1
  23. package/plugins/blog/tests/email.test.js +69 -0
  24. package/plugins/booking/CLAUDE.md +4 -0
  25. package/plugins/booking/admin/lib/email.js +191 -0
  26. package/plugins/booking/admin/views/panels.js +7 -3
  27. package/plugins/booking/plugin.json +1 -1
  28. package/plugins/booking/tests/email.test.js +69 -0
  29. package/plugins/free-tier.lock.json +27 -19
  30. package/plugins/shopping-cart/CLAUDE.md +4 -0
  31. package/plugins/shopping-cart/admin/lib/email.js +191 -0
  32. package/plugins/shopping-cart/admin/views/orders.js +11 -3
  33. package/plugins/shopping-cart/plugin.json +2 -2
  34. package/plugins/shopping-cart/tests/email.test.js +69 -0
  35. package/plugins/support/CLAUDE.md +4 -0
  36. package/plugins/support/admin/lib/email.js +191 -0
  37. package/plugins/support/admin/views/desk.js +4 -1
  38. package/plugins/support/admin/views/inbox.js +6 -2
  39. package/plugins/support/plugin.json +2 -2
  40. package/plugins/support/tests/email.test.js +69 -0
  41. package/server/routes/api/wordpress-import.js +220 -0
  42. package/server/server.js +12 -0
  43. package/server/services/hooks.js +1 -1
  44. package/server/services/permissionRegistry.js +10 -0
  45. package/server/services/roles.js +1 -1
  46. package/server/services/tools.js +2 -1
  47. package/server/services/wpImport/convert.js +491 -0
  48. package/server/services/wpImport/fetch.js +264 -0
  49. package/server/services/wpImport/index.js +832 -0
  50. package/server/services/wpImport/plan.js +549 -0
  51. package/server/services/wpImport/store.js +242 -0
  52. package/server/services/wpImport/wxr.js +167 -0
  53. package/server/services/wpImport/xml.js +261 -0
  54. package/server/services/wpImport/zip.js +142 -0
@@ -0,0 +1,191 @@
1
+ /**
2
+ * Email someone from this Tool: Email Pro's compose window when the user can
3
+ * send from it, otherwise the device's mail program through `mailto:`.
4
+ *
5
+ * Core makes the choice (`/admin/js/lib/mail-compose.js`, CMS 1.6.61+). It is
6
+ * imported at RUN time, never statically, so this Tool still loads on an older
7
+ * core - where every action keeps the plain `mailto:` it always had.
8
+ *
9
+ * `emailTo()` is for menu actions; `routeMailLinks()` is for a visible
10
+ * `<a href="mailto:…">` - the link stays a link (copy, hover, middle-click all
11
+ * still work) and a plain click goes the same way as the menu action.
12
+ *
13
+ * @module support/admin/lib/email
14
+ */
15
+
16
+ const CORE = '/admin/js/lib/mail-compose.js';
17
+ let core = null;
18
+
19
+ /** The core module, or null on a core without it. Asked once. */
20
+ const loadCore = () => (core ??= import(CORE).catch(() => null));
21
+
22
+ /** Longest subject or body carried (as core's). */
23
+ const MAX_TEXT = 2000;
24
+ /** Longest mailto: handed to the device - mail programs cut or refuse longer ones. */
25
+ const MAX_MAILTO = 1800;
26
+
27
+ /**
28
+ * Something that can only be an email address: one @, no spaces or line
29
+ * breaks, nothing that would start another recipient or a query.
30
+ *
31
+ * @param {*} value
32
+ * @returns {boolean}
33
+ */
34
+ export function looksLikeAddress(value) {
35
+ return typeof value === 'string' && /^[^\s@<>,;"?&]+@[^\s@<>,;"?&]+$/.test(value);
36
+ }
37
+
38
+ /** Valid addresses only, once each (ignoring case), from a string, `{address}`/`{email}`, or a list. */
39
+ function addressesOf(list) {
40
+ const seen = new Set();
41
+ return (Array.isArray(list) ? list : list == null ? [] : [list])
42
+ .map(x => String(typeof x === 'string' ? x : (x?.address ?? x?.email ?? '')).trim())
43
+ .filter(a => looksLikeAddress(a) && !seen.has(a.toLowerCase()) && seen.add(a.toLowerCase()));
44
+ }
45
+
46
+ /** Trim and cap; a subject loses its line breaks (they would be a header). */
47
+ function cleanText(value, {oneLine = false} = {}) {
48
+ let text = String(value ?? '');
49
+ if (oneLine) text = text.replace(/[\r\n]+/g, ' ');
50
+ return text.slice(0, MAX_TEXT).trim();
51
+ }
52
+
53
+ /**
54
+ * The request as a `mailto:` link (the fallback on an older core).
55
+ *
56
+ * @param {{to?: *, cc?: *, bcc?: *, subject?: string, body?: string}} request
57
+ * @returns {string}
58
+ */
59
+ export function mailtoHref({to, cc, bcc, subject, body} = {}) {
60
+ const enc = (list) => addressesOf(list).map(a => encodeURIComponent(a).replace(/%40/g, '@')).join(',');
61
+ const s = cleanText(subject, {oneLine: true});
62
+ const b = cleanText(body);
63
+ const query = [];
64
+ if (enc(cc)) query.push(`cc=${enc(cc)}`);
65
+ if (enc(bcc)) query.push(`bcc=${enc(bcc)}`);
66
+ if (s) query.push(`subject=${encodeURIComponent(s)}`);
67
+ if (b) query.push(`body=${encodeURIComponent(b)}`);
68
+ return `mailto:${enc(to)}${query.length ? `?${query.join('&')}` : ''}`;
69
+ }
70
+
71
+ /** Copy text: the clipboard, else the old copy command, else a box to copy it from by hand. */
72
+ async function copyText(text) {
73
+ try {
74
+ await navigator.clipboard.writeText(text);
75
+ return true;
76
+ } catch { /* next */ }
77
+ const box = document.createElement('textarea');
78
+ box.className = 'form-input';
79
+ box.readOnly = true;
80
+ box.rows = 6;
81
+ box.value = text;
82
+ try {
83
+ box.style.position = 'fixed';
84
+ box.style.opacity = '0';
85
+ document.body.appendChild(box);
86
+ box.select();
87
+ if (document.execCommand('copy')) return true;
88
+ } catch { /* next */ } finally {
89
+ box.remove();
90
+ box.style.position = '';
91
+ box.style.opacity = '';
92
+ }
93
+ const modal = E.modal({title: 'Copy the addresses', size: 'medium', buttons: [{id: 'close', text: 'Close', variant: 'secondary'}]});
94
+ modal.element.appendChild(box); // light DOM: setContent would strip the textarea
95
+ modal.open();
96
+ box.select();
97
+ return false;
98
+ }
99
+
100
+ /**
101
+ * Hand the request to the device's mail program - unless the link would be
102
+ * too long for it, when the addresses are copied instead and the toast says
103
+ * what did not go.
104
+ */
105
+ async function openMailto(request) {
106
+ const href = mailtoHref(request);
107
+ if (href.length <= MAX_MAILTO) {
108
+ location.href = href;
109
+ return 'mailto';
110
+ }
111
+ const all = addressesOf([...addressesOf(request.to), ...addressesOf(request.cc), ...addressesOf(request.bcc)]);
112
+ const copied = all.length ? await copyText(all.join(', ')) : false;
113
+ const lost = [cleanText(request.subject) && 'the subject', cleanText(request.body) && 'the message'].filter(Boolean);
114
+ E.toast(`Too long for your mail program to open. ${all.length
115
+ ? `${copied ? 'The' : 'Copy the'} ${all.length} address${all.length === 1 ? '' : 'es'}${copied ? ' are copied' : ''} - paste them into Bcc of a new email.` : ''}${lost.length ? ` Not carried over: ${lost.join(' and ')}.` : ''}`,
116
+ {type: 'warning', duration: 9000});
117
+ return 'copied';
118
+ }
119
+
120
+ const decode = (s) => { try { return decodeURIComponent(s); } catch { return s; } };
121
+
122
+ /**
123
+ * A `mailto:` link read back into a request (what a visible link carries).
124
+ * When the address part holds anything that is not an address, the query
125
+ * is ignored: an address stored as `me@x.com?body=…` must not prefill a message.
126
+ *
127
+ * @param {string} href
128
+ * @returns {null|{to: string[], cc: string[], bcc: string[], subject: string, body: string}}
129
+ */
130
+ export function parseMailto(href) {
131
+ const m = String(href ?? '').match(/^mailto:([^?]*)(?:\?(.*))?$/i);
132
+ if (!m) return null;
133
+ const split = (s) => decode(s || '').split(/[,;]/).map(x => x.trim()).filter(Boolean);
134
+ const named = split(m[1]);
135
+ const out = {to: addressesOf(named), cc: [], bcc: [], subject: '', body: ''};
136
+ if (named.some(a => !looksLikeAddress(a))) return out;
137
+ for (const pair of (m[2] || '').split('&')) {
138
+ const i = pair.indexOf('=');
139
+ if (i < 1) continue;
140
+ const key = pair.slice(0, i).toLowerCase();
141
+ const value = pair.slice(i + 1);
142
+ if (key === 'to') out.to = addressesOf([...out.to, ...split(value)]);
143
+ else if (key === 'cc' || key === 'bcc') out[key] = addressesOf([...out[key], ...split(value)]);
144
+ else if (key === 'subject') out.subject = cleanText(decode(value), {oneLine: true});
145
+ else if (key === 'body') out.body = cleanText(decode(value));
146
+ }
147
+ return out;
148
+ }
149
+
150
+ /**
151
+ * Email someone. Recipients are `{name, address}`, `{name, email}` or bare
152
+ * addresses; several people belong in `bcc`, so nobody sees the others.
153
+ * With nobody to address (a link to pass on) compose still opens, To empty.
154
+ *
155
+ * @param {{to?: *, cc?: *, bcc?: *, subject?: string, body?: string}} request
156
+ * @returns {Promise<'compose'|'mailto'|'copied'|'none'>}
157
+ */
158
+ export async function emailTo(request = {}) {
159
+ const mail = await loadCore();
160
+ if (!mail) return openMailto(request);
161
+ const r = mail.composeRequest(request);
162
+ if (r.to.length || r.cc.length || r.bcc.length) return mail.openEmail(r);
163
+ if (await mail.emailReady()) {
164
+ location.hash = mail.composeHash(r);
165
+ return 'compose';
166
+ }
167
+ location.href = mail.mailtoHref(r);
168
+ return 'mailto';
169
+ }
170
+
171
+ /**
172
+ * Send plain clicks on the `mailto:` links inside `root` through `emailTo()`.
173
+ * A modified click (new tab/window) or one already handled is left alone.
174
+ *
175
+ * @param {Element} root
176
+ * @returns {() => void} removes the listener
177
+ */
178
+ export function routeMailLinks(root) {
179
+ if (!root) return () => {};
180
+ const onClick = (e) => {
181
+ if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
182
+ const a = e.target?.closest?.('a[href^="mailto:" i]');
183
+ if (!a || !root.contains(a)) return;
184
+ const request = parseMailto(a.getAttribute('href'));
185
+ if (!request) return;
186
+ e.preventDefault();
187
+ emailTo(request);
188
+ };
189
+ root.addEventListener('click', onClick);
190
+ return () => root.removeEventListener('click', onClick);
191
+ }
@@ -18,6 +18,7 @@ import {
18
18
  refreshBadge, rowKeys, stamp, store, telHref, threadFiles, threadRows, when
19
19
  } from '../lib/kit.js';
20
20
  import {DRAFT_KEYS, afterSend, draftQuestion, draftText, draftValues, newDraftQuestion, ticketPrint} from '../lib/drafts.js';
21
+ import {looksLikeAddress, routeMailLinks} from '../lib/email.js';
21
22
 
22
23
  const ROW = `
23
24
  <div class="sp-row" data-id="{{key}}" tabindex="0" role="button" aria-haspopup="menu">
@@ -154,9 +155,11 @@ export async function mount(root, ctx) {
154
155
  const tel = view.querySelector('[data-ref="tel"]');
155
156
  scope(M.effect(() => {
156
157
  const c = contact.value || {};
157
- if (c.email) mailLink.setAttribute('href', `mailto:${c.email}`);
158
+ if (looksLikeAddress(c.email)) mailLink.setAttribute('href', `mailto:${encodeURIComponent(c.email)}`);
158
159
  if (c.phone) tel.setAttribute('href', telHref(c.phone));
159
160
  }));
161
+ // Domma's address stays a link; a click goes to Email Pro's compose when this user can send from it.
162
+ scope(routeMailLinks(view));
160
163
 
161
164
  // The first load shows a skeleton in the slot; a reload keeps the list on screen.
162
165
  let loaded = false;
@@ -13,6 +13,7 @@ import {
13
13
  threadFiles, threadRows, when
14
14
  } from '../lib/kit.js';
15
15
  import {DRAFT_KEYS, afterSend, draftQuestion, draftText, draftValues, ticketPrint} from '../lib/drafts.js';
16
+ import {looksLikeAddress, routeMailLinks} from '../lib/email.js';
16
17
 
17
18
  const ROW = `
18
19
  <div class="sp-row" data-id="{{key}}" tabindex="0" role="button" aria-haspopup="menu">
@@ -234,6 +235,7 @@ export async function mount(root, ctx) {
234
235
  let guard = null;
235
236
  let savedNotes = saved.notes;
236
237
  let stopScroll = () => {};
238
+ let stopMail = () => {};
237
239
  let menu = null;
238
240
 
239
241
  const run = async (fn) => {
@@ -249,7 +251,7 @@ export async function mount(root, ctx) {
249
251
  size: 'lg',
250
252
  model: {thread, status, statusLabel, tone, urgent, archived, draft, hasDraft, after, notes, busy},
251
253
  cleanup: () => {
252
- stopScroll(); stopFiles(); shown.dispose(); menu?.destroy?.();
254
+ stopScroll(); stopMail(); stopFiles(); shown.dispose(); menu?.destroy?.();
253
255
  [cur, thread, status, statusLabel, tone, urgent, archived, hasDraft].forEach(c => c.dispose?.());
254
256
  panels.release('detail');
255
257
  },
@@ -258,7 +260,7 @@ export async function mount(root, ctx) {
258
260
  <div><dt>Status</dt><dd><span class="sp-chip" data-bind-class="tone.value" data-bind-text="statusLabel.value"></span>
259
261
  <span class="sp-chip is-muted" data-bind-hidden="!archived.value">Archived</span></dd></div>
260
262
  <div><dt>Site</dt><dd>${siteUrl ? `<a href="${esc(siteUrl)}" target="_blank" rel="noopener">${esc(t.siteName)}</a>` : esc(t.siteName)}</dd></div>
261
- <div><dt>From</dt><dd>${esc(t.userName)}${t.userEmail ? ` · <a href="mailto:${esc(t.userEmail)}">${esc(t.userEmail)}</a>` : ''}</dd></div>
263
+ <div><dt>From</dt><dd>${esc(t.userName)}${t.userEmail ? ` · ${looksLikeAddress(t.userEmail) ? `<a href="mailto:${esc(encodeURIComponent(t.userEmail))}">${esc(t.userEmail)}</a>` : esc(t.userEmail)}` : ''}</dd></div>
262
264
  <div><dt>About</dt><dd>${esc(labelOf(t.category))}</dd></div>
263
265
  <div><dt>Opened</dt><dd>${esc(stamp(t.createdAt))}</dd></div>
264
266
  ${t.where ? `<div><dt>Where</dt><dd>${esc(t.where)}</dd></div>` : ''}
@@ -329,6 +331,8 @@ export async function mount(root, ctx) {
329
331
  was: offer.state === 'keep' ? offer.draft.was : ticketPrint(t, {notes: true})
330
332
  });
331
333
  panels.set('detail', {key: tid, close: (how) => guard.close(how)});
334
+ // The customer's address stays a link; a click goes to Email Pro's compose when it can.
335
+ stopMail = routeMailLinks(panel.panel);
332
336
  // Keep the newest message in view as the thread grows.
333
337
  const box = panel.panel.querySelector('.sp-thread-host');
334
338
  stopScroll = M.effect(() => { thread.value; Promise.resolve().then(() => { M.flush?.(); box.scrollTop = box.scrollHeight; }); });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "support",
3
3
  "displayName": "Support",
4
- "version": "1.2.0",
4
+ "version": "1.2.1",
5
5
  "tier": "free",
6
6
  "description": "Get help from Domma straight from your admin: our email, phone and hours always to hand, and a conversation with the Domma team for anything you need - urgent requests first. Right-click a request to archive, resolve or remove it.",
7
7
  "author": "Domma CMS",
@@ -61,7 +61,7 @@
61
61
  ],
62
62
  "views": {
63
63
  "plugin-support": {
64
- "entry": "support/admin/views/support.js?v=1.2.0",
64
+ "entry": "support/admin/views/support.js?v=1.2.1",
65
65
  "exportName": "supportView"
66
66
  }
67
67
  }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * admin/lib/email.js - the mailto: fallback and link reading (node has no
3
+ * /admin/js/lib/mail-compose.js, so emailTo() here is the older-core path).
4
+ */
5
+ import {test} from 'node:test';
6
+ import assert from 'node:assert/strict';
7
+ import {emailTo, looksLikeAddress, mailtoHref, parseMailto} from '../admin/lib/email.js';
8
+
9
+ test('mailtoHref: one person in To, subject and body encoded, @ readable', () => {
10
+ assert.equal(mailtoHref({to: [{name: 'Ann', address: 'ann+x@example.test'}], subject: 'Hi & bye', body: 'Line 1\nLine 2'}),
11
+ 'mailto:ann%2Bx@example.test?subject=Hi%20%26%20bye&body=Line%201%0ALine%202');
12
+ });
13
+
14
+ test('mailtoHref: several people in Bcc, strings and contacts alike', () => {
15
+ assert.equal(mailtoHref({bcc: ['a@x.test', {email: 'b@x.test'}], subject: 'S'}), 'mailto:?bcc=a@x.test,b@x.test&subject=S');
16
+ });
17
+
18
+ test('parseMailto reads back what mailtoHref writes, and plain links', () => {
19
+ const r = parseMailto(mailtoHref({to: 'a@x.test', cc: ['c@x.test'], bcc: ['b@x.test', 'd@x.test'], subject: 'Hi + there', body: 'Join: https://x.test/?a=1&b=2'}));
20
+ assert.deepEqual(r, {to: ['a@x.test'], cc: ['c@x.test'], bcc: ['b@x.test', 'd@x.test'], subject: 'Hi + there', body: 'Join: https://x.test/?a=1&b=2'});
21
+ assert.deepEqual(parseMailto('mailto:ann@example.test'), {to: ['ann@example.test'], cc: [], bcc: [], subject: '', body: ''});
22
+ assert.equal(parseMailto('https://example.test'), null);
23
+ });
24
+
25
+ test('emailTo on a core without mail-compose.js keeps the old mailto:', async () => {
26
+ const saved = globalThis.location;
27
+ globalThis.location = {href: '', hash: ''};
28
+ try {
29
+ assert.equal(await emailTo({to: [{name: 'Ann', address: 'ann@example.test'}], subject: 'Hello'}), 'mailto');
30
+ assert.equal(globalThis.location.href, 'mailto:ann@example.test?subject=Hello');
31
+ assert.equal(globalThis.location.hash, '');
32
+ } finally {
33
+ globalThis.location = saved;
34
+ }
35
+ });
36
+
37
+ test('fallback hardening: bad addresses dropped, repeats once, subject one line, text capped', () => {
38
+ for (const bad of ['a b@x.test', 'a@x.test\r\nBcc: c@x.test', '<a@x.test>', 'a@x.test,b@x.test', 'a@x.test?body=hi', 'nobody']) assert.equal(looksLikeAddress(bad), false, bad);
39
+ const href = mailtoHref({to: ['A@x.test', 'a@x.test', 'a b@x.test', 'me@x.test?body=evil'], subject: 'Hi\r\nBcc: c@x.test', body: 'x'.repeat(3000)});
40
+ assert.ok(href.startsWith('mailto:A@x.test?subject=Hi%20Bcc%3A%20c%40x.test&body='), href);
41
+ assert.equal(decodeURIComponent(href.split('body=')[1]).length, 2000);
42
+ });
43
+
44
+ test('parseMailto ignores the query when the address part is not an address', () => {
45
+ // An address stored as "me@x.com?body=..." and encoded into a link.
46
+ assert.deepEqual(parseMailto(`mailto:${encodeURIComponent('me@x.test?body=Pay now')}`), {to: [], cc: [], bcc: [], subject: '', body: ''});
47
+ assert.deepEqual(parseMailto('mailto:me@x.test%0D%0Abcc:z@x.test?subject=Hi&bcc=y@x.test'), {to: [], cc: [], bcc: [], subject: '', body: ''});
48
+ assert.deepEqual(parseMailto('mailto:?subject=Join&body=link').subject, 'Join');
49
+ });
50
+
51
+ test('emailTo on an old core: a mailto over 1800 characters is not opened - the addresses are copied', async () => {
52
+ const saved = {location: globalThis.location, E: globalThis.E, nav: Object.getOwnPropertyDescriptor(globalThis, 'navigator')};
53
+ let copied = '';
54
+ const toasts = [];
55
+ globalThis.location = {href: '', hash: ''};
56
+ globalThis.E = {toast: (m) => toasts.push(m)};
57
+ Object.defineProperty(globalThis, 'navigator', {value: {clipboard: {writeText: async (t) => { copied = t; }}}, configurable: true});
58
+ try {
59
+ const bcc = Array.from({length: 120}, (_, i) => `person${i}@example.test`);
60
+ assert.equal(await emailTo({bcc, subject: 'News'}), 'copied');
61
+ assert.equal(globalThis.location.href, '');
62
+ assert.equal(copied, bcc.join(', '));
63
+ assert.match(toasts[0], /120 addresses are copied.*Not carried over: the subject/);
64
+ } finally {
65
+ globalThis.location = saved.location;
66
+ globalThis.E = saved.E;
67
+ if (saved.nav) Object.defineProperty(globalThis, 'navigator', saved.nav); else delete globalThis.navigator;
68
+ }
69
+ });
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Import from WordPress - the API (mounted at /api/import/wordpress behind
3
+ * toolGate('wordpress-import'); every route needs `import.wordpress`). The
4
+ * work is services/wpImport.
5
+ *
6
+ * GET /status staged uploads, the running job, recent runs, what posts can become
7
+ * GET /count the sidebar badge
8
+ * POST /upload a WXR export (.xml, or a .zip holding one) - multipart, field "file"
9
+ * POST /staged/:id/uploads the wp-content/uploads folder as a .zip - multipart, field "file"
10
+ * DELETE /staged/:id/uploads drop that zip
11
+ * POST /staged/:id/preview {options} -> the plan; nothing is written
12
+ * POST /staged/:id/run {options} -> a background job
13
+ * DELETE /staged/:id discard an upload
14
+ * GET /jobs/:id a job's progress
15
+ * GET /history · GET|DELETE /history/:id
16
+ */
17
+ import fs from 'node:fs';
18
+ import {pipeline} from 'node:stream/promises';
19
+ import {Transform} from 'node:stream';
20
+ import {authenticate as defaultAuthenticate, requirePermission as defaultRequirePermission} from '../../middleware/auth.js';
21
+ import {
22
+ blogTarget, cleanOptions, createStaging, discardStaged, getJob, inspectStaged, previewImport, runningJob, stagingMeta,
23
+ stagingPath, startRun, updateStagingMeta, UPLOADS_ZIP_MAX_BYTES, WXR_MAX_BYTES, zipRel
24
+ } from '../../services/wpImport/index.js';
25
+ import {getReport, isId, listHistory, listStaging, removeHistory} from '../../services/wpImport/store.js';
26
+ import {openZip} from '../../services/wpImport/zip.js';
27
+ import {seoEngine} from '../../services/seo/index.js';
28
+ import {allowInGitignore} from '../../services/siteGitignore.js';
29
+ import {ago, clip, plural} from '../../services/sidebarBadges.js';
30
+
31
+ // Uploads, the mapping, the history and reports (names of who ran it, commenters) all stay out of the site's repo.
32
+ const IMPORT_RULE = '/content/imports/wordpress/';
33
+
34
+ /** Stream a multipart file to disk, stopping at `max` bytes. */
35
+ async function saveUpload(file, dest, max) {
36
+ let size = 0;
37
+ const counter = new Transform({
38
+ transform(chunk, _enc, cb) {
39
+ size += chunk.length;
40
+ if (size > max) return cb(Object.assign(new Error('too big'), {code: 'ETOOBIG'}));
41
+ cb(null, chunk);
42
+ }
43
+ });
44
+ await pipeline(file, counter, fs.createWriteStream(dest, {mode: 0o600}));
45
+ if (file.truncated) throw Object.assign(new Error('too big'), {code: 'ETOOBIG'});
46
+ return size;
47
+ }
48
+
49
+ const mb = (n) => `${Math.round(n / 1048576)} MB`;
50
+
51
+ /** What the screen shows of a plan - the plan minus internals. */
52
+ export function planForScreen(plan, blog) {
53
+ return {
54
+ source: plan.source,
55
+ site: plan.site,
56
+ blogBase: plan.blogBase,
57
+ blog: {available: blog.available, basePath: blog.basePath, comments: Boolean(blog.comments), reason: blog.reason || ''},
58
+ options: plan.options,
59
+ summary: plan.summary,
60
+ notes: plan.notes,
61
+ items: plan.items.map(r => ({
62
+ key: r.key, kind: r.kind, wpId: r.wpId, title: r.title, action: r.action, reason: r.reason || '',
63
+ target: r.target || '', oldUrl: r.oldUrl || '', status: r.status || '', wpStatus: r.wpStatus || '',
64
+ shortcodes: r.shortcodes || [], builder: r.builder || '', comments: r.comments || 0, count: r.count || 0,
65
+ replaces: Boolean(r.replaces), wanted: r.wanted || ''
66
+ })),
67
+ redirects: plan.redirects.map(r => ({from: r.from, to: r.to, action: r.action, reason: r.reason || ''}))
68
+ };
69
+ }
70
+
71
+ export async function wordpressImportRoutes(fastify, opts = {}) {
72
+ const authenticate = opts.authenticate || defaultAuthenticate;
73
+ const requirePermission = opts.requirePermission || defaultRequirePermission;
74
+ const may = {preHandler: [authenticate, requirePermission('import', 'wordpress')]};
75
+ const fail = (reply, err, fallback = 400) => reply.code(err.statusCode || fallback).send({error: err.message || String(err)});
76
+
77
+ fastify.get('/status', may, async () => {
78
+ const blog = await blogTarget();
79
+ const job = runningJob();
80
+ return {
81
+ staged: await listStaging(),
82
+ running: job ? {...job} : null,
83
+ history: (await listHistory()).slice(0, 50),
84
+ blog: {available: blog.available, basePath: blog.basePath, comments: Boolean(blog.comments), reason: blog.reason || ''},
85
+ redirects: Boolean(seoEngine()),
86
+ limits: {wxr: WXR_MAX_BYTES, uploads: UPLOADS_ZIP_MAX_BYTES}
87
+ };
88
+ });
89
+
90
+ fastify.get('/count', may, async () => {
91
+ const job = runningJob();
92
+ const runs = await listHistory();
93
+ const staged = await listStaging();
94
+ if (job) {
95
+ return {count: 1, label: `Importing - ${job.phase}`, tone: 'live',
96
+ details: {title: 'Import from WordPress', rows: [{label: 'Progress', value: `${job.done} of ${job.total}`}], items: [], empty: ''}};
97
+ }
98
+ const last = runs[0];
99
+ return {
100
+ count: runs.length,
101
+ label: runs.length ? `${plural(runs.length, 'import')} so far` : 'Nothing imported yet',
102
+ tone: 'info',
103
+ details: {
104
+ title: 'Import from WordPress',
105
+ rows: [
106
+ {label: 'Imports', value: String(runs.length)},
107
+ {label: 'Waiting to run', value: String(staged.length)},
108
+ ...(last ? [{label: 'Last', value: ago(last.at)}] : [])
109
+ ],
110
+ items: runs.slice(0, 5).map(r => ({text: clip(r.siteTitle || r.source || 'WordPress', 48), meta: `${ago(r.at)}${r.status === 'partial' ? ' · some failed' : ''}`, ...(r.status === 'partial' && {tone: 'danger'})})),
111
+ empty: 'Upload a WordPress export to start.'
112
+ }
113
+ };
114
+ });
115
+
116
+ fastify.post('/upload', may, async (request, reply) => {
117
+ const file = await request.file({limits: {fileSize: WXR_MAX_BYTES + 1, files: 1}}).catch(() => null);
118
+ if (!file) return reply.code(400).send({error: 'Choose the export file to upload.'});
119
+ const name = String(file.filename || '').slice(0, 200);
120
+ const kind = /\.zip$/i.test(name) ? 'zip' : /\.xml$/i.test(name) ? 'xml' : '';
121
+ if (!kind) {
122
+ file.file.resume();
123
+ return reply.code(400).send({error: 'Upload the .xml file WordPress exports (Tools › Export), or a .zip holding it.'});
124
+ }
125
+ try { allowInGitignore(process.cwd(), [IMPORT_RULE]); } catch { /* not a managed site repo */ }
126
+ const id = await createStaging({fileName: name.replace(/[^\w.() -]/g, '_'), kind, by: request.user?.email || ''});
127
+ try {
128
+ const size = await saveUpload(file.file, stagingPath(id, kind === 'zip' ? 'upload.zip' : 'export.xml'), WXR_MAX_BYTES);
129
+ await updateStagingMeta(id, {size});
130
+ return await inspectStaged(id);
131
+ } catch (err) {
132
+ await discardStaged(id);
133
+ if (err.code === 'ETOOBIG') return reply.code(413).send({error: `The export is over ${mb(WXR_MAX_BYTES)}. In WordPress, export the posts and the pages separately and import each.`});
134
+ return fail(reply, err);
135
+ }
136
+ });
137
+
138
+ fastify.post('/staged/:id/uploads', may, async (request, reply) => {
139
+ const {id} = request.params;
140
+ const meta = isId(id) ? await stagingMeta(id) : null;
141
+ if (!meta) return reply.code(404).send({error: 'That upload has gone - upload the export again.'});
142
+ if (runningJob()?.staged === id) return reply.code(409).send({error: 'That import is running.'});
143
+ const file = await request.file({limits: {fileSize: UPLOADS_ZIP_MAX_BYTES + 1, files: 1}}).catch(() => null);
144
+ if (!file) return reply.code(400).send({error: 'Choose the uploads zip.'});
145
+ if (!/\.zip$/i.test(file.filename || '')) {
146
+ file.file.resume();
147
+ return reply.code(400).send({error: 'Zip the wp-content/uploads folder and upload the .zip.'});
148
+ }
149
+ const dest = stagingPath(id, 'uploads.zip');
150
+ try {
151
+ const size = await saveUpload(file.file, dest, UPLOADS_ZIP_MAX_BYTES);
152
+ const zip = await openZip(dest);
153
+ const files = zip.entries.length;
154
+ const unsafe = zip.unsafe.length;
155
+ const sample = zip.entries.slice(0, 3).map(e => zipRel(e.name));
156
+ await zip.close();
157
+ if (!files) throw new Error('That zip has no files in it.');
158
+ return await updateStagingMeta(id, {uploads: {fileName: String(file.filename).slice(0, 200).replace(/[^\w.() -]/g, '_'), size, files, unsafe, sample}});
159
+ } catch (err) {
160
+ fs.rmSync(dest, {force: true});
161
+ await updateStagingMeta(id, {uploads: null});
162
+ if (err.code === 'ETOOBIG') return reply.code(413).send({error: `The zip is over ${mb(UPLOADS_ZIP_MAX_BYTES)}. Zip the uploads a few years at a time and import with each.`});
163
+ return fail(reply, err);
164
+ }
165
+ });
166
+
167
+ fastify.delete('/staged/:id/uploads', may, async (request, reply) => {
168
+ const {id} = request.params;
169
+ if (!isId(id) || !(await stagingMeta(id))) return reply.code(404).send({error: 'That upload has gone.'});
170
+ fs.rmSync(stagingPath(id, 'uploads.zip'), {force: true});
171
+ return updateStagingMeta(id, {uploads: null});
172
+ });
173
+
174
+ fastify.post('/staged/:id/preview', may, async (request, reply) => {
175
+ const {id} = request.params;
176
+ if (!isId(id)) return reply.code(404).send({error: 'That upload has gone.'});
177
+ try {
178
+ const {plan, state, meta} = await previewImport(id, cleanOptions(request.body?.options));
179
+ return {meta, plan: planForScreen(plan, state.blog), redirectsAvailable: state.redirectsAvailable};
180
+ } catch (err) {
181
+ return fail(reply, err);
182
+ }
183
+ });
184
+
185
+ fastify.post('/staged/:id/run', may, async (request, reply) => {
186
+ const {id} = request.params;
187
+ if (!isId(id)) return reply.code(404).send({error: 'That upload has gone.'});
188
+ try {
189
+ const job = await startRun(id, cleanOptions(request.body?.options), request.user, fastify.log);
190
+ return reply.code(202).send({job: {...job}});
191
+ } catch (err) {
192
+ return fail(reply, err);
193
+ }
194
+ });
195
+
196
+ fastify.delete('/staged/:id', may, async (request, reply) => {
197
+ const {id} = request.params;
198
+ if (!isId(id)) return reply.code(404).send({error: 'That upload has gone.'});
199
+ if (runningJob()?.staged === id) return reply.code(409).send({error: 'That import is running.'});
200
+ await discardStaged(id);
201
+ return {ok: true};
202
+ });
203
+
204
+ fastify.get('/jobs/:id', may, async (request, reply) => {
205
+ const job = getJob(String(request.params.id));
206
+ if (!job) return reply.code(404).send({error: 'No such import.'});
207
+ return {job: {...job}};
208
+ });
209
+
210
+ fastify.get('/history', may, async () => ({runs: await listHistory()}));
211
+ fastify.get('/history/:id', may, async (request, reply) => {
212
+ const report = await getReport(String(request.params.id));
213
+ if (!report) return reply.code(404).send({error: 'No such import.'});
214
+ return report;
215
+ });
216
+ fastify.delete('/history/:id', may, async (request, reply) => {
217
+ if (!(await removeHistory(String(request.params.id)))) return reply.code(404).send({error: 'No such import.'});
218
+ return {ok: true};
219
+ });
220
+ }
package/server/server.js CHANGED
@@ -600,6 +600,15 @@ await app.register(toolGate('contacts', contactsRoutes), {prefix: '/api/contacts
600
600
  await app.register(seoPlugin, {enabled: isToolEnabled('seo'), getBaseUrl, rolesService});
601
601
  }
602
602
  await app.register(toolGate('seo', seoRoutes), {prefix: '/api/seo'});
603
+ // Import from WordPress (services/wpImport): its API behind the Tool switch, and - always,
604
+ // whatever the switch - the hook that answers WordPress's query addresses (/?p=12) from what
605
+ // an import made: those redirects are part of the migrated site, not of the Tool.
606
+ {
607
+ const {wordpressImportPlugin} = await import('./services/wpImport/index.js');
608
+ const {wordpressImportRoutes} = await import('./routes/api/wordpress-import.js');
609
+ await app.register(wordpressImportPlugin);
610
+ await app.register(toolGate('wordpress-import', wordpressImportRoutes), {prefix: '/api/import/wordpress'});
611
+ }
603
612
  await app.register(toolsRoutes, {prefix: '/api'});
604
613
  // What plugins show beside a contact or a note (Calendar's Events) - 0.79.
605
614
  await app.register(relatedRoutes, {prefix: '/api/related'});
@@ -643,6 +652,9 @@ const coreToolItems = [
643
652
  for (const item of coreToolItems) {
644
653
  if (isToolEnabled(item.permission)) registerSidebarItem({item: {source: 'Built in', ...item}});
645
654
  }
655
+ if (isToolEnabled('wordpress-import')) {
656
+ registerSidebarItem({item: {source: 'Built in', text: 'Import from WordPress', url: '#/wordpress-import', icon: 'download', permission: 'import', countUrl: '/api/import/wordpress/count', inventory: true}});
657
+ }
646
658
  // Todo's to-dos on the calendar (registered when routes/api/todo.js loads).
647
659
  if (!isToolEnabled('todo')) {
648
660
  const {unregisterCalendarSource} = await import('./services/hooks.js');
@@ -80,7 +80,7 @@ const TOOL_GROUPS = new Map(Object.entries({
80
80
  Marketing: ['#/analytics', '#/seo', '#/plugins/analytics-pro/dashboard', '#/plugins/social', '#/plugins/feedback', '#/plugins/surveys'],
81
81
  Publishing: ['#/plugins/blog', '#/plugins/blog-pro', '#/plugins/docs', '#/plugins/theme-roller'],
82
82
  Business: ['#/plugins/shopping-cart', '#/plugins/booking', '#/plugins/invoice', '#/job-board', '#/plugins/garage'],
83
- Maintenance: ['#/plugins/security', '#/plugins/security-pro', '#/plugins/security-pro/my-2fa', '#/plugins/backup-pro', '#/plugins/data-transfer']
83
+ Maintenance: ['#/plugins/security', '#/plugins/security-pro', '#/plugins/security-pro/my-2fa', '#/plugins/backup-pro', '#/plugins/data-transfer', '#/wordpress-import']
84
84
  }).flatMap(([group, urls]) => urls.map(url => [url, group])));
85
85
 
86
86
  /**
@@ -251,6 +251,16 @@ export const REGISTRY = [
251
251
  {key: 'manage', label: 'Manage', description: 'Change page titles and descriptions, redirects and settings'}
252
252
  ]
253
253
  },
254
+ {
255
+ key: 'import',
256
+ label: 'Import',
257
+ description: 'Bring content in from other systems.',
258
+ icon: 'download',
259
+ group: 'Tools',
260
+ actions: [
261
+ {key: 'wordpress', label: 'From WordPress', description: 'Upload a WordPress export, preview it and import pages, posts, media, menus and redirects'}
262
+ ]
263
+ },
254
264
  {
255
265
  key: 'notes',
256
266
  label: 'Notes',
@@ -477,7 +477,7 @@ export async function grantPermissionOnce(roleName, permission, source) {
477
477
  * a later removal in System > Roles sticks. `contacts.manageGroups` is granted
478
478
  * the same way, separately (server.js).
479
479
  */
480
- export const ADMIN_TOOL_GRANTS = ['notes', 'todo', 'contacts', 'analytics', 'seo', 'calculator'];
480
+ export const ADMIN_TOOL_GRANTS = ['notes', 'todo', 'contacts', 'analytics', 'seo', 'calculator', 'import.wordpress'];
481
481
 
482
482
  /**
483
483
  * Grant the seeded `admin` role the built-in Tool permissions - once each.
@@ -50,7 +50,8 @@ export const CORE_TOOLS = Object.freeze([
50
50
  {name: 'todo', displayName: 'Todo', icon: 'check-square', description: 'To-do lists with due dates and reminders.'},
51
51
  {name: 'calculator', displayName: 'Calculator', icon: 'calculator', description: 'Basic and scientific calculations, on a page or in a floating panel.'},
52
52
  {name: 'analytics', displayName: 'Analytics', icon: 'chart-bar', description: 'Page views and visitors, counted on this server.'},
53
- {name: 'seo', displayName: 'SEO', icon: 'search', description: 'How the site looks to search engines: an audit of every page, redirects, the sitemap and robots.txt.'}
53
+ {name: 'seo', displayName: 'SEO', icon: 'search', description: 'How the site looks to search engines: an audit of every page, redirects, the sitemap and robots.txt.'},
54
+ {name: 'wordpress-import', displayName: 'Import from WordPress', icon: 'download', description: 'Bring a WordPress site across: pages, posts, media, menus, comments and redirects from the old addresses.'}
54
55
  ]);
55
56
 
56
57
  const CORE_NAMES = new Set(CORE_TOOLS.map(t => t.name));