Skip to content

Commit

Permalink
Merge pull request #39 from ConvertKit/v4-api-resources
Browse files Browse the repository at this point in the history
v4 API: Resources
  • Loading branch information
n7studios authored Apr 18, 2024
2 parents bffb525 + c33a5a9 commit cc3778d
Show file tree
Hide file tree
Showing 6 changed files with 638 additions and 87 deletions.
1 change: 1 addition & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TEST_TABLE_PREFIX=wp_
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_FORM_ID="2765139"
CONVERTKIT_API_LEGACY_FORM_ID="470099"
CONVERTKIT_API_LANDING_PAGE_ID="2765196"
Expand Down
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CONVERTKIT_OAUTH_ACCESS_TOKEN_NO_DATA=
CONVERTKIT_OAUTH_REFRESH_TOKEN_NO_DATA=
CONVERTKIT_OAUTH_CLIENT_ID=
CONVERTKIT_OAUTH_REDIRECT_URI=
CONVERTKIT_API_BROADCAST_ID="8697158"
CONVERTKIT_API_FORM_ID="2765139"
CONVERTKIT_API_LEGACY_FORM_ID="470099"
CONVERTKIT_API_LANDING_PAGE_ID="2765196"
Expand Down
10 changes: 7 additions & 3 deletions src/class-convertkit-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ConvertKit_API {
protected $api_endpoints_wordpress = array(
'posts',
'products',
'profile',
'profile/',
'recommendations_script',
'subscriber_authentication/send_code',
'subscriber_authentication/verify',
Expand Down Expand Up @@ -1018,8 +1018,12 @@ public function request( $endpoint, $method = 'get', $params = array(), $retry_i
// Send request.
switch ( strtolower( $method ) ) {
case 'get':
// We deliberate don't use add_query_arg(), as this converts double equal signs (typically
// provided by `start_cursor` and `end_cursor`) to a single equal sign, therefore breaking
// pagination. http_build_query() will encode equals signs instead, preserving them
// and ensuring paginated requests work correctly.
$result = wp_remote_get(
add_query_arg( $params, $this->get_api_url( $endpoint ) ),
$this->get_api_url( $endpoint ) . '?' . http_build_query( $params ),
array(
'headers' => $this->get_request_headers(),
'timeout' => $this->get_timeout(),
Expand Down Expand Up @@ -1316,7 +1320,7 @@ private function get_api_url( $endpoint ) {
}
}

// For all other endpoints, it's https://api.convertkit.com/v3/$endpoint.
// For all other endpoints, it's https://api.convertkit.com/v4/$endpoint.
return path_join( $this->api_url_base . $this->api_version, $endpoint );

}
Expand Down
109 changes: 96 additions & 13 deletions src/class-convertkit-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,23 +356,11 @@ public function refresh() {
// Fetch resources.
switch ( $this->type ) {
case 'forms':
$results = $this->api->get_forms();
break;

case 'landing_pages':
$results = $this->api->get_landing_pages();
break;

case 'tags':
$results = $this->api->get_tags();
break;

case 'sequences':
$results = $this->api->get_sequences();
break;

case 'custom_fields':
$results = $this->api->get_custom_fields();
$results = $this->get_all_resources( $this->type );
break;

case 'posts':
Expand All @@ -381,6 +369,11 @@ public function refresh() {

case 'products':
$results = $this->api->get_products();
if ( is_wp_error( $results ) ) {
return $results;
}

$results = $this->map( $results, array(), 'products' );
break;

default:
Expand Down Expand Up @@ -508,4 +501,94 @@ public function delete() {

}

/**
* Fetches all resources (forms, landing pages, tags etc) from the API,
* using cursor pagination until all results are returned.
*
* @since 2.0.0
*
* @param string $resource_type Resource (forms,landing_pages,tags,sequences,custom_fields).
* @param int $per_page Number of results to return per request.
*/
private function get_all_resources( $resource_type, $per_page = 100 ) {

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

// Bail if an error occured.
if ( is_wp_error( $response ) ) {
return $response;
}

// Append resources to array.
$items = $this->map( $response, array(), $resource_type );

// If no further resources to fetch, return.
if ( ! $response['pagination']['has_next_page'] ) {
return $items;
}

// Further resources need to be fetched.
while ( $response['pagination']['has_next_page'] ) {
// 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,
)
);

// Bail if an error occured.
if ( is_wp_error( $response ) ) {
return $response;
}

// Append resources to array.
$items = $this->map( $response, $items, $resource_type );
}

return $items;

}

/**
* Helper method to build an array of resources with keys as IDs.
*
* @since 2.0.0
*
* @param array $response API Response.
* @param array $items Key'd resources.
* @param string $resource_type Resource (forms,landing_pages,tags,sequences,custom_fields).
*/
private function map( $response, $items = array(), $resource_type = 'forms' ) {

// If we're building an array of landing pages, use the `form` key.
if ( $resource_type === 'landing_pages' ) {
$resource_type = 'forms';
}

foreach ( $response[ $resource_type ] as $item ) {
$items[ $item['id'] ] = $item;
}

return $items;

}

}
Loading

0 comments on commit cc3778d

Please sign in to comment.