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

Don't show the classic editor dropdown on unsupported post types. #3146

Merged
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
19 changes: 6 additions & 13 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,7 @@ function gutenberg_init( $return, $post ) {
return false;
}

$post_type = $post->post_type;
$post_type_object = get_post_type_object( $post_type );

if ( 'attachment' === $post_type ) {
Copy link
Member

Choose a reason for hiding this comment

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

Noting that we're no longer checking this in gutenberg_can_edit_post_type, and while I don't know the original reason why we were explicitly targeting attachment, my understanding is this should still be effective since post_type_supports( 'attachment', 'editor' ) === false.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yah, that's why I dropped it, it was an unnecessary extra check. :-)

return false;
}

if ( ! $post_type_object->show_in_rest ) {
return false;
}

if ( ! post_type_supports( $post_type, 'editor' ) ) {
if ( ! gutenberg_can_edit_post( $post ) ) {
return false;
}

Expand Down Expand Up @@ -349,7 +338,7 @@ function gutenberg_add_edit_link_filters() {
* @return array Updated post actions.
*/
function gutenberg_add_edit_link( $actions, $post ) {
if ( 'trash' === $post->post_status || ! post_type_supports( $post->post_type, 'editor' ) ) {
if ( ! gutenberg_can_edit_post( $post ) ) {
return $actions;
}

Expand Down Expand Up @@ -420,6 +409,10 @@ function gutenberg_modify_add_new_button_url( $url, $path ) {
* @since 1.5.0
*/
function gutenberg_replace_default_add_new_button() {
global $typenow;
if ( ! gutenberg_can_edit_post_type( $typenow ) ) {
return;
}
?>
<style type="text/css">
.split-page-title-action {
Expand Down
53 changes: 46 additions & 7 deletions lib/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,65 @@ function gutenberg_collect_meta_box_data() {
/**
* Return whether the post can be edited in Gutenberg and by the current user.
*
* Gutenberg depends on the REST API, and if the post type is not shown in the
* REST API, then the post cannot be edited in Gutenberg.
*
* @since 0.5.0
*
* @param int $post_id Post.
* @param int|WP_Post $post_id Post.
* @return bool Whether the post can be edited with Gutenberg.
*/
function gutenberg_can_edit_post( $post_id ) {
$post = get_post( $post_id );
if ( ! $post || ! post_type_exists( $post->post_type ) ) {
if ( ! $post ) {
return false;
}
$post_type_object = get_post_type_object( $post->post_type );
if ( ! $post_type_object->show_in_rest ) {

if ( 'trash' === $post->post_status ) {
return false;
}

if ( ! gutenberg_can_edit_post_type( $post->post_type ) ) {
return false;
}

return current_user_can( 'edit_post', $post_id );
}

/**
* Return whether the post type can be edited in Gutenberg.
*
* Gutenberg depends on the REST API, and if the post type is not shown in the
* REST API, then the post cannot be edited in Gutenberg.
*
* @since 1.5.2
*
* @param string $post_type The post type.
* @return bool Wehther the post type can be edited with Gutenberg.
*/
function gutenberg_can_edit_post_type( $post_type ) {
$can_edit = true;
if ( ! post_type_exists( $post_type ) ) {
$can_edit = false;
}

if ( ! post_type_supports( $post_type, 'editor' ) ) {
$can_edit = false;
}

$post_type_object = get_post_type_object( $post_type );
if ( ! $post_type_object->show_in_rest ) {
$can_edit = false;
}

/**
* Filter to allow plugins to enable/disable Gutenberg for particular post types.
*
* @since 1.5.2
*
* @param bool $can_edit Whether the post type can be edited or not.
* @param string $post_type The post type being checked.
*/
return apply_filters( 'gutenberg_can_edit_post_type', $can_edit, $post_type );
}

/**
* Determine whether a post has blocks.
*
Expand Down