UNPKG

@wordpress/core-data

Version:
207 lines (198 loc) 7.29 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getBlockPatternsForPostType = void 0; exports.getEntityRecordPermissions = getEntityRecordPermissions; exports.getHomePage = exports.getEntityRecordsPermissions = void 0; exports.getNavigationFallbackId = getNavigationFallbackId; exports.getPostsPageId = void 0; exports.getRegisteredPostMeta = getRegisteredPostMeta; exports.getTemplateId = void 0; exports.getUndoManager = getUndoManager; var _data = require("@wordpress/data"); var _selectors = require("./selectors"); var _name = require("./name"); var _lockUnlock = require("./lock-unlock"); var _logEntityDeprecation = _interopRequireDefault(require("./utils/log-entity-deprecation")); /** * WordPress dependencies */ /** * Internal dependencies */ /** * Returns the previous edit from the current undo offset * for the entity records edits history, if any. * * @param state State tree. * * @return The undo manager. */ function getUndoManager(state) { return state.undoManager; } /** * Retrieve the fallback Navigation. * * @param state Data state. * @return The ID for the fallback Navigation post. */ function getNavigationFallbackId(state) { return state.navigationFallbackId; } const getBlockPatternsForPostType = exports.getBlockPatternsForPostType = (0, _data.createRegistrySelector)(select => (0, _data.createSelector)((state, postType) => select(_name.STORE_NAME).getBlockPatterns().filter(({ postTypes }) => !postTypes || Array.isArray(postTypes) && postTypes.includes(postType)), () => [select(_name.STORE_NAME).getBlockPatterns()])); /** * Returns the entity records permissions for the given entity record ids. */ const getEntityRecordsPermissions = exports.getEntityRecordsPermissions = (0, _data.createRegistrySelector)(select => (0, _data.createSelector)((state, kind, name, ids) => { const normalizedIds = Array.isArray(ids) ? ids : [ids]; return normalizedIds.map(id => ({ delete: select(_name.STORE_NAME).canUser('delete', { kind, name, id }), update: select(_name.STORE_NAME).canUser('update', { kind, name, id }) })); }, state => [state.userPermissions])); /** * Returns the entity record permissions for the given entity record id. * * @param state Data state. * @param kind Entity kind. * @param name Entity name. * @param id Entity record id. * * @return The entity record permissions. */ function getEntityRecordPermissions(state, kind, name, id) { (0, _logEntityDeprecation.default)(kind, name, 'getEntityRecordPermissions'); return getEntityRecordsPermissions(state, kind, name, id)[0]; } /** * Returns the registered post meta fields for a given post type. * * @param state Data state. * @param postType Post type. * * @return Registered post meta fields. */ function getRegisteredPostMeta(state, postType) { var _state$registeredPost; return (_state$registeredPost = state.registeredPostMeta?.[postType]) !== null && _state$registeredPost !== void 0 ? _state$registeredPost : {}; } function normalizePageId(value) { if (!value || !['number', 'string'].includes(typeof value)) { return null; } // We also need to check if it's not zero (`'0'`). if (Number(value) === 0) { return null; } return value.toString(); } const getHomePage = exports.getHomePage = (0, _data.createRegistrySelector)(select => (0, _data.createSelector)(() => { const siteData = select(_name.STORE_NAME).getEntityRecord('root', '__unstableBase'); // Still resolving getEntityRecord. 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(_name.STORE_NAME).getDefaultTemplateId({ slug: 'front-page' }); // Still resolving getDefaultTemplateId. if (!frontPageTemplateId) { return null; } return { postType: 'wp_template', postId: frontPageTemplateId }; }, state => [(0, _selectors.getEntityRecord)(state, 'root', '__unstableBase'), (0, _selectors.getDefaultTemplateId)(state, { slug: 'front-page' })])); const getPostsPageId = exports.getPostsPageId = (0, _data.createRegistrySelector)(select => () => { const siteData = select(_name.STORE_NAME).getEntityRecord('root', '__unstableBase'); return siteData?.show_on_front === 'page' ? normalizePageId(siteData.page_for_posts) : null; }); const getTemplateId = exports.getTemplateId = (0, _data.createRegistrySelector)(select => (state, postType, postId) => { const homepage = (0, _lockUnlock.unlock)(select(_name.STORE_NAME)).getHomePage(); if (!homepage) { return; } // For the front page, we always use the front page template if existing. if (postType === 'page' && postType === homepage?.postType && postId.toString() === homepage?.postId) { // The /lookup endpoint cannot currently handle a lookup // when a page is set as the front page, so specifically in // that case, we want to check if there is a front page // template, and instead of falling back to the home // template, we want to fall back to the page template. const templates = select(_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; } // If no front page template is found, continue with the // logic below (fetching the page template). } const editedEntity = select(_name.STORE_NAME).getEditedEntityRecord('postType', postType, postId); if (!editedEntity) { return; } const postsPageId = (0, _lockUnlock.unlock)(select(_name.STORE_NAME)).getPostsPageId(); // Check if the current page is the posts page. if (postType === 'page' && postsPageId === postId.toString()) { return select(_name.STORE_NAME).getDefaultTemplateId({ slug: 'home' }); } // First see if the post/page has an assigned template and fetch it. const currentTemplateSlug = editedEntity.template; if (currentTemplateSlug) { const currentTemplate = select(_name.STORE_NAME).getEntityRecords('postType', 'wp_template', { per_page: -1 })?.find(({ slug }) => slug === currentTemplateSlug); if (currentTemplate) { return currentTemplate.id; } } // If no template is assigned, use the default template. let slugToCheck; // In `draft` status we might not have a slug available, so we use the `single` // post type templates slug(ex page, single-post, single-product etc..). // Pages do not need the `single` prefix in the slug to be prioritized // through template hierarchy. if (editedEntity.slug) { slugToCheck = postType === 'page' ? `${postType}-${editedEntity.slug}` : `single-${postType}-${editedEntity.slug}`; } else { slugToCheck = postType === 'page' ? 'page' : `single-${postType}`; } return select(_name.STORE_NAME).getDefaultTemplateId({ slug: slugToCheck }); }); //# sourceMappingURL=private-selectors.js.map