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

Allow plugins to add new items to the "Add new" menu on the Site Editor #46362

Closed
17 changes: 14 additions & 3 deletions lib/compat/wordpress-6.2/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@
*/

/**
* Remove home template setting for WP 6.2.
* Remove home template setting and add filtered extraTemplatesTypes setting for WP 6.2.
*
* @param array $settings Existing block editor settings.
* @param WP_Block_Editor_Context $context The current block editor context.
* @return array
*/
function gutenberg_site_editor_unset_homepage_setting( $settings, $context ) {
function gutenberg_site_editor_update_settings( $settings, $context ) {
if ( isset( $context->post ) ) {
return $settings;
}

unset( $settings['__unstableHomeTemplate'] );

/**
* Filters the list of extra templates.
*
* Template types added in this filter will show up under the `Add new` menu in the Site Editor.
*
* @since 6.2.0
*
* @param array $extra_template_types An array of template types, formatted as [ slug, title, description ] ].
*/
$settings['extraTemplateTypes'] = apply_filters( 'extra_template_types', array() );

return $settings;
}
add_filter( 'block_editor_settings_all', 'gutenberg_site_editor_unset_homepage_setting', 10, 2 );
add_filter( 'block_editor_settings_all', 'gutenberg_site_editor_update_settings', 10, 2 );
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
usePostTypeMenuItems,
useAuthorMenuItem,
usePostTypeArchiveMenuItems,
useExtraTemplateTypes,
} from './utils';
import AddCustomGenericTemplateModal from './add-custom-generic-template-modal';
import TemplateActionsLoadingScreen from './template-actions-loading-screen';
Expand Down Expand Up @@ -277,6 +278,7 @@ function useMissingTemplates(
DEFAULT_TEMPLATE_SLUGS.includes( template.slug ) &&
! existingTemplateSlugs.includes( template.slug )
);
const extraTemplateTypes = useExtraTemplateTypes();
const onClickMenuItem = ( _entityForSuggestions ) => {
setShowCustomTemplateModal( true );
setEntityForSuggestions( _entityForSuggestions );
Expand Down Expand Up @@ -327,5 +329,13 @@ function useMissingTemplates(
...postTypesMenuItems,
...taxonomiesMenuItems,
];
return missingTemplates;

const missingTemplateSlugs = ( missingTemplates || [] ).map(
( { slug } ) => slug
);
const missingExtraTemplateTypes = ( extraTemplateTypes || [] ).filter(
albarin marked this conversation as resolved.
Show resolved Hide resolved
( template ) => ! missingTemplateSlugs.includes( template.slug )
);

return [ ...missingTemplates, ...missingExtraTemplateTypes ];
}
5 changes: 5 additions & 0 deletions packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export const useDefaultTemplateTypes = () => {
[]
);
};
export const useExtraTemplateTypes = () => {
return useSelect( ( select ) => {
return select( editorStore ).__experimentalGetExtraTemplateTypes();
}, [] );
};

const usePublicPostTypes = () => {
const postTypes = useSelect(
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function reinitializeEditor( target, settings ) {
dispatch( editorStore ).updateEditorSettings( {
defaultTemplateTypes: settings.defaultTemplateTypes,
defaultTemplatePartAreas: settings.defaultTemplatePartAreas,
extraTemplateTypes: settings.extraTemplateTypes,
} );
}

Expand Down
11 changes: 11 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,17 @@ export function __experimentalGetDefaultTemplateTypes( state ) {
return getEditorSettings( state )?.defaultTemplateTypes;
}

/**
* Returns the extra template slugs.
*
* @param {Object} state Global application state.
*
* @return {Array} The template slugs.
*/
export function __experimentalGetExtraTemplateTypes( state ) {
return getEditorSettings( state )?.extraTemplateTypes;
}

/**
* Returns the default template part areas.
*
Expand Down