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

Commit

Permalink
Inherit template from original "page" template
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Jul 27, 2023
1 parent 69bf97e commit 43865c6
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,29 @@ protected function has_migrated_page( $page_id ) {
return (bool) get_option( 'has_migrated_' . $page_id, false );
}

/**
* Prepare default page template.
*
* @param \WP_Post $page Page object.
* @return string
*/
protected function get_default_migrate_page_template( $page ) {
$default_template_content = $this->get_block_template_part( 'header' );
$default_template_content .= '
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group">
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading">' . wp_kses_post( $page->post_title ) . '</h1>
<!-- /wp:heading -->
' . wp_kses_post( $page->post_content ) . '
</div>
<!-- /wp:group -->
';
$default_template_content .= $this->get_block_template_part( 'footer' );

return $default_template_content;
}

/**
* Migrates a page to a template if needed.
*
Expand All @@ -790,20 +813,27 @@ protected function migrate_page( $page_id, $page ) {
return;
}

// Use the page template if it exists, which we'll use over our default template if found.
$existing_page_template = BlockTemplateUtils::get_block_template( get_stylesheet() . '//page', 'wp_template' );

if ( $existing_page_template && ! empty( $existing_page_template->content ) ) {
// Massage the original content into something we can use. Replace post content with a group block.
$pattern = '/(<!--\s*)wp:post-content(.*?)(\/-->)/';
$replacement = '
<!-- wp:group $2 -->
<div class="wp-block-group">' . wp_kses_post( $page->post_content ) . '</div>
<!-- /wp:group -->
';
$template_content = preg_replace( $pattern, $replacement, $existing_page_template->content );
} else {
$template_content = $this->get_default_migrate_page_template( $page );
}

$request = new \WP_REST_Request( 'POST', '/wp/v2/templates/woocommerce/woocommerce//' . $page_id );
$request->set_body_params(
[
'id' => 'woocommerce/woocommerce//' . $page_id,
'content' => $this->get_block_template_part( 'header' ) .
'<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group">
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading">' . wp_kses_post( $page->post_title ) . '</h1>
<!-- /wp:heading -->
' . wp_kses_post( $page->post_content ) . '
</div>
<!-- /wp:group -->' .
$this->get_block_template_part( 'footer' ),
'content' => $template_content,
]
);
rest_get_server()->dispatch( $request );
Expand All @@ -818,7 +848,7 @@ protected function migrate_page( $page_id, $page ) {
* @return string
*/
protected function get_block_template_part( $part ) {
$template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );
$template_part = BlockTemplateUtils::get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );
if ( ! $template_part || empty( $template_part->content ) ) {
return '';
}
Expand Down

0 comments on commit 43865c6

Please sign in to comment.