Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v4-api' into v4-api-tests-no-data
Browse files Browse the repository at this point in the history
  • Loading branch information
n7studios committed Apr 23, 2024
2 parents 630c713 + 75fbe4a commit e2b3225
Show file tree
Hide file tree
Showing 6 changed files with 1,991 additions and 356 deletions.
3 changes: 3 additions & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ TEST_SITE_WP_URL=http://127.0.0.1
TEST_SITE_WP_DOMAIN=127.0.0.1
TEST_SITE_ADMIN_EMAIL=wordpress@convertkit.local
CONVERTKIT_API_BROADCAST_ID="8697158"
CONVERTKIT_API_CUSTOM_FIELD_ID="258240"
CONVERTKIT_API_FORM_ID="2765139"
CONVERTKIT_API_LEGACY_FORM_ID="470099"
CONVERTKIT_API_LANDING_PAGE_ID="2765196"
Expand All @@ -30,6 +31,8 @@ CONVERTKIT_API_PRODUCT_ID="36377"
CONVERTKIT_API_SEQUENCE_ID="1030824"
CONVERTKIT_API_TAG_NAME="wordpress"
CONVERTKIT_API_TAG_ID="2744672"
CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1"
CONVERTKIT_API_TAG_ID_2="2907192"
CONVERTKIT_API_SUBSCRIBER_EMAIL="optin@n7studios.com"
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"
CONVERTKIT_API_RECOMMENDATIONS_JS="https://cheerful-architect-3237.ck.page/WnaDZ370gtgOq750dwOl-recommendations.js"
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA=
CONVERTKIT_OAUTH_CLIENT_ID=
CONVERTKIT_OAUTH_REDIRECT_URI=
CONVERTKIT_API_BROADCAST_ID="8697158"
CONVERTKIT_API_CUSTOM_FIELD_ID="258240"
CONVERTKIT_API_FORM_ID="2765139"
CONVERTKIT_API_LEGACY_FORM_ID="470099"
CONVERTKIT_API_LANDING_PAGE_ID="2765196"
Expand All @@ -38,6 +39,8 @@ CONVERTKIT_API_PRODUCT_ID="36377"
CONVERTKIT_API_SEQUENCE_ID="1030824"
CONVERTKIT_API_TAG_NAME="wordpress"
CONVERTKIT_API_TAG_ID="2744672"
CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1"
CONVERTKIT_API_TAG_ID_2="2907192"
CONVERTKIT_API_SUBSCRIBER_EMAIL="optin@n7studios.com"
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"
CONVERTKIT_API_SIGNED_SUBSCRIBER_ID=
Expand Down
69 changes: 55 additions & 14 deletions src/class-convertkit-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1196,20 +1196,8 @@ public function request( $endpoint, $method = 'get', $params = array(), $retry_i

// Return the API error message as a WP_Error if the HTTP response code is a 4xx code.
if ( $http_response_code >= 400 ) {
// Define the error description.
$error = '';
if ( array_key_exists( 'errors', $response ) ) {
$error = implode( "\n", $response['errors'] );
} elseif ( array_key_exists( 'error_description', $response ) ) {
$error = $response['error_description'];
} elseif ( array_key_exists( 'error', $response ) ) {
// The 'error' key is present when exchanging an API Key and Secret for an Access Token
// and something went wrong e.g. invalid API credentials.
$error = $response['error'];
if ( array_key_exists( 'message', $response ) ) {
$error .= ': ' . $response['message'];
}
}
// Define the error message.
$error = $this->get_error_message_string( $response );

$this->log( 'API: Error: ' . $error );

Expand Down Expand Up @@ -1259,6 +1247,59 @@ public function request( $endpoint, $method = 'get', $params = array(), $retry_i

}

/**
* Inspects the given API response for errors, returning them as a string.
*
* @since 2.0.0
*
* @param array $response API Response.
* @return string Error Message(s).
*/
private function get_error_message_string( $response ) {

// Most API responses contain the `errors` key.
if ( array_key_exists( 'errors', $response ) ) {
$error_message = '';

// For sequences, each item in the `errors` key is an array.
// For other API endpoints, each item in the `errors` key is a string.
foreach ( $response['errors'] as $error ) {
if ( is_array( $error ) ) {
$error_message .= implode( "\n", $error );
continue;
}

// Error is a string.
$error_message .= "\n" . $error;
}

// Remove errant newlines and return.
return trim( $error_message );
}

// Some might provide an `error_description`.
if ( array_key_exists( 'error_description', $response ) ) {
return $response['error_description'];
}

// The `error` key is present when exchanging an API Key and Secret for an Access Token
// and something went wrong e.g. invalid API credentials.
if ( array_key_exists( 'error', $response ) ) {
$error_message = $response['error'];

// There might be additional information we can return.
if ( array_key_exists( 'message', $response ) ) {
$error_message .= ': ' . $response['message'];
}

return $error_message;
}

// If here, no error was detected.
return '';

}

/**
* Returns the headers to use in an API request.
*
Expand Down
62 changes: 48 additions & 14 deletions src/class-convertkit-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,16 +512,33 @@ public function delete() {
*/
private function get_all_resources( $resource_type, $per_page = 100 ) {

// Build array of arguments depending on the resource type.
switch ( $resource_type ) {
case 'forms':
case 'landing_pages':
$args = array(
'active',
false,
'',
'',
$per_page,
);
break;

default:
$args = array(
false,
'',
'',
$per_page,
);
break;
}

// Fetch resources.
$response = call_user_func_array(
array( $this->api, 'get_' . $resource_type ),
array(
'active',
false,
'',
'',
$per_page,
)
$args
);

// Bail if an error occured.
Expand All @@ -539,19 +556,36 @@ private function get_all_resources( $resource_type, $per_page = 100 ) {

// Further resources need to be fetched.
while ( $response['pagination']['has_next_page'] ) {
// Build array of arguments depending on the resource type.
switch ( $resource_type ) {
case 'forms':
case 'landing_pages':
$args = array(
'active',
false,
$response['pagination']['end_cursor'],
'',
$per_page,
);
break;

default:
$args = array(
false,
$response['pagination']['end_cursor'],
'',
$per_page,
);
break;
}

// Fetch next page of resources.
$response = call_user_func_array(
array(
$this->api,
'get_' . $resource_type,
),
array(
'active',
false,
$response['pagination']['end_cursor'],
'',
$per_page,
)
$args
);

// Bail if an error occured.
Expand Down
Loading

0 comments on commit e2b3225

Please sign in to comment.