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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user