Skip to content

Commit

Permalink
Change edit links for templates and template parts (#36294)
Browse files Browse the repository at this point in the history
* Change edit links for templates and template parts

* Unify the filter

* Minor adjustments

* Add a unit test.

Co-authored-by: George Mamadashvili <georgemamadashvili@gmail.com>
Co-authored-by: Anton Vlasenko <vlasenko.anton@gmail.com>
  • Loading branch information
3 people authored Nov 8, 2021
1 parent e5c5b82 commit 57ebf86
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
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';

return admin_url( sprintf( $edit_link, urlencode( $template->id ), $template->type ) );
}
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 );
}
}

0 comments on commit 57ebf86

Please sign in to comment.