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 #2467

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

mrabbani
Copy link
Member

@mrabbani mrabbani commented Dec 6, 2024

All Submissions:

  • My code follow the WordPress' coding standards
  • My code satisfies feature requirements
  • My code is tested
  • My code passes the PHPCS tests
  • My code has proper inline documentation
  • I've included related pull request(s) (optional)
  • I've included developer documentation (optional)
  • I've added proper labels to this pull request

Changes proposed in this Pull Request:

Related Pull Request(s)

  • Full PR Link

Closes

  • Closes #

How to test the changes in this Pull Request:

  • Steps or issue link

Changelog entry

Title

Detailed Description of the pull request. What was previous behaviour
and what will be changed in this PR.

Before Changes

Describe the issue before changes with screenshots(s).

After Changes

Describe the issue after changes with screenshot(s).

Feature Video (optional)

Link of detailed video if this PR is for a feature.

PR Self Review Checklist:

  • Code is not following code style guidelines
  • Bad naming: make sure you would understand your code if you read it a few months from now.
  • KISS: Keep it simple, Sweetie (not stupid!).
  • DRY: Don't Repeat Yourself.
  • Code that is not readable: too many nested 'if's are a bad sign.
  • Performance issues
  • Complicated constructions that need refactoring or comments: code should almost always be self-explanatory.
  • Grammar errors.

FOR PR REVIEWER ONLY:

As a reviewer, your feedback should be focused on the idea, not the person. Seek to understand, be respectful, and focus on constructive dialog.

As a contributor, your responsibility is to learn from suggestions and iterate your pull request should it be needed based on feedback. Seek to collaborate and produce the best possible contribution to the greater whole.

  • Correct — Does the change do what it’s supposed to? ie: code 100% fulfilling the requirements?
  • Secure — Would a nefarious party find some way to exploit this change? ie: everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities?
  • Readable — Will your future self be able to understand this change months down the road?
  • Elegant — Does the change fit aesthetically within the overall style and architecture?

Summary by CodeRabbit

  • New Features

    • Introduced a new method to manage the URL for the next step in the setup wizard.
    • Added logic to conditionally include the payment step based on active withdrawal methods.
  • Bug Fixes

    • Improved handling of the current step in the setup wizard to ensure correct views are rendered.
  • Refactor

    • Renamed variable for clarity and consistency across the setup wizard classes.
    • Streamlined the setup wizard's flow and enhanced readability.
    • Updated request data handling for improved error management.

Copy link
Contributor

coderabbitai bot commented Dec 6, 2024

Walkthrough

The pull request introduces changes to the SetupWizard and related classes, primarily focusing on renaming the protected variable $step to $current_step. This alteration is reflected throughout the codebase, ensuring consistent usage of the new variable name. Additionally, methods have been updated to accommodate this change, including the introduction of a new method set_steps() in the SetupWizard class to manage step initialization. The overall logic and functionality of the setup wizard process remain intact.

Changes

File Path Change Summary
includes/Admin/SetupWizard.php Renamed variable $step to $current_step, updated type declaration to protected string, and modified several methods to use the new variable consistently.
includes/Admin/SetupWizardNoWC.php Updated setup_wizard_content method to reference $current_step instead of $step.
includes/Vendor/SetupWizard.php Renamed property $step to $current_step, added methods get_next_step_link() and set_steps(), and updated logic in setup_wizard() to utilize the new variable.

Possibly related PRs

  • [Fix] Deprecated Noticed on Admin Setup Wizard #2414: This PR modifies the SetupWizard.php file, which is directly related to the main PR's changes in the same file, focusing on the setup wizard's functionality.
  • Fix admin fixed commission setting validation #2437: This PR also involves changes in SetupWizard.php, specifically enhancing the validation logic for commission settings, which is relevant to the overall setup process discussed in the main PR.
  • Fix commission upgrader #2463: This PR addresses the commission upgrader functionality, which is related to the setup process and commission handling in the SetupWizard class, connecting it to the changes made in the main PR.

Suggested labels

QA approved, :+1: Dev Review Done, Upcoming Release

Suggested reviewers

  • shohag121

🐇 In the wizard's flow, we hop and play,
With steps renamed, we clear the way.
Current steps lead us, bright and true,
In the setup dance, we start anew!
So let’s rejoice, with a joyful cheer,
For changes made, the path is clear! 🐰✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@mrabbani mrabbani mentioned this pull request Dec 6, 2024
12 tasks
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (1)
includes/Vendor/SetupWizard.php (1)

80-86: Sanitize the 'step' Parameter Before Comparison

Consider sanitizing the $_GET['step'] parameter using sanitize_key() before comparing it to 'payment' to ensure consistent data handling and enhance security.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 490a380 and e35a0c3.

📒 Files selected for processing (3)
  • includes/Admin/SetupWizard.php (7 hunks)
  • includes/Admin/SetupWizardNoWC.php (1 hunks)
  • includes/Vendor/SetupWizard.php (6 hunks)
🧰 Additional context used
🪛 GitHub Check: Run PHPCS inspection
includes/Vendor/SetupWizard.php

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

🔇 Additional comments (1)
includes/Admin/SetupWizardNoWC.php (1)

76-82: Maintain Consistent Variable Usage

The update from $this->step to $this->current_step ensures consistency across the setup wizard classes. The changes correctly reference the current step when checking for a view and rendering it.

/** @var string Currenct Step */
protected $step = '';
/** @var string Current Step */
protected string $current_step = '';
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

Confirm PHP Version Compatibility for Typed Properties

The use of typed properties (protected string $current_step) requires PHP 7.4 or higher. Please ensure that the project's minimum PHP version is set to 7.4 or higher to maintain compatibility with this feature.

}
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'] ) ) ) ) {
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

Clarify Boolean Expression with Parentheses

The condition on line 509 mixes && and || operators without explicit parentheses, which can cause confusion due to operator precedence rules. Adding parentheses will clarify the logic and ensure the condition evaluates as intended.

Apply this diff to fix the issue:

-            } 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.

Comment on lines +667 to +683
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' ),
]
);
}
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

Prevent Potential Undefined Index in Step Navigation

In the get_next_step_link() method, incrementing $step without bounds checking may lead to an undefined index if $step exceeds the array length of $keys. Please ensure that $step remains within the valid range to prevent errors.

Apply this diff to fix the issue:

             $keys = array_keys( $this->steps );
             $step = array_search( $this->current_step, $keys, true );
             ++$step;

+            if ( ! isset( $keys[ $step ] ) ) {
+                $next_step = '';
+            } else {
+                // 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 ] ?? '';
+            }
📝 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
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' ),
]
);
}
public function get_next_step_link(): string {
$keys = array_keys( $this->steps );
$step = array_search( $this->current_step, $keys, true );
++$step;
if ( ! isset( $keys[ $step ] ) ) {
$next_step = '';
} else {
// 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' ),
]
);
}

@@ -12,7 +12,7 @@
class SetupWizard {

/** @var string Currenct Step */
protected $step = '';
protected string $current_step = '';
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

Confirm PHP Version Compatibility for Typed Properties

The use of typed properties (protected string $current_step) requires PHP 7.4 or higher. Please ensure that the project's minimum PHP version is set to 7.4 or higher to maintain compatibility.

@@ -292,7 +292,7 @@

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 ],
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

Ensure Array Index Exists When Navigating Steps

In the get_next_step_link() method, accessing $keys[ array_search( $this->current_step, array_keys( $this->steps ), true ) + 1 ] may result in an undefined index if the current step is the last in the array. Please add a check to confirm that the next index exists before accessing it.

Apply this diff to fix the issue:

             $keys = array_keys( $this->steps );
             $current_index = array_search( $this->current_step, $keys, true );
+            if ( false !== $current_index && isset( $keys[ $current_index + 1 ] ) ) {
             $next_step = $keys[ $current_index + 1 ];
+            } else {
+                $next_step = '';
+            }
             return add_query_arg(
                 [
                     'step' => $next_step,
                     '_admin_sw_nonce' => wp_create_nonce( 'dokan_admin_setup_wizard_nonce' ),
                 ]
             );

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (4)
includes/Vendor/SetupWizard.php (4)

Line range hint 496-521: Simplify state validation logic

The state validation logic can be simplified for better readability and maintainability.

Consider this refactoring:

-        $country = $dokan_settings['address']['country'] ?? '';
-        $state = $dokan_settings['address']['state'] ?? '';
-        $country_has_states = isset( $states[ $country ] ) && count( $states[ $country ] ) > 0;
-        $state_is_empty = empty( $state );
-        } elseif ( ( $country_has_states && $state_is_empty ) || ( ! $country_has_states && $state_is_empty ) ) {
+        } elseif ( empty( $dokan_settings['address']['state'] ) ) {
             $is_valid_form = false;
             $_POST['error_address[state]'] = 'error';
         }

The simplified version achieves the same validation while being more concise.

🧰 Tools
🪛 GitHub Check: Run PHPCS inspection

[failure] 521-521:
Functions must not contain multiple empty lines in a row; found 2 empty lines


521-521: Fix code style: Remove extra empty line

There are multiple empty lines that should be reduced to a single empty line.

-

-
+
🧰 Tools
🪛 GitHub Check: Run PHPCS inspection

[failure] 521-521:
Functions must not contain multiple empty lines in a row; found 2 empty lines


602-606: Improve code formatting for array_filter callback

The indentation in the array_filter callback uses tabs instead of spaces.

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

80-85: Consider moving payment step check to a separate method

The payment step validation logic is duplicated between the setup_wizard() method and get_next_step_link(). Consider extracting this into a separate method.

+    /**
+     * Checks if payment step should be skipped
+     *
+     * @return bool True if payment step should be skipped
+     */
+    protected function should_skip_payment_step(): bool {
+        return empty( dokan_withdraw_get_active_methods() );
+    }
+
     public function setup_wizard() {
         // ...
-        if ( isset( $_GET['step'] ) && 'payment' === $_GET['step'] ) {
-            $active_methods = dokan_withdraw_get_active_methods();
-            if ( empty( $active_methods ) ) {
+        if ( isset( $_GET['step'] ) && 'payment' === $_GET['step'] && $this->should_skip_payment_step() ) {
                 wp_safe_redirect( esc_url_raw( $this->get_next_step_link() ) );
                 exit;
-            }
         }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e35a0c3 and ea8cc75.

📒 Files selected for processing (1)
  • includes/Vendor/SetupWizard.php (17 hunks)
🧰 Additional context used
🪛 GitHub Check: Run PHPCS inspection
includes/Vendor/SetupWizard.php

[failure] 521-521:
Functions must not contain multiple empty lines in a row; found 2 empty lines

🔇 Additional comments (2)
includes/Vendor/SetupWizard.php (2)

12-13: LGTM: Property type declaration enhances code safety

The addition of the type declaration string for $current_step improves type safety and code clarity.


666-743: Well-structured implementation of wizard navigation

The new methods get_next_step_link() and set_steps() provide a robust implementation for wizard navigation:

  1. Proper handling of payment step skipping when no withdrawal methods are active
  2. Clean separation of step configuration
  3. Good use of filters for extensibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants