Skip to content

Commit 36b9524

Browse files
authored
fix(my-account): support dynamic content around shortcode (#4328)
1 parent ba27c23 commit 36b9524

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

includes/plugins/woocommerce/my-account/class-my-account-ui-v1.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static function init() {
4646
\add_action( 'newspack_woocommerce_after_account_addresses', [ __CLASS__, 'delete_address_modals' ] );
4747
\add_action( 'woocommerce_after_save_address_validation', [ __CLASS__, 'handle_delete_address_submission' ], 10, 4 );
4848
\add_filter( 'woocommerce_address_to_edit', [ __CLASS__, 'reorder_address_fields' ], PHP_INT_MAX, 2 );
49+
\add_action( 'woocommerce_account_content', [ __CLASS__, 'render_content_around_shortcode' ], 0 );
4950
}
5051

5152
/**
@@ -870,5 +871,47 @@ public static function reorder_address_fields( $address, $load_address ) {
870871

871872
return $address;
872873
}
874+
875+
/**
876+
* Render the content around the [woocommerce_my_account] shortcode inside the
877+
* woocommerce-MyAccount-content context.
878+
*
879+
* This is necessary because of the highly customized grid layout.
880+
*/
881+
public static function render_content_around_shortcode() {
882+
// Only allow custom content under the dashboard (root) or edit-account (default redirect) pages.
883+
global $wp;
884+
if ( ! isset( $wp->query_vars['page'] ) && ! empty( $wp->query_vars ) && ! isset( $wp->query_vars['edit-account'] ) ) {
885+
return;
886+
}
887+
888+
$content = get_the_content();
889+
if ( empty( $content ) ) {
890+
return;
891+
}
892+
$parts = explode( '[woocommerce_my_account]', $content );
893+
894+
$before = ! empty( $parts[0] ) ? $parts[0] : '';
895+
if ( ! empty( $before ) ) {
896+
add_action(
897+
'woocommerce_account_content',
898+
function() use ( $before ) {
899+
echo apply_filters( 'the_content', $before ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
900+
},
901+
9 // Right before the shortcode.
902+
);
903+
}
904+
905+
$after = ! empty( $parts[1] ) ? $parts[1] : '';
906+
if ( ! empty( $after ) ) {
907+
add_action(
908+
'woocommerce_account_content',
909+
function() use ( $after ) {
910+
echo apply_filters( 'the_content', $after ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
911+
},
912+
11 // Right after the shortcode.
913+
);
914+
}
915+
}
873916
}
874917
My_Account_UI_V1::init();

includes/plugins/woocommerce/my-account/templates/v1/my-account.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,17 @@
3232
?>
3333

3434
<div class="main-content">
35-
<?php the_content(); ?>
35+
<?php
36+
/**
37+
* Given the highly customized grid layout, we'll be rendering the
38+
* shortcode directly and rely on a separate strategy to render
39+
* content around the shortcode.
40+
*
41+
* See My_Account_UI_V1::render_content_around_shortcode() for more
42+
* details.
43+
*/
44+
echo do_shortcode( '[woocommerce_my_account]' );
45+
?>
3646
</div>
3747

3848
<?php endwhile; ?>

src/my-account/v1/_content.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@
2323
section + section {
2424
margin-top: var(--newspack-ui-spacer-11);
2525
}
26+
.wp-block-details + .wp-block-details {
27+
margin-top: 0;
28+
}
2629
}
2730
}

0 commit comments

Comments
 (0)