Skip to content

Commit

Permalink
feat(donate-block): changes to button-after-success (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek authored Oct 30, 2023
1 parent 092007a commit ac1c6a6
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 129 deletions.
131 changes: 87 additions & 44 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,29 @@ public static function get_checkout_template( $template ) {
ob_end_flush();
}

/**
* Get after success button params.
*/
private static function get_after_success_params() {
return array_filter(
[
'after_success_behavior' => isset( $_REQUEST['after_success_behavior'] ) ? rawurlencode( sanitize_text_field( wp_unslash( $_REQUEST['after_success_behavior'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
'after_success_url' => isset( $_REQUEST['after_success_url'] ) ? rawurlencode( sanitize_text_field( wp_unslash( $_REQUEST['after_success_url'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
'after_success_button_label' => isset( $_REQUEST['after_success_button_label'] ) ? rawurlencode( sanitize_text_field( wp_unslash( $_REQUEST['after_success_button_label'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
]
);
}

/**
* Render hidden inputs to pass some params along.
*/
private static function render_hidden_inputs() {
foreach ( self::get_after_success_params() as $key => $value ) {
?>
<input type="hidden" name="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $value ); ?>" />
<?php
}
}

/**
* Return URL for modal checkout "thank you" page.
Expand All @@ -327,13 +350,13 @@ public static function woocommerce_get_return_url( $url, $order ) {
return $url;
}

$args = [
'modal_checkout' => '1',
'email' => isset( $_REQUEST['billing_email'] ) ? rawurlencode( \sanitize_email( \wp_unslash( $_REQUEST['billing_email'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
'after_success_behavior' => isset( $_REQUEST['after_success_behavior'] ) ? rawurlencode( sanitize_text_field( wp_unslash( $_REQUEST['after_success_behavior'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
'after_success_url' => isset( $_REQUEST['after_success_url'] ) ? rawurlencode( sanitize_text_field( wp_unslash( $_REQUEST['after_success_url'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
'after_success_button_label' => isset( $_REQUEST['after_success_button_label'] ) ? rawurlencode( sanitize_text_field( wp_unslash( $_REQUEST['after_success_button_label'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
];
$args = array_merge(
[
'modal_checkout' => '1',
'email' => isset( $_REQUEST['billing_email'] ) ? rawurlencode( \sanitize_email( \wp_unslash( $_REQUEST['billing_email'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
],
self::get_after_success_params()
);

// Pass order ID for modal checkout templates.
if ( $order && is_a( $order, 'WC_Order' ) ) {
Expand Down Expand Up @@ -562,28 +585,42 @@ public static function order_review_fragments( $fragments ) {
}

/**
* Adds an additional button to the Thank you page
* Render markup at the end of the "thank you" view.
*
* @param WC_Order $order The order related to the transaction.
*
* @return void
*/
public static function render_checkout_after_success_markup() {
public static function render_checkout_after_success_markup( $order ) {
if ( self::is_newsletter_signup_available() ) {
self::render_newsletter_signup_form( $order );
} else {
self::render_after_success_button();
}
}

/**
* Render markup at the end of the "thank you" view.
*
* @return void
*/
private static function render_after_success_button() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if (
empty( $_REQUEST['modal_checkout'] ) ||
empty( $_REQUEST['after_success_behavior'] ) ||
empty( $_REQUEST['after_success_url'] )
) {
if ( empty( $_REQUEST['modal_checkout'] ) ) {
return;
}

$button_label = ! empty( $_REQUEST['after_success_button_label'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['after_success_button_label'] ) ) : __( 'Continue browsing', 'newspack-blocks' );

$button_label = ! empty( $_REQUEST['after_success_button_label'] ) ? urldecode( wp_unslash( $_REQUEST['after_success_button_label'] ) ) : __( 'Continue browsing', 'newspack-blocks' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$url = ! empty( $_REQUEST['after_success_url'] ) ? urldecode( wp_unslash( $_REQUEST['after_success_url'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
?>
<a
class="button"
href="<?php echo esc_url( sanitize_text_field( wp_unslash( $_REQUEST['after_success_url'] ) ) ); ?>"
target="_top"
style="display:block;margin:16px 0;"
<?php if ( empty( $url ) ) : ?>
onclick="parent.newspackCloseModalCheckout(this);"
<?php else : ?>
href="<?php echo esc_url( $url ); ?>"
target="_top"
<?php endif; ?>
class="button newspack-modal-newsletters__button"
>
<?php echo esc_html( $button_label ); ?>
</a>
Expand All @@ -596,10 +633,7 @@ class="button"
*
* @param WC_Order $order The order related to the transaction.
*/
public static function render_newsletter_signup_form( $order ) {
if ( ! self::is_newsletter_signup_available() ) {
return false;
}
private static function render_newsletter_signup_form( $order ) {
$email_address = $order->get_billing_email();
if ( ! $email_address ) {
return;
Expand Down Expand Up @@ -646,6 +680,7 @@ function( $item ) {
<input type="hidden" name="modal_checkout" value="1" />
<input type="hidden" name="newsletter_signup_email" value="<?php echo esc_html( $email_address ); ?>" />
<?php
self::render_hidden_inputs();
foreach ( $newsletters_lists as $list ) {
$checkbox_id = sprintf( 'newspack-blocks-list-%s', $list['id'] );
?>
Expand Down Expand Up @@ -693,18 +728,22 @@ public static function confirm_newsletter_signup() {
}
$signup_data = self::get_newsletter_signup_data();
if ( false !== $signup_data ) {
$result = \Newspack_Newsletters_Subscription::add_contact(
[
'email' => $signup_data['email'],
'metadata' => [
'current_page_url' => home_url( add_query_arg( array(), \wp_get_referer() ) ),
'newsletters_subscription_method' => 'post-checkout',
if ( empty( $signup_data['lists'] ) ) {
return new \WP_Error( 'newspack_no_lists_selected', __( 'No lists selected.', 'newspack-blocks' ) );
} else {
$result = \Newspack_Newsletters_Subscription::add_contact(
[
'email' => $signup_data['email'],
'metadata' => [
'current_page_url' => home_url( add_query_arg( array(), \wp_get_referer() ) ),
'newsletters_subscription_method' => 'post-checkout',
],
],
],
$signup_data['lists']
);
if ( \is_wp_error( $result ) ) {
return $result;
$signup_data['lists']
);
if ( \is_wp_error( $result ) ) {
return $result;
}
}
return true;
}
Expand All @@ -718,22 +757,28 @@ public static function get_newsletter_signup_data() {
$newsletter_signup_email = isset( $_GET['newsletter_signup_email'] ) ? \sanitize_text_field( \wp_unslash( $_GET['newsletter_signup_email'] ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $newsletter_signup_email && isset( $_SERVER['REQUEST_URI'] ) ) {
parse_str( \wp_parse_url( \wp_unslash( $_SERVER['REQUEST_URI'] ) )['query'], $query ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$signup_data = [
'email' => $newsletter_signup_email,
'lists' => [],
];
if ( isset( $query['lists'] ) && count( $query['lists'] ) ) {
return [
'email' => $newsletter_signup_email,
'lists' => $query['lists'],
];
$signup_data['lists'] = $query['lists'];
}
return $signup_data;
}
return false;
}

/**
* Renders newsletter signup confirmation.
*
* @param bool $no_lists_selected Whether no lists were selected.
*/
public static function render_newsletter_confirmation() {
public static function render_newsletter_confirmation( $no_lists_selected = false ) {
?>
<h4><?php esc_html_e( 'Signup successful!', 'newspack-blocks' ); ?></h4>
<?php if ( ! $no_lists_selected ) : ?>
<h4><?php esc_html_e( 'Signup successful!', 'newspack-blocks' ); ?></h4>
<?php endif; ?>
<p>
<?php
echo esc_html(
Expand All @@ -745,10 +790,8 @@ public static function render_newsletter_confirmation() {
);
?>
</p>
<button onclick="parent.newspackCloseModalCheckout(this);" class="newspack-modal-newsletters__button">
<?php esc_html_e( 'Close', 'newspack-blocks' ); ?>
</button>
<?php
self::render_after_success_button();
}
}
Modal_Checkout::init();
3 changes: 2 additions & 1 deletion src/blocks/checkout-button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"type": "string"
},
"afterSuccessButtonLabel": {
"type": "string"
"type": "string",
"default": "Continue browsing"
},
"afterSuccessURL": {
"type": "string"
Expand Down
43 changes: 1 addition & 42 deletions src/blocks/checkout-button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import apiFetch from '@wordpress/api-fetch';
* Internal dependencies
*/
import './edit.scss';
import RedirectAfterSuccess from '../../components/redirect-after-success';

function getVariationName( variation ) {
const attributes = [];
Expand Down Expand Up @@ -153,48 +154,6 @@ function ProductControl( props ) {
);
}

function RedirectAfterSuccess( props ) {
const { attributes, setAttributes } = props;
return (
<>
<SelectControl
label={ __( 'Post-Checkout Button', 'newspack-blocks' ) }
help={ __(
'Select whether the user should be presented with a button to navigate after a successful purchase.',
'newspack-blocks'
) }
value={ attributes.afterSuccessBehavior }
options={ [
{ label: __( 'Do not show a button', 'newspack-blocks' ), value: '' },
{ label: __( 'Go to a custom URL', 'newspack-blocks' ), value: 'custom' },
{ label: __( 'Go to the previous page', 'newspack-blocks' ), value: 'referrer' },
] }
onChange={ value => {
setAttributes( { afterSuccessBehavior: value.toString() } );
} }
/>
{ attributes.afterSuccessBehavior !== '' && (
<>
<TextControl
label={ __( 'Button Label', 'newspack-blocks' ) }
placeholder={ __( 'Continue browsing', 'newspack-blocks' ) }
value={ attributes.afterSuccessButtonLabel || '' }
onChange={ value => setAttributes( { afterSuccessButtonLabel: value } ) }
/>
{ attributes.afterSuccessBehavior === 'custom' && (
<TextControl
label={ __( 'Custom URL', 'newspack-blocks' ) }
placeholder={ __( 'https://example.com', 'newspack-blocks' ) }
value={ attributes.afterSuccessURL || '' }
onChange={ value => setAttributes( { afterSuccessURL: value } ) }
/>
) }
</>
) }
</>
);
}

function CheckoutButtonEdit( props ) {
const { attributes, setAttributes, className } = props;
const { placeholder, style, text, product, price, variation } = attributes;
Expand Down
10 changes: 10 additions & 0 deletions src/blocks/donate/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@
"additionalFields": {
"type": "array",
"default": []
},
"afterSuccessBehavior": {
"type": "string"
},
"afterSuccessButtonLabel": {
"type": "string",
"default": "Continue browsing"
},
"afterSuccessURL": {
"type": "string"
}
},
"supports": {
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/donate/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
LAYOUT_OPTIONS,
DISABLED_IN_TIERS_BASED_LAYOUT_TIER_INDEX,
} from '../consts';
import RedirectAfterSuccess from '../../../components/redirect-after-success';

const TIER_LABELS = [
__( 'Low-tier', 'newspack-blocks' ),
Expand Down Expand Up @@ -421,6 +422,9 @@ const Edit = ( { attributes, setAttributes, className }: EditProps ) => {
</p>
</PanelBody>
) }
<PanelBody title={ __( 'After purchase', 'newspack-blocks' ) }>
<RedirectAfterSuccess setAttributes={ setAttributes } attributes={ attributes } />
</PanelBody>
</InspectorControls>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ protected static function render_streamlined_payment_ui( $attributes ) {
}

/**
* Render hidden form input indentifying a donate form submission.
* Render hidden form inputs.
*
* @param array $attributes The block attributes.
*/
protected static function render_donate_form_input() {
protected static function render_hidden_form_inputs( $attributes ) {
ob_start();
/**
* Action to add custom fields before the form fields of the donation block.
Expand All @@ -302,6 +304,16 @@ protected static function render_donate_form_input() {
?>
<input type='hidden' name='newspack_donate' value='1' />
<?php

foreach ( [ [ 'afterSuccessBehavior', 'after_success_behavior' ], [ 'afterSuccessButtonLabel', 'after_success_button_label' ], [ 'afterSuccessURL', 'after_success_url' ] ] as $attribute ) {
$attribute_name = $attribute[0];
$param_name = $attribute[1];
$value = isset( $attributes[ $attribute_name ] ) ? $attributes[ $attribute_name ] : '';
?>
<input type='hidden' name='<?php echo esc_attr( $param_name ); ?>' value='<?php echo esc_attr( $value ); ?>' />
<?php
}

return ob_get_clean();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class="untiered <?php echo esc_html( $configuration['container_classnames'] ); ?
id="<?php echo esc_html( $configuration['uid'] ); ?>"
>
<form data-streamlined-config="<?php echo esc_html( htmlspecialchars( wp_json_encode( $configuration['configuration_for_streamlined'] ), ENT_QUOTES, 'UTF-8' ) ); ?>">
<?php echo self::render_donate_form_input(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo self::render_hidden_form_inputs( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div class='wp-block-newspack-blocks-donate__options'>
<div class='wp-block-newspack-blocks-donate__frequencies frequencies'>
<?php foreach ( $configuration['frequencies'] as $frequency_slug => $frequency_name ) : ?>
Expand Down Expand Up @@ -154,7 +154,7 @@ class="tiered <?php echo esc_html( $configuration['container_classnames'] ); ?>"
id="<?php echo esc_html( $configuration['uid'] ); ?>"
>
<form data-streamlined-config="<?php echo esc_html( htmlspecialchars( wp_json_encode( $configuration['configuration_for_streamlined'] ), ENT_QUOTES, 'UTF-8' ) ); ?>">
<?php echo self::render_donate_form_input(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo self::render_hidden_form_inputs( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div class='wp-block-newspack-blocks-donate__options'>
<div class='wp-block-newspack-blocks-donate__frequencies frequencies'>
<?php foreach ( $configuration['frequencies'] as $frequency_slug => $frequency_name ) : ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class="<?php echo esc_html( $configuration['container_classnames'] ); ?>"
>
<form data-is-init-form <?php echo 'stripe' === $configuration['platform'] ? 'onsubmit="return false;"' : ''; ?>>
<div class="wpbnbd__tiers__view">
<?php echo self::render_donate_form_input(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo self::render_hidden_form_inputs( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<input type="hidden" name="<?php echo esc_attr( self::FREQUENCY_PARAM ); ?>" value="<?php echo esc_attr( $intial_selected_frequency ); ?>">
<div class="wpbnbd__tiers">
<div class="wpbnbd__tiers__selection">
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/donate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export type DonateBlockAttributes = OverridableConfiguration & {
tiersBasedOptions: [ TierBasedOptionValue, TierBasedOptionValue, TierBasedOptionValue ];
// Manual mode enables block-level overrides of the global Donate settings.
manual: boolean;
// Post-checkout button option.
afterSuccessBehavior: string;
afterSuccessButtonLabel: string;
afterSuccessURL: string;
// Legacy attributes.
suggestedAmounts?: [ number, number, number ];
suggestedAmountUntiered?: number;
Expand Down
Loading

0 comments on commit ac1c6a6

Please sign in to comment.