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

Commit 06504d4

Browse files
oprmikejolley
authored andcommitted
Update check for active cart template and migration routine (#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 <[email protected]>
1 parent beb0286 commit 06504d4

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/BlockTemplatesController.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ protected function migrate_page( $page_id, $page ) {
822822
// Use the page template if it exists, which we'll use over our default template if found.
823823
$existing_page_template = BlockTemplateUtils::get_block_template( get_stylesheet() . '//page', 'wp_template' );
824824

825-
if ( $existing_page_template && ! empty( $existing_page_template->content ) ) {
825+
if ( $existing_page_template && ! empty( $existing_page_template->content ) && strstr( $existing_page_template->content, 'wp:post-content' ) ) {
826826
// Massage the original content into something we can use. Replace post content with a group block.
827827
$pattern = '/(<!--\s*)wp:post-content(.*?)(\/-->)/';
828828
$replacement = '
@@ -835,15 +835,33 @@ protected function migrate_page( $page_id, $page ) {
835835
$template_content = $this->get_default_migrate_page_template( $page );
836836
}
837837

838-
$request = new \WP_REST_Request( 'POST', '/wp/v2/templates/woocommerce/woocommerce//' . $page_id );
839-
$request->set_body_params(
838+
$new_page_template = BlockTemplateUtils::get_block_template( 'woocommerce/woocommerce//' . $page_id, 'wp_template' );
839+
840+
// Check template validity--template must exist, and custom template must not be present already.
841+
if ( ! $new_page_template || $new_page_template->wp_id ) {
842+
update_option( 'has_migrated_' . $page_id, '1' );
843+
return;
844+
}
845+
846+
$new_page_template_id = wp_insert_post(
840847
[
841-
'id' => 'woocommerce/woocommerce//' . $page_id,
842-
'content' => $template_content,
843-
]
848+
'post_name' => $new_page_template->slug,
849+
'post_type' => 'wp_template',
850+
'post_status' => 'publish',
851+
'tax_input' => array(
852+
'wp_theme' => $new_page_template->theme,
853+
),
854+
'meta_input' => array(
855+
'origin' => $new_page_template->source,
856+
),
857+
'post_content' => $template_content,
858+
],
859+
true
844860
);
845-
rest_get_server()->dispatch( $request );
846-
update_option( 'has_migrated_' . $page_id, '1' );
861+
862+
if ( ! is_wp_error( $new_page_template_id ) ) {
863+
update_option( 'has_migrated_' . $page_id, '1' );
864+
}
847865
}
848866

849867
/**

src/Templates/CartTemplate.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public static function get_placeholder_page() {
3333
*/
3434
protected function is_active_template() {
3535
global $post;
36-
return $post instanceof \WP_Post && get_option( 'woocommerce_cart_page_endpoint' ) === $post->post_name;
36+
$placeholder = $this->get_placeholder_page();
37+
return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name;
3738
}
3839

3940
/**

src/Templates/CheckoutTemplate.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static function get_template_title() {
4242
*/
4343
public function is_active_template() {
4444
global $post;
45-
return $post instanceof \WP_Post && get_option( 'woocommerce_checkout_page_endpoint' ) === $post->post_name;
45+
$placeholder = $this->get_placeholder_page();
46+
return null !== $placeholder && $post instanceof \WP_Post && $placeholder->post_name === $post->post_name;
4647
}
4748
}

0 commit comments

Comments
 (0)