@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 4.55 kB
Source Map (JSON)
{"version":3,"file":"use-archive-label.cjs","names":["useSelect","coreStore"],"sources":["../../src/query-title/use-archive-label.js"],"sourcesContent":["import { store as coreStore } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\n\nexport function useArchiveLabel() {\n\tconst templateSlug = useSelect( ( select ) => {\n\t\t// @wordpress/block-library should not depend on @wordpress/editor.\n\t\t// Blocks can be loaded into a *non-post* block editor, so to avoid\n\t\t// declaring @wordpress/editor as a dependency, we must access its\n\t\t// store by string.\n\t\t// The solution here is to split WP specific blocks from generic blocks.\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\tconst taxonomyMatches = templateSlug?.match(\n\t\t/^(category|tag|taxonomy-([^-]+))$|^(((category|tag)|taxonomy-([^-]+))-(.+))$/\n\t);\n\tlet taxonomy;\n\tlet term;\n\tlet isAuthor = false;\n\tlet authorSlug;\n\tif ( taxonomyMatches ) {\n\t\t// If is for a all taxonomies of a type\n\t\tif ( taxonomyMatches[ 1 ] ) {\n\t\t\ttaxonomy = taxonomyMatches[ 2 ]\n\t\t\t\t? taxonomyMatches[ 2 ]\n\t\t\t\t: taxonomyMatches[ 1 ];\n\t\t}\n\t\t// If is for a all taxonomies of a type\n\t\telse if ( 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\tterm = taxonomyMatches[ 7 ];\n\t\t}\n\t\ttaxonomy = taxonomy === 'tag' ? 'post_tag' : taxonomy;\n\n\t\t//getTaxonomy( 'category' );\n\t\t//wp.data.select('core').getEntityRecords( 'taxonomy', 'category', {slug: 'newcat'} );\n\t} else {\n\t\tconst authorMatches = templateSlug?.match( /^(author)$|^author-(.+)$/ );\n\t\tif ( authorMatches ) {\n\t\t\tisAuthor = true;\n\t\t\tif ( authorMatches[ 2 ] ) {\n\t\t\t\tauthorSlug = authorMatches[ 2 ];\n\t\t\t}\n\t\t}\n\t}\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecords, getTaxonomy, getAuthors } =\n\t\t\t\tselect( coreStore );\n\t\t\tlet archiveTypeLabel;\n\t\t\tlet archiveNameLabel;\n\t\t\tif ( taxonomy ) {\n\t\t\t\tarchiveTypeLabel =\n\t\t\t\t\tgetTaxonomy( taxonomy )?.labels?.singular_name;\n\t\t\t}\n\t\t\tif ( term ) {\n\t\t\t\tconst records = getEntityRecords( 'taxonomy', taxonomy, {\n\t\t\t\t\tslug: term,\n\t\t\t\t\tper_page: 1,\n\t\t\t\t} );\n\t\t\t\tif ( records && records[ 0 ] ) {\n\t\t\t\t\tarchiveNameLabel = records[ 0 ].name;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif ( isAuthor ) {\n\t\t\t\tarchiveTypeLabel = 'Author';\n\t\t\t\tif ( authorSlug ) {\n\t\t\t\t\tconst authorRecords = getAuthors( { slug: authorSlug } );\n\t\t\t\t\tif ( authorRecords && authorRecords[ 0 ] ) {\n\t\t\t\t\t\tarchiveNameLabel = authorRecords[ 0 ].name;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tarchiveTypeLabel,\n\t\t\t\tarchiveNameLabel,\n\t\t\t};\n\t\t},\n\t\t[ authorSlug, isAuthor, taxonomy, term ]\n\t);\n}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