Account sign-up fatals when the Optimole API returns an empty or non-JSON response
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
inc/rest.php の467〜482行目付近を読みます。ここでは register_service() が false を WP_Error と同じように扱っています。377〜390行目付近の、ガードされた同類の connect() と select_application() と比較してください。inc/api.php の180〜191行目と217〜228行目を確認し、request() が false を返す経路を特定します。WP-CLI のスニペットまたはスタブ化した register_service() 呼び出しで再現してから、エラーメソッドの呼び出しを is_wp_error() の判定で条件分岐させ、false の場合は素のレスポンス "Error creating account." を返すようにします。空のボディ、HTML の502、code がないケース、200以外で error がないケースがすべて code: error を返し、WP_Error の分岐は従来どおり動作すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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
- 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. Replace1with 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() ); ' - Result:
PHP Fatal error: Uncaught Error: Call to a member function get_error_code() on false in .../inc/rest.php:468. - Expected: an array with
code => errorandmessage => "Error creating account.". - Same outcome without WP-CLI: I loaded the real
inc/api.phpandinc/rest.phpfrom v4.2.17 with WordPress functions stubbed and calledregister_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
masterat 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
falsethrowsErroron 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.
- 主要言語
- PHP
- スター
- 74
- フォーク
- 14
- 平均マージ
- 1日 22時間
- マージ済み PR(30日)
- 29
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Codeinwp/optimole-wp のほかの issue
-
Page profiler endpoint fatals on PHP 8 when the request has no `b` (background selectors) field対応中かも @girishpanchal30 が 1 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
Codeinwp/optimole-wp#1204 · コメント 4 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Background image with a gradient is not lazy-loaded on WordPress 7.1 (Quote, Pullquote, Verse, Post Content, Accordion)対応中かも @girishpanchal30 が 1 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Codeinwp/optimole-wp#1203 · コメント 3 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Page profiler never treats the detected LCP image as LCP, so it gets no fetchpriority="high" (stored ID is a string, compared with === to an int)対応中かも @girishpanchal30 が 1 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
Codeinwp/optimole-wp#1202 · コメント 3 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Renaming an image leaves the old URL in comments and term descriptions, with "Unknown column 'ID'" database errors対応中かも @girishpanchal30 が 1 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
Codeinwp/optimole-wp#1199 · コメント 3 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
bug-report customer report
難易度 4/5 3〜5日 初心者へのやさしさ 56/100
Codeinwp/optimole-wp#1211 ·
メンテナーはふだん 1 日以内に返信
Codeinwp/optimole-wp の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
help wanted
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
the-benchmarker/web-frameworks#10153 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
FreeCAD/FreeCAD-Homepage#366 · コメント 2 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
unopim/shopify-connector#55 ·