Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Allow plugin based template parts (#9667)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Jun 20, 2023
1 parent 53bd2f4 commit 9ae3e1a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,38 @@ protected function init() {
add_filter( 'woocommerce_settings_pages', array( $this, 'template_permalink_settings' ) );
add_filter( 'pre_update_option', array( $this, 'update_template_permalink' ), 10, 2 );
add_action( 'woocommerce_admin_field_permalink', array( SettingsUtils::class, 'permalink_input_field' ) );

// By default, the Template Part Block only supports template parts that are in the current theme directory.
// This render_callback wrapper allows us to add support for plugin-housed template parts.
add_filter(
'block_type_metadata_settings',
function( $settings, $metadata ) {
if ( isset( $metadata['name'], $settings['render_callback'] ) && 'core/template-part' === $metadata['name'] && 'render_block_core_template_part' === $settings['render_callback'] ) {
$settings['render_callback'] = [ $this, 'render_woocommerce_template_part' ];
}
return $settings;
},
10,
2
);
}
}

/**
* Renders the `core/template-part` block on the server.
*
* @param array $attributes The block attributes.
* @return string The render.
*/
public function render_woocommerce_template_part( $attributes ) {
if ( 'woocommerce/woocommerce' === $attributes['theme'] ) {
$template_part = BlockTemplateUtils::get_block_template( $attributes['theme'] . '//' . $attributes['slug'], 'wp_template_part' );

if ( $template_part && ! empty( $template_part->content ) ) {
return do_blocks( $template_part->content );
}
}
return \render_block_core_template_part( $attributes );
}

/**
Expand Down

0 comments on commit 9ae3e1a

Please sign in to comment.