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

Editor: New default rendering mode for editor via post type supports #68549

Merged
merged 5 commits into from
Jan 15, 2025
Merged
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/8123.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/8123

* https://github.com/WordPress/gutenberg/pull/68549

This file was deleted.

58 changes: 12 additions & 46 deletions lib/compat/wordpress-6.8/post.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,26 @@
<?php
/**
* Temporary compatibility shims for block APIs present in Gutenberg.
*
* @package gutenberg
*/

/**
* Get the available rendering modes for the Block Editor.
* Set the default editor mode for the page post type to `template-locked`.
*
* post-only: This mode extracts the post blocks from the template and renders only those.
* The idea is to allow the user to edit the post/page in isolation without the wrapping template.
* Note: This backports into `create_initial_post_types` in WordPress Core.
*
* template-locked: This mode renders both the template and the post blocks
* but the template blocks are locked and can't be edited. The post blocks are editable.
*
* @return array Array of available rendering modes.
*/
function gutenberg_post_type_rendering_modes() {
return array(
'post-only',
'template-locked',
);
}

/**
* Add the default_rendering_mode property to the WP_Post_Type object.
* This property can be overwritten by using the post_type_default_rendering_mode filter.
*
* @param array $args Array of post type arguments.
* @param string $post_type Post type key.
* @param array $args Array of post type arguments.
* @return array Updated array of post type arguments.
*/
function gutenberg_post_type_default_rendering_mode( $args, $post_type ) {
if ( ! wp_is_block_theme() || ! current_theme_supports( 'block-templates' ) ) {
function gutenberg_update_page_editor_support( $args ) {
if ( empty( $args['supports'] ) ) {
return $args;
}

// Make sure the post type supports the block editor.
if (
( isset( $args['show_in_rest'] ) && $args['show_in_rest'] ) &&
( ! empty( $args['supports'] ) && in_array( 'editor', $args['supports'], true ) )
) {
$rendering_mode = 'page' === $post_type ? 'template-locked' : 'post-only';
$rendering_modes = gutenberg_post_type_rendering_modes();

// Validate the supplied rendering mode.
if (
isset( $args['default_rendering_mode'] ) &&
in_array( $args['default_rendering_mode'], $rendering_modes, true )
) {
$rendering_mode = $args['default_rendering_mode'];
}

$args['default_rendering_mode'] = $rendering_mode;
$editor_support_key = array_search( 'editor', $args['supports'], true );
if ( false !== $editor_support_key ) {
unset( $args['supports'][ $editor_support_key ] );
$args['supports']['editor'] = array(
'default_mode' => 'template-locked',
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 know I'm focusing on non-important issueshere, but what's the naming convention here? Should it be default-mode instead of default_mode?

I recently examined the add_theme_support method, which seems to use dashes instead of underscores.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Mamaduka I'd go with consistency while it's possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, @peterwilsoncc!

I'll update the argument to use the dash separator.

Copy link
Member Author

Choose a reason for hiding this comment

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

Here's the follow-up #68745.

);
}

return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_post_type_default_rendering_mode', 10, 2 );
add_action( 'register_page_post_type_args', 'gutenberg_update_page_editor_support' );
15 changes: 0 additions & 15 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@
* @package gutenberg
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}

if ( ! function_exists( 'gutenberg_add_post_type_rendering_mode' ) ) {
/**
* Add Block Editor default rendering mode to the post type response.
*/
function gutenberg_add_post_type_rendering_mode() {
$controller = new Gutenberg_REST_Post_Types_Controller_6_8();
$controller->register_routes();
}
}
add_action( 'rest_api_init', 'gutenberg_add_post_type_rendering_mode' );

// When querying terms for a given taxonomy in the REST API, respect the default
// query arguments set for that taxonomy upon registration.
function gutenberg_respect_taxonomy_default_args_in_rest_api( $args ) {
Expand Down
1 change: 0 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function gutenberg_is_experiment_enabled( $name ) {
// WordPress 6.8 compat.
require __DIR__ . '/compat/wordpress-6.8/block-comments.php';
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php';
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php';
require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-hierarchical-sort.php';
require __DIR__ . '/compat/wordpress-6.8/rest-api.php';

Expand Down
36 changes: 24 additions & 12 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const NON_CONTEXTUAL_POST_TYPES = [
'wp_template_part',
];

/**
* These are rendering modes that the editor supports.
*/
const RENDERING_MODES = [ 'post-only', 'template-locked' ];

/**
* Depending on the post, template and template mode,
* returns the appropriate blocks and change handlers for the block editor provider.
Expand Down Expand Up @@ -171,7 +176,6 @@ export const ExperimentalEditorProvider = withRegistryProvider(
mode,
defaultMode,
postTypeEntities,
hasLoadedPostObject,
} = useSelect(
( select ) => {
const {
Expand All @@ -180,24 +184,32 @@ export const ExperimentalEditorProvider = withRegistryProvider(
getRenderingMode,
__unstableIsEditorReady,
} = select( editorStore );
const { getEntitiesConfig } = select( coreStore );
const {
getEntitiesConfig,
getPostType,
hasFinishedResolution,
} = select( coreStore );

const postTypeObject = select( coreStore ).getPostType(
post.type
const postTypeSupports = getPostType( post.type )?.supports;
const hasLoadedPostObject = hasFinishedResolution(
'getPostType',
[ post.type ]
);

const _hasLoadedPostObject = select(
coreStore
).hasFinishedResolution( 'getPostType', [ post.type ] );
const _defaultMode = Array.isArray( postTypeSupports?.editor )
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
? postTypeSupports.editor.find(
( features ) => 'default_mode' in features
Copy link
Member Author

Choose a reason for hiding this comment

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

What do you think about using default_mode as the key for this option, or should I revert it to rendering_mode?

)?.default_mode
: undefined;
const hasDefaultMode = RENDERING_MODES.includes( _defaultMode );
Copy link
Member Author

Choose a reason for hiding this comment

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

Had to go with the client-side validation since it's not possible to validate arguments passed via add_post_type_support.


return {
hasLoadedPostObject: _hasLoadedPostObject,
editorSettings: getEditorSettings(),
isReady: __unstableIsEditorReady(),
isReady: __unstableIsEditorReady() && hasLoadedPostObject,
mode: getRenderingMode(),
defaultMode:
hasTemplate && postTypeObject?.default_rendering_mode
? postTypeObject?.default_rendering_mode
hasTemplate && hasDefaultMode
? _defaultMode
: 'post-only',
selection: getEditorSelection(),
postTypeEntities:
Expand Down Expand Up @@ -334,7 +346,7 @@ export const ExperimentalEditorProvider = withRegistryProvider(
// Register the editor commands.
useCommands();

if ( ! isReady || ! mode || ! hasLoadedPostObject ) {
if ( ! isReady || ! mode ) {
return null;
}

Expand Down
Loading