diff --git a/packages/edit-site/src/components/add-new-template/index.js b/packages/edit-site/src/components/add-new-template/index.js
index ffdf1b89376903..1f1e855ccf38d3 100644
--- a/packages/edit-site/src/components/add-new-template/index.js
+++ b/packages/edit-site/src/components/add-new-template/index.js
@@ -8,9 +8,10 @@ import { store as coreStore } from '@wordpress/core-data';
* Internal dependencies
*/
import NewTemplate from './new-template';
+import { TEMPLATE_POST_TYPE } from '../../utils/constants';
export default function AddNewTemplate( {
- templateType = 'wp_template',
+ templateType = TEMPLATE_POST_TYPE,
...props
} ) {
const postType = useSelect(
@@ -22,7 +23,7 @@ export default function AddNewTemplate( {
return null;
}
- if ( templateType === 'wp_template' ) {
+ if ( templateType === TEMPLATE_POST_TYPE ) {
return ;
}
diff --git a/packages/edit-site/src/components/add-new-template/new-template.js b/packages/edit-site/src/components/add-new-template/new-template.js
index 31ddd754562dbf..377b26a8106e50 100644
--- a/packages/edit-site/src/components/add-new-template/new-template.js
+++ b/packages/edit-site/src/components/add-new-template/new-template.js
@@ -42,6 +42,11 @@ import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { privateApis as routerPrivateApis } from '@wordpress/router';
+/**
+ * Internal dependencies
+ */
+import { TEMPLATE_POST_TYPE } from '../../utils/constants';
+
/**
* Internal dependencies
*/
@@ -190,7 +195,7 @@ export default function NewTemplate( {
const { title, description, slug } = template;
const newTemplate = await saveEntityRecord(
'postType',
- 'wp_template',
+ TEMPLATE_POST_TYPE,
{
description,
// Slugs need to be strings, so this is for template `404`
diff --git a/packages/edit-site/src/components/add-new-template/utils.js b/packages/edit-site/src/components/add-new-template/utils.js
index 1869cdb72f5929..f94c230196053a 100644
--- a/packages/edit-site/src/components/add-new-template/utils.js
+++ b/packages/edit-site/src/components/add-new-template/utils.js
@@ -9,6 +9,11 @@ import { useMemo, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { blockMeta, post, archive } from '@wordpress/icons';
+/**
+ * Internal dependencies
+ */
+import { TEMPLATE_POST_TYPE } from '../../utils/constants';
+
/**
* @typedef IHasNameAndId
* @property {string|number} id The entity's id.
@@ -48,9 +53,13 @@ export const mapToIHasNameAndId = ( entities, path ) => {
export const useExistingTemplates = () => {
return useSelect(
( select ) =>
- select( coreStore ).getEntityRecords( 'postType', 'wp_template', {
- per_page: -1,
- } ),
+ select( coreStore ).getEntityRecords(
+ 'postType',
+ TEMPLATE_POST_TYPE,
+ {
+ per_page: -1,
+ }
+ ),
[]
);
};
diff --git a/packages/edit-site/src/components/block-editor/back-button.js b/packages/edit-site/src/components/block-editor/back-button.js
index ebe5af44c0250c..924dedd4f853e1 100644
--- a/packages/edit-site/src/components/block-editor/back-button.js
+++ b/packages/edit-site/src/components/block-editor/back-button.js
@@ -9,6 +9,10 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
/**
* Internal dependencies
*/
+import {
+ TEMPLATE_PART_POST_TYPE,
+ NAVIGATION_POST_TYPE,
+} from '../../utils/constants';
import { unlock } from '../../lock-unlock';
const { useLocation, useHistory } = unlock( routerPrivateApis );
@@ -16,8 +20,8 @@ const { useLocation, useHistory } = unlock( routerPrivateApis );
function BackButton() {
const location = useLocation();
const history = useHistory();
- const isTemplatePart = location.params.postType === 'wp_template_part';
- const isNavigationMenu = location.params.postType === 'wp_navigation';
+ const isTemplatePart = location.params.postType === TEMPLATE_PART_POST_TYPE;
+ const isNavigationMenu = location.params.postType === NAVIGATION_POST_TYPE;
const previousTemplateId = location.state?.fromTemplateId;
const isFocusMode = isTemplatePart || isNavigationMenu;
diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js
index 4bdd98b5d170e9..25fcbff830553e 100644
--- a/packages/edit-site/src/components/editor/index.js
+++ b/packages/edit-site/src/components/editor/index.js
@@ -44,6 +44,7 @@ import CanvasLoader from '../canvas-loader';
import { unlock } from '../../lock-unlock';
import useEditedEntityRecord from '../use-edited-entity-record';
import { SidebarFixedBottomSlot } from '../sidebar-edit-mode/sidebar-fixed-bottom';
+import { POST_TYPE_LABELS, TEMPLATE_POST_TYPE } from '../../utils/constants';
const { BlockRemovalWarningModal } = unlock( blockEditorPrivateApis );
@@ -58,13 +59,6 @@ const interfaceLabels = {
footer: __( 'Editor footer' ),
};
-const typeLabels = {
- wp_template: __( 'Template' ),
- wp_template_part: __( 'Template Part' ),
- wp_block: __( 'Pattern' ),
- wp_navigation: __( 'Navigation' ),
-};
-
// Prevent accidental removal of certain blocks, asking the user for
// confirmation.
const blockRemovalRules = {
@@ -171,7 +165,8 @@ export default function Editor( { isLoading } ) {
// translators: A breadcrumb trail in browser tab. %1$s: title of template being edited, %2$s: type of template (Template or Template Part).
__( '%1$s ‹ %2$s ‹ Editor' ),
getTitle(),
- typeLabels[ editedPostType ] ?? typeLabels.wp_template
+ POST_TYPE_LABELS[ editedPostType ] ??
+ POST_TYPE_LABELS[ TEMPLATE_POST_TYPE ]
);
}
diff --git a/packages/edit-site/src/components/page-patterns/duplicate-menu-item.js b/packages/edit-site/src/components/page-patterns/duplicate-menu-item.js
index 324b22e460447e..c562ffbeeb6c83 100644
--- a/packages/edit-site/src/components/page-patterns/duplicate-menu-item.js
+++ b/packages/edit-site/src/components/page-patterns/duplicate-menu-item.js
@@ -11,7 +11,11 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
/**
* Internal dependencies
*/
-import { TEMPLATE_PARTS, PATTERNS, SYNC_TYPES, USER_PATTERNS } from './utils';
+import {
+ TEMPLATE_PART_POST_TYPE,
+ PATTERN_TYPES,
+ PATTERN_SYNC_TYPES,
+} from '../../utils/constants';
import {
useExistingTemplateParts,
getUniqueTemplatePartTitle,
@@ -23,12 +27,12 @@ import usePatternCategories from '../sidebar-navigation-screen-patterns/use-patt
const { useHistory } = unlock( routerPrivateApis );
function getPatternMeta( item ) {
- if ( item.type === PATTERNS ) {
- return { wp_pattern_sync_status: SYNC_TYPES.unsynced };
+ if ( item.type === PATTERN_TYPES.theme ) {
+ return { wp_pattern_sync_status: PATTERN_SYNC_TYPES.unsynced };
}
const syncStatus = item.patternBlock.wp_pattern_sync_status;
- const isUnsynced = syncStatus === SYNC_TYPES.unsynced;
+ const isUnsynced = syncStatus === PATTERN_SYNC_TYPES.unsynced;
return {
...item.patternBlock.meta,
@@ -84,9 +88,9 @@ export default function DuplicateMenuItem( {
);
history.push( {
- postType: TEMPLATE_PARTS,
+ postType: TEMPLATE_PART_POST_TYPE,
postId: result?.id,
- categoryType: TEMPLATE_PARTS,
+ categoryType: TEMPLATE_PART_POST_TYPE,
categoryId,
} );
@@ -148,7 +152,7 @@ export default function DuplicateMenuItem( {
async function createPattern() {
try {
- const isThemePattern = item.type === PATTERNS;
+ const isThemePattern = item.type === PATTERN_TYPES.theme;
const title = sprintf(
/* translators: %s: Existing pattern title */
__( '%s (Copy)' ),
@@ -184,9 +188,9 @@ export default function DuplicateMenuItem( {
);
history.push( {
- categoryType: PATTERNS,
+ categoryType: PATTERN_TYPES.theme,
categoryId,
- postType: USER_PATTERNS,
+ postType: PATTERN_TYPES.user,
postId: result?.id,
} );
@@ -206,7 +210,9 @@ export default function DuplicateMenuItem( {
}
const createItem =
- item.type === TEMPLATE_PARTS ? createTemplatePart : createPattern;
+ item.type === TEMPLATE_PART_POST_TYPE
+ ? createTemplatePart
+ : createPattern;
return ;
}
diff --git a/packages/edit-site/src/components/page-patterns/grid-item.js b/packages/edit-site/src/components/page-patterns/grid-item.js
index 25aab2646cdfd0..5f79067f6ed299 100644
--- a/packages/edit-site/src/components/page-patterns/grid-item.js
+++ b/packages/edit-site/src/components/page-patterns/grid-item.js
@@ -38,7 +38,11 @@ import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';
*/
import RenameMenuItem from './rename-menu-item';
import DuplicateMenuItem from './duplicate-menu-item';
-import { PATTERNS, TEMPLATE_PARTS, USER_PATTERNS, SYNC_TYPES } from './utils';
+import {
+ PATTERN_TYPES,
+ TEMPLATE_PART_POST_TYPE,
+ PATTERN_SYNC_TYPES,
+} from '../../utils/constants';
import { store as editSiteStore } from '../../store';
import { useLink } from '../routes/link';
@@ -54,9 +58,9 @@ function GridItem( { categoryId, item, ...props } ) {
const { createErrorNotice, createSuccessNotice } =
useDispatch( noticesStore );
- const isUserPattern = item.type === USER_PATTERNS;
- const isNonUserPattern = item.type === PATTERNS;
- const isTemplatePart = item.type === TEMPLATE_PARTS;
+ const isUserPattern = item.type === PATTERN_TYPES.user;
+ const isNonUserPattern = item.type === PATTERN_TYPES.theme;
+ const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE;
const { onClick } = useLink( {
postType: item.type,
@@ -123,7 +127,8 @@ function GridItem( { categoryId, item, ...props } ) {
if ( ! isUserPattern && templatePartIcons[ categoryId ] ) {
itemIcon = templatePartIcons[ categoryId ];
} else {
- itemIcon = item.syncStatus === SYNC_TYPES.full ? symbol : undefined;
+ itemIcon =
+ item.syncStatus === PATTERN_SYNC_TYPES.full ? symbol : undefined;
}
const confirmButtonText = hasThemeFile ? __( 'Clear' ) : __( 'Delete' );
@@ -143,8 +148,12 @@ function GridItem( { categoryId, item, ...props } ) {
// @see https://reakit.io/docs/composite/#performance.
id={ `edit-site-patterns-${ item.name }` }
{ ...props }
- onClick={ item.type !== PATTERNS ? onClick : undefined }
- aria-disabled={ item.type !== PATTERNS ? 'false' : 'true' }
+ onClick={
+ item.type !== PATTERN_TYPES.theme ? onClick : undefined
+ }
+ aria-disabled={
+ item.type !== PATTERN_TYPES.theme ? 'false' : 'true'
+ }
aria-label={ item.title }
aria-describedby={
ariaDescriptions.length
@@ -194,7 +203,7 @@ function GridItem( { categoryId, item, ...props } ) {
) }
- { item.type === PATTERNS ? (
+ { item.type === PATTERN_TYPES.theme ? (
item.title
) : (
@@ -209,7 +218,7 @@ function GridItem( { categoryId, item, ...props } ) {
) }
- { item.type === PATTERNS && (
+ { item.type === PATTERN_TYPES.theme && (
area.area === categoryId
);
title = templatePartArea?.label;
description = templatePartArea?.description;
- } else if ( type === PATTERNS ) {
+ } else if ( type === PATTERN_TYPES.theme ) {
const patternCategory = patternCategories.find(
( category ) => category.name === categoryId
);
diff --git a/packages/edit-site/src/components/page-patterns/index.js b/packages/edit-site/src/components/page-patterns/index.js
index d90fc748442444..6861b26d653870 100644
--- a/packages/edit-site/src/components/page-patterns/index.js
+++ b/packages/edit-site/src/components/page-patterns/index.js
@@ -8,7 +8,7 @@ import { getQueryArgs } from '@wordpress/url';
/**
* Internal dependencies
*/
-import { DEFAULT_CATEGORY, DEFAULT_TYPE } from './utils';
+import { PATTERN_DEFAULT_CATEGORY, PATTERN_TYPES } from '../../utils/constants';
import Page from '../page';
import PatternsList from './patterns-list';
import usePatternSettings from './use-pattern-settings';
@@ -18,8 +18,8 @@ const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis );
export default function PagePatterns() {
const { categoryType, categoryId } = getQueryArgs( window.location.href );
- const type = categoryType || DEFAULT_TYPE;
- const category = categoryId || DEFAULT_CATEGORY;
+ const type = categoryType || PATTERN_TYPES.user;
+ const category = categoryId || PATTERN_DEFAULT_CATEGORY;
const settings = usePatternSettings();
// Wrap everything in a block editor provider.
diff --git a/packages/edit-site/src/components/page-patterns/patterns-list.js b/packages/edit-site/src/components/page-patterns/patterns-list.js
index 57c342ca3caa9c..95e6824c58144a 100644
--- a/packages/edit-site/src/components/page-patterns/patterns-list.js
+++ b/packages/edit-site/src/components/page-patterns/patterns-list.js
@@ -27,23 +27,23 @@ import usePatterns from './use-patterns';
import SidebarButton from '../sidebar-button';
import useDebouncedInput from '../../utils/use-debounced-input';
import { unlock } from '../../lock-unlock';
-import { SYNC_TYPES, PATTERNS } from './utils';
+import { PATTERN_SYNC_TYPES, PATTERN_TYPES } from '../../utils/constants';
import Pagination from './pagination';
const { useLocation, useHistory } = unlock( routerPrivateApis );
const SYNC_FILTERS = {
all: __( 'All' ),
- [ SYNC_TYPES.full ]: __( 'Synced' ),
- [ SYNC_TYPES.unsynced ]: __( 'Standard' ),
+ [ PATTERN_SYNC_TYPES.full ]: __( 'Synced' ),
+ [ PATTERN_SYNC_TYPES.unsynced ]: __( 'Standard' ),
};
const SYNC_DESCRIPTIONS = {
all: '',
- [ SYNC_TYPES.full ]: __(
+ [ PATTERN_SYNC_TYPES.full ]: __(
'Patterns that are kept in sync across the site.'
),
- [ SYNC_TYPES.unsynced ]: __(
+ [ PATTERN_SYNC_TYPES.unsynced ]: __(
'Patterns that can be changed freely without affecting the site.'
),
};
@@ -64,7 +64,7 @@ export default function PatternsList( { categoryId, type } ) {
const deferredSyncedFilter = useDeferredValue( syncFilter );
const isUncategorizedThemePatterns =
- type === PATTERNS && categoryId === 'uncategorized';
+ type === PATTERN_TYPES.theme && categoryId === 'uncategorized';
const { patterns, isResolving } = usePatterns(
type,
@@ -155,7 +155,7 @@ export default function PatternsList( { categoryId, type } ) {
__nextHasNoMarginBottom
/>
- { type === PATTERNS && (
+ { type === PATTERN_TYPES.theme && (
item.title );
@@ -29,7 +29,7 @@ export default function RenameMenuItem( { item, onClose } ) {
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
- if ( item.type === TEMPLATE_PARTS && ! item.isCustom ) {
+ if ( item.type === TEMPLATE_PART_POST_TYPE && ! item.isCustom ) {
return null;
}
@@ -50,7 +50,7 @@ export default function RenameMenuItem( { item, onClose } ) {
} );
createSuccessNotice(
- item.type === TEMPLATE_PARTS
+ item.type === TEMPLATE_PART_POST_TYPE
? __( 'Template part renamed.' )
: __( 'Pattern renamed.' ),
{
@@ -59,7 +59,7 @@ export default function RenameMenuItem( { item, onClose } ) {
);
} catch ( error ) {
const fallbackErrorMessage =
- item.type === TEMPLATE_PARTS
+ item.type === TEMPLATE_PART_POST_TYPE
? __(
'An error occurred while reverting the template part.'
)
diff --git a/packages/edit-site/src/components/page-patterns/search-items.js b/packages/edit-site/src/components/page-patterns/search-items.js
index 4ba9b9356af3cb..3c9beca587c24f 100644
--- a/packages/edit-site/src/components/page-patterns/search-items.js
+++ b/packages/edit-site/src/components/page-patterns/search-items.js
@@ -7,7 +7,7 @@ import { noCase } from 'change-case';
/**
* Internal dependencies
*/
-import { ALL_PATTERNS_CATEGORY } from './utils';
+import { PATTERN_DEFAULT_CATEGORY } from '../../utils/constants';
// Default search helpers.
const defaultGetName = ( item ) => item.name || '';
@@ -89,8 +89,9 @@ const removeMatchingTerms = ( unmatchedTerms, unprocessedTerms ) => {
*/
export const searchItems = ( items = [], searchInput = '', config = {} ) => {
const normalizedSearchTerms = getNormalizedSearchTerms( searchInput );
+ // Filter patterns by category: the default category indicates that all patterns will be shown.
const onlyFilterByCategory =
- config.categoryId !== ALL_PATTERNS_CATEGORY &&
+ config.categoryId !== PATTERN_DEFAULT_CATEGORY &&
! normalizedSearchTerms.length;
const searchRankConfig = { ...config, onlyFilterByCategory };
@@ -139,7 +140,8 @@ function getItemSearchRank( item, searchTerm, config ) {
} = config;
let rank =
- categoryId === ALL_PATTERNS_CATEGORY || hasCategory( item, categoryId )
+ categoryId === PATTERN_DEFAULT_CATEGORY ||
+ hasCategory( item, categoryId )
? 1
: 0;
diff --git a/packages/edit-site/src/components/page-patterns/use-patterns.js b/packages/edit-site/src/components/page-patterns/use-patterns.js
index 25919d55567108..42455d201a87ca 100644
--- a/packages/edit-site/src/components/page-patterns/use-patterns.js
+++ b/packages/edit-site/src/components/page-patterns/use-patterns.js
@@ -10,14 +10,13 @@ import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
+import { filterOutDuplicatesByName } from './utils';
import {
- CORE_PATTERN_SOURCES,
- PATTERNS,
- SYNC_TYPES,
- TEMPLATE_PARTS,
- USER_PATTERNS,
- filterOutDuplicatesByName,
-} from './utils';
+ PATTERN_CORE_SOURCES,
+ PATTERN_TYPES,
+ PATTERN_SYNC_TYPES,
+ TEMPLATE_PART_POST_TYPE,
+} from '../../utils/constants';
import { unlock } from '../../lock-unlock';
import { searchItems } from './search-items';
import { store as editSiteStore } from '../../store';
@@ -50,7 +49,7 @@ const selectTemplatePartsAsPatterns = (
const { __experimentalGetDefaultTemplatePartAreas } = select( editorStore );
const query = { per_page: -1 };
const rawTemplateParts =
- getEntityRecords( 'postType', TEMPLATE_PARTS, query ) ??
+ getEntityRecords( 'postType', TEMPLATE_PART_POST_TYPE, query ) ??
EMPTY_PATTERN_LIST;
const templateParts = rawTemplateParts.map( ( templatePart ) =>
templatePartToPattern( templatePart )
@@ -101,14 +100,14 @@ const selectThemePatterns = ( select ) => {
...( restBlockPatterns || [] ),
]
.filter(
- ( pattern ) => ! CORE_PATTERN_SOURCES.includes( pattern.source )
+ ( pattern ) => ! PATTERN_CORE_SOURCES.includes( pattern.source )
)
.filter( filterOutDuplicatesByName )
.filter( ( pattern ) => pattern.inserter !== false )
.map( ( pattern ) => ( {
...pattern,
keywords: pattern.keywords || [],
- type: 'pattern',
+ type: PATTERN_TYPES.theme,
blocks: parse( pattern.content, {
__unstableSkipMigrationLogs: true,
} ),
@@ -159,9 +158,9 @@ const patternBlockToPattern = ( patternBlock, categories ) => ( {
} ),
id: patternBlock.id,
name: patternBlock.slug,
- syncStatus: patternBlock.wp_pattern_sync_status || SYNC_TYPES.full,
+ syncStatus: patternBlock.wp_pattern_sync_status || PATTERN_SYNC_TYPES.full,
title: patternBlock.title.raw,
- type: USER_PATTERNS,
+ type: PATTERN_TYPES.user,
patternBlock,
} );
@@ -170,7 +169,7 @@ const selectUserPatterns = ( select, { search = '', syncStatus } = {} ) => {
select( coreStore );
const query = { per_page: -1 };
- const records = getEntityRecords( 'postType', USER_PATTERNS, query );
+ const records = getEntityRecords( 'postType', PATTERN_TYPES.user, query );
const categories = getUserPatternCategories();
let patterns = records
@@ -181,7 +180,7 @@ const selectUserPatterns = ( select, { search = '', syncStatus } = {} ) => {
const isResolving = getIsResolving( 'getEntityRecords', [
'postType',
- USER_PATTERNS,
+ PATTERN_TYPES.user,
query,
] );
@@ -208,18 +207,18 @@ export const usePatterns = (
) => {
return useSelect(
( select ) => {
- if ( categoryType === TEMPLATE_PARTS ) {
+ if ( categoryType === TEMPLATE_PART_POST_TYPE ) {
return selectTemplatePartsAsPatterns( select, {
categoryId,
search,
} );
- } else if ( categoryType === PATTERNS ) {
+ } else if ( categoryType === PATTERN_TYPES.theme ) {
return selectPatterns( select, {
categoryId,
search,
syncStatus,
} );
- } else if ( categoryType === USER_PATTERNS ) {
+ } else if ( categoryType === PATTERN_TYPES.user ) {
return selectUserPatterns( select, { search, syncStatus } );
}
return { patterns: EMPTY_PATTERN_LIST, isResolving: false };
diff --git a/packages/edit-site/src/components/page-patterns/utils.js b/packages/edit-site/src/components/page-patterns/utils.js
index 5ed57982e7cf16..f400d9855c5136 100644
--- a/packages/edit-site/src/components/page-patterns/utils.js
+++ b/packages/edit-site/src/components/page-patterns/utils.js
@@ -1,21 +1,2 @@
-export const ALL_PATTERNS_CATEGORY = 'all-patterns';
-export const DEFAULT_CATEGORY = ALL_PATTERNS_CATEGORY;
-export const PATTERNS = 'pattern';
-export const DEFAULT_TYPE = PATTERNS;
-export const TEMPLATE_PARTS = 'wp_template_part';
-export const USER_PATTERNS = 'wp_block';
-
-export const CORE_PATTERN_SOURCES = [
- 'core',
- 'pattern-directory/core',
- 'pattern-directory/featured',
- 'pattern-directory/theme',
-];
-
-export const SYNC_TYPES = {
- full: 'fully',
- unsynced: 'unsynced',
-};
-
export const filterOutDuplicatesByName = ( currentItem, index, items ) =>
index === items.findIndex( ( item ) => currentItem.name === item.name );
diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js
index 0f77cd8e5c1af2..0b7be99812dd67 100644
--- a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js
+++ b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js
@@ -19,7 +19,7 @@ import AddNewPattern from '../add-new-pattern';
import SidebarNavigationItem from '../sidebar-navigation-item';
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import CategoryItem from './category-item';
-import { DEFAULT_CATEGORY, DEFAULT_TYPE } from '../page-patterns/utils';
+import { PATTERN_DEFAULT_CATEGORY, PATTERN_TYPES } from '../../utils/constants';
import { useLink } from '../routes/link';
import usePatternCategories from './use-pattern-categories';
import useTemplatePartAreas from './use-template-part-areas';
@@ -70,8 +70,8 @@ function PatternCategoriesGroup( {
type="pattern"
isActive={
currentCategory === `${ category.name }` &&
- ( currentType === 'pattern' ||
- currentType === 'wp_block' )
+ ( currentType === PATTERN_TYPES.theme ||
+ currentType === PATTERN_TYPES.user )
}
/>
) ) }
@@ -83,8 +83,8 @@ function PatternCategoriesGroup( {
export default function SidebarNavigationScreenPatterns() {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const { categoryType, categoryId } = getQueryArgs( window.location.href );
- const currentCategory = categoryId || DEFAULT_CATEGORY;
- const currentType = categoryType || DEFAULT_TYPE;
+ const currentCategory = categoryId || PATTERN_DEFAULT_CATEGORY;
+ const currentType = categoryType || PATTERN_TYPES.user;
const { templatePartAreas, hasTemplateParts, isLoading } =
useTemplatePartAreas();
diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-pattern-categories.js b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-pattern-categories.js
index e3ab86e8b06151..367b293f2206dc 100644
--- a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-pattern-categories.js
+++ b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-pattern-categories.js
@@ -10,7 +10,7 @@ import { __ } from '@wordpress/i18n';
import useDefaultPatternCategories from './use-default-pattern-categories';
import useThemePatterns from './use-theme-patterns';
import usePatterns from '../page-patterns/use-patterns';
-import { USER_PATTERNS, ALL_PATTERNS_CATEGORY } from '../page-patterns/utils';
+import { PATTERN_TYPES, PATTERN_DEFAULT_CATEGORY } from '../../utils/constants';
export default function usePatternCategories() {
const defaultCategories = useDefaultPatternCategories();
@@ -20,7 +20,7 @@ export default function usePatternCategories() {
} );
const themePatterns = useThemePatterns();
const { patterns: userPatterns, categories: userPatternCategories } =
- usePatterns( USER_PATTERNS );
+ usePatterns( PATTERN_TYPES.user );
const patternCategories = useMemo( () => {
const categoryMap = {};
@@ -81,7 +81,7 @@ export default function usePatternCategories() {
a.label.localeCompare( b.label )
);
sortedCategories.unshift( {
- name: ALL_PATTERNS_CATEGORY,
+ name: PATTERN_DEFAULT_CATEGORY,
label: __( 'All Patterns' ),
description: __( 'A list of all patterns from all sources' ),
count: themePatterns.length + userPatterns.length,
diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-theme-patterns.js b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-theme-patterns.js
index bf6decf3414222..9e4983450ae5a3 100644
--- a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-theme-patterns.js
+++ b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-theme-patterns.js
@@ -8,10 +8,8 @@ import { useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
-import {
- CORE_PATTERN_SOURCES,
- filterOutDuplicatesByName,
-} from '../page-patterns/utils';
+import { filterOutDuplicatesByName } from '../page-patterns/utils';
+import { PATTERN_CORE_SOURCES } from '../../utils/constants';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
@@ -34,7 +32,7 @@ export default function useThemePatterns() {
[ ...( blockPatterns || [] ), ...( restBlockPatterns || [] ) ]
.filter(
( pattern ) =>
- ! CORE_PATTERN_SOURCES.includes( pattern.source )
+ ! PATTERN_CORE_SOURCES.includes( pattern.source )
)
.filter( filterOutDuplicatesByName )
.filter( ( pattern ) => pattern.inserter !== false ),
diff --git a/packages/edit-site/src/utils/constants.js b/packages/edit-site/src/utils/constants.js
index ed88c3e14c9473..d12c7f84cc356c 100644
--- a/packages/edit-site/src/utils/constants.js
+++ b/packages/edit-site/src/utils/constants.js
@@ -1,5 +1,40 @@
+/**
+ * WordPress dependencies
+ */
+import { __ } from '@wordpress/i18n';
+import { privateApis as patternPrivateApis } from '@wordpress/patterns';
+
+/**
+ * Internal dependencies
+ */
+import { unlock } from '../lock-unlock';
+
+// Navigation
+export const NAVIGATION_POST_TYPE = 'wp_navigation';
+
+// Templates.
+export const TEMPLATE_POST_TYPE = 'wp_template';
+export const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
+export const TEMPLATE_CUSTOM_SOURCE = 'custom';
+
+// Patterns.
+export const {
+ PATTERN_TYPES,
+ PATTERN_DEFAULT_CATEGORY,
+ PATTERN_CORE_SOURCES,
+ PATTERN_SYNC_TYPES,
+} = unlock( patternPrivateApis );
+
+// Entities that are editable in focus mode.
export const FOCUSABLE_ENTITIES = [
- 'wp_template_part',
- 'wp_navigation',
- 'wp_block',
+ TEMPLATE_PART_POST_TYPE,
+ NAVIGATION_POST_TYPE,
+ PATTERN_TYPES.user,
];
+
+export const POST_TYPE_LABELS = {
+ [ TEMPLATE_POST_TYPE ]: __( 'Template' ),
+ [ TEMPLATE_PART_POST_TYPE ]: __( 'Template Part' ),
+ [ PATTERN_TYPES.user ]: __( 'Pattern' ),
+ [ NAVIGATION_POST_TYPE ]: __( 'Navigation' ),
+};
diff --git a/packages/edit-site/src/utils/is-template-removable.js b/packages/edit-site/src/utils/is-template-removable.js
index 7ec203a9e0db56..02682e97334e59 100644
--- a/packages/edit-site/src/utils/is-template-removable.js
+++ b/packages/edit-site/src/utils/is-template-removable.js
@@ -1,3 +1,8 @@
+/**
+ * Internal dependencies
+ */
+import { TEMPLATE_CUSTOM_SOURCE } from './constants';
+
/**
* Check if a template is removable.
*
@@ -9,5 +14,7 @@ export default function isTemplateRemovable( template ) {
return false;
}
- return template.source === 'custom' && ! template.has_theme_file;
+ return (
+ template.source === TEMPLATE_CUSTOM_SOURCE && ! template.has_theme_file
+ );
}
diff --git a/packages/edit-site/src/utils/is-template-revertable.js b/packages/edit-site/src/utils/is-template-revertable.js
index c2d42f51c97018..89d1d8ea552a19 100644
--- a/packages/edit-site/src/utils/is-template-revertable.js
+++ b/packages/edit-site/src/utils/is-template-revertable.js
@@ -1,3 +1,8 @@
+/**
+ * Internal dependencies
+ */
+import { TEMPLATE_CUSTOM_SOURCE } from './constants';
+
/**
* Check if a template is revertable to its original theme-provided template file.
*
@@ -9,6 +14,8 @@ export default function isTemplateRevertable( template ) {
return false;
}
/* eslint-disable camelcase */
- return template?.source === 'custom' && template?.has_theme_file;
+ return (
+ template?.source === TEMPLATE_CUSTOM_SOURCE && template?.has_theme_file
+ );
/* eslint-enable camelcase */
}
diff --git a/packages/patterns/src/components/create-pattern-modal.js b/packages/patterns/src/components/create-pattern-modal.js
index 2b8d89cbfae368..44f08f53ba8765 100644
--- a/packages/patterns/src/components/create-pattern-modal.js
+++ b/packages/patterns/src/components/create-pattern-modal.js
@@ -14,12 +14,10 @@ import { useState, useCallback } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
-export const ALL_PATTERNS_CATEGORY = 'all-patterns';
-
-export const SYNC_TYPES = {
- full: undefined,
- unsynced: 'unsynced',
-};
+/**
+ * Internal dependencies
+ */
+import { PATTERN_DEFAULT_CATEGORY, PATTERN_SYNC_TYPES } from '../constants';
/**
* Internal dependencies
@@ -34,7 +32,7 @@ export default function CreatePatternModal( {
onClose,
className = 'patterns-menu-items__convert-modal',
} ) {
- const [ syncType, setSyncType ] = useState( SYNC_TYPES.full );
+ const [ syncType, setSyncType ] = useState( PATTERN_SYNC_TYPES.full );
const [ categories, setCategories ] = useState( [] );
const [ title, setTitle ] = useState( '' );
const { createPattern } = useDispatch( store );
@@ -51,7 +49,7 @@ export default function CreatePatternModal( {
);
onSuccess( {
pattern: newPattern,
- categoryId: ALL_PATTERNS_CATEGORY,
+ categoryId: PATTERN_DEFAULT_CATEGORY,
} );
} catch ( error ) {
createErrorNotice( error.message, {
@@ -108,12 +106,12 @@ export default function CreatePatternModal( {
help={ __(
'Editing the pattern will update it anywhere it is used.'
) }
- checked={ ! syncType }
+ checked={ syncType === PATTERN_SYNC_TYPES.full }
onChange={ () => {
setSyncType(
- syncType === SYNC_TYPES.full
- ? SYNC_TYPES.unsynced
- : SYNC_TYPES.full
+ syncType === PATTERN_SYNC_TYPES.full
+ ? PATTERN_SYNC_TYPES.unsynced
+ : PATTERN_SYNC_TYPES.full
);
} }
/>
diff --git a/packages/patterns/src/constants.js b/packages/patterns/src/constants.js
new file mode 100644
index 00000000000000..ecf7c7ddb3740c
--- /dev/null
+++ b/packages/patterns/src/constants.js
@@ -0,0 +1,16 @@
+export const PATTERN_TYPES = {
+ theme: 'pattern',
+ user: 'wp_block',
+};
+
+export const PATTERN_DEFAULT_CATEGORY = 'all-patterns';
+export const PATTERN_CORE_SOURCES = [
+ 'core',
+ 'pattern-directory/core',
+ 'pattern-directory/featured',
+ 'pattern-directory/theme',
+];
+export const PATTERN_SYNC_TYPES = {
+ full: 'fully',
+ unsynced: 'unsynced',
+};
diff --git a/packages/patterns/src/private-apis.js b/packages/patterns/src/private-apis.js
index 31f507c2b99143..306476efc31212 100644
--- a/packages/patterns/src/private-apis.js
+++ b/packages/patterns/src/private-apis.js
@@ -4,9 +4,19 @@
import { lock } from './lock-unlock';
import CreatePatternModal from './components/create-pattern-modal';
import PatternsMenuItems from './components';
+import {
+ PATTERN_TYPES,
+ PATTERN_DEFAULT_CATEGORY,
+ PATTERN_CORE_SOURCES,
+ PATTERN_SYNC_TYPES,
+} from './constants';
export const privateApis = {};
lock( privateApis, {
CreatePatternModal,
PatternsMenuItems,
+ PATTERN_TYPES,
+ PATTERN_DEFAULT_CATEGORY,
+ PATTERN_CORE_SOURCES,
+ PATTERN_SYNC_TYPES,
} );
diff --git a/packages/patterns/src/store/actions.js b/packages/patterns/src/store/actions.js
index 07680757762c38..589dad326d3b62 100644
--- a/packages/patterns/src/store/actions.js
+++ b/packages/patterns/src/store/actions.js
@@ -6,6 +6,11 @@ import { parse, serialize, createBlock } from '@wordpress/blocks';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
+/**
+ * Internal dependencies
+ */
+import { PATTERN_SYNC_TYPES } from '../constants';
+
/**
* Returns a generator converting one or more static blocks into a pattern, or creating a new empty pattern.
*
@@ -18,7 +23,7 @@ export const createPattern =
( title, syncType, clientIds, categories ) =>
async ( { registry, dispatch } ) => {
const meta =
- syncType === 'unsynced'
+ syncType === PATTERN_SYNC_TYPES.unsynced
? {
wp_pattern_sync_status: syncType,
}