Skip to content

Commit

Permalink
Filter custom block templates with PHP
Browse files Browse the repository at this point in the history
This method calls get_block_templates once and uses block template properties
directly for filtering. This way, we can avoid hitting the database for each
public post type.

The previous method is useful when we already know the current post type we
request templates for, like when using REST API.

Follows [52334].
See #54335.
Props mamaduka, youknowriad.

Built from https://develop.svn.wordpress.org/trunk@52365


git-svn-id: http://core.svn.wordpress.org/trunk@51957 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
noisysocks committed Dec 14, 2021
1 parent 935058d commit 3cd0e25
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion wp-includes/class-wp-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -1250,9 +1250,17 @@ public function get_post_templates() {
}

if ( current_theme_supports( 'block-templates' ) ) {
$block_templates = get_block_templates( array(), 'wp_template' );
foreach ( get_post_types( array( 'public' => true ) ) as $type ) {
$block_templates = get_block_templates( array( 'post_type' => $type ), 'wp_template' );
foreach ( $block_templates as $block_template ) {
if ( ! $block_template->is_custom ) {
continue;
}

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

$post_templates[ $type ][ $block_template->slug ] = $block_template->title;
}
}
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-beta2-52364';
$wp_version = '5.9-beta2-52365';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 3cd0e25

Please sign in to comment.