@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
226 lines (224 loc) • 8.1 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/core-data/src/private-selectors.ts
var private_selectors_exports = {};
__export(private_selectors_exports, {
getBlockPatternsForPostType: () => getBlockPatternsForPostType,
getEditorAssets: () => getEditorAssets,
getEditorSettings: () => getEditorSettings,
getEntityRecordPermissions: () => getEntityRecordPermissions,
getEntityRecordsPermissions: () => getEntityRecordsPermissions,
getHomePage: () => getHomePage,
getNavigationFallbackId: () => getNavigationFallbackId,
getPostsPageId: () => getPostsPageId,
getRegisteredPostMeta: () => getRegisteredPostMeta,
getTemplateId: () => getTemplateId,
getUndoManager: () => getUndoManager
});
module.exports = __toCommonJS(private_selectors_exports);
var import_data = require("@wordpress/data");
var import_selectors = require("./selectors");
var import_name = require("./name");
var import_lock_unlock = require("./lock-unlock");
var import_sync = require("./sync");
var import_log_entity_deprecation = __toESM(require("./utils/log-entity-deprecation"));
function getUndoManager(state) {
if (window.__experimentalEnableSync) {
if (globalThis.IS_GUTENBERG_PLUGIN) {
return (0, import_sync.getSyncManager)()?.undoManager ?? state.undoManager;
}
}
return state.undoManager;
}
function getNavigationFallbackId(state) {
return state.navigationFallbackId;
}
var getBlockPatternsForPostType = (0, import_data.createRegistrySelector)(
(select) => (0, import_data.createSelector)(
(state, postType) => select(import_name.STORE_NAME).getBlockPatterns().filter(
({ postTypes }) => !postTypes || Array.isArray(postTypes) && postTypes.includes(postType)
),
() => [select(import_name.STORE_NAME).getBlockPatterns()]
)
);
var getEntityRecordsPermissions = (0, import_data.createRegistrySelector)(
(select) => (0, import_data.createSelector)(
(state, kind, name, ids) => {
const normalizedIds = Array.isArray(ids) ? ids : [ids];
return normalizedIds.map((id) => ({
delete: select(import_name.STORE_NAME).canUser("delete", {
kind,
name,
id
}),
update: select(import_name.STORE_NAME).canUser("update", {
kind,
name,
id
})
}));
},
(state) => [state.userPermissions]
)
);
function getEntityRecordPermissions(state, kind, name, id) {
(0, import_log_entity_deprecation.default)(kind, name, "getEntityRecordPermissions");
return getEntityRecordsPermissions(state, kind, name, id)[0];
}
function getRegisteredPostMeta(state, postType) {
return state.registeredPostMeta?.[postType] ?? {};
}
function normalizePageId(value) {
if (!value || !["number", "string"].includes(typeof value)) {
return null;
}
if (Number(value) === 0) {
return null;
}
return value.toString();
}
var getHomePage = (0, import_data.createRegistrySelector)(
(select) => (0, import_data.createSelector)(
() => {
const siteData = select(import_name.STORE_NAME).getEntityRecord(
"root",
"__unstableBase"
);
if (!siteData) {
return null;
}
const homepageId = siteData?.show_on_front === "page" ? normalizePageId(siteData.page_on_front) : null;
if (homepageId) {
return { postType: "page", postId: homepageId };
}
const frontPageTemplateId = select(
import_name.STORE_NAME
).getDefaultTemplateId({
slug: "front-page"
});
if (!frontPageTemplateId) {
return null;
}
return { postType: "wp_template", postId: frontPageTemplateId };
},
(state) => [
// Even though getDefaultTemplateId.shouldInvalidate returns true when root/site changes,
// it doesn't seem to invalidate this cache, I'm not sure why.
(0, import_selectors.getEntityRecord)(state, "root", "site"),
(0, import_selectors.getEntityRecord)(state, "root", "__unstableBase"),
(0, import_selectors.getDefaultTemplateId)(state, {
slug: "front-page"
})
]
)
);
var getPostsPageId = (0, import_data.createRegistrySelector)((select) => () => {
const siteData = select(import_name.STORE_NAME).getEntityRecord(
"root",
"__unstableBase"
);
return siteData?.show_on_front === "page" ? normalizePageId(siteData.page_for_posts) : null;
});
var getTemplateId = (0, import_data.createRegistrySelector)(
(select) => (state, postType, postId) => {
const homepage = (0, import_lock_unlock.unlock)(select(import_name.STORE_NAME)).getHomePage();
if (!homepage) {
return;
}
if (postType === "page" && postType === homepage?.postType && postId.toString() === homepage?.postId) {
const templates = select(import_name.STORE_NAME).getEntityRecords(
"postType",
"wp_template",
{
per_page: -1
}
);
if (!templates) {
return;
}
const id = templates.find(({ slug }) => slug === "front-page")?.id;
if (id) {
return id;
}
}
const editedEntity = select(import_name.STORE_NAME).getEditedEntityRecord(
"postType",
postType,
postId
);
if (!editedEntity) {
return;
}
const postsPageId = (0, import_lock_unlock.unlock)(select(import_name.STORE_NAME)).getPostsPageId();
if (postType === "page" && postsPageId === postId.toString()) {
return select(import_name.STORE_NAME).getDefaultTemplateId({
slug: "home"
});
}
const currentTemplateSlug = editedEntity.template;
if (currentTemplateSlug) {
const currentTemplate = select(import_name.STORE_NAME).getEntityRecords("postType", "wp_template", {
per_page: -1
})?.find(({ slug }) => slug === currentTemplateSlug);
if (currentTemplate) {
return currentTemplate.id;
}
}
let slugToCheck;
if (editedEntity.slug) {
slugToCheck = postType === "page" ? `${postType}-${editedEntity.slug}` : `single-${postType}-${editedEntity.slug}`;
} else {
slugToCheck = postType === "page" ? "page" : `single-${postType}`;
}
return select(import_name.STORE_NAME).getDefaultTemplateId({
slug: slugToCheck
});
}
);
function getEditorSettings(state) {
return state.editorSettings;
}
function getEditorAssets(state) {
return state.editorAssets;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getBlockPatternsForPostType,
getEditorAssets,
getEditorSettings,
getEntityRecordPermissions,
getEntityRecordsPermissions,
getHomePage,
getNavigationFallbackId,
getPostsPageId,
getRegisteredPostMeta,
getTemplateId,
getUndoManager
});
//# sourceMappingURL=private-selectors.js.map