Skip to content

Commit

Permalink
Don't show the classic editor dropdown on unsupported post types. (#3146
Browse files Browse the repository at this point in the history
)

Also adds a new `gutenberg_can_edit_post_type` filter, so plugins can disable Gutenberg by post type.
  • Loading branch information
pento authored Oct 26, 2017
1 parent ab91160 commit c588bdd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
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 ) {
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

0 comments on commit c588bdd

Please sign in to comment.