Automattic\WooCommerce\Admin

PageController::maybe_redirect_payment_tasks_to_settingspublicWC 1.0

Redirect payment tasks to the settings page.

Redirects both 'payments' and 'woocommerce-payments' tasks to the Payments settings page, when it is safe to do so in terms of backwards compatibility.

Method of the class: PageController{}

No Hooks.

Returns

null. Nothing (null).

Usage

$PageController = new PageController();
$PageController->maybe_redirect_payment_tasks_to_settings();

PageController::maybe_redirect_payment_tasks_to_settings() code WC 10.4.3

public function maybe_redirect_payment_tasks_to_settings() {
	// Bail if we are not in the WP admin or not on a WC admin page.
	if ( ! is_admin() || ! self::is_admin_page() ) {
		return;
	}

	// Bail if we are not requesting a page for a WooCommerce task.
	// phpcs:ignore WordPress.Security.NonceVerification
	if ( empty( $_GET['task'] ) ) {
		return;
	}

	// Only sufficiently capable users should be redirected.
	if ( ! current_user_can( 'manage_woocommerce' ) ) {
		return;
	}

	// Get the current task ID.
	// phpcs:ignore WordPress.Security.NonceVerification
	$task_id = wc_clean( wp_unslash( $_GET['task'] ) );

	// Bail if the task is not a payments task.
	if ( ! in_array( $task_id, array( 'payments', 'woocommerce-payments' ), true ) ) {
		return;
	}

	$redirect_url = admin_url( 'admin.php?page=wc-settings&tab=checkout&from=WCADMIN_PAYMENT_TASK' );

	// The WooPayments task is always redirected to the settings page.
	if ( 'woocommerce-payments' === $task_id ) {
		wp_safe_redirect( $redirect_url );
		exit;
	}

	// The generic payments task is only redirected if the request is a regular user request,
	// not part of an onboarding flow or other special case.
	$special_request_params = array(
		// This is used by the legacy, Payments task-based suggestions onboarding flow.
		// Nobody should be using this anymore, but just in case.
		'connection-return',
		// This is used by the legacy, Payments task-based suggestions onboarding flow.
		// Nobody should be using this anymore, but just in case.
		'id',
		// Some params for gateway IDs, just in case.
		'gateway_id',
		'gateway-id',
		// Sometimes the gateway is referred to as 'method'. Stay clear of it.
		'method',
		// If there is a success or error param, better not redirect.
		'success',
		'error',
		// If the URL is nonced, better not redirect.
		'_wpnonce',
	);
	foreach ( $special_request_params as $param ) {
		// phpcs:ignore WordPress.Security.NonceVerification
		if ( isset( $_GET[ $param ] ) ) {
			return;
		}
	}

	// If we reach this point, we can safely redirect to the settings page.
	wp_safe_redirect( $redirect_url );
	exit;
}