Skip to content

Add get_forms() and get_landing_pages() API functions #41

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 2 commits into from
Mar 10, 2023
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
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