Delayed account creation: Prevent duplicate after saving content (#52456)

* Register block hooks on the PHP side

* changelog

* substitute name

* indenting
This commit is contained in:
Mike Jolley 2024-10-30 12:08:51 +00:00 committed by GitHub
parent 1f03f64e4e
commit c218a82b14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 36 deletions

View File

@ -7,9 +7,6 @@
"keywords": [
"WooCommerce"
],
"blockHooks": {
"woocommerce/order-confirmation-summary": "after"
},
"attributes": {
"customerEmail": {
"type": "string",

View File

@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: Fix for block hooks behind feature flag.

View File

@ -22,48 +22,69 @@ class CreateAccount extends AbstractOrderConfirmationBlock {
*/
protected function initialize() {
parent::initialize();
add_filter( 'hooked_block_woocommerce/order-confirmation-create-account', array( $this, 'hooked_block_content' ), 10, 3 );
if ( $this->is_feature_enabled() ) {
$this->initialize_hooks();
}
}
/**
* Undocumented function
* Initialize hooks.
*
* @param array $parsed_hooked_block Parsed hooked block.
* @param array $hooked_block_type Hooked block type.
* @param string $relative_position Relative position.
* @return array
* @see https://developer.wordpress.org/reference/hooks/hooked_block/
*/
public function hooked_block_content( $parsed_hooked_block, $hooked_block_type, $relative_position ) {
if ( get_option( 'woocommerce_enable_delayed_account_creation', 'yes' ) === 'no' ) {
return null;
}
protected function initialize_hooks() {
// This does not use the Block Hooks Trait used in mini cart. The implementation is simpler because we support
// versions higher than WP 6.5 when hooks were introduced. They should be consolodated in the future.
add_filter(
'hooked_block_types',
function ( $hooked_block_types, $relative_position, $anchor_block_type, $context ) {
if ( 'after' !== $relative_position || 'woocommerce/order-confirmation-summary' !== $anchor_block_type || ! $context instanceof \WP_Block_Template ) {
return $hooked_block_types;
}
if ( 'after' !== $relative_position || is_null( $parsed_hooked_block ) ) {
return $parsed_hooked_block;
}
if ( ! str_contains( $context->content, '<!-- wp:' . $this->get_full_block_name() ) ) {
$hooked_block_types[] = $this->get_full_block_name();
}
/* translators: %s: Site title */
$site_title_heading = sprintf( __( 'Create an account with %s', 'woocommerce' ), wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) );
$parsed_hooked_block['innerContent'] = array(
'<div class="wp-block-woocommerce-order-confirmation-create-account alignwide">
<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">' . esc_html( $site_title_heading ) . '</h3>
<!-- /wp:heading -->
<!-- wp:list {"className":"is-style-checkmark-list"} -->
<ul class="wp-block-list is-style-checkmark-list"><!-- wp:list-item -->
<li>' . esc_html__( 'Faster future purchases', 'woocommerce' ) . '</li>
<!-- /wp:list-item -->
<!-- wp:list-item -->
<li>' . esc_html__( 'Securely save payment info', 'woocommerce' ) . '</li>
<!-- /wp:list-item -->
<!-- wp:list-item -->
<li>' . esc_html__( 'Track orders &amp; view shopping history', 'woocommerce' ) . '</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->
</div>',
return $hooked_block_types;
},
10,
4
);
add_filter(
'hooked_block_woocommerce/order-confirmation-create-account',
function ( $parsed_hooked_block, $hooked_block_type, $relative_position ) {
if ( 'after' !== $relative_position || is_null( $parsed_hooked_block ) ) {
return $parsed_hooked_block;
}
return $parsed_hooked_block;
/* translators: %s: Site title */
$site_title_heading = sprintf( __( 'Create an account with %s', 'woocommerce' ), wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) );
$parsed_hooked_block['innerContent'] = array(
'<div class="wp-block-woocommerce-order-confirmation-create-account alignwide">
<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">' . esc_html( $site_title_heading ) . '</h3>
<!-- /wp:heading -->
<!-- wp:list {"className":"is-style-checkmark-list"} -->
<ul class="wp-block-list is-style-checkmark-list"><!-- wp:list-item -->
<li>' . esc_html__( 'Faster future purchases', 'woocommerce' ) . '</li>
<!-- /wp:list-item -->
<!-- wp:list-item -->
<li>' . esc_html__( 'Securely save payment info', 'woocommerce' ) . '</li>
<!-- /wp:list-item -->
<!-- wp:list-item -->
<li>' . esc_html__( 'Track orders &amp; view shopping history', 'woocommerce' ) . '</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->
</div>',
);
return $parsed_hooked_block;
},
10,
4
);
}
/**