Skip to content

Commit

Permalink
[refactor] [improve] Code
Browse files Browse the repository at this point in the history
  • Loading branch information
osmansufy committed Nov 15, 2024
1 parent 2660b74 commit d67d6f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
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
29 changes: 13 additions & 16 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 Down Expand Up @@ -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();
Expand Down Expand Up @@ -152,7 +152,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( site_url() ); ?>"><?php esc_attr_e( 'Return to the Marketplace', 'dokan-lite' ); ?></a>
<?php endif; ?>
</body>
Expand Down Expand Up @@ -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' ),
]
);
Expand Down Expand Up @@ -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 ) );
}
}

0 comments on commit d67d6f7

Please sign in to comment.