Skip to content

v4 API: Resources: Include Legacy Forms and Landing Pages #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/class-convertkit-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,15 +1133,18 @@ public function log( $entry ) {
}

/**
* Get HTML for the given URL.
* Get HTML for the given URL, which will be either a:
* - Legacy Form
* - Legacy Landing Page
* - Landing Page
*
* This isn't specifically an API function, but for now it's best suited here.
*
* @param string $url URL of Form or Landing Page.
* @param bool $body_only Return HTML between <body> and </body> tags only.
* @return WP_Error|string
*/
private function get_html( $url, $body_only = true ) {
public function get_html( $url, $body_only = true ) {

// Get HTML from URL.
$result = wp_remote_get(
Expand Down
52 changes: 49 additions & 3 deletions src/class-convertkit-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,25 @@ public function refresh() {
switch ( $this->type ) {
case 'forms':
case 'landing_pages':
$resources = $this->get_all_resources( $this->type );

// Bail if an error occured, as we don't want to cache errors.
if ( is_wp_error( $resources ) ) {
return $resources;
}

// Fetch legacy forms / landing pages.
$legacy_resources = $this->get_all_resources( 'legacy_' . $this->type );

// Bail if an error occured, as we don't want to cache errors.
if ( is_wp_error( $legacy_resources ) ) {
return $legacy_resources;
}

// Combine.
$results = $resources + $legacy_resources;
break;

case 'tags':
case 'sequences':
case 'custom_fields':
Expand Down Expand Up @@ -520,6 +539,16 @@ private function get_all_resources( $resource_type, $per_page = 100 ) {
);
break;

case 'legacy_forms':
case 'legacy_landing_pages':
$args = array(
false,
'',
'',
$per_page,
);
break;

default:
$args = array(
false,
Expand Down Expand Up @@ -564,6 +593,16 @@ private function get_all_resources( $resource_type, $per_page = 100 ) {
);
break;

case 'legacy_forms':
case 'legacy_landing_pages':
$args = array(
false,
$response['pagination']['end_cursor'],
'',
$per_page,
);
break;

default:
$args = array(
false,
Expand Down Expand Up @@ -607,9 +646,16 @@ private function get_all_resources( $resource_type, $per_page = 100 ) {
*/
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';
// If we're building an array of landing pages, use the appropriate key.
switch ( $resource_type ) {
case 'landing_pages':
$resource_type = 'forms';
break;

case 'legacy_forms':
case 'legacy_landing_pages':
$resource_type = 'legacy_landing_pages';
break;
}

foreach ( $response[ $resource_type ] as $item ) {
Expand Down
14 changes: 13 additions & 1 deletion tests/wpunit/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ public function testRefreshForms()

// Assert array keys are preserved.
$this->assertArrayHasKey($_ENV['CONVERTKIT_API_FORM_ID'], $resources);

// Assert the legacy form is included in the data i.e. refreshing forms
// did call both `get_forms` and `get_legacy_forms` methods.
$this->assertArrayHasKey($_ENV['CONVERTKIT_API_LEGACY_FORM_ID'], $resources);
$this->assertArrayHasKey('embed_url', $resources[ $_ENV['CONVERTKIT_API_LEGACY_FORM_ID'] ]);
$this->assertEquals('https://api.convertkit.com/api/v3/forms/' . $_ENV['CONVERTKIT_API_LEGACY_FORM_ID'] . '.html?api_key=' . $_ENV['CONVERTKIT_API_KEY'], $resources[ $_ENV['CONVERTKIT_API_LEGACY_FORM_ID'] ]['embed_url']);
}

/**
Expand Down Expand Up @@ -323,7 +329,7 @@ public function testRefreshLandingPages()

// Assert order of data is in ascending alphabetical order.
$this->assertEquals('Character Encoding', reset($result)[ $this->resource->order_by ]);
$this->assertEquals('Landing Page', end($result)[ $this->resource->order_by ]);
$this->assertEquals('Legacy Landing Page', end($result)[ $this->resource->order_by ]);

// Confirm resources stored in WordPress options.
$resources = get_option($this->resource->settings_name);
Expand All @@ -333,6 +339,12 @@ public function testRefreshLandingPages()

// Assert array keys are preserved.
$this->assertArrayHasKey($_ENV['CONVERTKIT_API_LANDING_PAGE_ID'], $resources);

// Assert the legacy landing page is included in the data i.e. refreshing landing pages
// did call both `get_landing_pages` and `get_legacy_landing_pages` methods.
$this->assertArrayHasKey($_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_ID'], $resources);
$this->assertArrayHasKey('url', $resources[ $_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_ID'] ]);
$this->assertEquals('https://app.convertkit.com/landing_pages/' . $_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_ID'], $resources[ $_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_ID'] ]['url']);
}

/**
Expand Down
Loading