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

Boilerplate for classic stylebook #66851

Draft
wants to merge 17 commits into
base: trunk
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions backport-changelog/6.8/7865.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7865

* https://github.com/WordPress/gutenberg/pull/66851
13 changes: 13 additions & 0 deletions lib/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ function gutenberg_get_block_editor_settings( $settings ) {
$global_styles[] = $block_classes;
}

// Get any additional css from the customizer and add it before global styles custom CSS.
$global_styles[] = array(
'css' => wp_get_custom_css(),
'__unstableType' => 'user',
'isGlobalStyles' => false,
);

/*
* Add the custom CSS as a separate stylesheet so any invalid CSS
* entered by users does not break other global styles.
Expand All @@ -74,6 +81,12 @@ function gutenberg_get_block_editor_settings( $settings ) {
$block_classes['css'] = $actual_css;
$global_styles[] = $block_classes;
}
// Get any additional css from the customizer.
$global_styles[] = array(
'css' => wp_get_custom_css(),
'__unstableType' => 'user',
'isGlobalStyles' => false,
);
}

$settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );
Expand Down
42 changes: 42 additions & 0 deletions lib/experimental/stylebook/classic-screen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Set up a screen to show stylebook for classic themes.
*
* @package gutenberg
*/

/**
* Add a Styles submenu under the Appearance menu
* for Classic themes.
*
* @global array $submenu
*/
function gutenberg_add_styles_submenu_item() {
if ( ! wp_is_block_theme() ) {
global $submenu;

$styles_menu_item = array(
__( 'Styles', 'gutenberg' ),
'edit_theme_options',
'site-editor.php?path=/style-book',
);
// Insert the Styles submenu item at position 2.
array_splice( $submenu['themes.php'], 2, 0, array( $styles_menu_item ) );
}
}
add_action( 'admin_init', 'gutenberg_add_styles_submenu_item' );

/**
* Filter the `wp_die_handler` to allow access to the Site Editor's Styles page
* for Classic themes.
*
* @param callable $default_handler The default handler.
* @return callable The default handler or a custom handler.
*/
function gutenberg_styles_wp_die_handler( $default_handler ) {
if ( ! wp_is_block_theme() && str_contains( $_SERVER['REQUEST_URI'], 'site-editor.php' ) && isset( $_GET['path'] ) && 'style-book' === sanitize_key( $_GET['path'] ) ) {
return '__return_false';
}
return $default_handler;
}
add_filter( 'wp_die_handler', 'gutenberg_styles_wp_die_handler' );
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel the url check that is done in Core should be removed entirely. We should just allow access to the full site editor in classic themes and let the frontend code handle the routing.

Copy link
Contributor

Choose a reason for hiding this comment

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

(nothing to be done here but maybe something for the backport PR)

1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/synchronization.php';
require __DIR__ . '/experimental/script-modules.php';
require __DIR__ . '/experimental/posts/load.php';
require __DIR__ . '/experimental/stylebook/classic-screen.php';

if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) {
require __DIR__ . '/experimental/disable-tinymce.php';
Expand Down
11 changes: 10 additions & 1 deletion packages/edit-site/src/components/resizable-frame/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
import { useInstanceId, useReducedMotion } from '@wordpress/compose';
import { __, isRTL } from '@wordpress/i18n';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
Expand Down Expand Up @@ -105,6 +107,10 @@ function ResizableFrame( {
'edit-site-resizable-frame-handle-help'
);
const defaultAspectRatio = defaultSize.width / defaultSize.height;
const isBlockTheme = useSelect( ( select ) => {
const { getCurrentTheme } = select( coreStore );
return getCurrentTheme()?.is_block_theme;
}, [] );

const handleResizeStart = ( _event, _direction, ref ) => {
// Remember the starting width so we don't have to get `ref.offsetWidth` on
Expand Down Expand Up @@ -152,7 +158,10 @@ function ResizableFrame( {
const remainingWidth =
ref.ownerDocument.documentElement.offsetWidth - ref.offsetWidth;

if ( remainingWidth > SNAP_TO_EDIT_CANVAS_MODE_THRESHOLD ) {
if (
remainingWidth > SNAP_TO_EDIT_CANVAS_MODE_THRESHOLD ||
! isBlockTheme
) {
// Reset the initial aspect ratio if the frame is resized slightly
// above the sidebar but not far enough to trigger full screen.
setFrameSize( INITIAL_FRAME_SIZE );
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/site-editor-routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { navigationEditRoute } from './navigation-edit';
import { navigationItemEditRoute } from './navigation-item-edit';
import { navigationItemViewRoute } from './navigation-item-view';
import { stylesViewRoute } from './styles-view';
import { staticStylebookRoute } from './static-stylebook';
import { patternsEditRoute } from './patterns-edit';
import { patternsViewRoute } from './patterns-view';
import { templatesEditRoute } from './templates-edit';
Expand All @@ -39,6 +40,7 @@ const routes = [
patternsViewRoute,
patternsEditRoute,
stylesViewRoute,
staticStylebookRoute,
navigationItemViewRoute,
navigationItemEditRoute,
navigationViewRoute,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import { StyleBookPreview } from '../style-book';

export const staticStylebookRoute = {
name: 'static-stylebook',
match: ( params ) => {
return (
params.path &&
params.path.startsWith( '/style-book' ) &&
params.canvas !== 'edit'
);
},
areas: {
sidebar: (
<SidebarNavigationScreen
title={ __( 'Styles' ) }
description={ __( 'Overview of styled blocks.' ) }
isRoot
/>
),
preview: <StyleBookPreview />,
mobile: <StyleBookPreview />,
},
widths: {
content: 380,
},
};
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/style-book/examples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ function getOverviewBlockExamples(
): BlockExample[] {
const examples: BlockExample[] = [];

// Get theme palette from colors.
const themePalette = colors.colors.find(
// Get theme palette from colors if they exist.
const themePalette = colors?.colors.find(
( origin: ColorOrigin ) => origin.slug === 'theme'
);

Expand Down
Loading
Loading