-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 24 commits
b795e9c
61b90b5
6aaf160
d141278
bde1d80
de703b5
f8962ce
fc95c3f
d20b8ad
d361487
afe5365
ac06843
69c3d6d
a5647de
2a04ba1
6723634
7241bad
8f47873
6acc708
7aab231
ffdcc1d
e395bf6
1f47713
078d2f6
fdc291d
b2c0bf5
8f8649b
230ba72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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['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 |
---|---|---|
@@ -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 |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes here also require an update to the preload path in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL! Thanks! fdc291d There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why didn't you keep the same order? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #67450. |
||
'url', | ||
].join( ',' ), | ||
}, | ||
|
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 | ||
|
@@ -18,7 +17,8 @@ const useTemplatePartsGroupedByArea = ( items ) => { | |
|
||
const templatePartAreas = useSelect( | ||
( select ) => | ||
select( editorStore ).__experimentalGetDefaultTemplatePartAreas(), | ||
select( coreStore ).getEntityRecord( 'root', '__unstableBase' ) | ||
?.default_template_part_areas || [], | ||
[] | ||
); | ||
|
||
|
@@ -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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
return accumulator; | ||
}, knownAreas ); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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, | ||
} ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good riddance. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file name is a bit weird but it's not important. Could be rest-api.php or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. I think a
rest-api.php
file is already in WP 6.8 compat dir. We can move this code there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! b2c0bf5