UNPKG

@wordpress/block-library

Version:
67 lines (66 loc) • 2.38 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_core_data = require("@wordpress/core-data"); let _wordpress_data = require("@wordpress/data"); // packages/block-library/src/query-title/use-archive-label.js function useArchiveLabel() { const templateSlug = (0, _wordpress_data.useSelect)((select) => { const { getCurrentPostId, getCurrentPostType, getCurrentTemplateId } = select("core/editor"); const currentPostType = getCurrentPostType(); const templateId = getCurrentTemplateId() || (currentPostType === "wp_template" ? getCurrentPostId() : null); return templateId ? select(_wordpress_core_data.store).getEditedEntityRecord("postType", "wp_template", templateId)?.slug : null; }, []); const taxonomyMatches = templateSlug?.match(/^(category|tag|taxonomy-([^-]+))$|^(((category|tag)|taxonomy-([^-]+))-(.+))$/); let taxonomy; let term; let isAuthor = false; let authorSlug; if (taxonomyMatches) { if (taxonomyMatches[1]) taxonomy = taxonomyMatches[2] ? taxonomyMatches[2] : taxonomyMatches[1]; else if (taxonomyMatches[3]) { taxonomy = taxonomyMatches[6] ? taxonomyMatches[6] : taxonomyMatches[4]; term = taxonomyMatches[7]; } taxonomy = taxonomy === "tag" ? "post_tag" : taxonomy; } else { const authorMatches = templateSlug?.match(/^(author)$|^author-(.+)$/); if (authorMatches) { isAuthor = true; if (authorMatches[2]) authorSlug = authorMatches[2]; } } return (0, _wordpress_data.useSelect)((select) => { const { getEntityRecords, getTaxonomy, getAuthors } = select(_wordpress_core_data.store); let archiveTypeLabel; let archiveNameLabel; if (taxonomy) archiveTypeLabel = getTaxonomy(taxonomy)?.labels?.singular_name; if (term) { const records = getEntityRecords("taxonomy", taxonomy, { slug: term, per_page: 1 }); if (records && records[0]) archiveNameLabel = records[0].name; } if (isAuthor) { archiveTypeLabel = "Author"; if (authorSlug) { const authorRecords = getAuthors({ slug: authorSlug }); if (authorRecords && authorRecords[0]) archiveNameLabel = authorRecords[0].name; } } return { archiveTypeLabel, archiveNameLabel }; }, [ authorSlug, isAuthor, taxonomy, term ]); } exports.useArchiveLabel = useArchiveLabel; //# sourceMappingURL=use-archive-label.cjs.map