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

Update check for active cart template and migration routine #10462

Merged
merged 6 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,33 @@ protected function migrate_page( $page_id, $page ) {
$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(
$new_page_template = BlockTemplateUtils::get_block_template( 'woocommerce/woocommerce//' . $page_id, 'wp_template' );

// Check template validity--template must exist, and custom template must not be present already.
if ( ! $new_page_template || $new_page_template->wp_id ) {
update_option( 'has_migrated_' . $page_id, '1' );
return;
}

$new_page_template_id = wp_insert_post(
[
'id' => 'woocommerce/woocommerce//' . $page_id,
'content' => $template_content,
]
'post_name' => $new_page_template->slug,
'post_type' => 'wp_template',
'post_status' => 'publish',
'tax_input' => array(
'wp_theme' => $new_page_template->theme,
),
'meta_input' => array(
'origin' => $new_page_template->source,
),
'post_content' => $template_content,
],
true
);
rest_get_server()->dispatch( $request );
update_option( 'has_migrated_' . $page_id, '1' );

if ( ! is_wp_error( $new_page_template_id ) ) {
update_option( 'has_migrated_' . $page_id, '1' );
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Templates/CartTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public static function get_placeholder_page() {
*/
protected function is_active_template() {
global $post;
return $post instanceof \WP_Post && get_option( 'woocommerce_cart_page_endpoint' ) === $post->post_name;
$placeholder = $this->get_placeholder_page();
return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Templates/CheckoutTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static function get_template_title() {
*/
public function is_active_template() {
global $post;
return $post instanceof \WP_Post && get_option( 'woocommerce_checkout_page_endpoint' ) === $post->post_name;
$placeholder = $this->get_placeholder_page();
return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name;
}
}