Skip to content

Commit a56125c

Browse files
committed
Probe MDI auto-draft persistence
1 parent c4fbc71 commit a56125c

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

packages/runtime-cloudflare/src/request-routing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type WorkerRequestRoute =
88
| { kind: "editor-probe"; phase: EditorProbePhase }
99
| { kind: "probe"; phase: string }
1010

11-
export type EditorProbePhase = "admin" | "auto-draft" | "block-editor"
11+
export type EditorProbePhase = "admin" | "auto-draft" | "auto-draft-no-persist" | "block-editor"
1212

1313
export function routeWorkerRequest(request: Request): WorkerRequestRoute {
1414
const phase = new URL(request.url).searchParams.get("phase")
@@ -20,6 +20,7 @@ export function routeWorkerRequest(request: Request): WorkerRequestRoute {
2020
if (phase === "operator-reset") return { kind: "operator-reset" }
2121
if (phase === "editor-probe-admin") return { kind: "editor-probe", phase: "admin" }
2222
if (phase === "editor-probe-auto-draft") return { kind: "editor-probe", phase: "auto-draft" }
23+
if (phase === "editor-probe-auto-draft-no-persist") return { kind: "editor-probe", phase: "auto-draft-no-persist" }
2324
if (phase === "editor-probe-block-editor") return { kind: "editor-probe", phase: "block-editor" }
2425
return { kind: "probe", phase }
2526
}

packages/runtime-cloudflare/src/worker.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ async function runCoordinatedEditorProbe(request: Request, env: Env, coordinator
106106
try {
107107
if (!lease.pointer) throw new Error("Canonical WordPress must be initialized before running an editor probe.")
108108
const runtime = await getRuntime(env, lease.pointer, new URL(request.url).origin)
109+
if (phase === "auto-draft-no-persist") patchAutoDraftPersistenceProbe(runtime.php)
109110
patchEditorProbe(runtime.php, phase)
110111
const response = toFetchResponse(request, await runtime.requestHandler.request(await toPHPRequest(request)))
111112
await releaseLease(coordinator, request.url, lease)
@@ -125,6 +126,7 @@ function patchEditorProbe(php: PHP, phase: EditorProbePhase): void {
125126
const markers: Record<EditorProbePhase, string> = {
126127
admin: "require_once __DIR__ . '/admin.php';",
127128
"auto-draft": "$post_ID = $post->ID;",
129+
"auto-draft-no-persist": "$post_ID = $post->ID;",
128130
"block-editor": "require_once ABSPATH . 'wp-admin/admin-footer.php';",
129131
}
130132
const marker = markers[phase]
@@ -135,6 +137,23 @@ function patchEditorProbe(php: PHP, phase: EditorProbePhase): void {
135137
php.writeFile(path, new TextEncoder().encode(`${source.slice(0, insertion)}${stop}${source.slice(insertion)}`))
136138
}
137139

140+
function patchAutoDraftPersistenceProbe(php: PHP): void {
141+
const path = "/wordpress/wp-content/plugins/markdown-database-integration/inc/class-wp-markdown-write-engine.php"
142+
const source = new TextDecoder().decode(php.readFileAsBuffer(path))
143+
const needle = `if ( $id ) {
144+
$this->mark_post_dirty( (int) $id );
145+
}`
146+
const replacement = `if ( $id ) {
147+
$rows = $this->driver->query( "SELECT post_status FROM \`{$this->prefix()}posts\` WHERE ID = " . (int) $id );
148+
if ( ! is_array( $rows ) || empty( $rows ) || 'auto-draft' !== ( $rows[0]->post_status ?? '' ) ) {
149+
$this->mark_post_dirty( (int) $id );
150+
}
151+
}`
152+
const index = source.indexOf(needle)
153+
if (index === -1 || index !== source.lastIndexOf(needle)) throw new Error("MDI auto-draft persistence probe marker is not unique.")
154+
php.writeFile(path, new TextEncoder().encode(`${source.slice(0, index)}${replacement}${source.slice(index + needle.length)}`))
155+
}
156+
138157
interface MarkdownManifestFile {
139158
path: string
140159
objectKey: string

tests/cloudflare-runtime.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ test("Cloudflare routing reserves phases while the phase-less route serves WordP
4545
assert.deepEqual(routeWorkerRequest(new Request("https://worker.example/?phase=operator-reset")), { kind: "operator-reset" })
4646
assert.deepEqual(routeWorkerRequest(new Request("https://worker.example/wp-admin/post-new.php?phase=editor-probe-admin")), { kind: "editor-probe", phase: "admin" })
4747
assert.deepEqual(routeWorkerRequest(new Request("https://worker.example/wp-admin/post-new.php?phase=editor-probe-auto-draft")), { kind: "editor-probe", phase: "auto-draft" })
48+
assert.deepEqual(routeWorkerRequest(new Request("https://worker.example/wp-admin/post-new.php?phase=editor-probe-auto-draft-no-persist")), { kind: "editor-probe", phase: "auto-draft-no-persist" })
4849
assert.deepEqual(routeWorkerRequest(new Request("https://worker.example/wp-admin/post-new.php?phase=editor-probe-block-editor")), { kind: "editor-probe", phase: "block-editor" })
4950
assert.deepEqual(routeWorkerRequest(new Request("https://worker.example/?phase=seeded-wordpress")), { kind: "probe", phase: "seeded-wordpress" })
5051
})
@@ -126,6 +127,7 @@ test("Cloudflare editor probes bound the authenticated editor lifecycle and disc
126127
assert.equal((probe.match(/await discardCachedRuntime\(\)/g) ?? []).length, 2)
127128
assert.match(probe, /\/wordpress\/wp-admin\/post-new\.php/)
128129
assert.match(probe, /memory_get_peak_usage/)
130+
assert.match(probe, /'auto-draft' !== \( \$rows\[0\]->post_status/)
129131
})
130132

131133
test("Cloudflare runtime packages a provenanced canonical MDI seed", async () => {

0 commit comments

Comments
 (0)