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

Add templates list page for site editor #36379

Merged
merged 14 commits into from Nov 19, 2021
60 changes: 58 additions & 2 deletions lib/full-site-editing/edit-site-page.php
Expand Up @@ -31,6 +31,15 @@ function gutenberg_is_edit_site_page( $page ) {
return 'appearance_page_gutenberg-edit-site' === $page;
}

/**
* Checks whether the provided page is the templates list page.
*
* @return bool True for Site Editor pages, false otherwise.
*/
function gutenberg_is_edit_site_list_page() {
return isset( $_GET['postType'] ) && ! isset( $_GET['postId'] );
Copy link
Member

Choose a reason for hiding this comment

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

Could you not use get_screen or similar.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure what you mean here? Sorry I'm not really familiar with PHP 😅 .

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we can. These are query arguments on the Site Editor page.

}

/**
* Load editor styles (this is copied from edit-form-blocks.php).
* Ideally the code is extracted into a reusable function.
Expand Down Expand Up @@ -68,7 +77,50 @@ function gutenberg_get_editor_styles() {
}

/**
* Initialize the Gutenberg Edit Site Page.
* Initialize the Gutenberg Templates List Page.
*/
function gutenberg_edit_site_list_init() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed the word 'list' is used throughout, and wonder if it's a bit too vague. Something more descriptive like 'entity list' might be an option?

wp_enqueue_script( 'wp-edit-site' );
wp_enqueue_style( 'wp-edit-site' );
wp_enqueue_media();

$template_type = $_GET['postType'];
Copy link
Member

Choose a reason for hiding this comment

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

We need to bail here if an invalid postType is provided, otherwise a malicious bit of HTML can be output when it is interpolated into the wp.editSite.initializeList below.

Fix: #36706

$post_type = get_post_type_object( $template_type );

$preload_data = array_reduce(
array(
'/',
"/wp/v2/types/$template_type?context=edit",
'/wp/v2/types?context=edit',
"/wp/v2/$post_type->rest_base?context=edit",
),
'rest_preload_api_request',
array()
);

wp_add_inline_script(
'wp-api-fetch',
sprintf(
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',
wp_json_encode( $preload_data )
),
'after'
);

wp_add_inline_script(
'wp-edit-site',
sprintf(
'wp.domReady( function() {
wp.editSite.initializeList( "%s", "%s" );
} );',
'edit-site-editor',
$template_type
)
);
}

/**
* Initialize the Gutenberg Site Editor.
*
* @since 7.2.0
*
Expand All @@ -90,6 +142,10 @@ static function( $classes ) {
}
);

if ( gutenberg_is_edit_site_list_page() ) {
return gutenberg_edit_site_list_init();
}

/**
* Make the WP Screen object aware that this is a block editor page.
* Since custom blocks check whether the screen is_block_editor,
Expand Down Expand Up @@ -135,7 +191,7 @@ static function( $classes ) {
'/wp/v2/themes/' . $active_theme . '/global-styles',
)
),
'initializer_name' => 'initialize',
'initializer_name' => 'initializeEditor',
'editor_settings' => $settings,
)
);
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/template-parts.php
Expand Up @@ -42,7 +42,7 @@ function gutenberg_register_template_part_post_type() {
'public' => false,
'has_archive' => false,
'show_ui' => true,
'show_in_menu' => 'themes.php',
'show_in_menu' => false,
'show_in_admin_bar' => false,
'show_in_rest' => true,
'rest_base' => 'template-parts',
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/templates.php
Expand Up @@ -42,7 +42,7 @@ function gutenberg_register_template_post_type() {
'public' => false,
'has_archive' => false,
'show_ui' => true,
'show_in_menu' => 'themes.php',
'show_in_menu' => false,
'show_in_admin_bar' => false,
'show_in_rest' => true,
'rest_base' => 'templates',
Expand Down
8 changes: 0 additions & 8 deletions lib/init.php
Expand Up @@ -106,14 +106,6 @@ function gutenberg_site_editor_menu() {
'gutenberg-edit-site',
'gutenberg_edit_site_page'
);

add_theme_page(
__( 'Styles', 'gutenberg' ),
__( 'Styles', 'gutenberg' ),
'edit_theme_options',
'gutenberg-edit-site&styles=open',
'gutenberg_edit_site_page'
);
}
}
add_action( 'admin_menu', 'gutenberg_site_editor_menu', 9 );
Expand Down
18 changes: 7 additions & 11 deletions packages/edit-site/src/components/editor/index.js
Expand Up @@ -33,9 +33,9 @@ import { ShortcutProvider } from '@wordpress/keyboard-shortcuts';
*/
import Header from '../header';
import { SidebarComplementaryAreaFills } from '../sidebar';
import NavigationSidebar from '../navigation-sidebar';
import BlockEditor from '../block-editor';
import KeyboardShortcuts from '../keyboard-shortcuts';
import NavigationSidebar from '../navigation-sidebar';
import URLQueryController from '../url-query-controller';
import InserterSidebar from '../secondary-sidebar/inserter-sidebar';
import ListViewSidebar from '../secondary-sidebar/list-view-sidebar';
Expand Down Expand Up @@ -113,11 +113,7 @@ function Editor( { initialSettings, onError } ) {
// 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.
const {
defaultTemplateTypes,
defaultTemplatePartAreas,
__experimentalNewMenuSidebar: newMenuSidebar,
} = settings;
const { defaultTemplateTypes, defaultTemplatePartAreas } = settings;
useEffect( () => {
updateEditorSettings( {
defaultTemplateTypes,
Expand Down Expand Up @@ -219,17 +215,17 @@ function Editor( { initialSettings, onError } ) {
<SidebarComplementaryAreaFills />
<InterfaceSkeleton
labels={ interfaceLabels }
drawer={
newMenuSidebar ? undefined : (
<NavigationSidebar />
)
}
secondarySidebar={ secondarySidebar() }
sidebar={
sidebarIsOpened && (
<ComplementaryArea.Slot scope="core/edit-site" />
)
}
drawer={
<NavigationSidebar
defaultIsOpen={ false }
/>
}
header={
<Header
openEntitiesSavedStates={
Expand Down
8 changes: 8 additions & 0 deletions packages/edit-site/src/components/editor/style.scss
@@ -1,3 +1,11 @@
html #wpadminbar {
display: none;
}

html.wp-toolbar {
padding-top: 0;
}

.edit-site-editor__toggle-save-panel {
z-index: z-index(".edit-site-editor__toggle-save-panel");
position: fixed !important; // Need to override the default relative positioning
Expand Down
11 changes: 0 additions & 11 deletions packages/edit-site/src/components/header/index.js
Expand Up @@ -19,15 +19,13 @@ import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import NavigationLink from './navigation-link';
import MoreMenu from './more-menu';
import SaveButton from '../save-button';
import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
import DocumentActions from './document-actions';
import TemplateDetails from '../template-details';
import { store as editSiteStore } from '../../store';
import MainDashboardButton from '../main-dashboard-button';

const preventDefault = ( event ) => {
event.preventDefault();
Expand All @@ -47,15 +45,13 @@ export default function Header( {
isListViewOpen,
listViewShortcut,
isLoaded,
newMenuSidebar,
} = useSelect( ( select ) => {
const {
__experimentalGetPreviewDeviceType,
getEditedPostType,
getEditedPostId,
isInserterOpened,
isListViewOpened,
getSettings,
} = select( editSiteStore );
const { getEditedEntityRecord } = select( coreStore );
const { __experimentalGetTemplateInfo: getTemplateInfo } = select(
Expand All @@ -79,7 +75,6 @@ export default function Header( {
listViewShortcut: getShortcutRepresentation(
'core/edit-site/toggle-list-view'
),
newMenuSidebar: getSettings().__experimentalNewMenuSidebar,
};
}, [] );

Expand Down Expand Up @@ -110,12 +105,6 @@ export default function Header( {
return (
<div className="edit-site-header">
<div className="edit-site-header_start">
{ newMenuSidebar && (
<MainDashboardButton.Slot>
<NavigationLink />
</MainDashboardButton.Slot>
) }

<div className="edit-site-header__toolbar">
<Button
ref={ inserterButton }
Expand Down
95 changes: 0 additions & 95 deletions packages/edit-site/src/components/header/navigation-link/index.js

This file was deleted.

This file was deleted.