Skip to content

Commit 525a8ee

Browse files
authored
Merge pull request #66 from ConvertKit/v4-api-legacy-resources
v4 API: Resources: Include Legacy Forms and Landing Pages
2 parents 702499b + a1eb51d commit 525a8ee

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

src/class-convertkit-api.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,15 +1133,18 @@ public function log( $entry ) {
11331133
}
11341134

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

11461149
// Get HTML from URL.
11471150
$result = wp_remote_get(

src/class-convertkit-resource.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,25 @@ public function refresh() {
357357
switch ( $this->type ) {
358358
case 'forms':
359359
case 'landing_pages':
360+
$resources = $this->get_all_resources( $this->type );
361+
362+
// Bail if an error occured, as we don't want to cache errors.
363+
if ( is_wp_error( $resources ) ) {
364+
return $resources;
365+
}
366+
367+
// Fetch legacy forms / landing pages.
368+
$legacy_resources = $this->get_all_resources( 'legacy_' . $this->type );
369+
370+
// Bail if an error occured, as we don't want to cache errors.
371+
if ( is_wp_error( $legacy_resources ) ) {
372+
return $legacy_resources;
373+
}
374+
375+
// Combine.
376+
$results = $resources + $legacy_resources;
377+
break;
378+
360379
case 'tags':
361380
case 'sequences':
362381
case 'custom_fields':
@@ -520,6 +539,16 @@ private function get_all_resources( $resource_type, $per_page = 100 ) {
520539
);
521540
break;
522541

542+
case 'legacy_forms':
543+
case 'legacy_landing_pages':
544+
$args = array(
545+
false,
546+
'',
547+
'',
548+
$per_page,
549+
);
550+
break;
551+
523552
default:
524553
$args = array(
525554
false,
@@ -564,6 +593,16 @@ private function get_all_resources( $resource_type, $per_page = 100 ) {
564593
);
565594
break;
566595

596+
case 'legacy_forms':
597+
case 'legacy_landing_pages':
598+
$args = array(
599+
false,
600+
$response['pagination']['end_cursor'],
601+
'',
602+
$per_page,
603+
);
604+
break;
605+
567606
default:
568607
$args = array(
569608
false,
@@ -607,9 +646,16 @@ private function get_all_resources( $resource_type, $per_page = 100 ) {
607646
*/
608647
private function map( $response, $items = array(), $resource_type = 'forms' ) {
609648

610-
// If we're building an array of landing pages, use the `form` key.
611-
if ( $resource_type === 'landing_pages' ) {
612-
$resource_type = 'forms';
649+
// If we're building an array of landing pages, use the appropriate key.
650+
switch ( $resource_type ) {
651+
case 'landing_pages':
652+
$resource_type = 'forms';
653+
break;
654+
655+
case 'legacy_forms':
656+
case 'legacy_landing_pages':
657+
$resource_type = 'legacy_landing_pages';
658+
break;
613659
}
614660

615661
foreach ( $response[ $resource_type ] as $item ) {

tests/wpunit/ResourceTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ public function testRefreshForms()
292292

293293
// Assert array keys are preserved.
294294
$this->assertArrayHasKey($_ENV['CONVERTKIT_API_FORM_ID'], $resources);
295+
296+
// Assert the legacy form is included in the data i.e. refreshing forms
297+
// did call both `get_forms` and `get_legacy_forms` methods.
298+
$this->assertArrayHasKey($_ENV['CONVERTKIT_API_LEGACY_FORM_ID'], $resources);
299+
$this->assertArrayHasKey('embed_url', $resources[ $_ENV['CONVERTKIT_API_LEGACY_FORM_ID'] ]);
300+
$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']);
295301
}
296302

297303
/**
@@ -323,7 +329,7 @@ public function testRefreshLandingPages()
323329

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

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

334340
// Assert array keys are preserved.
335341
$this->assertArrayHasKey($_ENV['CONVERTKIT_API_LANDING_PAGE_ID'], $resources);
342+
343+
// Assert the legacy landing page is included in the data i.e. refreshing landing pages
344+
// did call both `get_landing_pages` and `get_legacy_landing_pages` methods.
345+
$this->assertArrayHasKey($_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_ID'], $resources);
346+
$this->assertArrayHasKey('url', $resources[ $_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_ID'] ]);
347+
$this->assertEquals('https://app.convertkit.com/landing_pages/' . $_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_ID'], $resources[ $_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_ID'] ]['url']);
336348
}
337349

338350
/**

0 commit comments

Comments
 (0)