diff --git a/lib/compat/wordpress-6.8/template-parts.php b/lib/compat/wordpress-6.8/template-parts.php new file mode 100644 index 00000000000000..1f126d1bdd98ee --- /dev/null +++ b/lib/compat/wordpress-6.8/template-parts.php @@ -0,0 +1,38 @@ +data['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas(); + return $response; +} + +add_action( '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['defaultTemplateTypes'] = indexed_template_types(); + return $response; +} + +add_action( 'rest_index', 'gutenberg_add_default_template_types_to_index' ); diff --git a/lib/load.php b/lib/load.php index 2c8a0fd0347c92..d26d10cae6f173 100644 --- a/lib/load.php +++ b/lib/load.php @@ -49,6 +49,7 @@ function gutenberg_is_experiment_enabled( $name ) { // WordPress 6.8 compat. require __DIR__ . '/compat/wordpress-6.8/block-comments.php'; require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php'; + require __DIR__ . '/compat/wordpress-6.8/template-parts.php'; // Plugin specific code. require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php'; diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 8d09402087cf90..8c25533c453188 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -36,6 +36,8 @@ export const rootEntitiesConfig = [ 'site_icon_url', 'site_logo', 'timezone_string', + 'defaultTemplatePartAreas', + 'defaultTemplateTypes', 'url', ].join( ',' ), }, diff --git a/packages/edit-site/src/index.js b/packages/edit-site/src/index.js index 83d25bcd0c03ac..1d4780efe46e43 100644 --- a/packages/edit-site/src/index.js +++ b/packages/edit-site/src/index.js @@ -10,10 +10,7 @@ import { import { dispatch } from '@wordpress/data'; import deprecated from '@wordpress/deprecated'; import { createRoot, StrictMode } from '@wordpress/element'; -import { - store as editorStore, - privateApis as editorPrivateApis, -} from '@wordpress/editor'; +import { privateApis as editorPrivateApis } from '@wordpress/editor'; import { store as preferencesStore } from '@wordpress/preferences'; import { registerLegacyWidgetBlock, @@ -87,15 +84,6 @@ export function initializeEditor( id, settings ) { dispatch( editSiteStore ).updateSettings( settings ); - // Keep the defaultTemplateTypes in the core/editor settings too, - // so that they can be selected with core/editor selectors in any editor. - // This is needed because edit-site doesn't initialize with EditorProvider, - // which internally uses updateEditorSettings as well. - dispatch( editorStore ).updateEditorSettings( { - defaultTemplateTypes: settings.defaultTemplateTypes, - defaultTemplatePartAreas: settings.defaultTemplatePartAreas, - } ); - // Prevent the default browser action for files dropped outside of dropzones. window.addEventListener( 'dragover', ( e ) => e.preventDefault(), false ); window.addEventListener( 'drop', ( e ) => e.preventDefault(), false ); diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index 2396cb67c73e69..3aa1c4daca96e7 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -1702,16 +1702,11 @@ export const getBlockListSettings = getBlockEditorSelector( 'getBlockListSettings' ); -/** - * Returns the default template types. - * - * @param {Object} state Global application state. - * - * @return {Object} The template types. - */ -export function __experimentalGetDefaultTemplateTypes( state ) { - return getEditorSettings( state )?.defaultTemplateTypes; -} +export const __experimentalGetDefaultTemplateTypes = createRegistrySelector( + ( select ) => () => + select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) + ?.defaultTemplateTypes +); /** * Returns the default template part areas. @@ -1720,15 +1715,16 @@ export function __experimentalGetDefaultTemplateTypes( state ) { * * @return {Array} The template part areas. */ -export const __experimentalGetDefaultTemplatePartAreas = createSelector( - ( state ) => { +export const __experimentalGetDefaultTemplatePartAreas = createRegistrySelector( + ( select ) => () => { const areas = - getEditorSettings( state )?.defaultTemplatePartAreas ?? []; + select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) + ?.defaultTemplatePartAreas ?? []; + return areas.map( ( item ) => { return { ...item, icon: getTemplatePartIcon( item.icon ) }; } ); - }, - ( state ) => [ getEditorSettings( state )?.defaultTemplatePartAreas ] + } ); /** 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 a713629cb2da55..a59c435d63112e 100644 --- a/packages/fields/src/components/create-template-part-modal/index.tsx +++ b/packages/fields/src/components/create-template-part-modal/index.tsx @@ -29,8 +29,6 @@ import { } from '@wordpress/icons'; import { store as noticesStore } from '@wordpress/notices'; // @ts-ignore -import { store as blockEditorStore } from '@wordpress/block-editor'; -// @ts-ignore import { serialize } from '@wordpress/blocks'; /** @@ -98,17 +96,14 @@ const getTemplatePartIcon = ( iconName: string ) => { }; const getDefaultTemplatePartAreas = ( - settings: Record< string, any > & { - defaultTemplatePartAreas?: Array< { - icon: string; - label: string; - area: string; - description: string; - } >; - } + defaultTemplatePartAreas: Array< { + icon: string; + label: string; + area: string; + description: string; + } > ) => { - const areas = settings.defaultTemplatePartAreas ?? []; - return areas.map( ( item ) => { + return defaultTemplatePartAreas.map( ( item ) => { return { ...item, icon: getTemplatePartIcon( item.icon ) }; } ); }; @@ -144,14 +139,27 @@ export function CreateTemplatePartModalContents( { const [ isSubmitting, setIsSubmitting ] = useState( false ); const instanceId = useInstanceId( CreateTemplatePartModal ); - const settings = useSelect( + const { defaultTemplatePartAreas } = useSelect( // @ts-ignore - ( select ) => select( blockEditorStore ).getSettings(), + ( select ) => { + return { + defaultTemplatePartAreas: getDefaultTemplatePartAreas( + // @ts-expect-error The defaultTemplatePartAreas is not part of the core store type. + select( coreStore ).getEntityRecord< { + defaultTemplatePartAreas: Array< { + icon: string; + label: string; + area: string; + description: string; + } >; + } >( 'root', '__unstableBase' )?.defaultTemplatePartAreas ?? + [] + ), + }; + }, [] ); - const defaultTemplatePartAreas = getDefaultTemplatePartAreas( settings ); - async function createTemplatePart() { if ( ! title || isSubmitting ) { return;