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

Open
wants to merge 27 commits into
base: trunk
Choose a base branch
from

Conversation

TylerB24890
Copy link
Contributor

@TylerB24890 TylerB24890 commented Jun 4, 2024

What?

Related Issue & PR:

This PR adds a filterable default_rendering_mode property to the WP_Post_Type object allowing users to define the default rendering mode for the editor for that post type.

The default_rendering_mode property can be added to the arguments when registering the post type via register_post_type() and can be overwritten using the available filters:

  • post_type_{$post_type}_default_rendering_mode: To target an individual specific post type.
/**
 * Filters the block editor rendering for a specific post type.
 *
 * The dynamic portion of the hook name, `$post_type->name`, refers to the post type slug.
 *
 * @param string       $rendering_mode The current rendering mode set with the post type registration.
 * @param WP_Post_Type $post_type      The current post type object.
 */
$rendering_mode = apply_filters( "post_type_{$post_type->name}_default_rendering_mode", $post_type->default_rendering_mode, $post_type );
  • post_type_default_rendering_mode: To target all (or multiple) post types.
/**
 * Filters the block editor rendering for a post type.
 *
 * @param string       $rendering_mode  The current rendering mode set with the post type registration.
 * @param WP_Post_Type $post_type_name  The current post type object.
 */
$rendering_mode = apply_filters( 'post_type_default_rendering_mode', $post_type->default_rendering_mode, $post_type );

Why?

Currently there is no way to set the default rendering mode of the block editor. You can select the mode while in the block editor, but upon refreshing that mode is reset back to the default post-only. With this update developers have more control over the editing experience and provides a means of setting the default view for the block editor.

How?

The linked PR has a discussion that mentions this setting should be applied at the post type level, allowing for a difference editing mode per post type. This PR applies the default_rendering_mode property to the WP_Post_Type object itself and provides multiple ways of overriding or setting the default for a custom (or core) post type.

Testing Instructions

  1. Create a new post post type and observe the default editor UI.
  2. In your functions.php file (or similar) use one of the available filters to set the default_rendering_mode property to template-lock:
add_filter(
    'post_type_default_rendering_mode',
    function () {
        return 'template-lock';
    }
);
  1. Refresh the post editor and confirm you are now seeing the Template UI instead of the default Post UI.
  2. Create a new page post and confirm the page editor also loads with the Template UI.
  3. Update the filter in functions.php to target only the page post type:
add_filter(
    'post_type_page_default_rendering_mode',
    function () {
        return 'template-lock';
    }
);
  1. Reload the page editor and confirm it still renders the Template UI.
  2. Refresh the post editor and confirm it now renders the default Post UI.
  3. Update the filter in functions.php to set the rendering mode for the post and page post types, but no others:
add_filter(
    'post_type_default_rendering_mode',
    function ( $mode, $post_type ) {
        if ( 'page' === $post_type || 'post' === $post_type ) {
            return 'template-lock';
        }

        return $mode;
    },
    10,
    2
)
  1. Register a custom post type using register_post_type and set the default_rendering_mode parameter to template-lock:
register_post_type(
    'my_custom_type',
    [
        'show_in_rest'           => true,
        'supports'               => [ 'title', 'editor' ],
        'default_rendering_mode' => 'template-lock',
    ]
);
  1. Visit the editor for your custom post type and confirm it loads with the Template UI.
  2. Visit the Site Editor and select Pages in the left navigation.
  3. Select a page and confirm it loads with the Template view (if the code from step 8 is still in place).
  4. Change the filter value from step 8 to output post-only and refresh the Site Editor.
  5. Confirm the Page now loads with the Post Content instead of the template UI.

@fabiankaegy fabiankaegy added [Type] Enhancement A suggestion for improvement. [Feature] Template Editing Mode Related to the template editor available in the Block Editor REST API Interaction Related to REST API labels Jun 5, 2024
Copy link
Member

@fabiankaegy fabiankaegy left a comment

Choose a reason for hiding this comment

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

Thanks for you work on this @TylerB24890 🚀

This is working exactly as I hoped it would. :) I left some minor notes and will request some additional reviews :)

lib/compat/wordpress-6.6/post.php Outdated Show resolved Hide resolved
Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

This is looking very good to me. I'd appreciate broader review as well. @jameskoster @jasmussen @mcsf

@TylerB24890 TylerB24890 marked this pull request as ready for review June 5, 2024 14:29
Copy link

github-actions bot commented Jun 5, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: TylerB24890 <tyb@git.wordpress.org>
Co-authored-by: fabiankaegy <fabiankaegy@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: mcsf <mcsf@git.wordpress.org>
Co-authored-by: jasmussen <joen@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
Co-authored-by: jameskoster <jameskoster@git.wordpress.org>
Co-authored-by: dinhtungdu <dinhtungdu@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

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

This is working great for me for what it's worth. Nice addition.

I also checked what would happen if I logged in as an Author with template-locked enabled - it activates the "Show template" option 👍🏻

Just left a comment about value checking, though even if it's a valid remark, I don't think it should block the first version of this.

lib/compat/wordpress-6.7/post.php Outdated Show resolved Hide resolved
@jasmussen
Copy link
Contributor

Nice PR. Took it for a quick spin. In a very superficial test—this could use broader opinions—this seems to work as intended. If I change the default post-editor rendering mode to show templates, it does:

showing template

Otherwise it still shows the current default:

not showing template

I think of these defaults as valid user settings to set as well at some point, user settings that are reasonable to persist, so if we add this, it would be good that a future user-setting could also unset whatever a plugin or theme might do. I.e. if my theme sets my page editor to omit the template preview, but I really prefer editing with that, I should be able to override that in my user settings. Make sense?

@fabiankaegy
Copy link
Member

@jasmussen Agree that there should also be a user setting for this :) We can look into that as follow up 👍

@jasmussen
Copy link
Contributor

Not necessarily urgent, IMO, just something to consider for the technical review!

@@ -161,18 +165,30 @@ export const ExperimentalEditorProvider = withRegistryProvider(
BlockEditorProviderComponent = ExperimentalBlockEditorProvider,
__unstableTemplate: template,
} ) => {
const { editorSettings, selection, isReady, mode } = useSelect(
const mode = useSelect(
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason for separating the two useSelect calls?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope! I've merged them now 🙂

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

At the moment, the defaultRenderingMode for pages is "post-only", I guess we should make the update to "template-locked" as discussed above.

};
},
[]
[ post ]
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use post.type here, it seems it's the only thing we need.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All set!

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Would love some help testing this properly. (Maybe @fabiankaegy if you find some time) but this is looking good to me.

@annezazu
Copy link
Contributor

@WordPress/outreach for anyone who can help test and review this PR ❤️

Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

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

This is working well for me 🎉

If @fabiankaegy is happy that it satisfies #58038 then I suppose the next step is the accompanying Core patch?

Happy to help test that as well. Cheers!

*
* @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!

lib/compat/wordpress-6.7/post.php Outdated Show resolved Hide resolved
Copy link
Member

@fabiankaegy fabiankaegy left a comment

Choose a reason for hiding this comment

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

For me this is exactly doing what I had in mind and testing perfectly! :)

I'm not sure about the failing tests though. They don't seem to be related but I'm not sure.

@youknowriad would love to get your eyes on that if you have a moment <3

@youknowriad
Copy link
Contributor

I looked at this earlier today, and it's good to ship to me as well.

I think tests are somewhat stable these days, so if restart (or rebasing) doesn't really fix them than there must be a relationship with this PR.

I think the switch to "template by default" for pages is probably impactful for e2e tests for instance.

@ramonjd
Copy link
Member

ramonjd commented Jul 23, 2024

I've kicked off the tests again. 🤞🏻 🤞🏻 🤞🏻 🤞🏻

The final thing to do is create a Core backport PR.

See: https://github.com/WordPress/gutenberg/blob/trunk/backport-changelog/readme.md

Getting a draft PR up would be enough, and then I think the CI tests will be happy and the discussion about how to integrate into Core can move there.

Thanks a lot!

@ramonjd ramonjd added the Needs PHP backport Needs PHP backport to Core label Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Template Editing Mode Related to the template editor available in the Block Editor Needs Dev Note Requires a developer note for a major WordPress release cycle Needs PHP backport Needs PHP backport to Core REST API Interaction Related to REST API [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an option to change default renderingMode of Post Editor
7 participants