Skip to content

Commit

Permalink
fix: support new is_my_account() method from main plugin (#1869)
Browse files Browse the repository at this point in the history
* fix: support new is_my_account() method from main plugin

* fix: remove redundant checkout_fields filter

* fix: use query param instead of only referrer
  • Loading branch information
dkoo authored Sep 16, 2024
1 parent f0c0726 commit b9a3aba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 53 deletions.
39 changes: 20 additions & 19 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static function woocommerce_checkout_create_order_line_item( $item, $cart
* Change the post-transaction return URL.
* This is specifically for non-redirect-based flows, such as Apple Pay.
*
* @param array $result The return payload for a successfull transaction.
* @param array $result The return payload for a successful transaction.
*/
public static function woocommerce_payment_successful_result( $result ) {
$order_id = $result['order_id'];
Expand All @@ -134,24 +134,21 @@ public static function woocommerce_payment_successful_result( $result ) {
return $result;
}

$originating_from_modal = ! empty( $order->get_meta( '_newspack_modal_checkout_url' ) );
if ( $originating_from_modal ) {
$modal_checkout_url_query = \wp_parse_url( $modal_checkout_url, PHP_URL_QUERY );
\wp_parse_str( $modal_checkout_url_query, $modal_checkout_url_query_params );
$passed_params_names = [ 'modal_checkout', 'after_success_behavior', 'after_success_url', 'after_success_button_label' ];
// Pick passed params from the query params.
$passed_params = array_intersect_key( $modal_checkout_url_query_params, array_flip( $passed_params_names ) );

$result['redirect'] = \add_query_arg(
array_merge(
$passed_params,
[
'order_id' => $order_id,
]
),
$result['redirect']
);
}
$modal_checkout_url_query = \wp_parse_url( $modal_checkout_url, PHP_URL_QUERY );
\wp_parse_str( $modal_checkout_url_query, $modal_checkout_url_query_params );
$passed_params_names = [ 'modal_checkout', 'after_success_behavior', 'after_success_url', 'after_success_button_label' ];
// Pick passed params from the query params.
$passed_params = array_intersect_key( $modal_checkout_url_query_params, array_flip( $passed_params_names ) );

$result['redirect'] = \add_query_arg(
array_merge(
$passed_params,
[
'order_id' => $order_id,
]
),
$result['redirect']
);
return $result;
}

Expand Down Expand Up @@ -1077,6 +1074,10 @@ public static function hide_expiry_message_shop_link( $message ) {
* Is this request using the modal checkout?
*/
public static function is_modal_checkout() {
// Until we use the modal checkout flow from My Account, we don't want to show the modal checkout thank you template for checkouts originating from My Account.
if ( method_exists( 'Newspack\WooCommerce_My_Account', 'is_from_my_account' ) && \Newspack\WooCommerce_My_Account::is_from_my_account() ) {
return false;
}

$is_modal_checkout = isset( $_REQUEST['modal_checkout'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! $is_modal_checkout && isset( $_REQUEST['post_data'] ) && is_string( $_REQUEST['post_data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
* Server-side rendering of the `newspack-blocks/donate` block.
*/
class Newspack_Blocks_Donate_Renderer {
/**
* Constructor.
*/
public function __construct() {
add_filter( 'woocommerce_checkout_fields', [ __CLASS__, 'woocommerce_checkout_fields' ] );
}

/**
* Get the keys of the billing fields to render for logged out users.
Expand All @@ -40,34 +34,6 @@ public static function get_billing_fields_keys() {
return apply_filters( 'newspack_blocks_donate_billing_fields_keys', $fields );
}

/**
* Modify fields for modal checkout.
*
* @param array $fields Checkout fields.
*
* @return array
*/
public static function woocommerce_checkout_fields( $fields ) {
if ( ! class_exists( 'Newspack\Donations' ) || ! method_exists( 'Newspack\Donations', 'is_donation_cart' ) ) {
return $fields;
}
if ( ! \Newspack\Donations::is_donation_cart() ) {
return $fields;
}
$billing_fields = self::get_billing_fields_keys();
if ( empty( $billing_fields ) ) {
return $fields;
}
$billing_keys = array_keys( $fields['billing'] );
foreach ( $billing_keys as $key ) {
if ( in_array( $key, $billing_fields, true ) ) {
continue;
}
unset( $fields['billing'][ $key ] );
}
return $fields;
}

/**
* Enqueue frontend scripts and styles.
*
Expand Down

0 comments on commit b9a3aba

Please sign in to comment.