Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Skip] Payment from Setup wizard when no payment method is active #2434

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
22 changes: 11 additions & 11 deletions includes/Admin/SetupWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class SetupWizard {

/** @var string Currenct Step */
protected $step = '';
protected string $current_step = '';

/** @var array Steps for the setup wizard */
protected $steps = [];
Expand Down Expand Up @@ -266,10 +266,10 @@ public function setup_wizard() {
unset( $this->steps['recommended'] );
}

$this->step = current( array_keys( $this->steps ) );
$this->current_step = current( array_keys( $this->steps ) );
// get step from url
if ( isset( $_GET['_admin_sw_nonce'], $_GET['step'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_admin_sw_nonce'] ) ), 'dokan_admin_setup_wizard_nonce' ) ) {
$this->step = sanitize_key( wp_unslash( $_GET['step'] ) );
$this->current_step = sanitize_key( wp_unslash( $_GET['step'] ) );
}

$this->enqueue_scripts();
Expand All @@ -278,8 +278,8 @@ public function setup_wizard() {
isset( $_POST['_wpnonce'], $_POST['save_step'] )
&& wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'dokan-setup' )
&& ! empty( $_POST['save_step'] )
&& isset( $this->steps[ $this->step ]['handler'] ) ) {
call_user_func_array( $this->steps[ $this->step ]['handler'], [ $this ] );
&& isset( $this->steps[ $this->current_step ]['handler'] ) ) {
call_user_func_array( $this->steps[ $this->current_step ]['handler'], [ $this ] );
}

ob_start();
Expand All @@ -292,7 +292,7 @@ public function get_next_step_link() {

return add_query_arg(
[
'step' => $keys[ array_search( $this->step, array_keys( $this->steps ), true ) + 1 ],
'step' => $keys[ array_search( $this->current_step, array_keys( $this->steps ), true ) + 1 ],
'_admin_sw_nonce' => wp_create_nonce( 'dokan_admin_setup_wizard_nonce' ),
]
);
Expand Down Expand Up @@ -328,7 +328,7 @@ public function setup_wizard_header() {
*/
public function setup_wizard_footer() {
?>
<?php if ( 'next_steps' === $this->step ) : ?>
<?php if ( 'next_steps' === $this->current_step ) : ?>
<a class="wc-return-to-dashboard" href="<?php echo esc_url( admin_url() ); ?>"><?php esc_html_e( 'Return to the WordPress Dashboard', 'dokan-lite' ); ?></a>
<?php endif; ?>
</body>
Expand All @@ -347,9 +347,9 @@ public function setup_wizard_steps() {
<?php foreach ( $ouput_steps as $step_key => $step ) : ?>
<li class="
<?php
if ( $step_key === $this->step ) {
if ( $step_key === $this->current_step ) {
echo 'active';
} elseif ( array_search( $this->step, array_keys( $this->steps ), true ) > array_search( $step_key, array_keys( $this->steps ), true ) ) {
} elseif ( array_search( $this->current_step, array_keys( $this->steps ), true ) > array_search( $step_key, array_keys( $this->steps ), true ) ) {
echo 'done';
}
?>
Expand All @@ -363,13 +363,13 @@ public function setup_wizard_steps() {
* Output the content for the current step.
*/
public function setup_wizard_content() {
if ( empty( $this->steps[ $this->step ]['view'] ) ) {
if ( empty( $this->steps[ $this->current_step ]['view'] ) ) {
wp_safe_redirect( esc_url_raw( add_query_arg( 'step', 'introduction' ) ) );
exit;
}

echo '<div class="wc-setup-content">';
call_user_func( $this->steps[ $this->step ]['view'] );
call_user_func( $this->steps[ $this->current_step ]['view'] );
Comment on lines +366 to +372
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Implementation missing for payment step skipping.

The PR objective is to skip the payment step when no payment method is active, but this functionality is not implemented in the setup wizard content handling.

Would you like me to help implement the logic for skipping the payment step when no payment methods are active? This would involve:

  1. Adding a check for active payment methods
  2. Modifying the step progression logic to skip the payment step when appropriate

echo '</div>';
}

Expand Down
4 changes: 2 additions & 2 deletions includes/Admin/SetupWizardNoWC.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ protected function set_setup_wizard_template() {
* @return void
*/
public function setup_wizard_content() {
if ( empty( $this->steps[ $this->step ]['view'] ) ) {
if ( empty( $this->steps[ $this->current_step ]['view'] ) ) {
wp_safe_redirect( esc_url_raw( add_query_arg( 'step', 'install_woocommerce' ) ) );
exit;
}

echo '<div class="wc-setup-content">';
call_user_func( $this->steps[ $this->step ]['view'] );
call_user_func( $this->steps[ $this->current_step ]['view'] );
echo '</div>';
}

Expand Down
158 changes: 116 additions & 42 deletions includes/Vendor/SetupWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* Seller setup wizard class
*/
class SetupWizard extends DokanSetupWizard {
/** @var string Currenct Step */
protected $step = '';
/** @var string Current Step */
protected string $current_step = '';

/** @var array Steps for the setup wizard */
protected $steps = [];
Expand All @@ -36,7 +36,7 @@
}

// define the woocommerce_registration_redirect callback
public function filter_woocommerce_registration_redirect( $var ) {

Check warning on line 39 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

It is recommended not to use reserved keyword "var" as function parameter name. Found: $var
$url = $var;
$user = wp_get_current_user();

Expand Down Expand Up @@ -73,39 +73,19 @@
$this->store_id = dokan_get_current_user_id();
$this->store_info = dokan_get_store_info( $this->store_id );

$steps = [
'introduction' => [
'name' => __( 'Introduction', 'dokan-lite' ),
'view' => [ $this, 'dokan_setup_introduction' ],
'handler' => '',
],
'store' => [
'name' => __( 'Store', 'dokan-lite' ),
'view' => [ $this, 'dokan_setup_store' ],
'handler' => [ $this, 'dokan_setup_store_save' ],
],
'payment' => [
'name' => __( 'Payment', 'dokan-lite' ),
'view' => [ $this, 'dokan_setup_payment' ],
'handler' => [ $this, 'dokan_setup_payment_save' ],
],
'next_steps' => [
'name' => __( 'Ready!', 'dokan-lite' ),
'view' => [ $this, 'dokan_setup_ready' ],
'handler' => '',
],
];

$this->steps = apply_filters( 'dokan_seller_wizard_steps', $steps );
$this->step = current( array_keys( $this->steps ) );
// Setup wizard steps
$this->set_steps();

// get step from url
if ( isset( $_GET['_admin_sw_nonce'], $_GET['step'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_admin_sw_nonce'] ) ), 'dokan_admin_setup_wizard_nonce' ) ) {
$this->step = sanitize_key( wp_unslash( $_GET['step'] ) );
$this->current_step = sanitize_key( wp_unslash( $_GET['step'] ) ) ?? $this->get_first_step();
}

if ( ! empty( $_POST['save_step'] ) && isset( $this->steps[ $this->step ]['handler'] ) ) { // WPCS: CSRF ok.
call_user_func( $this->steps[ $this->step ]['handler'] );
if ( ! isset( $this->steps[ $this->current_step ]['handler'] ) ) {
$this->current_step = $this->get_first_step();
}
if ( ! empty( $_POST['save_step'] ) ) { // WPCS: CSRF ok.
call_user_func( $this->steps[ $this->current_step ]['handler'] );
}

$this->enqueue_scripts();
Expand Down Expand Up @@ -166,7 +146,7 @@
*/
public function setup_wizard_footer() {
?>
<?php if ( 'next_steps' === $this->step ) : ?>
<?php if ( 'next_steps' === $this->current_step ) : ?>
<a class="wc-return-to-dashboard" href="<?php echo esc_url( site_url() ); ?>"><?php esc_attr_e( 'Return to the Marketplace', 'dokan-lite' ); ?></a>
<?php endif; ?>
</body>
Expand All @@ -179,7 +159,7 @@
*/
public function dokan_setup_introduction() {
$dashboard_url = dokan_get_navigation_url();
$default_message = wp_kses_post( __( '<p>Thank you for choosing The Marketplace to power your online store! This quick setup wizard will help you configure the basic settings. <strong>It’s completely optional and shouldn’t take longer than two minutes.</strong></p>', 'dokan-lite' ) );

Check warning on line 162 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Translatable string should not be wrapped in HTML. Found: '<p>Thank you for choosing The Marketplace to power your online store! This quick setup wizard will help you configure the basic settings. <strong>It’s completely optional and shouldn’t take longer than two minutes.</strong></p>'
$setup_wizard_message = dokan_get_option( 'setup_wizard_message', 'dokan_general', $default_message );
?>
<h1><?php esc_attr_e( 'Welcome to the Marketplace!', 'dokan-lite' ); ?></h1>
Expand All @@ -189,7 +169,7 @@
<p><?php esc_attr_e( 'No time right now? If you don’t want to go through the wizard, you can skip and return to the Store!', 'dokan-lite' ); ?></p>
<p class="wc-setup-actions step">
<a href="<?php echo esc_url( $this->get_next_step_link() ); ?>" class="button-primary button button-large button-next lets-go-btn dokan-btn-theme"><?php esc_attr_e( 'Let\'s Go!', 'dokan-lite' ); ?></a>
<a href="<?php echo esc_url( $dashboard_url ); ?>" class="button button-large not-right-now-btn dokan-btn-theme"><?php esc_attr_e( 'Not right now', 'dokan-lite' ); ?></a>
<a href="<?php echo esc_url( $dashboard_url ); ?>" class="button button-large not-right-now-btn dokan-btn-secondary dokan-btn-theme"><?php esc_attr_e( 'Not right now', 'dokan-lite' ); ?></a>
</p>
<?php
do_action( 'dokan_seller_wizard_introduction', $this );
Expand Down Expand Up @@ -230,7 +210,7 @@
<input type="text" id="address[street_1]" name="address[street_1]" value="<?php echo esc_attr( $address_street1 ); ?>"/>
<span class="error-container">
<?php
if ( ! empty( $_POST['error_address[street_1]'] ) ) {

Check failure on line 213 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.
echo '<span class="required">' . __( 'This is required', 'dokan-lite' ) . '</span>';
}
?>
Expand All @@ -247,7 +227,7 @@
<input type="text" id="address[street_2]" name="address[street_2]" value="<?php echo esc_attr( $address_street2 ); ?>"/>
<span class="error-container">
<?php
if ( ! empty( $_POST['error_address[street_2]'] ) ) {

Check failure on line 230 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.
echo '<span class="required">' . __( 'This is required', 'dokan-lite' ) . '</span>';
}
?>
Expand All @@ -265,7 +245,7 @@
<input type="text" id="address[city]" name="address[city]" value="<?php echo esc_attr( $address_city ); ?>"/>
<span class="error-container">
<?php
if ( ! empty( $_POST['error_address[city]'] ) ) {

Check failure on line 248 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.
echo '<span class="required">' . __( 'This is required', 'dokan-lite' ) . '</span>';
}
?>
Expand All @@ -282,7 +262,7 @@
<input type="text" id="address[zip]" name="address[zip]" value="<?php echo esc_attr( $address_zip ); ?>"/>
<span class="error-container">
<?php
if ( ! empty( $_POST['error_address[zip]'] ) ) {

Check failure on line 265 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.
echo '<span class="required">' . __( 'This is required', 'dokan-lite' ) . '</span>';
}
?>
Expand All @@ -301,7 +281,7 @@
</select>
<span class="error-container">
<?php
if ( ! empty( $_POST['error_address[country]'] ) ) {

Check failure on line 284 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.
echo '<span class="required">' . __( 'This is required', 'dokan-lite' ) . '</span>';
}
?>
Expand All @@ -319,7 +299,7 @@
<input type="text" id="calc_shipping_state" name="address[state]" value="<?php echo esc_attr( $address_state ); ?>" / placeholder="<?php esc_attr_e( 'State Name', 'dokan-lite' ); ?>">
<span class="error-container">
<?php
if ( ! empty( $_POST['error_address[state]'] ) ) {

Check failure on line 302 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.
echo '<span class="required">' . __( 'This is required', 'dokan-lite' ) . '</span>';
}
?>
Expand Down Expand Up @@ -502,8 +482,11 @@
$dokan_settings['location'] = isset( $_POST['location'] ) ? sanitize_text_field( wp_unslash( $_POST['location'] ) ) : '';
$dokan_settings['find_address'] = isset( $_POST['find_address'] ) ? sanitize_text_field( wp_unslash( $_POST['find_address'] ) ) : '';
$dokan_settings['show_email'] = isset( $_POST['show_email'] ) ? 'yes' : 'no';

// Validating fileds.
$country = $dokan_settings['address']['country'] ?? '';
$state = $dokan_settings['address']['state'] ?? '';
$state_not_exists = ! isset( $states[ $country ] ); // If state not exists for the country array then user must provide the state name
$country_has_states = is_array( $states[ $country ] ) && count( $states[ $country ] ) > 0; // If country has states more than 0 then user must provide the state name
$state_is_empty = empty( $state );
$is_valid_form = true;
if ( empty( $dokan_settings['address']['street_1'] ) ) {
$is_valid_form = false;
Expand All @@ -520,12 +503,9 @@
if ( empty( $dokan_settings['address']['country'] ) ) {
$is_valid_form = false;
$_POST['error_address[country]'] = 'error';
}
else {
if ( ( isset( $states[ $dokan_settings['address']['country'] ] ) && count( $states[ $dokan_settings['address']['country'] ] ) && empty( $dokan_settings['address']['state'] ) || ( ! isset( $states[ $dokan_settings['address']['country'] ] ) && empty( $dokan_settings['address']['state'] ) ) ) ) {
$is_valid_form = false;
$_POST['error_address[state]'] = 'error';
}
} elseif ( ( $country_has_states && $state_is_empty ) || ( $state_not_exists && $state_is_empty ) ) {
$is_valid_form = false;
$_POST['error_address[state]'] = 'error';
}

if ( ! $is_valid_form ) {
Expand All @@ -549,6 +529,12 @@
public function dokan_setup_payment() {
$methods = dokan_withdraw_get_active_methods();
$store_info = $this->store_info;
// If payment step is accessed but no active methods exist, redirect to next step
if ( 'payment' === $this->current_step && empty( $methods ) ) {
wp_safe_redirect( esc_url_raw( $this->get_next_step_link() ) );
exit;
}

?>
<h1><?php esc_html_e( 'Payment Setup', 'dokan-lite' ); ?></h1>
<form method="post" id='dokan-seller-payment-setup-form' novalidate>
Expand Down Expand Up @@ -607,12 +593,16 @@
'swift' => $bank['swift'],
];

$user_bank_data = array_filter( $dokan_settings['payment']['bank'], function( $item ) { return ! empty( $item ); } );
$user_bank_data = array_filter(
$dokan_settings['payment']['bank'], function ( $item ) {
return ! empty( $item );
}
);
$require_fields = array_keys( dokan_bank_payment_required_fields() );

$has_bank_information = true;
foreach ( $require_fields as $require_field ) {
if( empty( $user_bank_data[ $require_field ] ) ) {
if ( empty( $user_bank_data[ $require_field ] ) ) {
$_POST[ 'error_' . $require_field ] = 'error';
$has_bank_information = false;
}
Expand Down Expand Up @@ -666,4 +656,88 @@
</div>
<?php
}

/**
* Gets the URL for the next step in the wizard
*
* Handles special logic to skip the payment step if no withdrawal methods
* are active, preventing users from accessing an empty payment step
*
* @since 2.9.27
*
* @return string The URL for the next step
*/
public function get_next_step_link(): string {
$keys = array_keys( $this->steps );
$step = array_search( $this->current_step, $keys, true );
++$step;

// If next step is payment but there are no active methods, skip to the following step
if ( 'payment' === ( $keys[ $step ] ?? '' ) && empty( dokan_withdraw_get_active_methods() ) ) {
osmansufy marked this conversation as resolved.
Show resolved Hide resolved
++$step;
}
$next_step = $keys[ $step ] ?? '';
$next_step = apply_filters( 'dokan_seller_wizard_next_step', $next_step, $this->current_step, $this->steps );
return add_query_arg(
osmansufy marked this conversation as resolved.
Show resolved Hide resolved
[
'step' => $next_step,
'_admin_sw_nonce' => wp_create_nonce( 'dokan_admin_setup_wizard_nonce' ),
]
);
}

/**
* Sets up the wizard steps
*
* Defines the steps for the setup wizard, conditionally including
* the payment step only if active withdrawal methods exist
*
* @since 2.9.27
*
* @return void
*/
protected function set_steps() {
$steps = [
'introduction' => [
'name' => __( 'Introduction', 'dokan-lite' ),
'view' => [ $this, 'dokan_setup_introduction' ],
'handler' => '',
],
'store' => [
'name' => __( 'Store', 'dokan-lite' ),
'view' => [ $this, 'dokan_setup_store' ],
'handler' => [ $this, 'dokan_setup_store_save' ],
],
];

// Only add payment step if there are active withdrawal methods
$active_methods = dokan_withdraw_get_active_methods();
if ( ! empty( $active_methods ) ) {
$steps['payment'] = [
'name' => __( 'Payment', 'dokan-lite' ),
'view' => [ $this, 'dokan_setup_payment' ],
'handler' => [ $this, 'dokan_setup_payment_save' ],
];
}

$steps['next_steps'] = [
'name' => __( 'Ready!', 'dokan-lite' ),
'view' => [ $this, 'dokan_setup_ready' ],
'handler' => '',
];

/**
* Filter the seller wizard steps
*
* @since 2.9.27
*
* @param array $steps Array of wizard steps
*/
$this->steps = apply_filters( 'dokan_seller_wizard_steps', $steps );
$this->current_step = $this->get_first_step();
}

protected function get_first_step() {
return current( array_keys( $this->steps ) );
}
}
Loading