Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move default template types and template part areas to REST API #66459

Merged
merged 28 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b795e9c
move default template types and template part areas to REST API
gigitux Oct 25, 2024
61b90b5
fix logic
gigitux Oct 25, 2024
6aaf160
fix E2E test
gigitux Nov 1, 2024
d141278
move default template types and template part areas to REST API
gigitux Nov 4, 2024
bde1d80
fix error
gigitux Nov 4, 2024
de703b5
remove not necessary file
gigitux Nov 4, 2024
f8962ce
fix naming
gigitux Nov 4, 2024
fc95c3f
remove duplicate code
gigitux Nov 4, 2024
d20b8ad
remove duplicated code
gigitux Nov 5, 2024
d361487
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/rest-…
gigitux Nov 5, 2024
afe5365
improve logic
gigitux Nov 5, 2024
ac06843
fix naming
gigitux Nov 5, 2024
69c3d6d
fix unit test
gigitux Nov 5, 2024
a5647de
update doc
gigitux Nov 5, 2024
2a04ba1
add unit test for getTemplateInfo function
gigitux Nov 5, 2024
6723634
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/rest-…
gigitux Nov 14, 2024
7241bad
restore not necessary changes
gigitux Nov 14, 2024
8f47873
fix e2e test
gigitux Nov 14, 2024
6acc708
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/rest-…
gigitux Nov 25, 2024
7aab231
remove not necessary variable
gigitux Nov 25, 2024
ffdcc1d
replace add_action with add_filter
gigitux Nov 25, 2024
e395bf6
improve readibility code
gigitux Nov 25, 2024
1f47713
make getTemplateInfo private
gigitux Nov 25, 2024
078d2f6
make templateAreas optional
gigitux Nov 25, 2024
fdc291d
add default_template_part_areas and default_template_types
gigitux Nov 25, 2024
b2c0bf5
move code to rest-api.php file
gigitux Nov 25, 2024
8f8649b
Merge branch 'trunk' of github.com:WordPress/gutenberg into fix/rest-…
gigitux Nov 26, 2024
230ba72
remove not used import
gigitux Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/compat/wordpress-6.8/preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function gutenberg_block_editor_preload_paths_6_8( $paths, $context ) {
'site_logo',
'timezone_string',
'url',
'default_template_part_areas',
'default_template_types',
)
);
$paths[] = '/wp/v2/templates/lookup?slug=front-page';
Expand Down
38 changes: 38 additions & 0 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,41 @@ function ( $taxonomy ) {
remove_filter( "rest_{$taxonomy}_query", 'gutenberg_respect_taxonomy_default_args_in_rest_api' );
}
);

/**
* 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' );
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useSelect } from '@wordpress/data';

// TODO: this util should perhaps be refactored somewhere like core-data.
import { createTemplatePartId } from '../template-part/edit/utils/create-template-part-id';
import { getTemplatePartIcon } from '../template-part/edit/utils/get-template-part-icon';

export default function useTemplatePartAreaLabel( clientId ) {
return useSelect(
Expand All @@ -35,16 +36,15 @@ export default function useTemplatePartAreaLabel( clientId ) {
return;
}

// FIXME: @wordpress/block-library should not depend on @wordpress/editor.
// Blocks can be loaded into a *non-post* block editor.
// This code is lifted from this file:
// packages/block-library/src/template-part/edit/advanced-controls.js
/* eslint-disable @wordpress/data-no-store-string-literals */
const definedAreas =
select(
'core/editor'
).__experimentalGetDefaultTemplatePartAreas();
/* eslint-enable @wordpress/data-no-store-string-literals */
const defaultTemplatePartAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas || [];

const definedAreas = defaultTemplatePartAreas.map( ( item ) => ( {
...item,
icon: getTemplatePartIcon( item.icon ),
} ) );

const { getCurrentTheme, getEditedEntityRecord } =
select( coreStore );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { SelectControl, TextControl } from '@wordpress/components';
import { sprintf, __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -54,19 +54,19 @@ export function TemplatePartAdvancedControls( {
templatePartId
);

const definedAreas = useSelect( ( select ) => {
// FIXME: @wordpress/block-library should not depend on @wordpress/editor.
// Blocks can be loaded into a *non-post* block editor.
/* eslint-disable-next-line @wordpress/data-no-store-string-literals */
return select(
'core/editor'
).__experimentalGetDefaultTemplatePartAreas();
}, [] );
const defaultTemplatePartAreas = useSelect(
( select ) =>
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas || [],
[]
);

const areaOptions = definedAreas.map( ( { label, area: _area } ) => ( {
label,
value: _area,
} ) );
const areaOptions = defaultTemplatePartAreas.map(
( { label, area: _area } ) => ( {
label,
value: _area,
} )
);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import {
header as headerIcon,
footer as footerIcon,
sidebar as sidebarIcon,
symbolFilled as symbolFilledIcon,
} from '@wordpress/icons';

export const getTemplatePartIcon = ( iconName ) => {
if ( 'header' === iconName ) {
return headerIcon;
} else if ( 'footer' === iconName ) {
return footerIcon;
} else if ( 'sidebar' === iconName ) {
return sidebarIcon;
}
return symbolFilledIcon;
};
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,9 @@ export function useCreateTemplatePartFromBlocks( area, setAttributes ) {
export function useTemplatePartArea( area ) {
return useSelect(
( select ) => {
// FIXME: @wordpress/block-library should not depend on @wordpress/editor.
// Blocks can be loaded into a *non-post* block editor.
/* eslint-disable @wordpress/data-no-store-string-literals */
const definedAreas =
select(
'core/editor'
).__experimentalGetDefaultTemplatePartAreas();
/* eslint-enable @wordpress/data-no-store-string-literals */
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas || [];

const selectedArea = definedAreas.find(
( definedArea ) => definedArea.area === area
Expand Down
20 changes: 4 additions & 16 deletions packages/block-library/src/template-part/variations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,11 @@
*/
import { store as coreDataStore } from '@wordpress/core-data';
import { select } from '@wordpress/data';
import {
header as headerIcon,
footer as footerIcon,
sidebar as sidebarIcon,
symbolFilled as symbolFilledIcon,
} from '@wordpress/icons';

function getTemplatePartIcon( iconName ) {
if ( 'header' === iconName ) {
return headerIcon;
} else if ( 'footer' === iconName ) {
return footerIcon;
} else if ( 'sidebar' === iconName ) {
return sidebarIcon;
}
return symbolFilledIcon;
}
/**
* Internal dependencies
*/
import { getTemplatePartIcon } from './edit/utils/get-template-part-icon';

export function enhanceTemplatePartVariations( settings, name ) {
if ( name !== 'core/template-part' ) {
Expand Down
2 changes: 2 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const rootEntitiesConfig = [
'site_icon_url',
'site_logo',
'timezone_string',
'default_template_part_areas',
'default_template_types',
Comment on lines +34 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes here also require an update to the preload path in lib/compat/wordpress-6.8/preload.php. See the comment above the _fields property.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL! Thanks! fdc291d

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't you keep the same order?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #67450.

'url',
].join( ',' ),
},
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { decodeEntities } from '@wordpress/html-entities';
import { useMemo, useCallback } from '@wordpress/element';
import { __, _x, sprintf } from '@wordpress/i18n';
Expand Down Expand Up @@ -69,7 +68,8 @@ export const useExistingTemplates = () => {
export const useDefaultTemplateTypes = () => {
return useSelect(
( select ) =>
select( editorStore ).__experimentalGetDefaultTemplateTypes(),
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_types || [],
[]
);
};
Expand Down
21 changes: 17 additions & 4 deletions packages/edit-site/src/components/editor/use-editor-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,46 @@
import { _x, sprintf } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { decodeEntities } from '@wordpress/html-entities';
import { privateApis as editorPrivateApis } from '@wordpress/editor';

/**
* Internal dependencies
*/
import useTitle from '../routes/use-title';
import { POST_TYPE_LABELS, TEMPLATE_POST_TYPE } from '../../utils/constants';
import { unlock } from '../../lock-unlock';

const { getTemplateInfo } = unlock( editorPrivateApis );

function useEditorTitle( postType, postId ) {
const { title, isLoaded } = useSelect(
( select ) => {
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const { __experimentalGetTemplateInfo: getTemplateInfo } =
select( editorStore );

const _record = getEditedEntityRecord(
'postType',
postType,
postId
);

const { default_template_types: templateTypes = [] } =
select( coreStore ).getEntityRecord(
'root',
'__unstableBase'
) ?? {};

const templateInfo = getTemplateInfo( {
template: _record,
templateTypes,
} );

const _isLoaded = hasFinishedResolution( 'getEditedEntityRecord', [
'postType',
postType,
postId,
] );
const templateInfo = getTemplateInfo( _record );

return {
title: templateInfo.title,
Expand Down
5 changes: 3 additions & 2 deletions packages/edit-site/src/components/page-patterns/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
__experimentalText as Text,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { moreVertical } from '@wordpress/icons';
Expand All @@ -32,7 +32,8 @@ export default function PatternsHeader( {
const { patternCategories } = usePatternCategories();
const templatePartAreas = useSelect(
( select ) =>
select( editorStore ).__experimentalGetDefaultTemplatePartAreas(),
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas || [],
[]
);

Expand Down
12 changes: 7 additions & 5 deletions packages/edit-site/src/components/page-patterns/use-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { parse } from '@wordpress/blocks';
import { useSelect, createSelector } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { useMemo } from '@wordpress/element';

/**
Expand All @@ -28,8 +27,7 @@ const selectTemplateParts = createSelector(
( select, categoryId, search = '' ) => {
const { getEntityRecords, isResolving: isResolvingSelector } =
select( coreStore );
const { __experimentalGetDefaultTemplatePartAreas } =
select( editorStore );

const query = { per_page: -1 };
const templateParts =
getEntityRecords( 'postType', TEMPLATE_PART_POST_TYPE, query ) ??
Expand All @@ -38,7 +36,10 @@ 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 = __experimentalGetDefaultTemplatePartAreas() || [];
const knownAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas || [];

const templatePartAreas = knownAreas.map( ( area ) => area.area );

const templatePartHasCategory = ( item, category ) => {
Expand Down Expand Up @@ -78,7 +79,8 @@ const selectTemplateParts = createSelector(
TEMPLATE_PART_POST_TYPE,
{ per_page: -1 },
] ),
select( editorStore ).__experimentalGetDefaultTemplatePartAreas(),
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas,
]
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* WordPress dependencies
*/
import { useEntityRecords } from '@wordpress/core-data';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';

/**
* Internal dependencies
Expand All @@ -18,7 +17,8 @@ const useTemplatePartsGroupedByArea = ( items ) => {

const templatePartAreas = useSelect(
( select ) =>
select( editorStore ).__experimentalGetDefaultTemplatePartAreas(),
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas || [],
[]
);

Expand All @@ -43,7 +43,7 @@ const useTemplatePartsGroupedByArea = ( items ) => {
const key = accumulator[ item.area ]
? item.area
: TEMPLATE_PART_AREA_DEFAULT_CATEGORY;
accumulator[ key ].templateParts.push( item );
accumulator[ key ]?.templateParts?.push( item );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be related. Does it fix a different error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is related. These changes affect the loading process. Since the variables aren’t preloaded anymore, knownAreas might not be defined when the page loads.

return accumulator;
}, knownAreas );

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 @@ -88,15 +85,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,
} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good riddance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, I think there's some php code that we need to remove from the core as well (setting these in the editor setting) in the backport PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// 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
Loading
Loading