forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query Title block: Rely on the editor store to apply the right archiv…
…e title placeholder (WordPress#63478) Co-authored-by: youknowriad <[email protected]> Co-authored-by: jameskoster <[email protected]> Co-authored-by: ntsekouras <[email protected]>
- Loading branch information
1 parent
48830b6
commit 9d30696
Showing
4 changed files
with
124 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/block-library/src/query-title/use-archive-label.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
export function useArchiveLabel() { | ||
const templateSlug = useSelect( ( select ) => { | ||
// @wordpress/block-library should not depend on @wordpress/editor. | ||
// Blocks can be loaded into a *non-post* block editor, so to avoid | ||
// declaring @wordpress/editor as a dependency, we must access its | ||
// store by string. | ||
// The solution here is to split WP specific blocks from generic blocks. | ||
// eslint-disable-next-line @wordpress/data-no-store-string-literals | ||
const { getCurrentPostId, getCurrentPostType, getCurrentTemplateId } = | ||
select( 'core/editor' ); | ||
const currentPostType = getCurrentPostType(); | ||
const templateId = | ||
getCurrentTemplateId() || | ||
( currentPostType === 'wp_template' ? getCurrentPostId() : null ); | ||
|
||
return templateId | ||
? select( coreStore ).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 is for a all taxonomies of a type | ||
if ( taxonomyMatches[ 1 ] ) { | ||
taxonomy = taxonomyMatches[ 2 ] | ||
? taxonomyMatches[ 2 ] | ||
: taxonomyMatches[ 1 ]; | ||
} | ||
// If is for a all taxonomies of a type | ||
else if ( taxonomyMatches[ 3 ] ) { | ||
taxonomy = taxonomyMatches[ 6 ] | ||
? taxonomyMatches[ 6 ] | ||
: taxonomyMatches[ 4 ]; | ||
term = taxonomyMatches[ 7 ]; | ||
} | ||
taxonomy = taxonomy === 'tag' ? 'post_tag' : taxonomy; | ||
|
||
//getTaxonomy( 'category' ); | ||
//wp.data.select('core').getEntityRecords( 'taxonomy', 'category', {slug: 'newcat'} ); | ||
} else { | ||
const authorMatches = templateSlug?.match( /^(author)$|^author-(.+)$/ ); | ||
if ( authorMatches ) { | ||
isAuthor = true; | ||
if ( authorMatches[ 2 ] ) { | ||
authorSlug = authorMatches[ 2 ]; | ||
} | ||
} | ||
} | ||
return useSelect( | ||
( select ) => { | ||
const { getEntityRecords, getTaxonomy, getAuthors } = | ||
select( coreStore ); | ||
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 ] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters