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

Commit

Permalink
Migrate pages to templates once (#9488)
Browse files Browse the repository at this point in the history
* Migrate content on init, once

* Skip migration if page does not exist

* Put back HTML for header and footer parts

* Fix page redirect due to wrong ID

* fix loading template part

* Removed unnecessary var

* update cart and checkout html templates

---------

Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com>
  • Loading branch information
mikejolley and wavvves authored May 16, 2023
1 parent e242e5c commit 58dfbd9
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 175 deletions.
77 changes: 71 additions & 6 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ protected function init() {
add_filter( 'post_type_archive_title', array( $this, 'update_product_archive_title' ), 10, 2 );

if ( wc_current_theme_is_fse_theme() ) {
add_action( 'init', array( $this, 'maybe_migrate_content' ) );
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' ) );
Expand Down Expand Up @@ -335,16 +336,11 @@ function( $template ) {
if ( ! $template->description ) {
$template->description = BlockTemplateUtils::get_block_template_description( $template->slug );
}

if ( str_contains( $template->slug, 'single-product' ) ) {
if ( ! is_admin() && ! BlockTemplateUtils::template_has_legacy_template_block( $template ) ) {

$new_content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content );
$template->content = $new_content;
$template->content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content );
}
return $template;
}

return $template;
},
$query_result
Expand Down Expand Up @@ -654,6 +650,75 @@ function_exists( 'is_shop' ) &&
return $post_type_name;
}

/**
* Migrates page content to templates if needed.
*/
public function maybe_migrate_content() {
if ( ! $this->has_migrated_page( 'cart' ) ) {
$this->migrate_page( 'cart', CartTemplate::get_placeholder_page() );
}
if ( ! $this->has_migrated_page( 'checkout' ) ) {
$this->migrate_page( 'checkout', CheckoutTemplate::get_placeholder_page() );
}
}

/**
* Check if a page has been migrated to a template.
*
* @param string $page_id Page ID.
* @return boolean
*/
protected function has_migrated_page( $page_id ) {
return (bool) get_option( 'has_migrated_' . $page_id, false );
}

/**
* Migrates a page to a template if needed.
*
* @param string $page_id Page ID.
* @param \WP_Post $page Page object.
*/
protected function migrate_page( $page_id, $page ) {
if ( ! $page || empty( $page->post_content ) ) {
update_option( 'has_migrated_' . $page_id, '1' );
return;
}

$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' ),
]
);
rest_get_server()->dispatch( $request );
update_option( 'has_migrated_' . $page_id, '1' );
}

/**
* Returns the requested template part.
*
* @param string $part The part to return.
*
* @return string
*/
protected function get_block_template_part( $part ) {
$template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );
if ( ! $template_part || empty( $template_part->content ) ) {
return '';
}
return $template_part->content;
}

/**
* Replaces page settings in WooCommerce with text based permalinks which point to a template.
*
Expand Down
29 changes: 1 addition & 28 deletions src/Templates/AbstractPageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function __construct() {
protected function init() {
add_filter( 'page_template_hierarchy', array( $this, 'page_template_hierarchy' ), 1 );
add_filter( 'frontpage_template_hierarchy', array( $this, 'page_template_hierarchy' ), 1 );
add_filter( 'woocommerce_blocks_template_content', array( $this, 'page_template_content' ), 10, 2 );
add_action( 'current_screen', array( $this, 'page_template_editor_redirect' ) );
add_filter( 'pre_get_document_title', array( $this, 'page_template_title' ) );
}
Expand Down Expand Up @@ -66,18 +65,6 @@ protected function get_edit_template_url() {
return admin_url( 'site-editor.php?postType=wp_template&postId=woocommerce%2Fwoocommerce%2F%2F' . $this->get_slug() );
}

/**
* Get the default content for a template.
*
* Overridden by child class to include their own logic.
*
* @param string $template_content The original content of the template.
* @return string
*/
protected function get_default_template_content( $template_content ) {
return $template_content;
}

/**
* When the page should be displaying the template, add it to the hierarchy.
*
Expand All @@ -94,20 +81,6 @@ public function page_template_hierarchy( $templates ) {
return $templates;
}

/**
* Returns the default template content.
*
* @param string $template_content The content of the template.
* @param object $template_file The template file object.
* @return string
*/
public function page_template_content( $template_content, $template_file ) {
if ( $this->get_slug() !== $template_file->slug ) {
return $template_content;
}
return $this->get_default_template_content( $template_content );
}

/**
* Redirect the edit page screen to the template editor.
*
Expand All @@ -117,7 +90,7 @@ public function page_template_editor_redirect( \WP_Screen $current_screen ) {
$page = $this->get_placeholder_page();
$edit_page_id = 'page' === $current_screen->id && ! empty( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( $page && $edit_page_id === $page->id ) {
if ( $page && $edit_page_id === $page->ID ) {
wp_safe_redirect( $this->get_edit_template_url() );
exit;
}
Expand Down
27 changes: 0 additions & 27 deletions src/Templates/CartTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,4 @@ protected function is_active_template() {
public static function get_template_title() {
return __( 'Cart', 'woo-gutenberg-products-block' );
}

/**
* Migrates an existing page using blocks to the block templates.
*
* @param string $template_content The content of the template.
* @return string
*/
public function get_default_template_content( $template_content ) {
$page = $this->get_placeholder_page();

if ( $page && ! empty( $page->post_content ) ) {
$template_content = '
<!-- wp:template-part {"slug":"header","tagName":"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 -->
<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->
';
}

return $template_content;
}
}
25 changes: 0 additions & 25 deletions src/Templates/CheckoutTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,4 @@ public static function get_template_title() {
public function is_active_template() {
return is_checkout();
}

/**
* Migrates an existing page using blocks to the block templates.
*
* @param string $template_content The content of the template.
* @return string
*/
public function get_default_template_content( $template_content ) {
$page = $this->get_placeholder_page();

if ( $page && ! empty( $page->post_content ) ) {
$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 -->
';
}

return $template_content;
}
}
20 changes: 2 additions & 18 deletions src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,8 @@ public static function build_template_result_from_file( $template_file, $templat
$template_is_from_theme = 'theme' === $template_file->source;
$theme_name = wp_get_theme()->get( 'TextDomain' );

/**
* Hook that allows the content to be filtered before it is returned.
*
* @since TBD
*
* @param string $content The content of the template.
* @param object $template_file The template file.
* @param string $template_type The type of template.
* @return string
*/
$template_content = apply_filters(
'woocommerce_blocks_template_content',
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( $template_file->path ),
$template_file,
$template_type
);

// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$template_content = file_get_contents( $template_file->path );
$template = new \WP_Block_Template();
$template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : self::PLUGIN_SLUG . '//' . $template_file->slug;
$template->theme = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG;
Expand Down
Loading

0 comments on commit 58dfbd9

Please sign in to comment.