if ( count( $templates ) &&
$fallback_template_slug ===
$templates[0
]->slug &&
'theme' ===
$templates[0
]->source
) { // Unfortunately, we cannot trust $templates[0]->theme, since it will always
// be set to the active theme's slug by _build_block_template_result_from_file(),
// even if the block template is really coming from the active theme's parent.
// (The reason for this is that we want it to be associated with the active theme
// -- not its parent -- once we edit it and store it to the DB as a wp_template CPT.)
// Instead, we use _get_block_template_file() to locate the block template file.
$template_file =
_get_block_template_file( 'wp_template',
$fallback_template_slug );
if ( $template_file &&
get_template() ===
$template_file['theme'
] ) { // The block template is part of the parent theme, so we
// have to give precedence to the child theme's PHP template.
array_shift( $templates );
} } } return count( $templates ) ?
$templates[0
] : null;
}