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

Feature: Set editor rendering mode by post type #62304

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
ab4b075
Add Post_Types_Controller compat class. Add rendering_mode parameter …
TylerB24890 Jun 4, 2024
ca0de5d
Add the rendering_mode property to the post type object properties
TylerB24890 Jun 4, 2024
3beef1e
Check that the rendering_mode property exists before attempting to ac…
TylerB24890 Jun 4, 2024
9b276d2
Fix PHP filters from overriding one another. Implement the defaultRen…
TylerB24890 Jun 4, 2024
a26a583
Remove unnecessary optional chain inside conditional
TylerB24890 Jun 4, 2024
b538ae6
Dont override the rendering_mode value if set during post type regist…
TylerB24890 Jun 4, 2024
e3912d4
Move all code to a new 6.7 compat directory. Update the post type pro…
TylerB24890 Jun 5, 2024
702a5be
Update rendering_mode filters to always be prefixed with post_type_
TylerB24890 Jun 6, 2024
964a148
Remove the editor mode props from the individual editors (site + post…
TylerB24890 Jun 6, 2024
aabbca8
Merge latest trunk and resolve conflicts.
TylerB24890 Jun 6, 2024
c9f2274
Merge latest trunk and resolve conflicts
TylerB24890 Jun 10, 2024
298c858
Prevent rendering editor until mode constant is defined.
TylerB24890 Jun 10, 2024
d85f56c
Add validation for rendering modes passed to post types.
TylerB24890 Jun 10, 2024
9c631fe
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
TylerB24890 Jun 11, 2024
de00c45
Use a single useSelect() hook
TylerB24890 Jun 11, 2024
bf3fbca
Set default rendering mode for pages to template-locked
TylerB24890 Jun 11, 2024
5c98bf6
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
TylerB24890 Jun 12, 2024
40c2fa3
Use post.type as hook dependency instead of full post object
TylerB24890 Jun 12, 2024
c2b1f92
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
TylerB24890 Jun 13, 2024
1f7ff09
Fix since documentation. Fix array standard.
TylerB24890 Jun 13, 2024
40ce652
Update rendering_modes function name to be prefixed with post_type_
TylerB24890 Jun 13, 2024
b361cd5
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
TylerB24890 Jun 28, 2024
24118c7
Merge latest trunk & resolve conflicts
TylerB24890 Jul 18, 2024
a0a766c
Create new setRenderingMode playwright utility to reset the rendering…
TylerB24890 Jul 18, 2024
882f69c
evaluate the page before attempting to access global variables
TylerB24890 Jul 18, 2024
a2ac4b3
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
TylerB24890 Jul 18, 2024
3496d55
Add rendering mode parameter to the setRenderingMode utility
TylerB24890 Jul 18, 2024
65c264b
check if active theme is block theme
Sidsector9 Jul 30, 2024
a27c07e
fix broken test
Sidsector9 Jul 30, 2024
ac9556d
Merge pull request #1 from Sidsector9/pr-62304-fixes
Sidsector9 Jul 30, 2024
148837e
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
TylerB24890 Aug 2, 2024
3619aed
Add core backport changelog entry
TylerB24890 Aug 2, 2024
b080013
Adjust order of rendering_mode filters
TylerB24890 Aug 2, 2024
c07a005
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
TylerB24890 Aug 5, 2024
42bbbae
Merge latest trunk & resolve conflicts
TylerB24890 Aug 16, 2024
f38b926
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
TylerB24890 Aug 20, 2024
5d9a3f4
Set the editor rendering mode when a page is created for the performa…
TylerB24890 Aug 20, 2024
4967bf0
write setEditorRenderingMode method for playwright performance tests
TylerB24890 Aug 20, 2024
e44556d
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
TylerB24890 Aug 20, 2024
7e72459
Fix the rendering mode switcher for performance tests
TylerB24890 Aug 21, 2024
1b18340
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
fabiankaegy Nov 14, 2024
a55cd68
fix move files to 6.8 compat folder
fabiankaegy Nov 14, 2024
429b338
fix move to 6.8 folder
fabiankaegy Nov 14, 2024
d523598
fix reset test overrides
fabiankaegy Nov 14, 2024
41601ec
fix reset whitespace changes
fabiankaegy Nov 14, 2024
05abd41
fix reset whitespace changes
fabiankaegy Nov 14, 2024
4904b5a
fix reset whitespace
fabiankaegy Nov 14, 2024
40d7324
fix reset change of if condition
fabiankaegy Nov 14, 2024
c2d58a9
fix ensure post type object has loaded before editor renders
fabiankaegy Nov 14, 2024
9e8e08c
fix remove template-lock rendering mode
fabiankaegy Nov 14, 2024
b94e309
Fix root post loading
youknowriad Nov 15, 2024
a742e4a
fix only apply default rendering mode on load
fabiankaegy Nov 15, 2024
fcb1449
fix: Expose rendering mode to mobile editor
dcalhoun Nov 15, 2024
ec543c5
test: Mobile editor mocks getPostType resolution
dcalhoun Nov 15, 2024
2aa288d
attempt to fix performance tests by forcing `post-only` rendering mod…
fabiankaegy Nov 15, 2024
f0c680f
Merge branch 'trunk' into feature/post-type-rendering-mode-58038
fabiankaegy Nov 19, 2024
557ec18
fix remove unused passed argument from useSpecificEditorSettings call
fabiankaegy Nov 19, 2024
9068ab1
fix clean up performance tests by moving rendering mode change into i…
fabiankaegy Nov 19, 2024
4512fce
simplify performance tests for rendering mode
fabiankaegy Nov 19, 2024
f282a47
fix move backport file to correct folder
fabiankaegy Nov 19, 2024
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
1 change: 0 additions & 1 deletion lib/compat/wordpress-6.6/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
function gutenberg_add_excerpt_support_to_wp_block() {
add_post_type_support( 'wp_block', 'excerpt' );
}

add_action( 'init', 'gutenberg_add_excerpt_support_to_wp_block' );

/**
Expand Down
1 change: 0 additions & 1 deletion lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ function gutenberg_add_class_list_to_public_post_types() {
}
add_action( 'rest_api_init', 'gutenberg_add_class_list_to_public_post_types' );


/**
* Registers the Global Styles Revisions REST API routes.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* REST API: Gutenberg_REST_Post_Types_Controller_6_7 class
*
* @package gutenberg
*/

/**
* Gutenberg_REST_Post_Types_Controller_6_7 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_7 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 specific post type.
*
* The dynamic portion of the hook name, `$item->name`, refers to the post type slug.
*
* @since 6.6.0
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
* @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", $item->default_rendering_mode, $item );

/**
* Filters the block editor rendering mode for a post type.
*
* @since 6.6.0
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
* @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', $rendering_mode, $item );

// Validate the filtered rendering mode.
if ( ! in_array( $rendering_mode, gutenberg_rendering_modes(), true ) ) {
$rendering_mode = 'post-only';
}

$response->data['default_rendering_mode'] = $rendering_mode;
}

return rest_ensure_response( $response );
}
}
57 changes: 57 additions & 0 deletions lib/compat/wordpress-6.7/post.php
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_rendering_modes() {
Copy link
Member

@ramonjd ramonjd Jun 13, 2024

Choose a reason for hiding this comment

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

Not a blocker, just a question - I'm assuming that this will be wp_rendering_modes or rendering_modes when it eventually makes it to Core.

What about making the name be more specific, e.g., gutenberg_post_type_rendering_modes or even gutenberg_get_post_type_rendering_modes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good question, and I assume you're correct about it being renamed to wp_rendering_modes in Core.

I think it makes sense to prefix it with post_type_ so the final function would be wp_post_type_rendering_modes()

I'm also not opposed to using get_ in the prefix, if the general that's the general consensus. In the meantime I've updated it to use gutenberg_post_type_rendering_modes() -- happy to update again if necessary!

return [

Check failure on line 20 in lib/compat/wordpress-6.7/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Short array syntax is not allowed
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
'post-only',
'template-lock',
'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_rendering_modes();

// Make sure the post type supports the block editor.
if (
( 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;
}

return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_post_type_default_rendering_mode', 10, 2 );
22 changes: 22 additions & 0 deletions lib/compat/wordpress-6.7/rest-api.php
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_7();
$controller->register_routes();
}
}
add_action( 'rest_api_init', 'gutenberg_add_post_type_rendering_mode' );
7 changes: 7 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php';
require __DIR__ . '/compat/wordpress-6.6/rest-api.php';

// WordPress 6.7 compat.
require __DIR__ . '/compat/wordpress-6.7/class-gutenberg-rest-post-types-controller-6-7.php';
require __DIR__ . '/compat/wordpress-6.7/rest-api.php';

// Plugin specific code.
require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php';
require_once __DIR__ . '/class-wp-rest-edit-site-export-controller-gutenberg.php';
Expand Down Expand Up @@ -141,6 +145,9 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.6/option.php';
require __DIR__ . '/compat/wordpress-6.6/post.php';

// WordPress 6.7 compat.
require __DIR__ . '/compat/wordpress-6.7/post.php';

// Experimental features.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
require __DIR__ . '/experimental/blocks.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,37 +114,28 @@ function useNavigateToPreviousEntityRecord() {

export function useSpecificEditorSettings() {
fabiankaegy marked this conversation as resolved.
Show resolved Hide resolved
const onNavigateToEntityRecord = useNavigateToEntityRecord();
const { templateSlug, canvasMode, settings, postWithTemplate } = useSelect(
( select ) => {
const {
getEditedPostType,
getEditedPostId,
getEditedPostContext,
getCanvasMode,
getSettings,
} = unlock( select( editSiteStore ) );
const { getEditedEntityRecord } = select( coreStore );
const usedPostType = getEditedPostType();
const usedPostId = getEditedPostId();
const _record = getEditedEntityRecord(
'postType',
usedPostType,
usedPostId
);
const _context = getEditedPostContext();
return {
templateSlug: _record.slug,
canvasMode: getCanvasMode(),
settings: getSettings(),
postWithTemplate: _context?.postId,
};
},
[]
);
const { templateSlug, canvasMode, settings } = useSelect( ( select ) => {
const {
getEditedPostType,
getEditedPostId,
getCanvasMode,
getSettings,
} = unlock( select( editSiteStore ) );
const { getEditedEntityRecord } = select( coreStore );
const usedPostType = getEditedPostType();
const usedPostId = getEditedPostId();
const _record = getEditedEntityRecord(
'postType',
usedPostType,
usedPostId
);
return {
templateSlug: _record.slug,
canvasMode: getCanvasMode(),
settings: getSettings(),
};
}, [] );
const archiveLabels = useArchiveLabel( templateSlug );
const defaultRenderingMode = postWithTemplate
? 'template-locked'
: 'post-only';
const onNavigateToPreviousEntityRecord =
useNavigateToPreviousEntityRecord();
const defaultEditorSettings = useMemo( () => {
Expand All @@ -154,7 +145,6 @@ export function useSpecificEditorSettings() {
richEditingEnabled: true,
supportsTemplateMode: true,
focusMode: canvasMode !== 'view',
defaultRenderingMode,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
// I wonder if they should be set in the post editor too
Expand All @@ -165,7 +155,6 @@ export function useSpecificEditorSettings() {
}, [
settings,
canvasMode,
defaultRenderingMode,
onNavigateToEntityRecord,
onNavigateToPreviousEntityRecord,
archiveLabels.archiveTypeLabel,
Expand Down
31 changes: 23 additions & 8 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import { useEffect, useLayoutEffect, useMemo } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { EntityProvider, useEntityBlockEditor } from '@wordpress/core-data';
import {
EntityProvider,
useEntityBlockEditor,
store as coreStore,
} from '@wordpress/core-data';
import {
BlockEditorProvider,
BlockContextProvider,
Expand Down Expand Up @@ -166,17 +170,20 @@ export const ExperimentalEditorProvider = withRegistryProvider(
const {
getEditorSettings,
getEditorSelection,
getRenderingMode,
__unstableIsEditorReady,
} = select( editorStore );

const postTypeObject = select( coreStore ).getPostType(
post.type
);
return {
editorSettings: getEditorSettings(),
isReady: __unstableIsEditorReady(),
mode: getRenderingMode(),
selection: getEditorSelection(),
mode: postTypeObject?.default_rendering_mode ?? 'post-only',
};
},
[]
[ post.type ]
);
const shouldRenderTemplate = !! template && mode !== 'post-only';
const rootLevelPost = shouldRenderTemplate ? template : post;
Expand Down Expand Up @@ -249,7 +256,15 @@ export const ExperimentalEditorProvider = withRegistryProvider(
}
);
}
}, [] );
}, [
createWarningNotice,
initialEdits,
settings,
post,
recovery,
setupEditor,
updatePostLock,
Copy link
Contributor

Choose a reason for hiding this comment

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

@fabiankaegy Is the addition of the dependencies here in relation to the current PR or was it done just to please the linter?

This is creating a bug when the post object is update, the setup happens again and initial edits are applied again even if they shouldn't be.

] );

// Synchronizes the active post with the state
useEffect( () => {
Expand All @@ -268,15 +283,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( mode );
}, [ mode, setRenderingMode ] );

useHideBlocksFromInserter( post.type, mode );

// Register the editor commands.
useCommands();

if ( ! isReady ) {
if ( ! isReady || ! mode ) {
fabiankaegy marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

Expand Down
Loading