Automattic\WooCommerce\EmailEditor\Engine\Templates
Templates::add_theme_templates
This is need to enable saving post – template association. When a theme doesn't support block_templates feature the association is not saved, because templates registered via register_block_template are not added to the list of available templates. https://github.com/WordPress/wordpress-develop/blob/cdc2f255acce57372b849d6278c4156e1056c749/src/wp-includes/class-wp-theme.php#L1355
This function ensures that the email templates are in the list which is used for checking if the template can be saved in the association. See https://github.com/WordPress/wordpress-develop/blob/cdc2f255acce57372b849d6278c4156e1056c749/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L1595-L1599
Method of the class: Templates{}
No Hooks.
Returns
Array.
Usage
$Templates = new Templates(); $Templates->add_theme_templates( $templates, $theme, $post, $post_type );
- $templates(array) (required)
- The templates.
- $theme(string) (required)
- The theme.
- $post(WP_Post) (required)
- The post.
- $post_type(string) (required)
- The post type.
Templates::add_theme_templates() Templates::add theme templates code WC 10.5.0
public function add_theme_templates( $templates, $theme, $post, $post_type ) {
if ( $post_type && ! in_array( $post_type, $this->post_types, true ) ) {
return $templates;
}
$block_templates = get_block_templates();
$email_templates_slugs = array_map(
function ( Template $template ) {
return $template->get_slug();
},
$this->templates_registry->get_all()
);
foreach ( $block_templates as $block_template ) {
if ( ! in_array( $block_template->slug, $email_templates_slugs, true ) ) {
continue;
}
if ( isset( $templates[ $block_template->slug ] ) ) {
continue;
}
$templates[ $block_template->slug ] = $block_template->title; // Requires only the template title, not the full template object.
}
return $templates;
}