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

Fix/setup wizard enhancements #3454 #2466

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'] );
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
138 changes: 102 additions & 36 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

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

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,25 @@
$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' => '',
],
];
// Setup wizard steps
$this->set_steps();

$this->steps = apply_filters( 'dokan_seller_wizard_steps', $steps );
$this->step = current( array_keys( $this->steps ) );
// If payment step is accessed but no active methods exist, redirect to next step
if ( isset( $_GET['step'] ) && 'payment' === $_GET['step'] ) {
$active_methods = dokan_withdraw_get_active_methods();
if ( empty( $active_methods ) ) {
wp_safe_redirect( esc_url_raw( $this->get_next_step_link() ) );
exit;
}
}

// 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'] ) ) ?? current( array_keys( $this->steps ) );
}

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

$this->enqueue_scripts();
Expand Down Expand Up @@ -166,7 +152,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 +165,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 168 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>'

Check warning on line 168 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>'

Check warning on line 168 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 Down Expand Up @@ -230,7 +216,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 219 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

Check failure on line 219 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 +233,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 236 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

Check failure on line 236 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 +251,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 254 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

Check failure on line 254 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 +268,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 271 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

Check failure on line 271 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 +287,7 @@
</select>
<span class="error-container">
<?php
if ( ! empty( $_POST['error_address[country]'] ) ) {

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

Check failure on line 290 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 +305,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 308 in includes/Vendor/SetupWizard.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Processing form data without nonce verification.

Check failure on line 308 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 @@ -520,12 +506,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'] ) ) ) ) {
} elseif ( ( 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'] ) ) ) ) {

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Mixing different binary boolean operators within an expression without using parentheses to clarify precedence is not allowed.

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Mixing different binary boolean operators within an expression without using parentheses to clarify precedence is not allowed.

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

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Mixing different binary boolean operators within an expression without using parentheses to clarify precedence is not allowed.
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add parentheses to clarify operator precedence

The condition on line 509 mixes && and || operators without parentheses, which can lead to confusion due to operator precedence rules. It's recommended to add parentheses to make the logic explicit and maintainable.

Apply this diff to clarify the condition:

- } elseif ( ( 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'] ) ) ) ) {
+ } elseif (
+     (
+         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'] )
+     )
+ ) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} elseif ( ( 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'] ) ) ) ) {
} elseif (
(
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'] )
)
) {
🧰 Tools
🪛 GitHub Check: Run PHPCS inspection

[failure] 509-509:
Mixing different binary boolean operators within an expression without using parentheses to clarify precedence is not allowed.

$is_valid_form = false;
$_POST['error_address[state]'] = 'error';
}
}

if ( ! $is_valid_form ) {
Expand Down Expand Up @@ -607,12 +590,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 +653,83 @@
</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() ) ) {
++$step;
}
$next_step = $keys[ $step ] ?? '';
return add_query_arg(
[
'step' => apply_filters( 'dokan_seller_wizard_next_step', $next_step, $this->current_step, $this->steps ),
'_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 = current( array_keys( $this->steps ) );
}
}
Loading