Skip to content

Commit 3cc8333

Browse files
authored
Merge pull request #41 from ConvertKit/get-forms-landing-pages
Add `get_forms()` and `get_landing_pages()` API functions
2 parents 85e0d53 + 4cd3ba4 commit 3cc8333

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

src/ConvertKit_API.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,30 @@ public function get_account()
150150
);
151151
}
152152

153+
/**
154+
* Gets all forms.
155+
*
156+
* @since 1.0.0
157+
*
158+
* @return false|mixed
159+
*/
160+
public function get_forms()
161+
{
162+
return $this->get_resources('forms');
163+
}
164+
165+
/**
166+
* Gets all landing pages.
167+
*
168+
* @since 1.0.0
169+
*
170+
* @return false|mixed
171+
*/
172+
public function get_landing_pages()
173+
{
174+
return $this->get_resources('landing_pages');
175+
}
176+
153177
/**
154178
* List subscriptions to a form
155179
*
@@ -295,9 +319,7 @@ public function get_resources(string $resource)
295319
$resources = $this->get(
296320
$request,
297321
[
298-
'api_key' => $this->api_key,
299-
'timeout' => 10,
300-
'Accept-Encoding' => 'gzip',
322+
'api_key' => $this->api_key,
301323
]
302324
);
303325

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

323-
// Exclude archived forms.
345+
// Build array of forms.
324346
foreach ($resources->forms as $form) {
347+
// Exclude archived forms.
325348
if (isset($form->archived) && $form->archived) {
326349
continue;
327350
}
328351

352+
// Exclude hosted forms.
353+
if ($form->type === 'hosted') {
354+
continue;
355+
}
356+
329357
$_resource[] = $form;
330358
}
331359
break;
@@ -338,12 +366,13 @@ public function get_resources(string $resource)
338366
return [];
339367
}
340368

341-
// Exclude forms and archived forms/landing pages.
342369
foreach ($resources->forms as $form) {
370+
// Exclude archived landing pages.
343371
if (isset($form->archived) && $form->archived) {
344372
continue;
345373
}
346374

375+
// Exclude non-hosted (i.e. forms).
347376
if ($form->type !== 'hosted') {
348377
continue;
349378
}

tests/ConvertKitAPITest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,55 @@ public function testGetAccount()
6565
$this->assertArrayHasKey('primary_email_address', $result);
6666
}
6767

68+
/**
69+
* Test that get_forms() returns the expected data.
70+
*
71+
* @since 1.0.0
72+
*
73+
* @return void
74+
*/
75+
public function testGetForms()
76+
{
77+
$result = $this->api->get_forms();
78+
$this->assertIsArray($result);
79+
80+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
81+
$form = get_object_vars($result[0]);
82+
$this->assertArrayHasKey('id', $form);
83+
$this->assertArrayHasKey('name', $form);
84+
$this->assertArrayHasKey('created_at', $form);
85+
$this->assertArrayHasKey('type', $form);
86+
$this->assertArrayHasKey('format', $form);
87+
$this->assertArrayHasKey('embed_js', $form);
88+
$this->assertArrayHasKey('embed_url', $form);
89+
$this->assertArrayHasKey('archived', $form);
90+
}
91+
92+
/**
93+
* Test that get_landing_pages() returns the expected data.
94+
*
95+
* @since 1.0.0
96+
*
97+
* @return void
98+
*/
99+
public function testGetLandingPages()
100+
{
101+
$result = $this->api->get_landing_pages();
102+
$this->assertIsArray($result);
103+
104+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
105+
$landingPage = get_object_vars($result[0]);
106+
$this->assertArrayHasKey('id', $landingPage);
107+
$this->assertArrayHasKey('name', $landingPage);
108+
$this->assertArrayHasKey('created_at', $landingPage);
109+
$this->assertArrayHasKey('type', $landingPage);
110+
$this->assertEquals('hosted', $landingPage['type']);
111+
$this->assertArrayHasKey('format', $landingPage);
112+
$this->assertArrayHasKey('embed_js', $landingPage);
113+
$this->assertArrayHasKey('embed_url', $landingPage);
114+
$this->assertArrayHasKey('archived', $landingPage);
115+
}
116+
68117
/**
69118
* Test that get_form_subscriptions() returns the expected data
70119
* when a valid Form ID is specified.

0 commit comments

Comments
 (0)