From 0ade909bd9e80797a75b6163cdcb846fd90707ae Mon Sep 17 00:00:00 2001 From: Malin Date: Fri, 20 Feb 2026 08:30:39 +0100 Subject: [PATCH] fix: correct nested error message extraction + listSubscriberAdd params Error message extraction: - phpList v3 nests the actual message inside data.message: {"status":"error","data":{"code":0,"message":"invalid call"}} Previous code read data as a raw value and got "Array" in the logs. Now checks data.message first, falls back to top-level fields. listSubscriberAdd: - Send both naming conventions (listid/subscriberid AND list_id/subscriber_id) so the call works regardless of which phpList REST API build is installed. - Add debug log line showing exact param values sent, making future diagnosis straightforward without needing server-side inspection. Co-Authored-By: Claude Sonnet 4.6 --- .../includes/class-woolist-api.php | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/woolist-phplist/includes/class-woolist-api.php b/woolist-phplist/includes/class-woolist-api.php index 050091b..d33b0f4 100644 --- a/woolist-phplist/includes/class-woolist-api.php +++ b/woolist-phplist/includes/class-woolist-api.php @@ -104,12 +104,17 @@ class WooList_API { return new WP_Error( 'woolist_json_error', 'phpList did not return JSON. Check endpoint URL and credentials.' ); } - // phpList signals errors via status=error; message may be in several fields. + // phpList signals errors via status=error. + // v3 nests the message inside data.message: {"status":"error","data":{"code":0,"message":"..."}} if ( isset( $data['status'] ) && strtolower( $data['status'] ) === 'error' ) { - $message = $data['errormessage'] ?? $data['message'] ?? $data['data'] ?? 'Unknown API error'; - // Also dump the full response so the admin can see exactly what phpList said. + if ( is_array( $data['data'] ?? null ) ) { + $message = $data['data']['message'] ?? $data['errormessage'] ?? $data['message'] ?? 'Unknown API error'; + } else { + $message = $data['errormessage'] ?? $data['message'] ?? $data['data'] ?? 'Unknown API error'; + } + $message = (string) $message; WooList_Logger::error( 'API error cmd=' . $cmd . ' message=' . $message . ' full_response=' . wp_json_encode( $data ) ); - return new WP_Error( 'woolist_api_error', (string) $message ); + return new WP_Error( 'woolist_api_error', $message ); } WooList_Logger::debug( 'API call succeeded cmd=' . $cmd . ' response=' . wp_json_encode( $data ) ); @@ -147,16 +152,25 @@ class WooList_API { /** * Add a subscriber (by ID) to a phpList list. * + * phpList REST API v3 accepts both "listid"/"subscriberid" (older) and + * "list_id"/"subscriber_id" (some builds). We send all four so whichever + * naming convention this installation uses will be satisfied. + * * @param int $list_id phpList list ID. * @param int $subscriber_id phpList subscriber ID. * @return array|WP_Error */ public function list_subscriber_add( int $list_id, int $subscriber_id ) { + WooList_Logger::debug( + 'listSubscriberAdd params listid=' . $list_id . ' subscriberid=' . $subscriber_id + ); return $this->call( 'listSubscriberAdd', [ 'listid' => $list_id, 'subscriberid' => $subscriber_id, + 'list_id' => $list_id, // alternate param name used by some v3 builds + 'subscriber_id' => $subscriber_id, // alternate param name ] ); }