Account sign-up fatals when the Optimole API returns an empty or non-JSON response

Offen Anfängerfreundlich
#1,200 3 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@girishpanchal30 arbeitet bereits daran.

Seit 29.10.2026.

  • #1195 von @pirate-bot — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
85/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
php
Bereich
api

Rechercherichtung

Lies inc/rest.php etwa in den Zeilen 467-482, wo register_service() false wie ein WP_Error behandelt, und vergleiche es mit den abgesicherten Gegenstücken connect() und select_application() etwa in den Zeilen 377-390. Prüfe inc/api.php in den Zeilen 180-191 und 217-228, um die Pfade zu bestätigen, in denen request() false zurückgibt. Reproduziere das Problem mit dem WP-CLI-Snippet oder dem gestubbten register_service()-Aufruf und mache dann die Aufrufe der Fehlermethoden von is_wp_error() abhängig. Für false gib die einfache "Error creating account."-Antwort zurück. Fertig ist es, wenn der leere Body, das HTML-502, der Fall ohne code und der Fall mit Nicht-200 ohne error alle code: error zurückgeben, während der WP_Error-Zweig weiterhin wie bisher funktioniert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug
Description

When an admin creates an Optimole account from the plugin dashboard (POST /optml/v1/register_service), Optml_Api::request() returns false if the API answers with an empty body, a non-JSON body (for example a proxy's HTML 502 page), JSON without a code key, or a non-200 code without an error key.

register_service() sends false into the same branch as WP_Error and calls $user->get_error_code() on it. This throws Error: Call to a member function get_error_code() on false. WordPress then returns a 500 "There has been a critical error on this website." and the dashboard shows its generic REST error instead of "Error creating account.".

Expected: when the API returns false, the endpoint should return the normal code: error response ("Error creating account."), as it did up to 3.11.3 and as connect() and select_application() already do.

Step-by-step reproduction instructions
  1. On a test site with Optimole 4.2.17 active and not connected, run this command. It fakes an empty 502 reply from the account API through pre_http_request, so no real request is sent. Replace 1 with an administrator's user ID.
    wp eval '
    add_filter( "pre_http_request", function ( $pre, $args, $url ) {
        return false !== strpos( $url, "complete_register_remote" )
            ? [ "headers" => [], "body" => "", "response" => [ "code" => 502, "message" => "Bad Gateway" ], "cookies" => [], "filename" => null ]
            : $pre;
    }, 10, 3 );
    wp_set_current_user( 1 );
    $r = new WP_REST_Request( "POST", "/optml/v1/register_service" );
    $r->set_param( "email", "[email protected]" );
    var_dump( rest_do_request( $r )->get_data() );
    '
    
  2. Result: PHP Fatal error: Uncaught Error: Call to a member function get_error_code() on false in .../inc/rest.php:468.
  3. Expected: an array with code => error and message => "Error creating account.".
  4. Same outcome without WP-CLI: I loaded the real inc/api.php and inc/rest.php from v4.2.17 with WordPress functions stubbed and called register_service() with canned API bodies (PHP 8.4.19):
    == empty body ==
    UNCAUGHT Error: Call to a member function get_error_code() on false at rest.php:468
    == HTML 502 page from a proxy ==
    UNCAUGHT Error: Call to a member function get_error_code() on false at rest.php:468
    == JSON without code ==
    UNCAUGHT Error: Call to a member function get_error_code() on false at rest.php:468
    == JSON code 500, no error key ==
    UNCAUGHT Error: Call to a member function get_error_code() on false at rest.php:468
    == control: JSON code 400 with error ==
    returned WP_REST_Response {"data":null,"message":"Error creating account. Some API error","code":"error"}
    
Screenshots, screen recording, code snippet or Help Scout ticket

false takes the WP_Error branch and is used as an object:
https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/rest.php#L467-L468

if ( $user === false || is_wp_error( $user ) ) {
    if ( $user->get_error_code() === 'domain_not_accessible' ) {

The same $user->get_error_message() call is repeated at https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/rest.php#L482.

The places where request() returns false:
https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/api.php#L180-L191 and https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/api.php#L217-L228

if ( empty( $response ) ) { return false; }
$response = json_decode( $response, true );
...
if ( ! isset( $response['code'] ) ) { return false; }

The sibling handlers already guard the error-object calls with is_wp_error(): https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/rest.php#L377-L390

Core does not catch the error (call_user_func( $handler['callback'], $request ) in WP_REST_Server::respond_to_request(), wp-includes/rest-api/class-wp-rest-server.php:1293 in WordPress 7.1.3). The fatal handler answers with HTTP 500 (wp-includes/class-wp-fatal-error-handler.php:212).

A likely fix is to only call get_error_code() / get_error_message() when is_wp_error( $user ), and return the plain "Error creating account." response for false.

Environment info
  • Optimole 4.2.17 (the code is the same on master at 63f3f7c)
  • Any WordPress version the plugin supports (5.5 and later; core lines cited from 7.1.3)
  • PHP 7.4 and later. Calling a method on false throws Error on every PHP 7 and 8 version. Reproduced on PHP 8.4.19.
Is the issue you are reporting a regression

Yes. Up to 3.11.3, register_service() returned "Error creating account." for false without calling any method on it (https://github.com/Codeinwp/optimole-wp/blob/v3.11.3/inc/rest.php#L357-L366). The $user->get_error_code() call first ships in 3.12.0.

Found while writing the WordPress compatibility checks for Optimole.

Vorherrschende Sprache
PHP
Sterne
74
Forks
14
Ø Merge
1 T. 23 Std.
Gemergte PRs (30 T.)
31

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus Codeinwp/optimole-wp

Alle Issues in Codeinwp/optimole-wp

Ähnliche Issues

Weitere Issues zu PHP

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.