From 84eea15779cb9ab4dc14dc6414d5c234193e7f1f Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 5 Mar 2025 08:49:55 +0400 Subject: [PATCH 1/3] REST API: Add additional default template data fields for the active theme --- lib/compat/wordpress-6.8/rest-api.php | 93 ++++++++++++++++----------- 1 file changed, 57 insertions(+), 36 deletions(-) diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/rest-api.php index f965dbbdda4495..25a9240e994a87 100644 --- a/lib/compat/wordpress-6.8/rest-api.php +++ b/lib/compat/wordpress-6.8/rest-api.php @@ -35,42 +35,6 @@ function ( $taxonomy ) { } ); -/** - * Adds the default template part areas to the REST API index. - * - * This function exposes the default template part areas through the WordPress REST API. - * Note: This function backports into the wp-includes/rest-api/class-wp-rest-server.php file. - * - * @param WP_REST_Response $response REST API response. - * @return WP_REST_Response Modified REST API response with default template part areas. - */ -function gutenberg_add_default_template_part_areas_to_index( WP_REST_Response $response ) { - $response->data['default_template_part_areas'] = get_allowed_block_template_part_areas(); - return $response; -} -add_filter( 'rest_index', 'gutenberg_add_default_template_part_areas_to_index' ); - -/** - * Adds the default template types to the REST API index. - * - * This function exposes the default template types through the WordPress REST API. - * Note: This function backports into the wp-includes/rest-api/class-wp-rest-server.php file. - * - * @param WP_REST_Response $response REST API response. - * @return WP_REST_Response Modified REST API response with default template part areas. - */ -function gutenberg_add_default_template_types_to_index( WP_REST_Response $response ) { - $indexed_template_types = array(); - foreach ( get_default_block_template_types() as $slug => $template_type ) { - $template_type['slug'] = (string) $slug; - $indexed_template_types[] = $template_type; - } - - $response->data['default_template_types'] = $indexed_template_types; - return $response; -} -add_filter( 'rest_index', 'gutenberg_add_default_template_types_to_index' ); - /** * Adds the site reading options to the REST API index. * @@ -129,3 +93,60 @@ function gutenberg_modify_post_collection_query( $args, WP_REST_Request $request return $args; } add_filter( 'rest_post_query', 'gutenberg_modify_post_collection_query', 10, 2 ); + +/** + * Registers `default_template_types` and `default_template_part_areas` fields for the active theme. + * + * Note: Backports into the wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php file. + * + * @return void + */ +function gutenberg_register_rest_theme_fields() { + register_rest_field( + 'theme', + 'default_template_types', + array( + 'get_callback' => static function ( $response_data ) { + if ( ! isset( $response_data['status'] ) || 'active' !== $response_data['status'] ) { + return null; + } + + $default_template_types = array(); + foreach ( get_default_block_template_types() as $slug => $template_type ) { + $template_type['slug'] = (string) $slug; + $default_template_types[] = $template_type; + } + + return $default_template_types; + }, + 'schema' => array( + 'description' => __( 'A list of default template types.' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ) + ); + register_rest_field( + 'theme', + 'default_template_part_areas', + array( + 'get_callback' => static function ( $response_data ) { + if ( ! isset( $response_data['status'] ) || 'active' !== $response_data['status'] ) { + return null; + } + + return get_allowed_block_template_part_areas(); + }, + 'schema' => array( + 'description' => __( 'A list of allowed area values for template parts.' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ) + ); +} +add_action( 'rest_api_init', 'gutenberg_register_rest_theme_fields' ); From f13184014a86777c9d487f907ba1e5c7732e577d Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 5 Mar 2025 09:10:39 +0400 Subject: [PATCH 2/3] Update data selection --- .../use-template-part-area-label.js | 12 +- .../template-part/edit/advanced-controls.js | 2 +- .../src/template-part/edit/utils/hooks.js | 2 +- .../src/components/add-new-template/utils.js | 3 +- .../src/components/editor/use-editor-title.js | 12 +- .../src/components/page-patterns/header.js | 2 +- .../components/page-patterns/use-patterns.js | 14 +-- .../use-template-part-areas.js | 2 +- .../src/components/document-bar/index.js | 4 +- .../entity-record-item.js | 5 +- .../src/components/post-card-panel/index.js | 4 +- .../editor/src/store/private-selectors.js | 6 +- packages/editor/src/store/selectors.js | 23 ++-- .../create-template-part-modal/index.tsx | 110 +++++++++--------- 14 files changed, 95 insertions(+), 106 deletions(-) diff --git a/packages/block-library/src/navigation/use-template-part-area-label.js b/packages/block-library/src/navigation/use-template-part-area-label.js index 7b4d514975e113..7ae8c6bcbfb1ad 100644 --- a/packages/block-library/src/navigation/use-template-part-area-label.js +++ b/packages/block-library/src/navigation/use-template-part-area-label.js @@ -36,24 +36,24 @@ export default function useTemplatePartAreaLabel( clientId ) { return; } + const { getCurrentTheme, getEditedEntityRecord } = + select( coreStore ); + + const currentTheme = getCurrentTheme(); const defaultTemplatePartAreas = - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) - ?.default_template_part_areas || []; + currentTheme?.default_template_part_areas || []; const definedAreas = defaultTemplatePartAreas.map( ( item ) => ( { ...item, icon: getTemplatePartIcon( item.icon ), } ) ); - const { getCurrentTheme, getEditedEntityRecord } = - select( coreStore ); - for ( const templatePartClientId of parentTemplatePartClientIds ) { const templatePartBlock = getBlock( templatePartClientId ); // The 'area' usually isn't stored on the block, but instead // on the entity. - const { theme = getCurrentTheme()?.stylesheet, slug } = + const { theme = currentTheme?.stylesheet, slug } = templatePartBlock.attributes; const templatePartEntityId = createTemplatePartId( theme, diff --git a/packages/block-library/src/template-part/edit/advanced-controls.js b/packages/block-library/src/template-part/edit/advanced-controls.js index f63c01b71be02c..e9915f40a8acea 100644 --- a/packages/block-library/src/template-part/edit/advanced-controls.js +++ b/packages/block-library/src/template-part/edit/advanced-controls.js @@ -36,7 +36,7 @@ export function TemplatePartAdvancedControls( { const defaultTemplatePartAreas = useSelect( ( select ) => - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) + select( coreStore ).getCurrentTheme() ?.default_template_part_areas || [], [] ); diff --git a/packages/block-library/src/template-part/edit/utils/hooks.js b/packages/block-library/src/template-part/edit/utils/hooks.js index c71327db0290c4..bb5fbb650b261f 100644 --- a/packages/block-library/src/template-part/edit/utils/hooks.js +++ b/packages/block-library/src/template-part/edit/utils/hooks.js @@ -137,7 +137,7 @@ export function useTemplatePartArea( area ) { return useSelect( ( select ) => { const definedAreas = - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) + select( coreStore ).getCurrentTheme() ?.default_template_part_areas || []; const selectedArea = definedAreas.find( 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 e781036bb13d5b..fd7d2277fae042 100644 --- a/packages/edit-site/src/components/add-new-template/utils.js +++ b/packages/edit-site/src/components/add-new-template/utils.js @@ -68,8 +68,7 @@ export const useExistingTemplates = () => { export const useDefaultTemplateTypes = () => { return useSelect( ( select ) => - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) - ?.default_template_types || [], + select( coreStore ).getCurrentTheme()?.default_template_types || [], [] ); }; diff --git a/packages/edit-site/src/components/editor/use-editor-title.js b/packages/edit-site/src/components/editor/use-editor-title.js index 6f0b36f8e3b8b6..e88b91676814e3 100644 --- a/packages/edit-site/src/components/editor/use-editor-title.js +++ b/packages/edit-site/src/components/editor/use-editor-title.js @@ -19,8 +19,11 @@ const { getTemplateInfo } = unlock( editorPrivateApis ); function useEditorTitle( postType, postId ) { const { title, isLoaded } = useSelect( ( select ) => { - const { getEditedEntityRecord, hasFinishedResolution } = - select( coreStore ); + const { + getEditedEntityRecord, + getCurrentTheme, + hasFinishedResolution, + } = select( coreStore ); if ( ! postId ) { return { isLoaded: false }; @@ -33,10 +36,7 @@ function useEditorTitle( postType, postId ) { ); const { default_template_types: templateTypes = [] } = - select( coreStore ).getEntityRecord( - 'root', - '__unstableBase' - ) ?? {}; + getCurrentTheme() ?? {}; const templateInfo = getTemplateInfo( { template: _record, diff --git a/packages/edit-site/src/components/page-patterns/header.js b/packages/edit-site/src/components/page-patterns/header.js index 0d3763aec62c1a..26deda55498243 100644 --- a/packages/edit-site/src/components/page-patterns/header.js +++ b/packages/edit-site/src/components/page-patterns/header.js @@ -32,7 +32,7 @@ export default function PatternsHeader( { const { patternCategories } = usePatternCategories(); const templatePartAreas = useSelect( ( select ) => - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) + select( coreStore ).getCurrentTheme() ?.default_template_part_areas || [], [] ); 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 3b3e33d5650e63..596e5371d05467 100644 --- a/packages/edit-site/src/components/page-patterns/use-patterns.js +++ b/packages/edit-site/src/components/page-patterns/use-patterns.js @@ -25,8 +25,11 @@ const EMPTY_PATTERN_LIST = []; const selectTemplateParts = createSelector( ( select, categoryId, search = '' ) => { - const { getEntityRecords, isResolving: isResolvingSelector } = - select( coreStore ); + const { + getEntityRecords, + getCurrentTheme, + isResolving: isResolvingSelector, + } = select( coreStore ); const query = { per_page: -1 }; const templateParts = @@ -36,9 +39,7 @@ const selectTemplateParts = createSelector( // In the case where a custom template part area has been removed we need // the current list of areas to cross check against so orphaned template // parts can be treated as uncategorized. - const knownAreas = - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) - ?.default_template_part_areas || []; + const knownAreas = getCurrentTheme()?.default_template_part_areas || []; const templatePartAreas = knownAreas.map( ( area ) => area.area ); @@ -79,8 +80,7 @@ const selectTemplateParts = createSelector( TEMPLATE_PART_POST_TYPE, { per_page: -1 }, ] ), - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) - ?.default_template_part_areas, + select( coreStore ).getCurrentTheme()?.default_template_part_areas, ] ); diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-template-part-areas.js b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-template-part-areas.js index 6a67a8f8a30fb9..58aa6f557c94b4 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-template-part-areas.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-template-part-areas.js @@ -17,7 +17,7 @@ const useTemplatePartsGroupedByArea = ( items ) => { const templatePartAreas = useSelect( ( select ) => - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) + select( coreStore ).getCurrentTheme() ?.default_template_part_areas || [], [] ); diff --git a/packages/editor/src/components/document-bar/index.js b/packages/editor/src/components/document-bar/index.js index 544b5024d88a89..e83d955f3fab82 100644 --- a/packages/editor/src/components/document-bar/index.js +++ b/packages/editor/src/components/document-bar/index.js @@ -74,6 +74,7 @@ export default function DocumentBar( props ) { const { getEditedEntityRecord, getPostType, + getCurrentTheme, isResolving: isResolvingSelector, } = select( coreStore ); const _postType = getCurrentPostType(); @@ -85,8 +86,7 @@ export default function DocumentBar( props ) { ); const { default_template_types: templateTypes = [] } = - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) ?? - {}; + getCurrentTheme() ?? {}; const _templateInfo = getTemplateInfo( { templateTypes, diff --git a/packages/editor/src/components/entities-saved-states/entity-record-item.js b/packages/editor/src/components/entities-saved-states/entity-record-item.js index ba2e93088be93e..74b090a3941ac9 100644 --- a/packages/editor/src/components/entities-saved-states/entity-record-item.js +++ b/packages/editor/src/components/entities-saved-states/entity-record-item.js @@ -36,10 +36,7 @@ export default function EntityRecordItem( { record, checked, onChange } ) { ); const { default_template_types: templateTypes = [] } = - select( coreStore ).getEntityRecord( - 'root', - '__unstableBase' - ) ?? {}; + select( coreStore ).getCurrentTheme() ?? {}; return { entityRecordTitle: getTemplateInfo( { diff --git a/packages/editor/src/components/post-card-panel/index.js b/packages/editor/src/components/post-card-panel/index.js index a2ed6f5adaad82..a858110bdbed7b 100644 --- a/packages/editor/src/components/post-card-panel/index.js +++ b/packages/editor/src/components/post-card-panel/index.js @@ -48,7 +48,7 @@ export default function PostCardPanel( { ); const { postTitle, icon, labels } = useSelect( ( select ) => { - const { getEditedEntityRecord, getEntityRecord, getPostType } = + const { getEditedEntityRecord, getCurrentTheme, getPostType } = select( coreStore ); const { getPostIcon } = unlock( select( editorStore ) ); let _title = ''; @@ -59,7 +59,7 @@ export default function PostCardPanel( { ); if ( postIds.length === 1 ) { const { default_template_types: templateTypes = [] } = - getEntityRecord( 'root', '__unstableBase' ) ?? {}; + getCurrentTheme() ?? {}; const _templateInfo = [ TEMPLATE_POST_TYPE, diff --git a/packages/editor/src/store/private-selectors.js b/packages/editor/src/store/private-selectors.js index d381d4caa7492b..b60e0a07f16021 100644 --- a/packages/editor/src/store/private-selectors.js +++ b/packages/editor/src/store/private-selectors.js @@ -105,10 +105,8 @@ export const getPostIcon = createRegistrySelector( postType === 'wp_template' ) { const templateAreas = - select( coreStore ).getEntityRecord( - 'root', - '__unstableBase' - )?.default_template_part_areas || []; + select( coreStore ).getCurrentTheme() + ?.default_template_part_areas || []; const areaData = templateAreas.find( ( item ) => options.area === item.area diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index a19f173982d9f0..c7b705ca18722e 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -1709,11 +1709,10 @@ export const __experimentalGetDefaultTemplateTypes = createRegistrySelector( { since: '6.8', alternative: - "select('core/core-data').getEntityRecord( 'root', '__unstableBase' )?.default_template_types", + "select('core/core-data').getCurrentTheme()?.default_template_types", } ); - return select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) - ?.default_template_types; + return select( coreStore ).getCurrentTheme()?.default_template_types; } ); @@ -1732,12 +1731,12 @@ export const __experimentalGetDefaultTemplatePartAreas = createRegistrySelector( { since: '6.8', alternative: - "select('core/core-data').getEntityRecord( 'root', '__unstableBase' )?.default_template_part_areas", + "select('core/core-data').getCurrentTheme()?.default_template_part_areas", } ); const areas = - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) + select( coreStore ).getCurrentTheme() ?.default_template_part_areas || []; return areas.map( ( item ) => { @@ -1763,10 +1762,8 @@ export const __experimentalGetDefaultTemplateType = createRegistrySelector( since: '6.8', } ); - const templateTypes = select( coreStore ).getEntityRecord( - 'root', - '__unstableBase' - )?.default_template_types; + const templateTypes = + select( coreStore ).getCurrentTheme()?.default_template_types; if ( ! templateTypes ) { return EMPTY_OBJECT; @@ -1799,13 +1796,11 @@ export const __experimentalGetTemplateInfo = createRegistrySelector( return EMPTY_OBJECT; } - const templateTypes = - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) - ?.default_template_types || []; + const currentTheme = select( coreStore ).getCurrentTheme(); + const templateTypes = currentTheme?.default_template_types || []; const templateAreas = - select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) - ?.default_template_part_areas || []; + currentTheme?.default_template_part_areas || []; return getTemplateInfo( { template, diff --git a/packages/fields/src/components/create-template-part-modal/index.tsx b/packages/fields/src/components/create-template-part-modal/index.tsx index 927192eee17fcd..7825f4f7d71f0c 100644 --- a/packages/fields/src/components/create-template-part-modal/index.tsx +++ b/packages/fields/src/components/create-template-part-modal/index.tsx @@ -128,9 +128,7 @@ export function CreateTemplatePartModalContents( { const defaultTemplatePartAreas = useSelect( ( select ) => - select( coreStore ).getEntityRecord< { - default_template_part_areas: Array< TemplatePartArea >; - } >( 'root', '__unstableBase' )?.default_template_part_areas, + select( coreStore ).getCurrentTheme()?.default_template_part_areas, [] ); @@ -200,59 +198,61 @@ export function CreateTemplatePartModalContents( { { __( 'Area' ) }
- { ( defaultTemplatePartAreas ?? [] ).map( ( item ) => { - const icon = getTemplatePartIcon( item.icon ); - return ( -
- { - setArea( item.area ); - } } - aria-describedby={ getAreaRadioDescriptionId( - item.area, - instanceId - ) } - /> - -
+ ); + } + ) }
From 35ad5f122f2793018f90d6eaf4380f8a93072a81 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 5 Mar 2025 09:34:34 +0400 Subject: [PATCH 3/3] Core Data: Update base entity --- lib/compat/wordpress-6.8/preload.php | 2 -- packages/core-data/src/entities.js | 2 -- packages/core-data/src/entity-types/base.ts | 10 ---------- 3 files changed, 14 deletions(-) diff --git a/lib/compat/wordpress-6.8/preload.php b/lib/compat/wordpress-6.8/preload.php index 4e6875bf9fb080..32d43eba911d72 100644 --- a/lib/compat/wordpress-6.8/preload.php +++ b/lib/compat/wordpress-6.8/preload.php @@ -85,8 +85,6 @@ function gutenberg_block_editor_preload_paths_6_8( $paths, $context ) { 'site_icon_url', 'site_logo', 'timezone_string', - 'default_template_part_areas', - 'default_template_types', 'url', 'page_for_posts', 'page_on_front', diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index a6d3335fa9583e..c7a06463d47d1b 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -31,8 +31,6 @@ export const rootEntitiesConfig = [ 'site_icon_url', 'site_logo', 'timezone_string', - 'default_template_part_areas', - 'default_template_types', 'url', 'page_for_posts', 'page_on_front', diff --git a/packages/core-data/src/entity-types/base.ts b/packages/core-data/src/entity-types/base.ts index 79a3039ad140dc..862d7c7a6a90ad 100644 --- a/packages/core-data/src/entity-types/base.ts +++ b/packages/core-data/src/entity-types/base.ts @@ -65,16 +65,6 @@ declare module './base-entity-records' { * Site URL. */ url: string; - - /** - * Default template part areas. - */ - default_template_part_areas?: Array< TemplatePartArea >; - - /** - * Default template types - */ - default_template_types?: Array< TemplateType >; } } }