Skip to content

Commit

Permalink
Merge pull request #41 from ConvertKit/get-forms-landing-pages
Browse files Browse the repository at this point in the history
Add `get_forms()` and `get_landing_pages()` API functions
  • Loading branch information
n7studios authored Mar 10, 2023
2 parents 85e0d53 + 4cd3ba4 commit 3cc8333
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ public function get_account()
);
}

/**
* Gets all forms.
*
* @since 1.0.0
*
* @return false|mixed
*/
public function get_forms()
{
return $this->get_resources('forms');
}

/**
* Gets all landing pages.
*
* @since 1.0.0
*
* @return false|mixed
*/
public function get_landing_pages()
{
return $this->get_resources('landing_pages');
}

/**
* List subscriptions to a form
*
Expand Down Expand Up @@ -295,9 +319,7 @@ public function get_resources(string $resource)
$resources = $this->get(
$request,
[
'api_key' => $this->api_key,
'timeout' => 10,
'Accept-Encoding' => 'gzip',
'api_key' => $this->api_key,
]
);

Expand All @@ -320,12 +342,18 @@ public function get_resources(string $resource)
return [];
}

// Exclude archived forms.
// Build array of forms.
foreach ($resources->forms as $form) {
// Exclude archived forms.
if (isset($form->archived) && $form->archived) {
continue;
}

// Exclude hosted forms.
if ($form->type === 'hosted') {
continue;
}

$_resource[] = $form;
}
break;
Expand All @@ -338,12 +366,13 @@ public function get_resources(string $resource)
return [];
}

// Exclude forms and archived forms/landing pages.
foreach ($resources->forms as $form) {
// Exclude archived landing pages.
if (isset($form->archived) && $form->archived) {
continue;
}

// Exclude non-hosted (i.e. forms).
if ($form->type !== 'hosted') {
continue;
}
Expand Down
49 changes: 49 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,55 @@ public function testGetAccount()
$this->assertArrayHasKey('primary_email_address', $result);
}

/**
* Test that get_forms() returns the expected data.
*
* @since 1.0.0
*
* @return void
*/
public function testGetForms()
{
$result = $this->api->get_forms();
$this->assertIsArray($result);

// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
$form = get_object_vars($result[0]);
$this->assertArrayHasKey('id', $form);
$this->assertArrayHasKey('name', $form);
$this->assertArrayHasKey('created_at', $form);
$this->assertArrayHasKey('type', $form);
$this->assertArrayHasKey('format', $form);
$this->assertArrayHasKey('embed_js', $form);
$this->assertArrayHasKey('embed_url', $form);
$this->assertArrayHasKey('archived', $form);
}

/**
* Test that get_landing_pages() returns the expected data.
*
* @since 1.0.0
*
* @return void
*/
public function testGetLandingPages()
{
$result = $this->api->get_landing_pages();
$this->assertIsArray($result);

// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
$landingPage = get_object_vars($result[0]);
$this->assertArrayHasKey('id', $landingPage);
$this->assertArrayHasKey('name', $landingPage);
$this->assertArrayHasKey('created_at', $landingPage);
$this->assertArrayHasKey('type', $landingPage);
$this->assertEquals('hosted', $landingPage['type']);
$this->assertArrayHasKey('format', $landingPage);
$this->assertArrayHasKey('embed_js', $landingPage);
$this->assertArrayHasKey('embed_url', $landingPage);
$this->assertArrayHasKey('archived', $landingPage);
}

/**
* Test that get_form_subscriptions() returns the expected data
* when a valid Form ID is specified.
Expand Down

0 comments on commit 3cc8333

Please sign in to comment.