Skip to content

Commit

Permalink
Fix template saving issue after switching themes. (#29842)
Browse files Browse the repository at this point in the history
* verify queried posts tax term actually matches our search

* minor refactor

* update comment

* updating comment again

* holy whack-a-mole-y

* fix pre-filter

* update tests class to catch these problems in the future

* add comment for TODO on multi-theme support for unique slug function

* post__not_in needs array

* check terms for theme name

* rename _slug to override_slug
  • Loading branch information
Addison-Stavlo authored Mar 24, 2021
1 parent 2f838c6 commit 0c14e0b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
54 changes: 35 additions & 19 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
}
list( $theme, $slug ) = $parts;
$wp_query_args = array(
'name' => $slug,
'post_name__in' => array( $slug ),
'post_type' => $template_type,
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
'posts_per_page' => 1,
Expand Down Expand Up @@ -380,47 +380,63 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
/**
* Generates a unique slug for templates or template parts.
*
* @param string $slug The resolved slug (post_name).
* @param string $override_slug The filtered value of the slug (starts as `null` from apply_filter).
* @param string $slug The original/un-filtered slug (post_name).
* @param int $post_ID Post ID.
* @param string $post_status No uniqueness checks are made if the post is still draft or pending.
* @param string $post_type Post type.
* @return string The original, desired slug.
*/
function gutenberg_filter_wp_template_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if ( 'wp_template' !== $post_type || 'wp_template_part' !== $post_type ) {
return $slug;
function gutenberg_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) {
if ( 'wp_template' !== $post_type && 'wp_template_part' !== $post_type ) {
return $override_slug;
}

if ( ! $override_slug ) {
$override_slug = $slug;
}

// Template slugs must be unique within the same theme.
$theme = get_the_terms( $post_ID, 'wp_theme' )[0]->slug;
// TODO - Figure out how to update this to work for a multi-theme
// environment. Unfortunately using `get_the_terms` for the 'wp-theme'
// term does not work in the case of new entities since is too early in
// the process to have been saved to the entity. So for now we use the
// currently activated theme for creation.
$theme = wp_get_theme()->get_stylesheet();
$terms = get_the_terms( $post_ID, 'wp_theme' );
if ( $terms && ! is_wp_error( $terms ) ) {
$theme = $terms[0]->name;
}

$check_query_args = array(
'post_name' => $slug,
'post_name__in' => array( $override_slug ),
'post_type' => $post_type,
'posts_per_page' => 1,
'post__not_in' => $post_ID,
'no_found_rows' => true,
'post__not_in' => array( $post_ID ),
'tax_query' => array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $theme,
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $theme,
),
),
'no_found_rows' => true,
);
$check_query = new WP_Query( $check_query_args );
$posts = $check_query->get_posts();

if ( count( $posts ) > 0 ) {
$suffix = 2;
do {
$query_args = $check_query_args;
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
$query_args['post_name'] = $alt_post_name;
$query = new WP_Query( $check_query_args );
$query_args = $check_query_args;
$alt_post_name = _truncate_post_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
$query_args['post_name__in'] = array( $alt_post_name );
$query = new WP_Query( $query_args );
$suffix++;
} while ( count( $query->get_posts() ) > 0 );
$slug = $alt_post_name;
$override_slug = $alt_post_name;
}

return $slug;
return $override_slug;
}
add_filter( 'wp_unique_post_slug', 'gutenberg_filter_wp_template_unique_post_slug', 10, 4 );
add_filter( 'pre_wp_unique_post_slug', 'gutenberg_filter_wp_template_unique_post_slug', 10, 5 );
18 changes: 18 additions & 0 deletions phpunit/class-block-templates-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ public static function wpSetUpBeforeClass() {
gutenberg_register_wp_theme_taxonomy();
gutenberg_register_wp_template_part_area_taxonomy();

// Set up a template post corresponding to a different theme.
// We do this to ensure resolution and slug creation works as expected,
// even with another post of that same name present for another theme.
$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(
'this-theme-should-not-resolve',
),
),
);
self::$post = self::factory()->post->create_and_get( $args );
wp_set_post_terms( self::$post->ID, 'this-theme-should-not-resolve', 'wp_theme' );

// Set up template post.
$args = array(
'post_type' => 'wp_template',
Expand Down

0 comments on commit 0c14e0b

Please sign in to comment.