Skip to content

Commit bc76c3b

Browse files
author
Smart Cloud Solutions Inc.
committed
Fix WordPress 7.0 form sync and design token selectors
1 parent f27b2ba commit bc76c3b

14 files changed

Lines changed: 156 additions & 55 deletions

File tree

blocks/dist/editor.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'react-dom', 'react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'ced11c44c28da7562d3b');
1+
<?php return array('dependencies' => array('react', 'react-dom', 'react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'e7e4dac274f398676ff4');

blocks/dist/editor.css

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks/dist/editor.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks/dist/form/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "https://schemas.wp.org/trunk/block.json",
33
"apiVersion": 3,
44
"name": "smartcloud-flow/form",
5-
"version": "1.1.0",
5+
"version": "1.1.1",
66
"title": "Flow Form",
77
"category": "smartcloud-flow",
88
"icon": "feedback",

blocks/dist/view.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('jquery', 'react', 'react-dom', 'react-jsx-runtime', 'wp-data', 'wp-i18n'), 'version' => '1354a70c41787ea0aa00');
1+
<?php return array('dependencies' => array('jquery', 'react', 'react-dom', 'react-jsx-runtime', 'wp-data', 'wp-i18n'), 'version' => '18d3a3c1d901ad56c8f4');

blocks/dist/view.css

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@smart-cloud/flow-blocks",
33
"private": true,
4-
"version": "1.1.0",
4+
"version": "1.1.1",
55
"type": "module",
66
"license": "ISC",
77
"scripts": {

blocks/src/form/block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "https://schemas.wp.org/trunk/block.json",
33
"apiVersion": 3,
44
"name": "smartcloud-flow/form",
5-
"version": "1.1.0",
5+
"version": "1.1.1",
66
"title": "Flow Form",
77
"category": "smartcloud-flow",
88
"icon": "feedback",

blocks/src/form/edit.tsx

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,38 @@ const EMPTY_FORM_ACTION: EditableFormAction = {
8282
enabled: true,
8383
};
8484

85+
type EditorPostRecord = {
86+
id?: unknown;
87+
wp_id?: unknown;
88+
status?: unknown;
89+
type?: unknown;
90+
};
91+
92+
type EditorPostContext = {
93+
postId?: number;
94+
postType?: string;
95+
postStatus?: string;
96+
};
97+
98+
function resolveNumericEditorEntityId(value: unknown): number | undefined {
99+
if (typeof value === "number" && Number.isFinite(value)) {
100+
return value;
101+
}
102+
103+
if (typeof value === "string") {
104+
const trimmed = value.trim();
105+
if (/^\d+$/.test(trimmed)) {
106+
return Number(trimmed);
107+
}
108+
}
109+
110+
return undefined;
111+
}
112+
113+
function resolveEditorString(value: unknown): string | undefined {
114+
return typeof value === "string" && value.trim() ? value : undefined;
115+
}
116+
85117
type FlowBlockInstance = ReturnType<typeof createBlock>;
86118

87119
function normalizeActionKey(value: string): string {
@@ -677,13 +709,31 @@ export default function Edit({
677709
...EMPTY_FORM_ACTION,
678710
});
679711

680-
// Get current post ID
681-
const postId = useSelect((select) => {
712+
// Resolve the current edited entity metadata in a way that also works in the
713+
// Site Editor, where getCurrentPostId() may be a template slug and the
714+
// numeric entity ID lives on currentPost.wp_id.
715+
const editorPostContext = useSelect((select) => {
682716
const editorSelect = select(editorStore) as unknown as {
683-
getCurrentPostId: () => string | number;
717+
getCurrentPostId: () => string | number | null;
718+
getCurrentPost: () => EditorPostRecord | undefined;
719+
getCurrentPostType: () => string | undefined;
720+
getEditedPostAttribute: (attributeName: string) => unknown;
684721
};
685-
const id = editorSelect.getCurrentPostId();
686-
return typeof id === "number" ? id : parseInt(String(id), 10);
722+
const currentPost = editorSelect.getCurrentPost?.();
723+
const postId =
724+
resolveNumericEditorEntityId(editorSelect.getCurrentPostId()) ||
725+
resolveNumericEditorEntityId(currentPost?.id) ||
726+
resolveNumericEditorEntityId(currentPost?.wp_id);
727+
728+
return {
729+
postId,
730+
postType:
731+
resolveEditorString(editorSelect.getCurrentPostType?.()) ||
732+
resolveEditorString(currentPost?.type),
733+
postStatus:
734+
resolveEditorString(editorSelect.getEditedPostAttribute?.("status")) ||
735+
resolveEditorString(currentPost?.status),
736+
} satisfies EditorPostContext;
687737
}, []);
688738

689739
// Get backend sync settings
@@ -1035,7 +1085,9 @@ export default function Edit({
10351085

10361086
// Use backend sync hook
10371087
const { syncStatus, performSync } = useFormSync({
1038-
postId,
1088+
postId: editorPostContext.postId,
1089+
postType: editorPostContext.postType,
1090+
postStatus: editorPostContext.postStatus,
10391091
enabled: backendSyncEnabled,
10401092
formAttributes: attributes,
10411093
fields,
@@ -1392,10 +1444,23 @@ export default function Edit({
13921444
</Notice>
13931445
)}
13941446

1447+
{backendSyncEnabled && !editorPostContext.postId && (
1448+
<Notice status="warning" isDismissible={false}>
1449+
<p style={{ margin: 0, fontSize: "13px" }}>
1450+
{__(
1451+
"Backend sync is waiting for the current post or template part to expose a numeric entity ID.",
1452+
TEXT_DOMAIN,
1453+
)}
1454+
</p>
1455+
</Notice>
1456+
)}
1457+
13951458
<Button
13961459
variant="secondary"
13971460
onClick={performSync}
1398-
disabled={syncStatus.status === "syncing"}
1461+
disabled={
1462+
syncStatus.status === "syncing" || !editorPostContext.postId
1463+
}
13991464
>
14001465
{syncStatus.status === "syncing"
14011466
? __("Syncing...", TEXT_DOMAIN)

blocks/src/form/sync-meta-api.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,22 @@ export interface FormSyncMetadata {
1313
sourceKind: "post" | "pattern" | "reusable_block";
1414
}
1515

16+
function assertValidPostId(postId: number): number {
17+
if (!Number.isFinite(postId)) {
18+
throw new Error(
19+
"Cannot manage form sync metadata without a numeric post ID.",
20+
);
21+
}
22+
23+
return postId;
24+
}
25+
1626
export async function getFormSyncMeta(
1727
postId: number,
1828
): Promise<FormSyncMetadata> {
29+
const resolvedPostId = assertValidPostId(postId);
1930
const response = await fetch(
20-
`/wp-json/smartcloud-flow/v1/forms/${postId}/sync-meta`,
31+
`/wp-json/smartcloud-flow/v1/forms/${resolvedPostId}/sync-meta`,
2132
{
2233
method: "GET",
2334
headers: {
@@ -40,8 +51,9 @@ export async function updateFormSyncMeta(
4051
postId: number,
4152
updates: Partial<FormSyncMetadata>,
4253
): Promise<FormSyncMetadata> {
54+
const resolvedPostId = assertValidPostId(postId);
4355
const response = await fetch(
44-
`/wp-json/smartcloud-flow/v1/forms/${postId}/sync-meta`,
56+
`/wp-json/smartcloud-flow/v1/forms/${resolvedPostId}/sync-meta`,
4557
{
4658
method: "POST",
4759
headers: {

0 commit comments

Comments
 (0)