Skip to content

Conversation

@sixela
Copy link
Contributor

@sixela sixela commented Sep 19, 2025

When using MainWP REST API I expect it to respect the setting "Verify SSL certificate" (which translates to option "mainwp_sslVerifyCertificate").

However, the wp_remote_post used to check if the REST API is enabled does not follow the settings and will always try to verify the SSL certificate. This is particularly annoying in case one's using a self-signed certificate

Summary by CodeRabbit

  • New Features
    • Added a setting to toggle SSL certificate verification when checking REST API availability, improving compatibility with hosts that have non-standard SSL configurations. Enabled by default.

@coderabbitai
Copy link

coderabbitai bot commented Sep 19, 2025

Walkthrough

Adds SSL certificate verification control to the REST API availability check by introducing an sslverify option in the HTTP args of check_rest_api_enabled(). Request arg formatting adjusted; control flow, endpoints, and parsing remain unchanged.

Changes

Cohort / File(s) Summary
REST API check logic
pages/page-mainwp-rest-api-page.php
Adds 'sslverify' => (bool) get_option('mainwp_sslVerifyCertificate', true) to HTTP args in check_rest_api_enabled(). Retains GET, 45s timeout, headers, cookie handling, endpoint /wp-json, response parsing, and recursive fallback when not logged-in. Minor argument alignment/formatting updates.

Sequence Diagram(s)

sequenceDiagram
  participant Caller as Caller
  participant Plugin as check_rest_api_enabled()
  participant WPHTTP as wp_remote_post
  participant WP as WordPress Options

  Caller->>Plugin: check_rest_api_enabled($site_url, $check_logged_in)
  Plugin->>WP: get_option('mainwp_sslVerifyCertificate', true)
  WP-->>Plugin: bool sslverify
  alt $check_logged_in && cookies exist
    Plugin->>Plugin: Attach cookies to args
  end
  Plugin->>WPHTTP: POST $site_url/wp-json with args {method: GET, timeout:45, headers, sslverify}
  WPHTTP-->>Plugin: Response
  alt Response has data.routes
    Plugin-->>Caller: true
  else Response invalid and !$check_logged_in
    Plugin->>Plugin: Recurse with $check_logged_in = true
    Plugin-->>Caller: result
  else
    Plugin-->>Caller: false
  end

  Note over Plugin: New: sslverify is toggled via WP option
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly communicates the primary change—making check_rest_api_enabled respect the mainwp_sslVerifyCertificate option so SSL verification can be toggled (fixing use with self-signed certificates); it is specific, concise, and directly related to the PR’s described changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link

Please retry analysis of this Pull-Request directly on SonarQube Cloud

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pages/page-mainwp-rest-api-page.php (1)

1236-1236: Use logical AND (&&), not bitwise &.

Bitwise & evaluates both sides and can trigger notices on non-arrays; && short-circuits and is the intended logic.

Apply:

-        if ( is_array( $data ) & isset( $data['routes'] ) && ! empty( $data['routes'] ) ) {
+        if ( is_array( $data ) && isset( $data['routes'] ) && ! empty( $data['routes'] ) ) {
🧹 Nitpick comments (2)
pages/page-mainwp-rest-api-page.php (2)

1219-1225: Good addition; parse the option to a real boolean.

Casting may misread values like "no"/"false" as true. Use FILTER_VALIDATE_BOOLEAN for robustness.

Apply:

-            'sslverify' => (bool) get_option( 'mainwp_sslVerifyCertificate', true ),
+            'sslverify' => filter_var( get_option( 'mainwp_sslVerifyCertificate', true ), FILTER_VALIDATE_BOOLEAN ),

1203-1216: Guard LOGGED_IN_COOKIE usage.

Avoid notices when the cookie is missing by checking existence and initializing the variable.

Apply:

-        if ( $check_logged_in && is_user_logged_in() && defined( 'LOGGED_IN_COOKIE' ) ) {
-            $cookies      = array();
-            $auth_cookies = wp_parse_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' ); // phpcs:ignore -- ok.
+        if ( $check_logged_in && is_user_logged_in() && defined( 'LOGGED_IN_COOKIE' ) ) {
+            $cookies       = array();
+            $auth_cookies  = null;
+            if ( isset( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
+                $auth_cookies = wp_parse_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' ); // phpcs:ignore -- ok.
+            }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 82f2682 and 00f1eeb.

📒 Files selected for processing (1)
  • pages/page-mainwp-rest-api-page.php (1 hunks)

Comment on lines +1219 to 1225
'method' => 'GET',
'timeout' => 45,
'headers' => array(
'content-type' => 'application/json',
),
'sslverify' => (bool) get_option( 'mainwp_sslVerifyCertificate', true ),
);
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Use wp_remote_get + rest_url() and drop the method override.

wp_remote_post() forces POST, so 'method' => 'GET' is ignored. The REST index expects GET; POST can yield rest_no_route. Also no need to send Content-Type for a GET.

Apply:

-        $args = array(
-            'method'    => 'GET',
-            'timeout'   => 45,
-            'headers'   => array(
-                'content-type' => 'application/json',
-            ),
-            'sslverify' => filter_var( get_option( 'mainwp_sslVerifyCertificate', true ), FILTER_VALIDATE_BOOLEAN ),
-        );
+        $args = array(
+            'timeout'   => 45,
+            'sslverify' => filter_var( get_option( 'mainwp_sslVerifyCertificate', true ), FILTER_VALIDATE_BOOLEAN ),
+        );-        $site_url = get_option( 'home' );
-        $response = wp_remote_post( $site_url . '/wp-json', $args );
+        $response = wp_remote_get( rest_url(), $args );
+        if ( is_wp_error( $response ) ) {
+            return false;
+        }

Also applies to: 1231-1233

🤖 Prompt for AI Agents
In pages/page-mainwp-rest-api-page.php around lines 1219-1225 (and also apply
same change for lines 1231-1233), the code uses wp_remote_post with a 'method'
=> 'GET' override and sets a Content-Type header for a GET; wp_remote_post
forces POST so the override is ignored which can trigger rest_no_route. Replace
wp_remote_post with wp_remote_get, remove the explicit 'method' entry and drop
the 'content-type' header (no Content-Type needed for GET), keep timeout and
sslverify options as-is, and update any variable names or call sites if
necessary to reflect using wp_remote_get.

@thanghv thanghv merged commit da40d79 into mainwp:master Sep 23, 2025
6 checks passed
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.

3 participants