-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Feature: Set editor rendering mode by post type #62304
Changes from all commits
ab4b075
ca0de5d
3beef1e
9b276d2
a26a583
b538ae6
e3912d4
702a5be
964a148
aabbca8
c9f2274
298c858
d85f56c
9c631fe
de00c45
bf3fbca
5c98bf6
40c2fa3
c2b1f92
1f7ff09
40ce652
b361cd5
24118c7
a0a766c
882f69c
a2ac4b3
3496d55
65c264b
a27c07e
ac9556d
148837e
3619aed
b080013
c07a005
42bbbae
f38b926
5d9a3f4
4967bf0
e44556d
7e72459
1b18340
a55cd68
429b338
d523598
41601ec
05abd41
4904b5a
40d7324
c2d58a9
9e8e08c
b94e309
a742e4a
fcb1449
ec543c5
2aa288d
f0c680f
557ec18
9068ab1
4512fce
f282a47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/7129 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/62304 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* REST API: Gutenberg_REST_Post_Types_Controller_6_8 class | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Gutenberg_REST_Post_Types_Controller_6_8 class | ||
* | ||
* Add Block Editor default rendering mode to the post type response | ||
* to allow enabling/disabling at the post type level. | ||
*/ | ||
class Gutenberg_REST_Post_Types_Controller_6_8 extends WP_REST_Post_Types_Controller { | ||
/** | ||
* Add Block Editor default rendering mode setting to the response. | ||
* | ||
* @param WP_Post_Type $item Post type object. | ||
* @param WP_REST_Request $request Request object. | ||
* @return WP_REST_Response Response object. | ||
*/ | ||
public function prepare_item_for_response( $item, $request ) { | ||
$response = parent::prepare_item_for_response( $item, $request ); | ||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; | ||
|
||
// Property will only exist if the post type supports the block editor. | ||
if ( 'edit' === $context && property_exists( $item, 'default_rendering_mode' ) ) { | ||
/** | ||
* Filters the block editor rendering mode for a post type. | ||
* | ||
* @since 6.8.0 | ||
* @param string $default_rendering_mode Default rendering mode for the post type. | ||
* @param WP_Post_Type $post_type Post type name. | ||
* @return string Default rendering mode for the post type. | ||
*/ | ||
$rendering_mode = apply_filters( 'post_type_default_rendering_mode', $item->default_rendering_mode, $item ); | ||
|
||
/** | ||
* Filters the block editor rendering mode for a specific post type. | ||
* Applied after the generic `post_type_default_rendering_mode` filter. | ||
* | ||
* The dynamic portion of the hook name, `$item->name`, refers to the post type slug. | ||
* | ||
* @since 6.8.0 | ||
* @param string $default_rendering_mode Default rendering mode for the post type. | ||
* @param WP_Post_Type $post_type Post type object. | ||
* @return string Default rendering mode for the post type. | ||
*/ | ||
$rendering_mode = apply_filters( "post_type_{$item->name}_default_rendering_mode", $rendering_mode, $item ); | ||
|
||
// Validate the filtered rendering mode. | ||
if ( ! in_array( $rendering_mode, gutenberg_post_type_rendering_modes(), true ) ) { | ||
$rendering_mode = 'post-only'; | ||
} | ||
|
||
$response->data['default_rendering_mode'] = $rendering_mode; | ||
} | ||
|
||
return rest_ensure_response( $response ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Temporary compatibility shims for block APIs present in Gutenberg. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Get the available rendering modes for the Block Editor. | ||
* | ||
* 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. | ||
* | ||
* 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. | ||
* @return array Updated array of post type arguments. | ||
*/ | ||
function gutenberg_post_type_default_rendering_mode( $args, $post_type ) { | ||
$rendering_mode = 'page' === $post_type ? 'template-locked' : 'post-only'; | ||
$rendering_modes = gutenberg_post_type_rendering_modes(); | ||
|
||
// Make sure the post type supports the block editor. | ||
if ( | ||
wp_is_block_theme() && | ||
( isset( $args['show_in_rest'] ) && $args['show_in_rest'] ) && | ||
( isset( $args['supports'] ) && in_array( 'editor', $args['supports'], true ) ) | ||
) { | ||
// 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surprised that we even allow adding custom args... We should have added an allowlist. |
||
} | ||
|
||
return $args; | ||
} | ||
add_filter( 'register_post_type_args', 'gutenberg_post_type_default_rendering_mode', 10, 2 ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* PHP and WordPress configuration compatibility functions for the Gutenberg | ||
* editor plugin changes related to REST API. | ||
* | ||
* @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' ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,8 +72,7 @@ const NON_CONTEXTUAL_POST_TYPES = [ | |
* @return {Array} Block editor props. | ||
*/ | ||
function useBlockEditorProps( post, template, mode ) { | ||
const rootLevelPost = | ||
mode === 'post-only' || ! template ? 'post' : 'template'; | ||
const rootLevelPost = mode === 'template-locked' ? 'template' : 'post'; | ||
const [ postBlocks, onInput, onChange ] = useEntityBlockEditor( | ||
'postType', | ||
post.type, | ||
|
@@ -164,30 +163,48 @@ export const ExperimentalEditorProvider = withRegistryProvider( | |
BlockEditorProviderComponent = ExperimentalBlockEditorProvider, | ||
__unstableTemplate: template, | ||
} ) => { | ||
const { editorSettings, selection, isReady, mode, postTypeEntities } = | ||
useSelect( | ||
( select ) => { | ||
const { | ||
getEditorSettings, | ||
getEditorSelection, | ||
getRenderingMode, | ||
__unstableIsEditorReady, | ||
} = select( editorStore ); | ||
const { getEntitiesConfig } = select( coreStore ); | ||
const { | ||
editorSettings, | ||
selection, | ||
isReady, | ||
mode, | ||
defaultMode, | ||
postTypeEntities, | ||
hasLoadedPostObject, | ||
} = useSelect( | ||
( select ) => { | ||
const { | ||
getEditorSettings, | ||
getEditorSelection, | ||
getRenderingMode, | ||
__unstableIsEditorReady, | ||
} = select( editorStore ); | ||
const { getEntitiesConfig } = select( coreStore ); | ||
|
||
const postTypeObject = select( coreStore ).getPostType( | ||
post.type | ||
); | ||
|
||
const _hasLoadedPostObject = select( | ||
coreStore | ||
).hasFinishedResolution( 'getPostType', [ post.type ] ); | ||
|
||
return { | ||
editorSettings: getEditorSettings(), | ||
isReady: __unstableIsEditorReady(), | ||
mode: getRenderingMode(), | ||
selection: getEditorSelection(), | ||
postTypeEntities: | ||
post.type === 'wp_template' | ||
? getEntitiesConfig( 'postType' ) | ||
: null, | ||
}; | ||
}, | ||
[ post.type ] | ||
); | ||
return { | ||
hasLoadedPostObject: _hasLoadedPostObject, | ||
editorSettings: getEditorSettings(), | ||
isReady: __unstableIsEditorReady(), | ||
mode: getRenderingMode(), | ||
defaultMode: | ||
postTypeObject?.default_rendering_mode ?? 'post-only', | ||
selection: getEditorSelection(), | ||
postTypeEntities: | ||
post.type === 'wp_template' | ||
? getEntitiesConfig( 'postType' ) | ||
: null, | ||
}; | ||
}, | ||
[ post.type ] | ||
); | ||
const shouldRenderTemplate = !! template && mode !== 'post-only'; | ||
const rootLevelPost = shouldRenderTemplate ? template : post; | ||
const defaultBlockContext = useMemo( () => { | ||
|
@@ -282,7 +299,15 @@ export const ExperimentalEditorProvider = withRegistryProvider( | |
} | ||
); | ||
} | ||
}, [] ); | ||
}, [ | ||
createWarningNotice, | ||
initialEdits, | ||
settings, | ||
post, | ||
recovery, | ||
setupEditor, | ||
updatePostLock, | ||
] ); | ||
|
||
// Synchronizes the active post with the state | ||
useEffect( () => { | ||
|
@@ -301,15 +326,15 @@ export const ExperimentalEditorProvider = withRegistryProvider( | |
|
||
// Sets the right rendering mode when loading the editor. | ||
useEffect( () => { | ||
fabiankaegy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setRenderingMode( settings.defaultRenderingMode ?? 'post-only' ); | ||
}, [ settings.defaultRenderingMode, setRenderingMode ] ); | ||
setRenderingMode( defaultMode ); | ||
}, [ defaultMode, setRenderingMode ] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is one that I'm not entirely sure about. I think the default rendering mode should only be enforced when the "edited entity" changes. In practice though, this seems to have the same effect. Anyway, not a big deal for now. |
||
|
||
useHideBlocksFromInserter( post.type, mode ); | ||
|
||
// Register the editor commands. | ||
useCommands(); | ||
|
||
if ( ! isReady ) { | ||
if ( ! isReady || ! mode || ! hasLoadedPostObject ) { | ||
return null; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is causing a crash when the
supports
arg toregister_post_type
isfalse
:WooCommerce registers its
product_variation
post type withsupports => false
. It's a supported and documented value forsupports
, whose type isarray|false
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixing in #67225.