| 1 |
import { useSelect } from "@wordpress/data"; |
| 2 |
import { store as blockEditorStore } from "@wordpress/block-editor"; |
| 3 |
|
| 4 |
const useBlockLocation = (clientId) => { |
| 5 |
return useSelect( |
| 6 |
(select) => { |
| 7 |
const { getBlockParentsByBlockName, getBlock } = select(blockEditorStore); |
| 8 |
const editPost = select("core/edit-post"); |
| 9 |
const editSite = select("core/edit-site"); |
| 10 |
let editorType = "unknown"; |
| 11 |
let postType = null; |
| 12 |
let templateId = null; |
| 13 |
let areaName = null; |
| 14 |
// Determine editor context |
| 15 |
if (editPost?.getCurrentPostType) { |
| 16 |
editorType = "post-editor"; |
| 17 |
postType = editPost.getCurrentPostType(); |
| 18 |
} else if (editSite?.getEditedPostId) { |
| 19 |
editorType = "site-editor"; |
| 20 |
templateId = editSite.getEditedPostId(); |
| 21 |
postType = editSite.getEditedPostType?.() || ""; |
| 22 |
} |
| 23 |
const coreParents = getBlockParentsByBlockName(clientId, "core/block"); |
| 24 |
if (coreParents.length > 0) { |
| 25 |
const parentBlock = getBlock(coreParents[coreParents.length - 1]); |
| 26 |
areaName = parentBlock?.attributes?.ref || "unknown"; |
| 27 |
return { |
| 28 |
location: "wp_block", |
| 29 |
editor: editorType, |
| 30 |
areaName, |
| 31 |
blockType: "wp_block", |
| 32 |
}; |
| 33 |
} |
| 34 |
// Check for widget area |
| 35 |
const widgetParents = getBlockParentsByBlockName(clientId, "core/widget-area"); |
| 36 |
if (widgetParents.length > 0) { |
| 37 |
const parentBlock = getBlock(widgetParents[widgetParents.length - 1]); |
| 38 |
return { |
| 39 |
location: "widget-area", |
| 40 |
editor: editorType, |
| 41 |
areaName: parentBlock?.attributes?.id || "unknown", |
| 42 |
templateId, |
| 43 |
postType, |
| 44 |
blockType: "widget-area", |
| 45 |
}; |
| 46 |
} |
| 47 |
|
| 48 |
// Check for template part |
| 49 |
const templatePartParents = getBlockParentsByBlockName(clientId, "core/template-part"); |
| 50 |
if (templatePartParents.length > 0) { |
| 51 |
const parentBlock = getBlock(templatePartParents[templatePartParents.length - 1]); |
| 52 |
areaName = parentBlock?.attributes?.area || parentBlock?.attributes?.slug || "unknown"; |
| 53 |
|
| 54 |
return { |
| 55 |
location: "wp_template_part", |
| 56 |
editor: editorType, |
| 57 |
areaName, |
| 58 |
templateId: areaName, // Use area name as template ID for template parts |
| 59 |
postType: "wp_template_part", |
| 60 |
blockType: "template-part", |
| 61 |
}; |
| 62 |
} |
| 63 |
|
| 64 |
// Check for template |
| 65 |
const templateParents = getBlockParentsByBlockName(clientId, "core/template"); |
| 66 |
if (templateParents.length > 0) { |
| 67 |
return { |
| 68 |
location: "wp_template", |
| 69 |
editor: editorType, |
| 70 |
templateId, |
| 71 |
postType: "wp_template", |
| 72 |
blockType: "template", |
| 73 |
}; |
| 74 |
} |
| 75 |
|
| 76 |
if (editSite?.getEditedPostId) { |
| 77 |
editorType = "site-editor"; |
| 78 |
templateId = editSite.getEditedPostId(); |
| 79 |
postType = editSite.getEditedPostType?.() || ""; |
| 80 |
return { |
| 81 |
location: "wp_template", |
| 82 |
editor: editorType, |
| 83 |
postType, |
| 84 |
templateId, |
| 85 |
blockType: "site-editor", |
| 86 |
}; |
| 87 |
} |
| 88 |
|
| 89 |
// Default: Post content |
| 90 |
return { |
| 91 |
location: "post-content", |
| 92 |
editor: editorType, |
| 93 |
postType, |
| 94 |
templateId, |
| 95 |
blockType: "post-content", |
| 96 |
}; |
| 97 |
}, |
| 98 |
[clientId] |
| 99 |
); |
| 100 |
}; |
| 101 |
|
| 102 |
export default useBlockLocation; |
| 103 |
|