import { useSelect } from '@wordpress/data'; import { useMemo } from '@wordpress/element'; import { store as coreStore } from '@wordpress/core-data'; import type { Taxonomy } from '@wordpress/core-data'; import { store as blockEditorStore } from '@wordpress/block-editor'; import { decodeEntities } from '@wordpress/html-entities'; import { __ } from '@wordpress/i18n'; import { cloneBlock, store as blocksStore } from '@wordpress/blocks'; import type { Block, BlockVariation } from '@wordpress/blocks'; import type { IHasNameAndId, OrderByOption, QueryAttributes, QueryBlockPattern, QueryEntitiesInfo, } from './types'; /** * Returns a helper object with mapping from Objects that implement * the `IHasNameAndId` interface. The returned object is used for * integration with `FormTokenField` component. * * @param entities The entities to extract of helper object. * @return The object with the entities information. */ export const getEntitiesInfo = < T extends IHasNameAndId >( entities?: T[] ): QueryEntitiesInfo< T > => { const mapping = entities?.reduce( ( accumulator, entity ) => { const { mapById, mapByName, names } = accumulator; mapById[ entity.id ] = entity; mapByName[ entity.name ] = entity; names.push( entity.name ); return accumulator; }, { mapById: {}, mapByName: {}, names: [] } as Omit< QueryEntitiesInfo< T >, 'entities' > ); return { entities: entities as T[], ...mapping!, }; }; /** * Helper util to return a value from a certain path of the object. * Path is specified as a string of properties, separated by dots, * for example: "parent.child". * * @param object Input object. * @param path Path to the object property. * @return Value of the object property at the specified path. */ export const getValueFromObjectPath = ( object: Record< string, unknown >, path: string ): unknown => { const normalizedPath = path.split( '.' ); let value: unknown = object; normalizedPath.forEach( ( fieldName ) => { value = ( value as Record< string, unknown > | undefined )?.[ fieldName ]; } ); return value; }; /** * Helper util to map records to add a `name` prop from a * provided path, in order to handle all entities in the same * fashion(implementing`IHasNameAndId` interface). * * @param entities The array of entities. * @param path The path to map a `name` property from the entity. * @return An array of entities that now implement the `IHasNameAndId` interface. */ export const mapToIHasNameAndId = < T extends Record< string, unknown > & { id: string | number }, >( entities: T[] | null | undefined, path: string ): ( T & IHasNameAndId )[] => { return ( entities || [] ).map( ( entity ) => ( { ...entity, name: decodeEntities( getValueFromObjectPath( entity, path ) as string ), } ) ); }; /** * Returns a helper object that contains `postTypesSelectOptions`: the post * types that can be queried, as options to be passed to a `SelectControl`, or * `undefined` while they load. * * @return The helper object related to post types. */ export const usePostTypes = () => { const postTypes = useSelect( ( select ) => { const { getPostTypes, hasFinishedResolution } = select( coreStore ); const excludedPostTypes = [ 'attachment' ]; const query = { per_page: -1 }; const records = getPostTypes( query ); // A failed request, such as a 403, finishes without records. return hasFinishedResolution( 'getPostTypes', [ query ] ) ? ( records ?? [] ).filter( ( { viewable, slug } ) => viewable && ! excludedPostTypes.includes( slug ) ) : undefined; }, [] ); const postTypesSelectOptions = useMemo( () => postTypes?.map( ( { labels, slug } ) => ( { label: labels.singular_name, value: slug, } ) ), [ postTypes ] ); return { postTypesSelectOptions }; }; /** * Hook that returns the taxonomies associated with a specific post type. * * @param postType The post type from which to retrieve the associated taxonomies. * @return An array of the associated taxonomies. */ export const useTaxonomies = ( postType: string ): Taxonomy< 'edit' >[] | undefined => { const taxonomies = useSelect( ( select ) => { const { getTaxonomies, getPostType } = select( coreStore ); // Does the post type have taxonomies? if ( getPostType( postType )?.taxonomies?.length! > 0 ) { return getTaxonomies( { type: postType, per_page: -1, } ); } return []; }, [ postType ] ); return useMemo( () => { return taxonomies?.filter( ( { visibility } ) => !! visibility?.publicly_queryable ); }, [ taxonomies ] ); }; /** * Hook that returns whether a specific post type is hierarchical. * * @param postType The post type to check. * @return Whether a specific post type is hierarchical. */ export function useIsPostTypeHierarchical( postType: string ) { return useSelect( ( select ) => { const type = select( coreStore ).getPostType( postType ); return type?.viewable && type?.hierarchical; }, [ postType ] ); } /** * List of available options to order by. * * @param postType The post type to check. * @return List of order options. */ export function useOrderByOptions( postType: string ): OrderByOption[] { const supportsCustomOrder = useSelect( ( select ) => { const type = select( coreStore ).getPostType( postType ); return !! type?.supports?.[ 'page-attributes' ]; }, [ postType ] ); return useMemo( () => { const orderByOptions: OrderByOption[] = [ { label: __( 'Newest to oldest' ), value: 'date/desc', }, { label: __( 'Oldest to newest' ), value: 'date/asc', }, { /* translators: Label for ordering posts by title in ascending order. */ label: __( 'A → Z' ), value: 'title/asc', }, { /* translators: Label for ordering posts by title in descending order. */ label: __( 'Z → A' ), value: 'title/desc', }, ]; if ( supportsCustomOrder ) { orderByOptions.push( { /* translators: Label for ordering posts by ascending menu order. */ label: __( 'Ascending by order' ), value: 'menu_order/asc', }, { /* translators: Label for ordering posts by descending menu order. */ label: __( 'Descending by order' ), value: 'menu_order/desc', } ); } return orderByOptions; }, [ supportsCustomOrder ] ); } /** * Hook that returns the query properties' names defined by the active * block variation, to determine which block's filters to show. * * @param attributes Block attributes. * @return An array of the query attributes. */ export function useAllowedControls( attributes: QueryAttributes ): string[] | undefined { return useSelect( ( select ) => ( select( blocksStore ).getActiveBlockVariation( 'core/query', attributes ) as | ( BlockVariation & { allowedControls?: string[] } ) | undefined )?.allowedControls, [ attributes ] ); } export function isControlAllowed( allowedControls: string[] | undefined, key: string ) { // Every controls is allowed if the list is not defined. if ( ! allowedControls ) { return true; } return allowedControls.includes( key ); } /** * Clones a pattern's blocks and then recurses over that list of blocks, * transforming them to retain some `query` attribute properties. * For now we retain the `postType` and `inherit` properties as they are * fundamental for the expected functionality of the block and don't affect * its design and presentation. * * Returns the cloned/transformed blocks and array of existing Query Loop * client ids for further manipulation, in order to avoid multiple recursions. * * @param blocks The list of blocks to look through and transform(mutate). * @param queryBlockAttributes The existing Query Loop's attributes. * @return An object with the cloned/transformed blocks and all the Query Loop clients from these blocks. */ export const getTransformedBlocksFromPattern = ( blocks: Block[], queryBlockAttributes: QueryAttributes ) => { const { query: { postType, inherit }, namespace, } = queryBlockAttributes; const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) ); const queryClientIds: string[] = []; const blocksQueue = [ ...clonedBlocks ]; while ( blocksQueue.length > 0 ) { const block = blocksQueue.shift()!; if ( block.name === 'core/query' ) { block.attributes.query = { ...( block.attributes.query as Record< string, unknown > ), postType, inherit, }; if ( namespace ) { block.attributes.namespace = namespace; } queryClientIds.push( block.clientId ); } block.innerBlocks?.forEach( ( innerBlock ) => { blocksQueue.push( innerBlock ); } ); } return { newBlocks: clonedBlocks, queryClientIds }; }; /** * Helper hook that determines if there is an active variation of the block * and if there are available specific patterns for this variation. * If there are, these patterns are going to be the only ones suggested to * the user in setup and replace flow, without including the default ones * for Query Loop. * * If there are no such patterns, the default ones for Query Loop are going * to be suggested. * * @param clientId The block's client ID. * @param attributes The block's attributes. * @return The block name to be used in the patterns suggestions. */ export function useBlockNameForPatterns( clientId: string, attributes: QueryAttributes ) { return useSelect( ( select ) => { const activeVariationName = select( blocksStore ).getActiveBlockVariation( 'core/query', attributes )?.name; if ( ! activeVariationName ) { return 'core/query'; } const { getBlockRootClientId, getPatternsByBlockTypes } = select( blockEditorStore ); const rootClientId = getBlockRootClientId( clientId ); const activePatterns = getPatternsByBlockTypes( `core/query/${ activeVariationName }`, rootClientId ); return activePatterns.length > 0 ? `core/query/${ activeVariationName }` : 'core/query'; }, [ clientId, attributes ] ); } /** * Helper hook that determines if there is an active variation of the block * and if there are available specific scoped `block` variations connected with * this variation. * * If there are, these variations are going to be the only ones suggested * to the user in setup flow when clicking to `start blank`, without including * the default ones for Query Loop. * * If there are no such scoped `block` variations, the default ones for Query * Loop are going to be suggested. * * The way we determine such variations is with the convention that they have the `namespace` * attribute defined as an array. This array should contain the names(`name` property) of any * variations they want to be connected to. * For example, if we have a `Query Loop` scoped `inserter` variation with the name `products`, * we can connect a scoped `block` variation by setting its `namespace` attribute to `['products']`. * If the user selects this variation, the `namespace` attribute will be overridden by the * main `inserter` variation. * * @param attributes The block's attributes. * @return The block variations to be suggested in setup flow, when clicking to `start blank`. */ export function useScopedBlockVariations( attributes: QueryAttributes ): BlockVariation[] { const { activeVariationName, blockVariations } = useSelect( ( select ) => { const { getActiveBlockVariation, getBlockVariations } = select( blocksStore ); return { activeVariationName: getActiveBlockVariation( 'core/query', attributes )?.name, blockVariations: getBlockVariations( 'core/query', 'block' ), }; }, [ attributes ] ); const variations = useMemo( () => { // Filter out the variations that have defined a `namespace` attribute, // which means they are 'connected' to specific variations of the block. const isNotConnected = ( variation: BlockVariation ) => ! variation.attributes?.namespace; if ( ! activeVariationName ) { return blockVariations!.filter( isNotConnected ); } const connectedVariations = blockVariations!.filter( ( variation ) => ( variation.attributes?.namespace as string[] | undefined )?.includes( activeVariationName ) ); if ( !! connectedVariations.length ) { return connectedVariations; } return blockVariations!.filter( isNotConnected ); }, [ activeVariationName, blockVariations ] ); return variations; } /** * Hook that returns the block patterns for a specific block type. * * @param clientId The block's client ID. * @param name The block type name. * @return An array of valid block patterns. */ export const usePatterns = ( clientId: string, name: string ): QueryBlockPattern[] => { return useSelect( ( select ) => { const { getBlockRootClientId, getPatternsByBlockTypes } = select( blockEditorStore ); const rootClientId = getBlockRootClientId( clientId ); return getPatternsByBlockTypes( name, rootClientId ) as unknown as QueryBlockPattern[]; }, [ name, clientId ] ); }; /** * Hook that, given a block clientId, returns the titles of the unsupported * blocks it contains, without duplicates. * * @param clientId The block's client ID. * @return The titles of the unsupported blocks. */ export const useUnsupportedBlocks = ( clientId: string ) => { return useSelect( ( select ) => { const { getClientIdsOfDescendants, getBlockName } = select( blockEditorStore ); const { getBlockSupport, getBlockType } = select( blocksStore ); const blockTitles = new Set< string >(); getClientIdsOfDescendants( clientId ).forEach( ( descendantClientId ) => { const blockName = getBlockName( descendantClientId )!; /* * Client side navigation can be true in two states: * - supports.interactivity = true; * - supports.interactivity.clientNavigation = true; */ const blockSupportsInteractivity = Object.is( getBlockSupport( blockName, 'interactivity' ), true ); const blockSupportsInteractivityClientNavigation = getBlockSupport( blockName, 'interactivity.clientNavigation' ); if ( ! blockSupportsInteractivity && ! blockSupportsInteractivityClientNavigation ) { blockTitles.add( getBlockType( blockName )?.title || blockName ); } } ); return [ ...blockTitles ]; }, [ clientId ] ); }; /** * Helper function that returns the query context from the editor based on the * available template slug. * * @param templateSlug Current template slug based on context. * @return An object with isSingular and templateType properties. */ export function getQueryContextFromTemplate( templateSlug?: string ) { // In the Post Editor, the template slug is not available. if ( ! templateSlug ) { return { isSingular: true }; } let isSingular = false; let templateType = templateSlug === 'wp' ? 'custom' : templateSlug; const singularTemplates = [ '404', 'blank', 'single', 'page', 'custom' ]; const templateTypeFromSlug = templateSlug.includes( '-' ) ? templateSlug.split( '-', 1 )[ 0 ] : templateSlug; const queryFromTemplateSlug = templateSlug.includes( '-' ) ? templateSlug.split( '-' ).slice( 1 ).join( '-' ) : ''; if ( queryFromTemplateSlug ) { templateType = templateTypeFromSlug; } isSingular = singularTemplates.includes( templateType ); return { isSingular, templateType }; }