-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Let Gutenberg be default editor for posts with blocks; add links to classic editor #1797
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5390ea2
Prevent showing Gutenberg edit link if post type is not available
westonruter 990ee41
Link to Gurenberg editor if post has blocks; add classic edit post li…
westonruter da3c25f
Show Gutenberg editor and classic editor in admin bar (with submenu)
westonruter a658729
Ensure edit post link on frontend are filtered to use Gutenberg for b…
westonruter b66439e
Use strpos instead of regex in gutenberg_post_has_blocks()
westonruter d319cc6
Add unit tests for functions added in register.php
westonruter cb336ba
Prevent showing links to Gutenberg if editor not among post type supp…
westonruter 63ff18d
Admin: Show Classic Editor, Gutenberg links consistently
aduth 65f752e
Admin: Always show full "Edit in" text in admin bar
aduth 547d038
Admin: Show post display state for Gutenberg posts
aduth 54df7bc
Admin: Generate classic editor URL by removed edit filter
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,62 @@ function gutenberg_menu() { | |
} | ||
add_action( 'admin_menu', 'gutenberg_menu' ); | ||
|
||
/** | ||
* Provide an edit link for posts and terms. | ||
* | ||
* @since 0.5.0 | ||
* | ||
* @param WP_Admin_Bar $wp_admin_bar Admin bar. | ||
*/ | ||
function gutenberg_add_admin_bar_edit_link( $wp_admin_bar ) { | ||
$edit_node = $wp_admin_bar->get_node( 'edit' ); | ||
if ( ! $edit_node ) { | ||
return; | ||
} | ||
|
||
$queried_object = get_queried_object(); | ||
if ( empty( $queried_object ) || empty( $queried_object->post_type ) || ! post_type_exists( $queried_object->post_type ) || ! gutenberg_can_edit_post( $queried_object->ID ) ) { | ||
return; | ||
} | ||
$post = $queried_object; | ||
|
||
if ( ! get_post_type_object( $post->post_type )->show_in_admin_bar ) { | ||
return; | ||
} | ||
|
||
$classic_text = __( 'Edit in Classic Editor', 'gutenberg' ); | ||
remove_filter( 'get_edit_post_link', 'gutenberg_filter_edit_post_link', 10 ); | ||
$classic_url = get_edit_post_link( $post->ID, 'raw' ); | ||
add_filter( 'get_edit_post_link', 'gutenberg_filter_edit_post_link', 10, 3 ); | ||
|
||
if ( empty( $classic_url ) || ! post_type_supports( $post->post_type, 'editor' ) ) { | ||
return; | ||
} | ||
|
||
$gutenberg_text = __( 'Edit in Gutenberg', 'gutenberg' ); | ||
$gutenberg_url = gutenberg_get_edit_post_url( $post->ID ); | ||
|
||
$is_gutenberg_default = gutenberg_post_has_blocks( $post->ID ); | ||
|
||
// Update title for edit link to indicate default editor. | ||
$wp_admin_bar->add_node( array_merge( | ||
(array) $edit_node, | ||
array( | ||
'title' => $is_gutenberg_default ? $gutenberg_text : $classic_text, | ||
) | ||
) ); | ||
|
||
// Add submenu item under link to go to Gutenberg editor or classic editor. | ||
$wp_admin_bar->add_node( array( | ||
'id' => 'edit_alt', | ||
'parent' => 'edit', | ||
'href' => $is_gutenberg_default ? $classic_url : $gutenberg_url, | ||
'title' => $is_gutenberg_default ? $classic_text : $gutenberg_text, | ||
) ); | ||
|
||
} | ||
add_action( 'admin_bar_menu', 'gutenberg_add_admin_bar_edit_link', 81 ); | ||
|
||
/** | ||
* Adds the filters to register additional links for the Gutenberg editor in | ||
* the post/page screens. | ||
|
@@ -82,39 +138,146 @@ function gutenberg_add_edit_links_filters() { | |
* | ||
* @since 0.1.0 | ||
* | ||
* @param array $actions Post actions. | ||
* @param array $post Edited post. | ||
* @param array $actions Post actions. | ||
* @param WP_Post $post Edited post. | ||
* | ||
* @return array Updated post actions. | ||
*/ | ||
function gutenberg_add_edit_links( $actions, $post ) { | ||
$can_edit_post = current_user_can( 'edit_post', $post->ID ); | ||
$title = _draft_or_post_title( $post->ID ); | ||
$post_type = get_post_type( $post ); | ||
if ( ! gutenberg_can_edit_post( $post->ID ) || | ||
'trash' === $post->post_status || | ||
! post_type_supports( $post->post_type, 'editor' ) || | ||
! apply_filters( 'gutenberg_add_edit_link_for_post_type', true, $post->post_type, $post ) ) { | ||
return $actions; | ||
} | ||
|
||
if ( $can_edit_post && 'trash' !== $post->post_status && apply_filters( 'gutenberg_add_edit_link_for_post_type', true, $post_type, $post ) ) { | ||
// Build the Gutenberg edit action. See also: WP_Posts_List_Table::handle_row_actions(). | ||
$gutenberg_url = menu_page_url( 'gutenberg', false ); | ||
$gutenberg_action = sprintf( | ||
if ( gutenberg_post_has_blocks( $post->ID ) ) { | ||
remove_filter( 'get_edit_post_link', 'gutenberg_filter_edit_post_link', 10 ); | ||
add_filter( 'get_edit_post_link', 'gutenberg_filter_edit_post_link', 10, 3 ); | ||
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. As it stands right now, this |
||
} | ||
|
||
// Build the new edit actions. See also: WP_Posts_List_Table::handle_row_actions(). | ||
$title = _draft_or_post_title( $post->ID ); | ||
$edit_actions = array( | ||
'classic hide-if-no-js' => sprintf( | ||
'<a href="%s" aria-label="%s">%s</a>', | ||
add_query_arg( 'post_id', $post->ID, $gutenberg_url ), | ||
esc_url( get_edit_post_link( $post->ID, 'raw' ) ), | ||
esc_attr( sprintf( | ||
/* translators: %s: post title */ | ||
__( 'Edit “%s” in the classic editor', 'gutenberg' ), | ||
$title | ||
) ), | ||
__( 'Classic Editor', 'gutenberg' ) | ||
), | ||
'gutenberg hide-if-no-js' => sprintf( | ||
'<a href="%s" aria-label="%s">%s</a>', | ||
esc_url( gutenberg_get_edit_post_url( $post->ID ) ), | ||
esc_attr( sprintf( | ||
/* translators: %s: post title */ | ||
__( 'Edit “%s” in the Gutenberg editor', 'gutenberg' ), | ||
$title | ||
) ), | ||
__( 'Gutenberg', 'gutenberg' ) | ||
); | ||
// Insert the Gutenberg action immediately after the Edit action. | ||
$edit_offset = array_search( 'edit', array_keys( $actions ), true ); | ||
$actions = array_merge( | ||
array_slice( $actions, 0, $edit_offset + 1 ), | ||
array( | ||
'gutenberg hide-if-no-js' => $gutenberg_action, | ||
), | ||
array_slice( $actions, $edit_offset + 1 ) | ||
); | ||
} | ||
), | ||
); | ||
|
||
// Insert the new actions in place of the Edit action. | ||
$edit_offset = array_search( 'edit', array_keys( $actions ), true ); | ||
$actions = array_merge( | ||
array_slice( $actions, 0, $edit_offset ), | ||
$edit_actions, | ||
array_slice( $actions, $edit_offset + 1 ) | ||
); | ||
|
||
return $actions; | ||
} | ||
|
||
/** | ||
* Get the edit post URL for Gutenberg. | ||
* | ||
* @since 0.5.0 | ||
* | ||
* @param int $post_id Post ID. | ||
* @return string|false URL or false if not available. | ||
*/ | ||
function gutenberg_get_edit_post_url( $post_id ) { | ||
// Note that menu_page_url() cannot be used because it does not work on the frontend. | ||
$gutenberg_url = admin_url( 'admin.php?page=gutenberg' ); | ||
$gutenberg_url = add_query_arg( 'post_id', $post_id, $gutenberg_url ); | ||
return $gutenberg_url; | ||
} | ||
|
||
/** | ||
* Filters the post edit link to default to the Gutenberg editor when the post content contains a block. | ||
* | ||
* @since 0.5.0 | ||
* | ||
* @param string $url The edit link URL. | ||
* @param int $post_id Post ID. | ||
* @param string $context The link context. If set to 'display' then ampersands are encoded. | ||
* @return string Edit post link. | ||
*/ | ||
function gutenberg_filter_edit_post_link( $url, $post_id, $context ) { | ||
$post = get_post( $post_id ); | ||
if ( gutenberg_can_edit_post( $post_id ) && gutenberg_post_has_blocks( $post_id ) && post_type_supports( get_post_type( $post_id ), 'editor' ) ) { | ||
$gutenberg_url = gutenberg_get_edit_post_url( $post->ID ); | ||
if ( 'display' === $context ) { | ||
$gutenberg_url = esc_url( $gutenberg_url ); | ||
} | ||
$url = $gutenberg_url; | ||
} | ||
return $url; | ||
} | ||
add_filter( 'get_edit_post_link', 'gutenberg_filter_edit_post_link', 10, 3 ); | ||
|
||
/** | ||
* 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. | ||
* @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 ) ) { | ||
return false; | ||
} | ||
$post_type_object = get_post_type_object( $post->post_type ); | ||
if ( ! $post_type_object->show_in_rest ) { | ||
return false; | ||
} | ||
return current_user_can( 'edit_post', $post_id ); | ||
} | ||
|
||
/** | ||
* Determine whether a post has blocks. | ||
* | ||
* @since 0.5.0 | ||
* | ||
* @param int $post_id Post ID. | ||
* @return bool Whether the post has blocks. | ||
*/ | ||
function gutenberg_post_has_blocks( $post_id ) { | ||
$post = get_post( $post_id ); | ||
return $post && strpos( $post->post_content, '<!-- wp:' ) !== false; | ||
} | ||
|
||
/** | ||
* Adds a "Gutenberg" post state for post tables, if the post contains blocks. | ||
* | ||
* @param array $post_states An array of post display states. | ||
* @param WP_Post $post The current post object. | ||
* @return array A filtered array of post display states. | ||
*/ | ||
function gutenberg_add_gutenberg_post_state( $post_states, $post ) { | ||
if ( gutenberg_post_has_blocks( $post->ID ) ) { | ||
$post_states[] = 'Gutenberg'; | ||
} | ||
|
||
return $post_states; | ||
} | ||
add_filter( 'display_post_states', 'gutenberg_add_gutenberg_post_state', 10, 2 ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@westonruter Should we just add a default filter handler for
get_edit_post_link
to always override with the preferred editor by content?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.
Isn't that what is already done here?
gutenberg/lib/register.php
Lines 210 to 231 in 547d038
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.
The reason why the
remove_filter
andadd_filter
are done above is to facilitate obtaining the classic post editor URL, even for a post that has blocks in it:gutenberg/lib/register.php
Lines 88 to 91 in 547d038
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.
Yeah, seeing that now in the changes prior to 63ff18d. I guess this might not be working correctly for the "Classic Editor" link for Gutenberg posts currently then.