From 4ba3dd050b1cea39435c705f9ed60b5566a62876 Mon Sep 17 00:00:00 2001 From: Thomas Roberts <5656702+opr@users.noreply.github.com> Date: Fri, 4 Aug 2023 16:05:39 +0100 Subject: [PATCH] Update check for active cart template and migration routine (https://github.com/woocommerce/woocommerce-blocks/pull/10462) * Update cart/checkout endpoints * Remove updating option on every page load * Check placeholder page vs current page * Check placeholder page vs current page * Switch from Rest to PHP for migrating templates * Existing page used for migration must contain post-content to be suitable --------- Co-authored-by: Mike Jolley --- .../src/BlockTemplatesController.php | 34 ++++++++++++++----- .../src/Templates/CartTemplate.php | 3 +- .../src/Templates/CheckoutTemplate.php | 3 +- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/plugins/woocommerce-blocks/src/BlockTemplatesController.php b/plugins/woocommerce-blocks/src/BlockTemplatesController.php index f8232803040..2d717697bd8 100644 --- a/plugins/woocommerce-blocks/src/BlockTemplatesController.php +++ b/plugins/woocommerce-blocks/src/BlockTemplatesController.php @@ -822,7 +822,7 @@ class BlockTemplatesController { // 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 ) ) { + if ( $existing_page_template && ! empty( $existing_page_template->content ) && strstr( $existing_page_template->content, 'wp:post-content' ) ) { // Massage the original content into something we can use. Replace post content with a group block. $pattern = '/()/'; $replacement = ' @@ -835,15 +835,33 @@ class BlockTemplatesController { $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' ); + } } /** diff --git a/plugins/woocommerce-blocks/src/Templates/CartTemplate.php b/plugins/woocommerce-blocks/src/Templates/CartTemplate.php index af804d10113..ed126951543 100644 --- a/plugins/woocommerce-blocks/src/Templates/CartTemplate.php +++ b/plugins/woocommerce-blocks/src/Templates/CartTemplate.php @@ -33,7 +33,8 @@ class CartTemplate extends AbstractPageTemplate { */ 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; } /** diff --git a/plugins/woocommerce-blocks/src/Templates/CheckoutTemplate.php b/plugins/woocommerce-blocks/src/Templates/CheckoutTemplate.php index c69df17f9c4..000c6edbe18 100644 --- a/plugins/woocommerce-blocks/src/Templates/CheckoutTemplate.php +++ b/plugins/woocommerce-blocks/src/Templates/CheckoutTemplate.php @@ -42,6 +42,7 @@ class CheckoutTemplate extends AbstractPageTemplate { */ 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; } }