Skip to content
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
25 changes: 25 additions & 0 deletions lib/full-site-editing/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor

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_query to build $edit_link?
cc @Mamaduka

Copy link
Member

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_query or add_query_arg.

P.S. In the future, this path can be a part of a post type of object via the _edit_link argument.

Copy link
Contributor

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).


return admin_url( sprintf( $edit_link, urlencode( $template->id ), $template->type ) );
Copy link
Member

Choose a reason for hiding this comment

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

The core function uses admin_url as well instead of the path - https://developer.wordpress.org/reference/functions/get_edit_post_link/

}
add_filter( 'get_edit_post_link', 'gutenberg_get_edit_template_link', 10, 2 );
44 changes: 44 additions & 0 deletions phpunit/full-site-editing/template-loader-test.php
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 );
}
}