@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 4.14 kB
Source Map (JSON)
{"version":3,"file":"use-term-name.mjs","names":["coreStore"],"sources":["../../src/term-name/use-term-name.js"],"sourcesContent":["import { store as coreStore } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Hook to fetch term name based on context or fallback to template parsing.\n *\n * This hook prioritizes context-provided termId and taxonomy, but falls back to\n * template-based detection when no context is available.\n *\n * @param {string|number} termId The term ID from context\n * @param {string} taxonomy The taxonomy name from context\n */\nexport function useTermName( termId, taxonomy ) {\n\t// Get term from context if available.\n\tconst contextBasedTerm = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! termId || ! taxonomy ) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn select( coreStore ).getEntityRecord(\n\t\t\t\t'taxonomy',\n\t\t\t\ttaxonomy,\n\t\t\t\ttermId\n\t\t\t);\n\t\t},\n\t\t[ termId, taxonomy ]\n\t);\n\n\t// Fallback approach: Parse template slug when no context is available.\n\tconst templateBasedTerm = useTemplateBasedTermData();\n\tconst hasContext = Boolean( termId && taxonomy );\n\n\treturn {\n\t\thasContext,\n\t\tterm: hasContext ? contextBasedTerm : templateBasedTerm,\n\t};\n}\n\n/**\n * Fallback hook to fetch term data from template context.\n * Parses the template slug to determine if we're on a specific term archive.\n */\nfunction useTemplateBasedTermData() {\n\tconst templateSlug = useSelect( ( select ) => {\n\t\t// Access core/editor by string to avoid @wordpress/editor dependency.\n\t\t// eslint-disable-next-line @wordpress/data-no-store-string-literals\n\t\tconst { getCurrentPostId, getCurrentPostType, getCurrentTemplateId } =\n\t\t\tselect( 'core/editor' );\n\t\tconst currentPostType = getCurrentPostType();\n\t\tconst templateId =\n\t\t\tgetCurrentTemplateId() ||\n\t\t\t( currentPostType === 'wp_template' ? getCurrentPostId() : null );\n\n\t\treturn templateId\n\t\t\t? select( coreStore ).getEditedEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\ttemplateId\n\t\t\t\t)?.slug\n\t\t\t: null;\n\t}, [] );\n\n\tconst taxonomyMatches = templateSlug?.match(\n\t\t/^(category|tag|taxonomy-([^-]+))$|^(((category|tag)|taxonomy-([^-]+))-(.+))$/\n\t);\n\n\tlet taxonomy;\n\tlet termSlug;\n\n\tif ( taxonomyMatches ) {\n\t\t// If it's for a specific term (e.g., category-news, tag-featured).\n\t\tif ( taxonomyMatches[ 3 ] ) {\n\t\t\ttaxonomy = taxonomyMatches[ 6 ]\n\t\t\t\t? taxonomyMatches[ 6 ]\n\t\t\t\t: taxonomyMatches[ 4 ];\n\t\t\ttermSlug = taxonomyMatches[ 7 ];\n\t\t}\n\n\t\ttaxonomy = taxonomy === 'tag' ? 'post_tag' : taxonomy;\n\t}\n\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! taxonomy || ! termSlug ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst { getEntityRecords } = select( coreStore );\n\n\t\t\tconst termRecords = getEntityRecords( 'taxonomy', taxonomy, {\n\t\t\t\tslug: termSlug,\n\t\t\t\tper_page: 1,\n\t\t\t} );\n\n\t\t\tif ( termRecords && termRecords[ 0 ] ) {\n\t\t\t\treturn termRecords[ 0 ];\n\t\t\t}\n\n\t\t\treturn null;\n\t\t},\n\t\t[ taxonomy, termSlug ]\n\t);\n}\n"],"mappings":";;;;;;;;;;;;AAYA,SAAgB,YAAa,QAAQ,UAAW;CAE/C,MAAM,mBAAmB,WACtB,WAAY;EACb,IAAK,CAAE,UAAU,CAAE,UAClB,OAAO;EAER,OAAO,OAAQA,KAAU,CAAC,CAAC,gBAC1B,YACA,UACA,MACD;CACD,GACA,CAAE,QAAQ,QAAS,CACpB;CAGA,MAAM,oBAAoB,yBAAyB;CACnD,MAAM,aAAa,QAAS,UAAU,QAAS;CAE/C,OAAO;EACN;EACA,MAAM,aAAa,mBAAmB;CACvC;AACD;;;;;AAMA,SAAS,2BAA2B;CAoBnC,M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