8000 Fix server info base by marcusquinn · Pull Request #461 · mainwp/mainwp-child · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix server info base #461

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 8000 send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

marcusquinn
Copy link
@marcusquinn marcusquinn commented Apr 19, 2025

All Submissions:

Changes proposed in this Pull Request:

This PR fixes code review issues identified by Codacy in PR #456, specifically addressing the static access to the MainWP_Child_Server_Information_Base class. Instead of using static method calls to get_wordpress_version(), the code now uses the global $wp_version variable directly, which is the standard WordPress approach.

Files modified:

  • class/class-mainwp-child-plugins-check.php
  • class/class-mainwp-child-updates.php
  • class/class-mainwp-child-vulnerability-checker.php
  • class/class-mainwp-child-actions.php (two instances)

These changes maintain the same functionality while following better coding practices and WordPress standards.

How to test the changes in this Pull Request:

  1. Apply the changes to PR Fixed: wp version issue for some host #456
  2. Run Codacy code review to verify the static access issues are resolved
  3. Test the affected functionality to ensure it works as expected (WordPress version detection in various contexts)

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable? (N/A - code style changes only)
  • Have you successfully run tests with your changes locally?

Changelog entry

Fix static access to MainWP_Child_Server_Information_Base class by using global $wp_version variable directly instead of static method calls, improving code quality and adherence to WordPress standards.

Summary by CodeRabbit

  • Refactor
    • Updated how the WordPress version is retrieved throughout the application, replacing direct use of the global variable with a centralized method for improved consistency.
    • Enhanced reliability of WordPress version detection by using a modern approach where available.
    • Standardized internal handling of version information across backup, update, stats, diagnostics, and vulnerability checking features.
    • Minor change to timestamp handling in action records for improved accuracy.

Copy link
coderabbitai bot commented Apr 19, 2025

Walkthrough

The changes systematically refactor how the WordPress version is retrieved across multiple classes. The global $wp_version variable is replaced by a centralized static method, MainWP_Child_Server_Information_Base::get_wordpress_version(), in most locations. This method is made public and enhanced to use \wp_get_wp_version() if available, falling back to the global variable otherwise. Additionally, some methods now use local variables for the WordPress version, and in one case, the timestamp function is updated from a helper to PHP's native time(). No public method signatures are changed, and no control flow or error handling is altered.

Changes

File(s) Change Summary
class/class-mainwp-child-server-information-base.php Changed get_wordpress_version() from protected to public; now checks for \wp_get_wp_version() before falling back to $wp_version.
class/class-mainwp-child-stats.php
class/class-mainwp-child-back-up-buddy.php
class/class-mainwp-child-updraft-plus-backups.php
class/class-mainwp-child-wordfence.php
class/class-mainwp-clone.php
Replaced usage of global $wp_version with MainWP_Child_Server_Information_Base::get_wordpress_version() in relevant methods.
class/class-mainwp-child-actions.php Adjusted global variable declarations for $wp_version and $pagenow; replaced MainWP_Helper::get_timestamp() with time() for action creation timestamps.
class/class-mainwp-child-plugins-check.php
class/class-mainwp-child-updates.php
class/class-mainwp-child-vulnerability-checker.php
Standardized usage of $wp_version by assigning it to a local variable $wp_ver before use, instead of referencing the global directly.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant ServerInfoBase as MainWP_Child_Server_Information_Base
    participant WordPressCore

    Caller->>ServerInfoBase: get_wordpress_version()
    alt \wp_get_wp_version() exists
        ServerInfoBase->>WordPressCore: \wp_get_wp_version()
        WordPressCore-->>ServerInfoBase: WordPress version
        ServerInfoBase-->>Caller: WordPress version
    else
        ServerInfoBase->>WordPressCore: access $wp_version global
        WordPressCore-->>ServerInfoBase: WordPress version
        ServerInfoBase-->>Caller: WordPress version
    end
Loading
sequenceDiagram
    participant MainWP_Class
    participant ServerInfoBase as MainWP_Child_Server_Information_Base

    MainWP_Class->>ServerInfoBase: get_wordpress_version()
    ServerInfoBase-->>MainWP_Class: WordPress version
    MainWP_Class->>MainWP_Class: Use version in logic (backup, diagnostics, stats, etc.)
Loading

Possibly related PRs

  • Mainwp child dev #455: Also modifies the save_actions method in class-mainwp-child-actions.php focusing on user login and system user detection logic; related by affecting the same method but with different internal changes.

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

Copy link
@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

🧹 Nitpick comments (2)
class/class-mainwp-child-back-up-buddy.php (1)

3047-3051: 🔧 Qualify class reference for clarity
You’re correctly replacing direct global access with MainWP_Child_Server_Information_Base::get_wordpress_version(). To avoid any ambiguity in namespace resolution, consider one of the following optional refinements:

  1. Prepend a leading backslash:
    $wp_ver = \MainWP\Child\MainWP_Child_Server_Information_Base::get_wordpress_version();
  2. Or add at the top of the file:
    use MainWP\Child\MainWP_Child_Server_Information_Base;
    and then keep your existing call.
class/class-mainwp-child-updraft-plus-backups.php (1)

2252-2252: Consider removing the possibly redundant version.php include.

Since you're now using MainWP_Child_Server_Information_Base::get_wordpress_version() instead of the global $wp_version variable, the include of version.php might be redundant unless it's needed elsewhere in the method.

-include_once ABSPATH . WPINC . '/version.php'; // NOSONAR - WP compatible.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 69c082b and b54e79a.

📒 Files selected for processing (10)
  • class/class-mainwp-child-actions.php (3 hunks)
  • class/class-mainwp-child-back-up-buddy.php (2 hunks)
  • class/class-mainwp-child-plugins-check.php (1 hunks)
  • class/class-mainwp-child-server-information-base.php (2 hunks)
  • class/class-mainwp-child-stats.php (3 hunks)
  • class/class-mainwp-child-updates.php (2 hunks)
  • class/class-mainwp-child-updraft-plus-backups.php (2 hunks)
  • class/class-mainwp-child-vulnerability-checker.php (2 hunks)
  • class/class-mainwp-child-wordfence.php (1 hunks)
  • class/class-mainwp-clone.php (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
class/class-mainwp-child-wordfence.php (1)
class/class-mainwp-child-server-information-base.php (1)
  • get_wordpress_version (300-314)
class/class-mainwp-child-stats.php (2)
class/class-mainwp-child-server-information-base.php (2)
  • MainWP_Child_Server_Information_Base (19-795)
  • get_wordpress_version (300-314)
class/class-mainwp-helper.php (3)
  • MainWP_Helper (17-1223)
  • is_wp_engine (921-923)
  • get_wp_host (946-949)
class/class-mainwp-clone.php (1)
class/class-mainwp-child-server-information-base.php (1)
  • get_wordpress_version (300-314)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (24)
class/class-mainwp-child-server-information-base.php (1)

300-314: Good refactoring of the WordPress version retrieval method.

The change from protected static to public static access modifier for get_wordpress_version() and the added fallback mechanism improves the architecture by:

  1. Centralizing WordPress version retrieval in one publicly accessible method
  2. Using modern WordPress API function \wp_get_wp_version() when available
  3. Maintaining backward compatibility with the global $wp_version variable

This change aligns with the PR objectives to fix static access issues and improve adherence to WordPress coding standards.

class/class-mainwp-child-plugins-check.php (2)

349-352: Correctly replaced static method call with direct global variable access.

Using a local variable $wp_ver assigned from the global $wp_version is a good practice. This:

  1. Ensures one-time access to the global variable
  2. Maintains code readability with a clear local reference
  3. Aligns with the PR objectives to fix static access to MainWP_Child_Server_Information_Base class

This change is consistent with the stated goal of using the global $wp_version variable directly.


356-356: Correctly updated user-agent string to use local variable.

The user-agent string now properly uses the local $wp_ver variable, maintaining the functionality while improving the code structure.

class/class-mainwp-child-back-up-buddy.php (1)

1516-1518: ✅ Centralize WordPress version retrieval
Switching to MainWP_Child_Server_Information_Base::get_wordpress_version() instead of using the global ensures consistency and leverages the new fallback to \wp_get_wp_version(). Confirm that MainWP_Child_Server_Information_Base is autoloaded or pre‑loaded in the MainWP\Child namespace so this call never fails.

class/class-mainwp-child-stats.php (5)

107-108: Improved WordPress version retrieval method.

The code now uses MainWP_Child_Server_Information_Base::get_wordpress_version() instead of directly accessing the global $wp_version variable, which is a better practice as it centralizes version retrieval logic.


588-590: Good refactoring to use centralized version retrieval.

Using the static method for getting the WordPress version makes the code more maintainable and consistent with the project's approach to accessing the WordPress version.


595-596: Consistent use of the version retrieval method.

The change ensures consistent use of MainWP_Child_Server_Information_Base::get_wordpress_version() throughout the code.


634-635: Good practice: storing result in a local variable.

Creating a local variable $wp_ver to store the WordPress version is a good practice as it:

  1. Avoids multiple calls to the static method
  2. Makes the code more readable
  3. Slightly improves performance by reducing function calls

641-641: Proper usage of the local variable in condition.

The version comparison now properly uses the previously stored $wp_ver variable instead of accessing the global, maintaining consistency with the refactoring approach.

class/class-mainwp-child-updates.php (3)

1218-1220: Improved version handling with local variable.

The code now properly uses a local variable $wp_ver assigned from the global $wp_version, which improves code maintainability and readability. This approach:

  1. Makes it clear which variable is being used in subsequent code
  2. Follows the pattern of storing global values in local variables for cleaner code

1226-1227: Consistent use of the local variable.

The code now correctly uses the $wp_ver local variable for version comparison and for storing the old version reference. This maintains consistency with the refactoring approach.


1253-1254: Appropriate use of local variable in fallback code path.

The code correctly uses the $wp_ver variable in this alternative code path as well, ensuring consistent behavior throughout all execution branches.

class/class-mainwp-child-wordfence.php (1)

3340-3341: Good improvement replacing global variable with centralized method.

The change from using global $wp_version to MainWP_Child_Server_Information_Base::get_wordpress_version() improves code quality by centralizing version retrieval. This approach adds an abstraction layer that handles version retrieval consistently across the codebase and adds forward compatibility by preferring \wp_get_wp_version() when available.

class/class-mainwp-clone.php (2)

345-348: Good refactoring to use centralized version retrieval.

The code now uses MainWP_Child_Server_Information_Base::get_wordpress_version() instead of accessing the global $wp_version directly. This improves code maintainability by centralizing WordPress version retrieval logic and follows the PR objective to properly access the WordPress version.


467-467: Consistent version retrieval implementation.

Using the MainWP_Child_Server_Information_Base::get_wordpress_version() method here maintains consistency with the version retrieval approach used elsewhere in the file.

class/class-mainwp-child-vulnerability-checker.php (4)

311-314: Good practice: using a local variable for the global WordPress version.

Using a local variable $wp_ver instead of directly referencing $wp_version throughout the code is a good practice. It prevents accidental modification of the global variable and makes the code more maintainable.


317-317: Consistent use of local WordPress version variable.

The code correctly uses the local $wp_ver variable in constructing the API URL, maintaining consistency with the variable declaration approach.


326-327: Consistent use of local WordPress version variable.

Properly using the local $wp_ver variable when calling get_vuln_nvd_nist_info() method.


331-332: Consistent use of local WordPress version variable.

Properly using the local $wp_ver variable when calling get_vuln_wordfence_info() method.

class/class-mainwp-child-actions.php (3)

608-618: Improved code readability with separate global declarations.

Separating the global declarations for $pagenow and $wp_version improves code readability and makes it clearer which global variables are being used.


642-646: Consistent approach to global variable declarations.

Similar to the earlier change, this modification separates global declarations for improved readability.


826-826: Simplified timestamp generation.

Using PHP's native time() function directly instead of MainWP_Helper::get_timestamp() simplifies the code and reduces unnecessary method calls, which is a good practice when the helper method doesn't provide additional value.

class/class-mainwp-child-updraft-plus-backups.php (2)

2247-2249: Good refactoring to use the centralized WordPress version method.

The change from using the global $wp_version variable to calling MainWP_Child_Server_Information_Base::get_wordpress_version() is a positive improvement that aligns with the PR objective to address static access issues. This creates better encapsulation and makes the code more maintainable by centralizing version retrieval.


2391-2392: Successfully updated the version comparison logic.

The version comparison code has been correctly updated to use the new $wp_ver variable instead of the global $wp_version, maintaining the same functionality while using the improved version retrieval method.

Copy link

@marcusquinn
Copy link
Author

replaced #456

marcusquinn added a commit to wpallstars/mainwp-child that referenced this pull request Apr 20, 2025
< 8000 div class="pr-1 d-md-inline-block d-none">
- Qualify class reference in class-mainwp-child-back-up-buddy.php
- Use MainWP_Child_Server_Information_Base::get_wordpress_version() instead of global
- Remove redundant version.php include in class-mainwp-child-updraft-plus-backups.php
@marcusquinn
Copy link
Author

Hi there! I wanted to let you know that we've addressed the nitpick comments from CodeRabbitAI in our PR at wpallstars#1.

Specifically, we've:

  1. Qualified the class reference in class-mainwp-child-back-up-buddy.php by using the fully qualified namespace \MainWP\Child\MainWP_Child_Server_Information_Base.

  2. Removed the redundant version.php include in class-mainwp-child-updraft-plus-backups.php since we're now using MainWP_Child_Server_Information_Base::get_wordpress_version() instead of the global $wp_version variable.

These changes improve code clarity and remove unnecessary includes, making the code more maintainable and efficient.

@marcusquinn
Copy link
Author

@coderabbitai review

Copy link
coderabbitai bot commented Apr 20, 2025
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@marcusquinn
Copy link
Author

@coderabbitai full review

Copy link
coderabbitai bot commented Apr 20, 2025
✅ Actions performed

Full review triggered.

Copy link
@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: 2

🔭 Outside diff range comments (1)
class/class-mainwp-child-actions.php (1)

635-646: ⚠️ Potential issue

Undefined $new_version variable
In callback__core_updated_successfully(), you assign $old_version and $auto_updated but never set $new_version before calling save_actions(). This will trigger an undefined variable notice. For example:

global $wp_version;
$old_version = $wp_version;
$new_version = MainWP_Child_Server_Information_Base::get_wordpress_version();

Ensure $new_version is initialized to avoid errors.

🧹 Nitpick comments (5)
class/class-mainwp-child-server-information-base.php (1)

300-314: Improved WordPress version retrieval method

The change to make get_wordpress_version() public and to add a check for the newer \wp_get_wp_version() function is a good improvement. This creates a centralized, consistent method for retrieving the WordPress version across the codebase.

Consider adding a comment explaining why this method was made public and why it checks for \wp_get_wp_version() first, to document the design decision for future maintainers.

    public static function get_wordpress_version() {

        /**
         * The installed version of WordPress.
         *
         * @global string $wp_version The installed version of WordPress.
         */
        global $wp_version;

+        // Use the newer WordPress core function if available, falling back to global variable
        if ( function_exists( '\wp_get_wp_version' ) ) {
            return \wp_get_wp_version();
        }

        return $wp_version;
    }
class/class-mainwp-child-plugins-check.php (1)

347-347: Consider removing unnecessary include

The include statement for version.php is no longer necessary since you're directly using the global variable that's already available in WordPress core.

-        include_once ABSPATH . WPINC . '/version.php'; // NOSONAR - WP compatible.

You can verify this with:

#!/bin/bash
# Check if $wp_version is available without including version.php
rg -A 3 "\$wp_version" wp-settings.php
class/class-mainwp-child-vulnerability-checker.php (2)

292-294: Remove superfluous blank line

The extra blank line after wp_json_encode( $plug_vuln_filter ); is purely stylistic and interrupts the flow. Consider removing it to keep the method body compact and consistent.


317-319: URL‑encode the version parameter

When constructing the NVD API URL, wrap the version in rawurlencode() to prevent malformed URLs if the version string contains unexpected characters:

$url = $this->wpvulndb_nvd_api
     . '?virtualMatchString=cpe:2.3:a:WordPress:WordPress:'
     . rawurlencode( $wp_ver );
class/class-mainwp-child-actions.php (1)

607-617: Remove unused global and unify version lookup
In callback_automatic_updates_complete(), the $pagenow global is declared but never used—this can be removed to clean up the code. Also, consider using MainWP_Child_Server_Information_Base::get_wordpress_version() instead of raw $wp_version for consistency across the codebase.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 69c082b and 93f9160.

📒 Files selected for processing (10)
  • class/class-mainwp-child-actions.php (3 hunks)
  • class/class-mainwp-child-back-up-buddy.php (2 hunks)
  • class/class-mainwp-child-plugins-check.php (1 hunks)
  • class/class-mainwp-child-server-information-base.php (2 hunks)
  • class/class-mainwp-child-stats.php (3 hunks)
  • class/class-mainwp-child-updates.php (2 hunks)
  • class/class-mainwp-child-updraft-plus-backups.php (2 hunks)
  • class/class-mainwp-child-vulnerability-checker.php (3 hunks)
  • class/class-mainwp-child-wordfence.php (1 hunks)
  • class/class-mainwp-clone.php (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
class/class-mainwp-clone.php (1)
class/class-mainwp-child-server-information-base.php (1)
  • get_wordpress_version (300-314)
class/class-mainwp-child-wordfence.php (1)
class/class-mainwp-child-server-information-base.php (1)
  • get_wordpress_version (300-314)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (17)
class/class-mainwp-child-plugins-check.php (2)

349-352: Replaced static method call with direct global variable access

This change aligns with the PR's goal of fixing static access to the MainWP_Child_Server_Information_Base class. The approach of using a local variable for clarity while still accessing the global directly is appropriate. The comment helps explain the design decision.


356-356: Updated user-agent to use local variable

This correctly uses the newly defined local variable instead of directly accessing the global.

class/class-mainwp-child-back-up-buddy.php (2)

1516-1518: Replace direct $wp_version global with static method call.

Using MainWP_Child_Server_Information_Base::get_wordpress_version() here centralizes version retrieval and aligns with WordPress standards. Ensure that this class is loaded (via autoloader or include) before calling it to avoid runtime errors.


3047-3051: Pass WordPress version into Live API token method.

Injecting $wp_ver into ITXAPI_Helper2::get_access_token() removes the old global dependency and uses the same centralized method. Double‑check that the signature of get_access_token() accepts this new parameter so the token exchange continues to work.

class/class-mainwp-child-stats.php (5)

108-108: Good refactoring from global variable to static method

This change reduces direct usage of global variables by using the centralized MainWP_Child_Server_Information_Base::get_wordpress_version() method to retrieve the WordPress version.


589-589: Good refactoring from global variable to static method

Consistent with the earlier change, this replaces direct global variable access with the static method call.


596-596: Consistent 6D40 refactoring to static method

The change maintains consistency with other changes in this file by using the static method for WordPress version retrieval in the site info array.


634-635: Good practice using local variable

Storing the WordPress version in a local variable $wp_ver improves code readability and efficiency by avoiding multiple static method calls.


641-641: Correctly updated version comparison

The version comparison now uses the local $wp_ver variable instead of the global, maintaining consistency with the earlier changes.

class/class-mainwp-child-updates.php (2)

1226-1227: Correctly updated version comparison to use local variable

The version comparison logic is properly updated to use the local $wp_ver variable rather than the global.


1253-1254: Consistent use of local variable

This change consistently uses the local $wp_ver variable throughout the function, which is a good practice.

class/class-mainwp-child-wordfence.php (1)

3340-3346: Centralize WordPress version retrieval

Switching to MainWP_Child_Server_Information_Base::get_wordpress_version() ensures consistent version fetching (using wp_get_wp_version() when available, falling back to the global variable) and aligns with the PR objective of standardizing version access across classes.

class/class-mainwp-clone.php (2)

345-348: Centralize WordPress version retrieval
Good refactor to pull the current WP version via MainWP_Child_Server_Information_Base::get_wordpress_version() instead of relying solely on the global. This centralizes the logic and ensures consistency with the new static method’s fallback to wp_get_wp_version().


467-468: Pass WordPress version in remote requests
Nice addition of the 'wpversion' => MainWP_Child_Server_Information_Base::get_wordpress_version() parameter in the fetch_url() call. This guarantees the child endpoint always knows the correct WP version context.

class/class-mainwp-child-actions.php (1)

826-831: Switch to native time() for timestamps
Replacing the helper call with PHP’s built‑in time() simplifies the code and removes an unnecessary abstraction. This change is clear and effective.

class/class-mainwp-child-updraft-plus-backups.php (2)

2247-2249: Improved code by using a central method to get WordPress version

The code now correctly uses MainWP_Child_Server_Information_Base::get_wordpress_version() instead of the global $wp_version variable, which aligns with the PR objective to fix static access issues identified by Codacy.


2391-2392: Consistent use of WordPress version variable

The code correctly uses the locally stored $wp_ver variable in the version comparison, maintaining consistency with the changes made above.

Comment on lines +1218 to +1220
// Get WordPress version using global variable instead of static method call.
global $wp_version;

$wp_ver = $wp_version;
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify consistency with other files

While this approach of directly using the global $wp_version works, it appears inconsistent with the approach in other files that use MainWP_Child_Server_Information_Base::get_wordpress_version(). Consider whether this inconsistency is intentional based on the specific requirements of this function.


🏁 Script executed:

#!/bin/bash
# Search for instances of both approaches to see which is more common in the codebase

echo "Files using global \$wp_version:"
rg "\$wp_version" --type php

echo -e "\nFiles using MainWP_Child_Server_Information_Base::get_wordpress_version():"
rg "MainWP_Child_Server_Information_Base::get_wordpress_version" --type php

Length of output: 1620


Use the static method for WordPress version for consistency

To align with the rest of the codebase, replace the direct use of the global $wp_version with MainWP_Child_Server_Information_Base::get_wordpress_version().

• File: class/class-mainwp-child-updates.php
Lines: ~1218–1220
Diff:

- // Get WordPress version using global variable instead of static method call.
- global $wp_version;
- $wp_ver       = $wp_version;
+ // Get WordPress version via the shared helper method for consistency.
+ $wp_ver = MainWP_Child_Server_Information_Base::get_wordpress_version();
📝 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
// Get WordPress version using global variable instead of static method call.
global $wp_version;
$wp_ver = $wp_version;
// Get WordPress version via the shared helper method for consistency.
$wp_ver = MainWP_Child_Server_Information_Base::get_wordpress_version();

Comment on lines +311 to +315
$wp_vuln = get_transient( 'mainwp_vulnche_trans_wp_json' );
// Get WordPress version using global variable instead of static method call.
global $wp_version;
$wp_ver = $wp_version;
$number_version = str_replace( '.', '', $wp_ver );
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Use centralized version retrieval method

Instead of pulling in the global $wp_version, call MainWP_Child_Server_Information_Base::get_wordpress_version() here. This aligns with the refactoring in other classes and leverages the built‑in fallback to \wp_get_wp_version().

marcusquinn added a commit to wpallstars/mainwp-child that referenced this pull request Apr 20, 2025
- Fix undefined  variable in class-mainwp-child-actions.php
- Make get_wordpress_version() public and add check for wp_get_wp_version()
- Remove unnecessary include in class-mainwp-child-plugins-check.php
- Remove superfluous blank line in class-mainwp-child-vulnerability-checker.php
- URL-encode the version parameter in class-mainwp-child-vulnerability-checker.php
- Remove unused global and unify version lookup in class-mainwp-child-actions.php
- Use static method for WordPress version in class-mainwp-child-updates.php
- Use centralized version retrieval method in class-mainwp-child-vulnerability-checker.php
@marcusquinn
Copy link
Author

Hi there! I wanted to let you know that we've addressed all the feedback from CodeRabbitAI in our PR at wpallstars#1.

Specifically, we've:

  1. Fixed the undefined $new_version variable in class-mainwp-child-actions.php

  2. Made get_wordpress_version() public and added a check for wp_get_wp_version() with detailed documentation

  3. Removed the unnecessary include in class-mainwp-child-plugins-check.php

  4. Removed the superfluous blank line in class-mainwp-child-vulnerability-checker.php

  5. URL-encoded the version parameter in class-mainwp-child-vulnerability-checker.php

  6. Removed unused global and unified version lookup in class-mainwp-child-actions.php

  7. Used the static method for WordPress version in class-mainwp-child-updates.php

  8. Used the centralized version retrieval method in class-mainwp-child-vulnerability-checker.php

These changes improve code consistency, security, and maintainability across the codebase.

@marcusquinn
Copy link
Author
marcusquinn commented Apr 20, 2025

@coderabbitai full review

Copy link
coderabbitai bot commented Apr 20, 2025
✅ Actions performed

Full review triggered.

Copy link
@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: 1

♻️ Duplicate comments (2)
class/class-mainwp-child-updates.php (1)

1218-1220: 🛠️ Refactor suggestion

Use the static method for WordPress version consistency.

This approach directly uses the global $wp_version which is inconsistent with other files in the codebase that use MainWP_Child_Server_Information_Base::get_wordpress_version(). For better maintainability and consistency, this should be updated.

- // Get WordPress version using global variable instead of static method call.
- global $wp_version;
- $wp_ver       = $wp_version;
+ // Get WordPress version via the centralized helper method.
+ $wp_ver = MainWP_Child_Server_Information_Base::get_wordpress_version();
class/class-mainwp-child-vulnerability-checker.php (1)

311-315: Use centralized version retrieval method

Instead of pulling in the global $wp_version, consider calling MainWP_Child_Server_Information_Base::get_wordpress_version() here. This would align with the refactoring in other classes and leverage the built-in fallback to \wp_get_wp_version().

- // Get WordPress version using global variable instead of static method call.
- global $wp_version;
- $wp_ver         = $wp_version;
+ $wp_ver         = MainWP_Child_Server_Information_Base::get_wordpress_version();
🧹 Nitpick comments (2)
class/class-mainwp-child-wordfence.php (1)

3340-3346: Centralize WordPress version retrieval
Swapping out direct $wp_version access for MainWP_Child_Server_Information_Base::get_wordpress_version() centralizes version logic and aligns with other classes updated in this PR.

Consider removing the require_once ABSPATH . 'wp-includes/version.php' (around line 3312), as WordPress core already loads version.php and get_wordpress_version() gracefully falls back to the global $wp_version.

class/class-mainwp-child-vulnerability-checker.php (1)

318-318: URL-encode the version parameter

The WordPress version is being used directly in a URL. Consider URL-encoding it to ensure any special characters are properly escaped.

- $url = $this->wpvulndb_nvd_api . '?virtualMatchString=cpe:2.3:a:WordPress:WordPress:' . $wp_ver;
+ $url = $this->wpvulndb_nvd_api . '?virtualMatchString=cpe:2.3:a:WordPress:WordPress:' . urlencode($wp_ver);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 69c082b and 93f9160.

📒 Files selected for processing (10)
  • class/class-mainwp-child-actions.php (3 hunks)
  • class/class-mainwp-child-back-up-buddy.php (2 hunks)
  • class/class-mainwp-child-plugins-check.php (1 hunks)
  • class/class-mainwp-child-server-information-base.php (2 hunks)
  • class/class-mainwp-child-stats.php (3 hunks)
  • class/class-mainwp-child-updates.php (2 hunks)
  • class/class-mainwp-child-updraft-plus-backups.php (2 hunks)
  • class/class-mainwp-child-vulnerability-checker.php (3 hunks)
  • class/class-mainwp-child-wordfence.php (1 hunks)
  • class/class-mainwp-clone.php (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
class/class-mainwp-clone.php (1)
class/class-mainwp-child-server-information-base.php (1)
  • get_wordpress_version (300-314)
class/class-mainwp-child-wordfence.php (1)
class/class-mainwp-child-server-information-base.php (1)
  • get_wordpress_version (300-314)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (17)
class/class-mainwp-child-server-information-base.php (1)

300-313: Excellent improvement to centralize WordPress version retrieval.

This change properly centralizes the WordPress version retrieval by:

  1. Making the method public to allow access from other classes
  2. Adding forward compatibility with \wp_get_wp_version() function
  3. Maintaining backward compatibility with the global $wp_version fallback

This standardization will make the codebase more maintainable and follows good software engineering practices.

class/class-mainwp-child-plugins-check.php (2)

349-352: Good use of a local variable for increased readability.

Using a local variable $wp_ver makes the code more explicit about what value is being used in the request options, which improves code readability.


356-356: Good update to use the local variable in the user-agent string.

This change properly uses the newly defined local variable $wp_ver in the user-agent string, maintaining consistent code style.

class/class-mainwp-child-back-up-buddy.php (2)

1516-1518: Use centralized WordPress version retrieval
Replacing the global $wp_version with MainWP_Child_Server_Information_Base::get_wordpress_version() ensures a single source of truth for the WordPress version and aligns with the refactoring across the codebase.


3047-3051: Consistent version lookup in Live setup
Great refactor to fetch the WordPress version via the public static get_wordpress_version() method before calling the external API. This keeps version detection consistent and testable.

class/class-mainwp-child-stats.php (3)

107-108: Good implementation of centralized version retrieval.

Using the static method MainWP_Child_Server_Information_Base::get_wordpress_version() instead of the global variable improves code maintainability and follows the centralization pattern.


589-596: Consistent use of centralized version retrieval.

The change maintains consistency by using MainWP_Child_Server_Information_Base::get_wordpress_version() for both the wpversion property and within the site_info array. This approach centralizes version retrieval, making future updates easier to maintain.


634-641: Improved version comparison approach.

The change now properly retrieves the WordPress version using the centralized method and stores it in a local variable $wp_ver for use in comparisons. This is cleaner and more maintainable than using the global variable directly in comparisons.

class/class-mainwp-child-updraft-plus-backups.php (2)

2247-2250: Good refactoring to use centralized WordPress version retrieval

The code now uses MainWP_Child_Server_Information_Base::get_wordpress_version() instead of directly accessing the global $wp_version variable. This is a better approach that centralizes version information access, improving maintainability.


2391-2392: Properly updated version comparison to use the new variable

The version comparison logic has been correctly updated to use the new $wp_ver variable, maintaining the same functionality while benefiting from the centralized version retrieval method.

class/class-mainwp-clone.php (2)

345-348: Improved WordPress version retrieval

The code now uses the centralized MainWP_Child_Server_Information_Base::get_wordpress_version() method instead of directly accessing the WordPress version from a parameter. This approach is more maintainable and consistent with the refactoring across the codebase.


467-467: Consistent version retrieval pattern applied

The code now consistently uses MainWP_Child_Server_Information_Base::get_wordpress_version() for WordPress version retrieval in the clone backup process, improving maintainability.

class/class-mainwp-child-actions.php (3)

608-618: Improved global variable declaration with clarifying comment

The global declarations have been separated with an explanatory comment, making the code more readable and documenting the reason for using the global variable instead of a static method call.


642-646: Consistent pattern for global variable usage

Similar to the previous change, the global declarations are separated with explanatory comments, maintaining consistency throughout the file.


826-826: Simplified timestamp generation

Replaced the helper method MainWP_Helper::get_timestamp() with PHP's native time() function for simplicity.

class/class-mainwp-child-vulnerability-checker.php (2)

293-293: Improved code formatting

Added a blank line after JSON encoding for better readability.


327-333: Consistent handling of WordPress version

The code now consistently uses the local $wp_ver variable throughout the method, which improves maintainability.

@@ -346,17 +346,14 @@ private function try_get_response_body( $plugin, $second_pass ) { //phpcs:ignore
// Get the WordPress current version to be polite in the API call.
include_once ABSPATH . WPINC . '/version.php'; // NOSONAR - WP compatible.
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Consider if the version.php include is still necessary.

Since you're using the global $wp_version variable directly and not the static method, you still need this include. However, according to the PR comments, a similar include was removed in another file (class-mainwp-child-updraft-plus-backups.php) because it switched to using the static method. For consistency across the codebase, consider whether this file should also use MainWP_Child_Server_Information_Base::get_wordpress_version() instead.


🏁 Script executed:

#!/bin/bash
# Check if other files still include version.php or if they've moved to the static method

# Look for files that include version.php
echo "Files that include version.php:"
rg "include.*version\.php" class/

# Look for files that use the static method
echo "\nFiles that use MainWP_Child_Server_Information_Base::get_wordpress_version():"
rg "MainWP_Child_Server_Information_Base::get_wordpress_version" class/

Length of output: 2151


🏁 Script executed:

#!/bin/bash
# Find the definition of get_wordpress_version in the codebase
echo "Searching for get_wordpress_version() definition:"
rg -n "function get_wordpress_version" -n .

# Also search for the method in the specific class file
echo -e "\nInspecting MainWP_Child_Server_Information_Base class:"
rg -n "class MainWP_Child_Server_Information_Base" -n -A5
rg -n "get_wordpress_version" -n -A5

Length of output: 9432


Refactor class-mainwp-child-plugins-check.php to use the static version helper

We’ve confirmed that:

  • Other classes (Wordfence, Updraft‑Plus, Themes Check) still include version.php even after switching to MainWP_Child_Server_Information_Base::get_wordpress_version().
  • This file currently pulls in version.php and uses the global $wp_version directly.

For consistency, update this file to call the static method and keep the include:

• File: class/class-mainwp-child-plugins-check.php
– Keep the existing
php include_once ABSPATH . WPINC . '/version.php'; // NOSONAR - WP compatible.
– Replace any direct references to $wp_version with:
diff - global $wp_version; - $current_version = $wp_version; + $current_version = MainWP_Child_Server_Information_Base::get_wordpress_version();

@bogdan-mainwp
Copy link
Contributor

Hi @marcusquinn, thanks for the PR. It looks like there are some problems with this PR. It includes rollback changes for tweaks that we introduced in last update for getting the WP version number. For example, $wp_ver = MainWP_Child_Server_Information_Base::get_wordpress_version(); is getting rolled back to global $wp_version; $wp_ver = $wp_version;

Can you please review this Pull Request?

@bogdan-mainwp
Copy link
Contributor

Hi @marcusquinn, just a quick follow-up on this PR.

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.

4 participants
0