Skip to content

Commit

Permalink
Refactor redirects logic in payments (#8590)
Browse files Browse the repository at this point in the history
Co-authored-by: oaratovskyi <oleksandr.aratovskyi@automattic.com>
  • Loading branch information
oaratovskyi and oaratovskyi authored Jun 27, 2024
1 parent 56b35cf commit c17029a
Show file tree
Hide file tree
Showing 10 changed files with 633 additions and 710 deletions.
4 changes: 4 additions & 0 deletions changelog/dev-7654-refactor-redirects-logic-in-payments
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Refactor redirects logic in payments
55 changes: 4 additions & 51 deletions includes/admin/class-wc-payments-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

use Automattic\Jetpack\Identity_Crisis as Jetpack_Identity_Crisis;
use Automattic\WooCommerce\Admin\PageController;
use WCPay\Core\Server\Request;
use WCPay\Database_Cache;
use WCPay\Logger;
Expand Down Expand Up @@ -203,8 +202,7 @@ public function init_hooks() {

// Add menu items.
add_action( 'admin_menu', [ $this, 'add_payments_menu' ], 0 );
add_action( 'admin_init', [ $this, 'maybe_redirect_to_onboarding' ], 11 ); // Run this after the WC setup wizard and onboarding redirection logic.
add_action( 'admin_enqueue_scripts', [ $this, 'maybe_redirect_overview_to_connect' ], 1 ); // Run this late (after `admin_init`) but before any scripts are actually enqueued.
add_action( 'admin_init', [ $this, 'maybe_redirect_from_payments_admin_child_pages' ], 11 ); // Run this after the WC setup wizard and onboarding redirection logic.
add_action( 'admin_enqueue_scripts', [ $this, 'register_payments_scripts' ], 9 );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_payments_scripts' ], 9 );
add_action( 'woocommerce_admin_field_payment_gateways', [ $this, 'payment_gateways_container' ] );
Expand Down Expand Up @@ -1086,10 +1084,10 @@ private function get_settings_menu_item_name() {
}

/**
* Checks if Stripe account is connected and redirects to the onboarding page
* if it is not and the user is attempting to view a WCPay admin page.
* If the user is attempting to view a WCPay admin page without a connected Stripe account,
* redirect them to the connect account page.
*/
public function maybe_redirect_to_onboarding() {
public function maybe_redirect_from_payments_admin_child_pages() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
Expand Down Expand Up @@ -1126,51 +1124,6 @@ public function maybe_redirect_to_onboarding() {
$this->account->redirect_to_onboarding_welcome_page();
}

/**
* Avoid WC Admin /payments/overview error page when the current account associated Stripe account is not valid.
*
* The errored page happens because we don't register a /payments/overview WC admin page when the Stripe account
* is not valid and register only a /payments/connect top level menu page.
*
* Places around our plugin redirect merchants to the overview page by default (or using it for the Stripe KYC
* return URL) leading to poor UX.
* This is a safety net to prevent that from happening.
*
* @see self::add_payments_menu()
*/
public function maybe_redirect_overview_to_connect() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( wp_doing_ajax() ) {
return;
}

// If the current page is registered, let it pass.
if ( wc_admin_is_registered_page() ) {
return;
}

$url_params = wp_unslash( $_GET ); // phpcs:ignore WordPress.Security.NonceVerification
if ( empty( $url_params['page'] ) || 'wc-admin' !== $url_params['page']
|| empty( $url_params['path'] ) || '/payments/overview' !== $url_params['path'] ) {
return;
}

/**
* Determine the path of the top level menu page since that can change between payments/connect and payments/overview.
*
* @see self::add_payments_menu()
*/
$top_level_page_path = PageController::get_instance()->get_path_from_id( 'wc-payments' );
// If the top level page path is not the payments/connect one, bail.
if ( 'wc-admin&path=/payments/connect' !== $top_level_page_path ) {
return;
}

$this->account->redirect_to_onboarding_welcome_page();
}

/**
* Add woopay as a payment method to the edit order on admin.
*
Expand Down
Loading

0 comments on commit c17029a

Please sign in to comment.