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 5a5a7e9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 29 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
23 changes: 10 additions & 13 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,10 +1715,12 @@ 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 ) };
} );
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 5a5a7e9

Please sign in to comment.