Skip to content
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

Use theme mods for templates and template parts #31971

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
406c69d
no message
carlomanf May 19, 2021
5bcc469
Fix some errors
carlomanf May 19, 2021
dd3486a
Don't allow new posts to replace existing slugs
carlomanf May 20, 2021
45fd485
Fix some unwanted behaviour with slugs
carlomanf May 20, 2021
26133df
https://core.trac.wordpress.org/ticket/28099
carlomanf May 20, 2021
8b43314
Let theme files resolve (fixes error with last commit)
carlomanf May 20, 2021
5483d90
Add isset check
carlomanf May 20, 2021
8c3e2f1
Validate theme mods entries
carlomanf May 20, 2021
651b35a
Fix style errors
carlomanf May 25, 2021
2fc6c82
Merge branch 'trunk' of https://github.com/WordPress/gutenberg into t…
carlomanf May 26, 2021
f25014a
Temporarily disable inactive access for 5.8 version.
carlomanf Jun 3, 2021
a280490
Amend _gutenberg_build_template_result_from_post
carlomanf Jun 10, 2021
30b76ad
Change theme_slugs to ids
carlomanf Jun 10, 2021
65184de
Amend unit test
carlomanf Jun 10, 2021
77b264f
Amend prepare_item_for_database
carlomanf Jun 10, 2021
5ef6d6e
Remove inactive access totally for 5.8 version
carlomanf Jun 10, 2021
83a8645
Fix some other errors
carlomanf Jun 10, 2021
6cbb605
Use $template->source instead of $template->has_theme_file
carlomanf Jun 10, 2021
c5fee72
Merge branch 'trunk' of https://github.com/WordPress/gutenberg into t…
carlomanf Sep 29, 2021
2fb5420
Detach theme association from save_post hook
carlomanf Sep 29, 2021
2447033
Update new unit test
carlomanf Sep 29, 2021
735b3ff
Add theme association to update_item method
carlomanf Sep 29, 2021
76d5f8a
Use $template->slug instead of $request['id']
carlomanf Sep 29, 2021
27c28d0
Instantiate tax_input for template part area.
carlomanf Sep 29, 2021
a4bf0aa
Update admin view
carlomanf Oct 2, 2021
764d484
Stop using post_name altogether
carlomanf Oct 2, 2021
0a46d34
Improve customize_template function
carlomanf Oct 6, 2021
d307f35
Clean up prepare_item_for_database
carlomanf Oct 6, 2021
6a3407e
Fix unit test
carlomanf Oct 6, 2021
5aa6998
Add gutenberg prefix
carlomanf Oct 6, 2021
8e72522
Merge branch 'trunk' of https://github.com/WordPress/gutenberg into t…
carlomanf Oct 28, 2021
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
174 changes: 58 additions & 116 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,27 +280,29 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t
* @return WP_Block_Template|WP_Error Template.
*/
function _gutenberg_build_template_result_from_post( $post ) {
$default_template_types = gutenberg_get_default_template_types();
$terms = get_the_terms( $post, 'wp_theme' );

if ( is_wp_error( $terms ) ) {
return $terms;
if ( ! in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ), true ) ) {
return new WP_Error( 'template_wrong_post_type', __( 'An invalid post was provided for this template.', 'gutenberg' ) );
}

if ( ! $terms ) {
$default_template_types = gutenberg_get_default_template_types();
$ids = get_theme_mod( $post->post_type, array() );
$active = in_array( $post->ID, $ids, true );

// Temporarily disable inactive access for 5.8 version.
if ( ! $active ) {
return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'gutenberg' ) );
}

$theme = $terms[0]->name;
$has_theme_file = wp_get_theme()->get_stylesheet() === $theme &&
null !== _gutenberg_get_template_file( $post->post_type, $post->post_name );
$theme = wp_get_theme()->get_stylesheet();
$slug = array_search( $post->ID, $ids, true );
$has_theme_file = null !== _gutenberg_get_template_file( $post->post_type, $slug );

$template = new WP_Block_Template();
$template->wp_id = $post->ID;
$template->id = $theme . '//' . $post->post_name;
$template->id = $theme . '//' . $slug;
$template->theme = $theme;
$template->content = $post->post_content;
$template->slug = $post->post_name;
$template->slug = $slug;
$template->source = 'custom';
$template->type = $post->post_type;
$template->description = $post->post_excerpt;
Expand All @@ -326,15 +328,15 @@ function _gutenberg_build_template_result_from_post( $post ) {
/**
* Retrieves a list of unified template objects based on a query.
*
* @param array $query {
* @param array $query {
* Optional. Arguments to retrieve templates.
*
* @type array $slug__in List of slugs to include.
* @type int $wp_id Post ID of customized template.
* @type string $area A 'wp_template_part_area' taxonomy value to filter by (for wp_template_part template type only).
* @type string $post_type Post type to get the templates for.
* }
* @param array $template_type wp_template or wp_template_part.
* @param string $template_type wp_template or wp_template_part.
*
* @return array Templates.
*/
Expand Down Expand Up @@ -362,22 +364,20 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
return $templates;
}

$ids = get_theme_mod( $template_type, array() );
$post__in = 'post__in';

$post_type = isset( $query['post_type'] ) ? $query['post_type'] : '';
$wp_query_args = array(
'post_status' => array( 'auto-draft', 'draft', 'publish' ),
'post_type' => $template_type,
'posts_per_page' => -1,
'no_found_rows' => true,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => wp_get_theme()->get_stylesheet(),
),
),
$post__in => array_values( $ids ),
);

if ( 'wp_template_part' === $template_type && isset( $query['area'] ) ) {
$wp_query_args['tax_query'] = array();
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'wp_template_part_area',
'field' => 'name',
Expand All @@ -387,7 +387,12 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
}

if ( isset( $query['slug__in'] ) ) {
$wp_query_args['post_name__in'] = $query['slug__in'];
$wp_query_args['post__in'] = array();
foreach ( $query['slug__in'] as $slug ) {
if ( ! empty( $ids[ $slug ] ) ) {
$wp_query_args['post__in'][] = $ids[ $slug ];
}
}
}

// This is only needed for the regular templates/template parts CPT listing and editor.
Expand All @@ -397,20 +402,24 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
$wp_query_args['post_status'] = 'publish';
}

$template_query = new WP_Query( $wp_query_args );
$query_result = array();
foreach ( $template_query->posts as $post ) {
$template = _gutenberg_build_template_result_from_post( $post );
$query_result = array();

if ( is_wp_error( $template ) ) {
continue;
}
// See https://core.trac.wordpress.org/ticket/28099 for context.
if ( ! isset( $wp_query_args['post__in'] ) || array() !== $wp_query_args['post__in'] ) {
$template_query = new WP_Query( $wp_query_args );
foreach ( $template_query->posts as $post ) {
$template = _gutenberg_build_template_result_from_post( $post );

if ( $post_type && ! $template->is_custom ) {
continue;
}
if ( is_wp_error( $template ) ) {
continue;
}

if ( $post_type && ! $template->is_custom ) {
continue;
}

$query_result[] = $template;
$query_result[] = $template;
}
}

if ( ! isset( $query['wp_id'] ) ) {
Expand Down Expand Up @@ -465,8 +474,8 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
/**
* Retrieves a single unified template object using its id.
*
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param string $template_type wp_template or wp_template_part.
*
* @return WP_Block_Template|null Template.
*/
Expand All @@ -493,25 +502,22 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
return null;
}
list( $theme, $slug ) = $parts;
$wp_query_args = array(
'post_name__in' => array( $slug ),
'post_type' => $template_type,
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
'posts_per_page' => 1,
'no_found_rows' => true,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $theme,
),
),
);
$template_query = new WP_Query( $wp_query_args );
$posts = $template_query->posts;

if ( count( $posts ) > 0 ) {
$template = _gutenberg_build_template_result_from_post( $posts[0] );
$active = wp_get_theme()->get_stylesheet() === $theme;

// Temporarily disable inactive access for 5.8 version.
if ( ! $active ) {
return null;
}

$ids = get_theme_mod( $template_type, array() );

if ( ! empty( $ids[ $slug ] ) ) {
$post = get_post( $ids[ $slug ] );
}

if ( $post && $template_type === $post->post_type ) {
$template = _gutenberg_build_template_result_from_post( $post );

if ( ! is_wp_error( $template ) ) {
return $template;
Expand Down Expand Up @@ -590,67 +596,3 @@ function gutenberg_get_block_file_template( $id, $template_type = 'wp_template'
*/
return apply_filters( 'get_block_file_template', $block_template, $id, $template_type );
}

/**
* Generates a unique slug for templates or template parts.
*
* @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( $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.
// 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__in' => array( $override_slug ),
'post_type' => $post_type,
'posts_per_page' => 1,
'no_found_rows' => true,
'post__not_in' => array( $post_ID ),
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $theme,
),
),
);
$check_query = new WP_Query( $check_query_args );
$posts = $check_query->posts;

if ( count( $posts ) > 0 ) {
$suffix = 2;
do {
$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->posts ) > 0 );
$override_slug = $alt_post_name;
}

return $override_slug;
}
add_filter( 'pre_wp_unique_post_slug', 'gutenberg_filter_wp_template_unique_post_slug', 10, 5 );
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ public function update_item( $request ) {
return $result;
}

if ( 'custom' !== $template->source ) {
gutenberg_customize_template( $template->slug, $result );
}

$template = gutenberg_get_block_template( $request['id'], $this->post_type );
$fields_update = $this->update_additional_fields_for_object( $template, $request );
if ( is_wp_error( $fields_update ) ) {
Expand Down Expand Up @@ -258,6 +262,9 @@ public function create_item( $request ) {
if ( is_wp_error( $result ) ) {
return $result;
}

gutenberg_customize_template( $request['slug'], $result, false );

$posts = gutenberg_get_block_templates( array( 'wp_id' => $result ), $this->post_type );
if ( ! count( $posts ) ) {
return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.', 'gutenberg' ) );
Expand Down Expand Up @@ -341,21 +348,10 @@ public function delete_item( $request ) {
protected function prepare_item_for_database( $request ) {
$template = $request['id'] ? gutenberg_get_block_template( $request['id'], $this->post_type ) : null;
$changes = new stdClass();
if ( null === $template ) {
if ( null === $template || 'custom' !== $template->source ) {
$changes->post_type = $this->post_type;
$changes->post_status = 'publish';
$changes->tax_input = array(
'wp_theme' => isset( $request['theme'] ) ? $request['theme'] : wp_get_theme()->get_stylesheet(),
);
} elseif ( 'custom' !== $template->source ) {
$changes->post_name = $template->slug;
$changes->post_type = $this->post_type;
$changes->post_status = 'publish';
$changes->tax_input = array(
'wp_theme' => $template->theme,
);
} else {
$changes->post_name = $template->slug;
$changes->ID = $template->wp_id;
$changes->post_status = 'publish';
}
Expand All @@ -377,11 +373,11 @@ protected function prepare_item_for_database( $request ) {

if ( 'wp_template_part' === $this->post_type ) {
if ( isset( $request['area'] ) ) {
$changes->tax_input['wp_template_part_area'] = gutenberg_filter_template_part_area( $request['area'] );
$changes->tax_input = array( 'wp_template_part_area' => gutenberg_filter_template_part_area( $request['area'] ) );
} elseif ( null !== $template && 'custom' !== $template->source && $template->area ) {
$changes->tax_input['wp_template_part_area'] = gutenberg_filter_template_part_area( $template->area );
$changes->tax_input = array( 'wp_template_part_area' => gutenberg_filter_template_part_area( $template->area ) );
} elseif ( ! $template->area ) {
$changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
$changes->tax_input = array( 'wp_template_part_area' => WP_TEMPLATE_PART_AREA_UNCATEGORIZED );
}
}

Expand Down
30 changes: 0 additions & 30 deletions lib/full-site-editing/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,36 +131,6 @@ function gutenberg_fix_template_part_admin_menu_entry() {
// Customize the `wp_template` admin list.
add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_templates_lists_custom_columns' );
add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 );
add_filter( 'views_edit-wp_template_part', 'gutenberg_filter_templates_edit_views' );

/**
* Sets a custom slug when creating auto-draft template parts.
* This is only needed for auto-drafts created by the regular WP editor.
* If this page is to be removed, this won't be necessary.
*
* @param int $post_id Post ID.
*/
function set_unique_slug_on_create_template_part( $post_id ) {
$post = get_post( $post_id );
if ( 'auto-draft' !== $post->post_status ) {
return;
}

if ( ! $post->post_name ) {
wp_update_post(
array(
'ID' => $post_id,
'post_name' => 'custom_slug_' . uniqid(),
)
);
}

$terms = get_the_terms( $post_id, 'wp_theme' );
if ( ! $terms || ! count( $terms ) ) {
wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
}
}
add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' );

/**
* Returns a filtered list of allowed area values for template parts.
Expand Down
Loading