From 8ae16214a87d22a73ba95f2cde6796d1aec4b41d Mon Sep 17 00:00:00 2001 From: osman sufy Date: Thu, 14 Nov 2024 16:45:41 +0600 Subject: [PATCH 1/5] [Skip] Payment from setup when no payment method is active --- includes/Vendor/SetupWizard.php | 134 ++++++++++++++++++++++++-------- 1 file changed, 102 insertions(+), 32 deletions(-) diff --git a/includes/Vendor/SetupWizard.php b/includes/Vendor/SetupWizard.php index 8510d5375f..052ad6dba4 100644 --- a/includes/Vendor/SetupWizard.php +++ b/includes/Vendor/SetupWizard.php @@ -73,35 +73,21 @@ public function setup_wizard() { $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(); + + // 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->step = sanitize_key( $_GET['step'] ) ?? current( array_keys( $this->steps ) ); } if ( ! empty( $_POST['save_step'] ) && isset( $this->steps[ $this->step ]['handler'] ) ) { // WPCS: CSRF ok. @@ -520,12 +506,9 @@ public function dokan_setup_store_save() { 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'] ) ) ) ) { $is_valid_form = false; $_POST['error_address[state]'] = 'error'; - } } if ( ! $is_valid_form ) { @@ -607,12 +590,16 @@ public function dokan_setup_payment_save() { '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; } @@ -666,4 +653,87 @@ public function dokan_setup_ready() { steps ); + $step = array_search( $this->step, $keys, true ); + $next_step = $keys[ $step + 1 ]; + + // If next step is payment but there are no active methods, skip to the following step + if ( 'payment' === $next_step ) { + $active_methods = dokan_withdraw_get_active_methods(); + if ( empty( $active_methods ) ) { + $next_step = $keys[ $step + 2 ]; + } + } + + return add_query_arg( + [ + '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->step = current( array_keys( $this->steps ) ); + } } From 87641854bda4bfe7f2324972625f7b3d7fcb81e1 Mon Sep 17 00:00:00 2001 From: osman sufy Date: Thu, 14 Nov 2024 16:58:01 +0600 Subject: [PATCH 2/5] [add] wp_unslash --- includes/Vendor/SetupWizard.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/Vendor/SetupWizard.php b/includes/Vendor/SetupWizard.php index 052ad6dba4..07c90cb8c1 100644 --- a/includes/Vendor/SetupWizard.php +++ b/includes/Vendor/SetupWizard.php @@ -87,7 +87,7 @@ public function setup_wizard() { // 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( $_GET['step'] ) ?? current( array_keys( $this->steps ) ); + $this->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. @@ -664,8 +664,7 @@ public function dokan_setup_ready() { * * @return string The URL for the next step */ - public function get_next_step_link(): string - { + public function get_next_step_link(): string { $keys = array_keys( $this->steps ); $step = array_search( $this->step, $keys, true ); $next_step = $keys[ $step + 1 ]; From 803288fa9542fb0bfc30e2012b4e01ae1858742e Mon Sep 17 00:00:00 2001 From: osman sufy Date: Thu, 14 Nov 2024 16:45:41 +0600 Subject: [PATCH 3/5] [Skip] Payment from setup when no payment method is active --- includes/Vendor/SetupWizard.php | 134 ++++++++++++++++++++++++-------- 1 file changed, 102 insertions(+), 32 deletions(-) diff --git a/includes/Vendor/SetupWizard.php b/includes/Vendor/SetupWizard.php index 8510d5375f..052ad6dba4 100644 --- a/includes/Vendor/SetupWizard.php +++ b/includes/Vendor/SetupWizard.php @@ -73,35 +73,21 @@ public function setup_wizard() { $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(); + + // 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->step = sanitize_key( $_GET['step'] ) ?? current( array_keys( $this->steps ) ); } if ( ! empty( $_POST['save_step'] ) && isset( $this->steps[ $this->step ]['handler'] ) ) { // WPCS: CSRF ok. @@ -520,12 +506,9 @@ public function dokan_setup_store_save() { 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'] ) ) ) ) { $is_valid_form = false; $_POST['error_address[state]'] = 'error'; - } } if ( ! $is_valid_form ) { @@ -607,12 +590,16 @@ public function dokan_setup_payment_save() { '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; } @@ -666,4 +653,87 @@ public function dokan_setup_ready() { steps ); + $step = array_search( $this->step, $keys, true ); + $next_step = $keys[ $step + 1 ]; + + // If next step is payment but there are no active methods, skip to the following step + if ( 'payment' === $next_step ) { + $active_methods = dokan_withdraw_get_active_methods(); + if ( empty( $active_methods ) ) { + $next_step = $keys[ $step + 2 ]; + } + } + + return add_query_arg( + [ + '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->step = current( array_keys( $this->steps ) ); + } } From 2fbfec3d1192c0eebcb7a8c85fb9647b176bbd69 Mon Sep 17 00:00:00 2001 From: osman sufy Date: Thu, 14 Nov 2024 16:58:01 +0600 Subject: [PATCH 4/5] [add] wp_unslash --- includes/Vendor/SetupWizard.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/Vendor/SetupWizard.php b/includes/Vendor/SetupWizard.php index 052ad6dba4..07c90cb8c1 100644 --- a/includes/Vendor/SetupWizard.php +++ b/includes/Vendor/SetupWizard.php @@ -87,7 +87,7 @@ public function setup_wizard() { // 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( $_GET['step'] ) ?? current( array_keys( $this->steps ) ); + $this->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. @@ -664,8 +664,7 @@ public function dokan_setup_ready() { * * @return string The URL for the next step */ - public function get_next_step_link(): string - { + public function get_next_step_link(): string { $keys = array_keys( $this->steps ); $step = array_search( $this->step, $keys, true ); $next_step = $keys[ $step + 1 ]; From d67d6f7d49c703dbef5ae1cc0c066cea0a928f14 Mon Sep 17 00:00:00 2001 From: osman sufy Date: Fri, 15 Nov 2024 17:50:33 +0600 Subject: [PATCH 5/5] [refactor] [improve] Code --- includes/Admin/SetupWizard.php | 22 +++++++++++----------- includes/Admin/SetupWizardNoWC.php | 4 ++-- includes/Vendor/SetupWizard.php | 29 +++++++++++++---------------- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/includes/Admin/SetupWizard.php b/includes/Admin/SetupWizard.php index 0525cc8d85..ab36507aba 100644 --- a/includes/Admin/SetupWizard.php +++ b/includes/Admin/SetupWizard.php @@ -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 = []; @@ -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(); @@ -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(); @@ -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' ), ] ); @@ -328,7 +328,7 @@ public function setup_wizard_header() { */ public function setup_wizard_footer() { ?> - step ) : ?> + current_step ) : ?> @@ -347,9 +347,9 @@ public function setup_wizard_steps() { $step ) : ?>
  • '; - call_user_func( $this->steps[ $this->step ]['view'] ); + call_user_func( $this->steps[ $this->current_step ]['view'] ); echo ''; } diff --git a/includes/Admin/SetupWizardNoWC.php b/includes/Admin/SetupWizardNoWC.php index 3c15210fe9..4872c19f8b 100644 --- a/includes/Admin/SetupWizardNoWC.php +++ b/includes/Admin/SetupWizardNoWC.php @@ -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 '
    '; - call_user_func( $this->steps[ $this->step ]['view'] ); + call_user_func( $this->steps[ $this->current_step ]['view'] ); echo '
    '; } diff --git a/includes/Vendor/SetupWizard.php b/includes/Vendor/SetupWizard.php index 07c90cb8c1..0c7d332f05 100644 --- a/includes/Vendor/SetupWizard.php +++ b/includes/Vendor/SetupWizard.php @@ -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 = []; @@ -87,11 +87,11 @@ public function setup_wizard() { // 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'] ) ) ?? current( array_keys( $this->steps ) ); + $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(); @@ -152,7 +152,7 @@ public function setup_wizard_header() { */ public function setup_wizard_footer() { ?> - step ) : ?> + current_step ) : ?> @@ -666,20 +666,17 @@ public function dokan_setup_ready() { */ public function get_next_step_link(): string { $keys = array_keys( $this->steps ); - $step = array_search( $this->step, $keys, true ); - $next_step = $keys[ $step + 1 ]; + $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' === $next_step ) { - $active_methods = dokan_withdraw_get_active_methods(); - if ( empty( $active_methods ) ) { - $next_step = $keys[ $step + 2 ]; - } + if ( 'payment' === $keys[ $step ] && empty( dokan_withdraw_get_active_methods() ) ) { + ++$step; } - + $next_step = $keys[ $step ] ?? ''; return add_query_arg( [ - 'step' => $next_step, + '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' ), ] ); @@ -733,6 +730,6 @@ protected function set_steps() { * @param array $steps Array of wizard steps */ $this->steps = apply_filters( 'dokan_seller_wizard_steps', $steps ); - $this->step = current( array_keys( $this->steps ) ); + $this->current_step = current( array_keys( $this->steps ) ); } }