Skip to content

Commit

Permalink
move default template types and template part areas to REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Oct 25, 2024
1 parent 8571360 commit 86b2e8f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 44 deletions.
38 changes: 38 additions & 0 deletions lib/compat/wordpress-6.8/template-parts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* 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['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' );
2 changes: 2 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const rootEntitiesConfig = [
'site_icon_url',
'site_logo',
'timezone_string',
'defaultTemplatePartAreas',
'defaultTemplateTypes',
'url',
].join( ',' ),
},
Expand Down
14 changes: 1 addition & 13 deletions packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 );
Expand Down
26 changes: 11 additions & 15 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 ]
}
);

/**
Expand Down
40 changes: 24 additions & 16 deletions packages/fields/src/components/create-template-part-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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 ) };
} );
};
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 86b2e8f

Please sign in to comment.