-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Change edit links for templates and template parts #36294
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -266,3 +266,28 @@ function gutenberg_resolve_template_for_new_post( $wp_query ) { | |
| $wp_query->set( 'post_status', 'auto-draft' ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Redirect the edit links for templates to the site editor. | ||
| * | ||
| * @param string $link The original link. | ||
| * @param int $post_id The custom post id. | ||
| */ | ||
| function gutenberg_get_edit_template_link( $link, $post_id ) { | ||
| $post = get_post( $post_id ); | ||
|
|
||
| if ( ! in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ), true ) ) { | ||
| return $link; | ||
| } | ||
|
|
||
| $template = _build_block_template_result_from_post( $post ); | ||
|
|
||
| if ( is_wp_error( $template ) ) { | ||
| return $link; | ||
| } | ||
|
|
||
| $edit_link = 'themes.php?page=gutenberg-edit-site&postId=%1$s&postType=%2$s'; | ||
|
|
||
| return admin_url( sprintf( $edit_link, urlencode( $template->id ), $template->type ) ); | ||
|
Member
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. The core function uses |
||
| } | ||
| add_filter( 'get_edit_post_link', 'gutenberg_get_edit_template_link', 10, 2 ); | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
| /** | ||
| * This class tests block template loader functions | ||
| * | ||
| * @package Gutenberg | ||
| */ | ||
|
|
||
| class WP_Template_Loader_Test extends WP_UnitTestCase { | ||
|
|
||
| public function test_gutenberg_get_edit_template_link_returns_admin_url_link_for_template_posts() { | ||
| $stylesheet = get_stylesheet(); | ||
| $args = array( | ||
| 'post_type' => 'wp_template', | ||
| 'post_name' => 'my_template', | ||
| 'post_title' => 'My Template', | ||
| 'post_content' => 'Content', | ||
| 'post_excerpt' => 'Description of my template.', | ||
| 'tax_input' => array( | ||
| 'wp_theme' => array( | ||
| $stylesheet, | ||
| ), | ||
| ), | ||
| ); | ||
| $post = self::factory()->post->create_and_get( $args ); | ||
|
|
||
| wp_set_post_terms( $post->ID, get_stylesheet(), 'wp_theme' ); | ||
| $default_url = 'https://some.link'; | ||
| $url = gutenberg_get_edit_template_link( $default_url, $post->ID ); | ||
| $this->assertIsString( $url ); | ||
| $this->assertNotSame( $default_url, $url ); | ||
| $this->assertStringContainsString( 'themes.php', $url ); | ||
| $this->assertStringContainsString( 'postId', $url ); | ||
| $this->assertStringContainsString( $stylesheet, $url ); | ||
| wp_delete_post( $post->ID ); | ||
| } | ||
|
|
||
| public function test_gutenberg_get_edit_template_link_returns_default_link_for_non_template_posts() { | ||
| $post = self::factory()->post->create_and_get(); | ||
| $default_url = 'https://some.link'; | ||
| $url = gutenberg_get_edit_template_link( $default_url, $post->ID ); | ||
| $this->assertSame( $default_url, $url ); | ||
| wp_delete_post( $post->ID ); | ||
| } | ||
| } |
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.
Shouldn't we use
build_queryto build$edit_link?cc @Mamaduka
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.
I think it's a simple enough query that there's no need to use
build_queryoradd_query_arg.P.S. In the future, this path can be a part of a post type of object via the
_edit_linkargument.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.
There is a library function for building queries, and it feels like we are inventing the wheel here.
But I'm not going to insist we need to change it because we encode the request parameters (as it should be).