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 ] ); }