Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
get_block_templates and entity collection back compat
  • Loading branch information
ellatrix committed Oct 13, 2025
commit 6668287edcdf89ca2bcb9e5575d1b434b901b582
226 changes: 221 additions & 5 deletions lib/compat/wordpress-6.9/template-activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@

add_action( 'pre_get_block_template', 'gutenberg_get_block_template', 10, 3 );

///////////////////////////////////////////////////////////////////////
// This function is a copy of core's, except for the marked section. //
///////////////////////////////////////////////////////////////////////
function gutenberg_get_block_template( $output, $id, $template_type ) {
if ( 'wp_template' !== $template_type ) {
return $output;
Expand All @@ -550,17 +553,61 @@
return null;
}
list( $theme, $slug ) = $parts;

//////////////////////////////
// START CORE MODIFICATIONS //
//////////////////////////////
$active_templates = get_option( 'active_templates', array() );

Check warning on line 560 in lib/compat/wordpress-6.9/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned correctly; expected 1 space but found 5 spaces

if ( ! empty( $active_templates[ $slug ] ) ) {
$post = get_post( $active_templates[ $slug ] );
if ( $post && 'publish' === $post->post_status ) {
$template = _build_block_template_result_from_post( $post );
if ( is_int( $active_templates[ $slug ] ) ) {
$post = get_post( $active_templates[ $slug ] );
if ( $post && 'publish' === $post->post_status ) {
$template = _build_block_template_result_from_post( $post );

if ( ! is_wp_error( $template ) && $theme === $template->theme ) {
return $template;
if ( ! is_wp_error( $template ) && $theme === $template->theme ) {
return $template;
}
}
} else if ( $active_templates[ $slug ] === false ) {

Check failure on line 572 in lib/compat/wordpress-6.9/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Use Yoda Condition checks, you must.

Check warning on line 572 in lib/compat/wordpress-6.9/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Usage of ELSE IF is discouraged; use ELSEIF instead
return null;
}
}
////////////////////////////
// END CORE MODIFICATIONS //
////////////////////////////

$wp_query_args = array(

Check warning on line 580 in lib/compat/wordpress-6.9/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 8 spaces
'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 );

Check warning on line 594 in lib/compat/wordpress-6.9/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 1 space but found 7 spaces
$posts = $template_query->posts;

Check warning on line 595 in lib/compat/wordpress-6.9/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 10 spaces but found 16 spaces

if ( count( $posts ) > 0 ) {
$template = _build_block_template_result_from_post( $posts[0] );

//////////////////////////////
// START CORE MODIFICATIONS //
//////////////////////////////
// Custom templates don't need to be activated, so if it's a custom
// template, return it.
if ( ! is_wp_error( $template ) && $template->is_custom ) {
return $template;
}
////////////////////////////
// END CORE MODIFICATIONS //
////////////////////////////
}

$block_template = get_block_file_template( $id, $template_type );
Expand All @@ -576,3 +623,172 @@
*/
return apply_filters( 'get_block_template', $block_template, $id, $template_type );
}

add_action( 'pre_get_block_templates', 'gutenberg_get_block_templates', 10, 3 );

///////////////////////////////////////////////////////////////////////
// This function is a copy of core's, except for the marked section. //
///////////////////////////////////////////////////////////////////////
function gutenberg_get_block_templates( $output, $query = array(), $template_type = 'wp_template' ) {
if ( 'wp_template' !== $template_type ) {
return $output;
}

$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,
'lazy_load_term_meta' => false,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => get_stylesheet(),
),
),
);

if ( ! empty( $query['slug__in'] ) ) {
$wp_query_args['post_name__in'] = $query['slug__in'];
$wp_query_args['posts_per_page'] = count( array_unique( $query['slug__in'] ) );
}

// This is only needed for the regular templates/template parts post type listing and editor.
if ( isset( $query['wp_id'] ) ) {
$wp_query_args['p'] = $query['wp_id'];
} else {
$wp_query_args['post_status'] = 'publish';
}

//////////////////////////////
// START CORE MODIFICATIONS //
//////////////////////////////
$active_templates = get_option( 'active_templates', array() );

Check warning on line 668 in lib/compat/wordpress-6.9/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned correctly; expected 1 space but found 5 spaces
////////////////////////////
// END CORE MODIFICATIONS //
////////////////////////////

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

if ( is_wp_error( $template ) ) {
continue;
}

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

if (
$post_type &&
isset( $template->post_types ) &&
! in_array( $post_type, $template->post_types, true )
) {
continue;
}

//////////////////////////////
// START CORE MODIFICATIONS //
//////////////////////////////
if ( $template->is_custom ) {
// Custom templates don't need to be activated, leave them be.
$query_result[] = $template;
} else if ( isset( $active_templates[ $template->slug ] ) && $active_templates[ $template->slug ] === $post->ID ) {

Check warning on line 700 in lib/compat/wordpress-6.9/template-activate.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Usage of ELSE IF is discouraged; use ELSEIF instead
// Only include active templates.
$query_result[] = $template;
}
////////////////////////////
// END CORE MODIFICATIONS //
////////////////////////////
}

if ( ! isset( $query['wp_id'] ) ) {
/*
* If the query has found some user templates, those have priority
* over the theme-provided ones, so we skip querying and building them.
*/
$query['slug__not_in'] = wp_list_pluck( $query_result, 'slug' );
/*
* We need to unset the post_type query param because some templates
* would be excluded otherwise, like `page.html` when looking for
* `page` templates. We need all templates so we can exclude duplicates
* from plugin-registered templates.
* See: https://github.com/WordPress/gutenberg/issues/65584
*/
$template_files_query = $query;
unset( $template_files_query['post_type'] );
$template_files = _get_block_templates_files( $template_type, $template_files_query );
foreach ( $template_files as $template_file ) {
// If the query doesn't specify a post type, or it does and the template matches the post type, add it.
if (
! isset( $query['post_type'] ) ||
(
isset( $template_file['postTypes'] ) &&
in_array( $query['post_type'], $template_file['postTypes'], true )
)
) {
$query_result[] = _build_block_template_result_from_file( $template_file, $template_type );
} elseif ( ! isset( $template_file['postTypes'] ) ) {
// The custom templates with no associated post types are available for all post types as long
// as they are not default templates.
$candidate = _build_block_template_result_from_file( $template_file, $template_type );
$default_template_types = get_default_block_template_types();
if ( ! isset( $default_template_types[ $candidate->slug ] ) ) {
$query_result[] = $candidate;
}
}
}

if ( 'wp_template' === $template_type ) {
// Add templates registered in the template registry. Filtering out the ones which have a theme file.
$registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $query );
$matching_registered_templates = array_filter(
$registered_templates,
function ( $registered_template ) use ( $template_files ) {
foreach ( $template_files as $template_file ) {
if ( $template_file['slug'] === $registered_template->slug ) {
return false;
}
}
return true;
}
);

$matching_registered_templates = array_map(
function ( $template ) {
$template->content = apply_block_hooks_to_content(
$template->content,
$template,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);
return $template;
},
$matching_registered_templates
);

$query_result = array_merge( $query_result, $matching_registered_templates );
}
}

/**
* Filters the array of queried block templates array after they've been fetched.
*
* @since 5.9.0
*
* @param WP_Block_Template[] $query_result Array of found block templates.
* @param array $query {
* Arguments to retrieve templates. All arguments are optional.
*
* @type string[] $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 string $template_type wp_template or wp_template_part.
*/
return apply_filters( 'get_block_templates', $query_result, $query, $template_type );
}
15 changes: 14 additions & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,20 @@ export const getEntityRecords =
};
}

const path = addQueryArgs( entityConfig.baseURL, {
let { baseURL } = entityConfig;
const { combinedTemplates = true } = query;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we document this better?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, it is more understandable now here, although maybe a different variable name could help, like includeThemeTemplates?

Copy link
Member Author

Choose a reason for hiding this comment

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

The thing is that it doesn’t just add theme templates. It also combines db templates that are active and omits the rest


if (
kind === 'postType' &&
name === 'wp_template' &&
combinedTemplates
) {
baseURL =
baseURL.slice( 0, baseURL.lastIndexOf( '/' ) ) +
'/templates';
}

const path = addQueryArgs( baseURL, {
...entityConfig.baseURLParams,
...query,
} );
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default function PageTemplates() {
const { records: userRecords, isResolving: isLoadingUserRecords } =
useEntityRecordsWithPermissions( 'postType', TEMPLATE_POST_TYPE, {
per_page: -1,
combinedTemplates: false,
} );
const { records: staticRecords, isResolving: isLoadingStaticData } =
useEntityRecordsWithPermissions( 'postType', 'wp_registered_template', {
Expand Down
Loading