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

REST API: Add additional default template data fields for the active theme #69417

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 0 additions & 2 deletions lib/compat/wordpress-6.8/preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ function gutenberg_block_editor_preload_paths_6_8( $paths, $context ) {
'site_icon_url',
'site_logo',
'timezone_string',
'default_template_part_areas',
'default_template_types',
'url',
'page_for_posts',
'page_on_front',
Expand Down
93 changes: 57 additions & 36 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,6 @@ function ( $taxonomy ) {
}
);

/**
* 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' );

/**
* Adds the site reading options to the REST API index.
*
Expand Down Expand Up @@ -129,3 +93,60 @@ function gutenberg_modify_post_collection_query( $args, WP_REST_Request $request
return $args;
}
add_filter( 'rest_post_query', 'gutenberg_modify_post_collection_query', 10, 2 );

/**
* Registers `default_template_types` and `default_template_part_areas` fields for the active theme.
*
* Note: Backports into the wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php file.
*
* @return void
*/
function gutenberg_register_rest_theme_fields() {
register_rest_field(
'theme',
'default_template_types',
array(
'get_callback' => static function ( $response_data ) {
if ( ! isset( $response_data['status'] ) || 'active' !== $response_data['status'] ) {
return null;
}

$default_template_types = array();
foreach ( get_default_block_template_types() as $slug => $template_type ) {
$template_type['slug'] = (string) $slug;
$default_template_types[] = $template_type;
}

return $default_template_types;
},
'schema' => array(
'description' => __( 'A list of default template types.' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
)
);
register_rest_field(
'theme',
'default_template_part_areas',
array(
'get_callback' => static function ( $response_data ) {
if ( ! isset( $response_data['status'] ) || 'active' !== $response_data['status'] ) {
return null;
}

return get_allowed_block_template_part_areas();
},
'schema' => array(
'description' => __( 'A list of allowed area values for template parts.' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
)
);
}
add_action( 'rest_api_init', 'gutenberg_register_rest_theme_fields' );
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ export default function useTemplatePartAreaLabel( clientId ) {
return;
}

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

const currentTheme = getCurrentTheme();
const defaultTemplatePartAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas || [];
currentTheme?.default_template_part_areas || [];

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

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

for ( const templatePartClientId of parentTemplatePartClientIds ) {
const templatePartBlock = getBlock( templatePartClientId );

// The 'area' usually isn't stored on the block, but instead
// on the entity.
const { theme = getCurrentTheme()?.stylesheet, slug } =
const { theme = currentTheme?.stylesheet, slug } =
templatePartBlock.attributes;
const templatePartEntityId = createTemplatePartId(
theme,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function TemplatePartAdvancedControls( {

const defaultTemplatePartAreas = useSelect(
( select ) =>
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
select( coreStore ).getCurrentTheme()
?.default_template_part_areas || [],
[]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function useTemplatePartArea( area ) {
return useSelect(
( select ) => {
const definedAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
select( coreStore ).getCurrentTheme()
?.default_template_part_areas || [];

const selectedArea = definedAreas.find(
Expand Down
2 changes: 0 additions & 2 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export const rootEntitiesConfig = [
'site_icon_url',
'site_logo',
'timezone_string',
'default_template_part_areas',
'default_template_types',
'url',
'page_for_posts',
'page_on_front',
Expand Down
10 changes: 0 additions & 10 deletions packages/core-data/src/entity-types/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ declare module './base-entity-records' {
* Site URL.
*/
url: string;

/**
* Default template part areas.
*/
default_template_part_areas?: Array< TemplatePartArea >;

/**
* Default template types
*/
default_template_types?: Array< TemplateType >;
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ export const useExistingTemplates = () => {
export const useDefaultTemplateTypes = () => {
return useSelect(
( select ) =>
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_types || [],
select( coreStore ).getCurrentTheme()?.default_template_types || [],
[]
);
};
Expand Down
12 changes: 6 additions & 6 deletions packages/edit-site/src/components/editor/use-editor-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ const { getTemplateInfo } = unlock( editorPrivateApis );
function useEditorTitle( postType, postId ) {
const { title, isLoaded } = useSelect(
( select ) => {
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const {
getEditedEntityRecord,
getCurrentTheme,
hasFinishedResolution,
} = select( coreStore );

if ( ! postId ) {
return { isLoaded: false };
Expand All @@ -33,10 +36,7 @@ function useEditorTitle( postType, postId ) {
);

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

const templateInfo = getTemplateInfo( {
template: _record,
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/page-patterns/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function PatternsHeader( {
const { patternCategories } = usePatternCategories();
const templatePartAreas = useSelect(
( select ) =>
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
select( coreStore ).getCurrentTheme()
?.default_template_part_areas || [],
[]
);
Expand Down
14 changes: 7 additions & 7 deletions packages/edit-site/src/components/page-patterns/use-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ const EMPTY_PATTERN_LIST = [];

const selectTemplateParts = createSelector(
( select, categoryId, search = '' ) => {
const { getEntityRecords, isResolving: isResolvingSelector } =
select( coreStore );
const {
getEntityRecords,
getCurrentTheme,
isResolving: isResolvingSelector,
} = select( coreStore );

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

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

Expand Down Expand Up @@ -79,8 +80,7 @@ const selectTemplateParts = createSelector(
TEMPLATE_PART_POST_TYPE,
{ per_page: -1 },
] ),
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas,
select( coreStore ).getCurrentTheme()?.default_template_part_areas,
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const useTemplatePartsGroupedByArea = ( items ) => {

const templatePartAreas = useSelect(
( select ) =>
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
select( coreStore ).getCurrentTheme()
?.default_template_part_areas || [],
[]
);
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/document-bar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default function DocumentBar( props ) {
const {
getEditedEntityRecord,
getPostType,
getCurrentTheme,
isResolving: isResolvingSelector,
} = select( coreStore );
const _postType = getCurrentPostType();
Expand All @@ -85,8 +86,7 @@ export default function DocumentBar( props ) {
);

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

const _templateInfo = getTemplateInfo( {
templateTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ export default function EntityRecordItem( { record, checked, onChange } ) {
);

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

return {
entityRecordTitle: getTemplateInfo( {
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/post-card-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function PostCardPanel( {
);
const { postTitle, icon, labels } = useSelect(
( select ) => {
const { getEditedEntityRecord, getEntityRecord, getPostType } =
const { getEditedEntityRecord, getCurrentTheme, getPostType } =
select( coreStore );
const { getPostIcon } = unlock( select( editorStore ) );
let _title = '';
Expand All @@ -59,7 +59,7 @@ export default function PostCardPanel( {
);
if ( postIds.length === 1 ) {
const { default_template_types: templateTypes = [] } =
getEntityRecord( 'root', '__unstableBase' ) ?? {};
getCurrentTheme() ?? {};

const _templateInfo = [
TEMPLATE_POST_TYPE,
Expand Down
6 changes: 2 additions & 4 deletions packages/editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ export const getPostIcon = createRegistrySelector(
postType === 'wp_template'
) {
const templateAreas =
select( coreStore ).getEntityRecord(
'root',
'__unstableBase'
)?.default_template_part_areas || [];
select( coreStore ).getCurrentTheme()
?.default_template_part_areas || [];

const areaData = templateAreas.find(
( item ) => options.area === item.area
Expand Down
23 changes: 9 additions & 14 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1709,11 +1709,10 @@ export const __experimentalGetDefaultTemplateTypes = createRegistrySelector(
{
since: '6.8',
alternative:
"select('core/core-data').getEntityRecord( 'root', '__unstableBase' )?.default_template_types",
"select('core/core-data').getCurrentTheme()?.default_template_types",
}
);
return select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_types;
return select( coreStore ).getCurrentTheme()?.default_template_types;
}
);

Expand All @@ -1732,12 +1731,12 @@ export const __experimentalGetDefaultTemplatePartAreas = createRegistrySelector(
{
since: '6.8',
alternative:
"select('core/core-data').getEntityRecord( 'root', '__unstableBase' )?.default_template_part_areas",
"select('core/core-data').getCurrentTheme()?.default_template_part_areas",
}
);

const areas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
select( coreStore ).getCurrentTheme()
?.default_template_part_areas || [];

return areas.map( ( item ) => {
Expand All @@ -1763,10 +1762,8 @@ export const __experimentalGetDefaultTemplateType = createRegistrySelector(
since: '6.8',
}
);
const templateTypes = select( coreStore ).getEntityRecord(
'root',
'__unstableBase'
)?.default_template_types;
const templateTypes =
select( coreStore ).getCurrentTheme()?.default_template_types;

if ( ! templateTypes ) {
return EMPTY_OBJECT;
Expand Down Expand Up @@ -1799,13 +1796,11 @@ export const __experimentalGetTemplateInfo = createRegistrySelector(
return EMPTY_OBJECT;
}

const templateTypes =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_types || [];
const currentTheme = select( coreStore ).getCurrentTheme();

const templateTypes = currentTheme?.default_template_types || [];
const templateAreas =
select( coreStore ).getEntityRecord( 'root', '__unstableBase' )
?.default_template_part_areas || [];
currentTheme?.default_template_part_areas || [];

return getTemplateInfo( {
template,
Expand Down
Loading
Loading