Skip to content

Commit f211633

Browse files
committed
Added get_forms() and get_landing_pages() API functions
1 parent c60e6bf commit f211633

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
* Gets all sequences
155179
*
@@ -266,9 +290,7 @@ public function get_resources(string $resource)
266290
$resources = $this->get(
267291
$request,
268292
[
269-
'api_key' => $this->api_key,
270-
'timeout' => 10,
271-
'Accept-Encoding' => 'gzip',
293+
'api_key' => $this->api_key,
272294
]
273295
);
274296

@@ -291,12 +313,18 @@ public function get_resources(string $resource)
291313
return [];
292314
}
293315

294-
// Exclude archived forms.
316+
// Build array of forms.
295317
foreach ($resources->forms as $form) {
318+
// Exclude archived forms.
296319
if (isset($form->archived) && $form->archived) {
297320
continue;
298321
}
299322

323+
// Exclude hosted forms.
324+
if ($form->type === 'hosted') {
325+
continue;
326+
}
327+
300328
$_resource[] = $form;
301329
}
302330
break;
@@ -309,12 +337,13 @@ public function get_resources(string $resource)
309337
return [];
310338
}
311339

312-
// Exclude forms and archived forms/landing pages.
313340
foreach ($resources->forms as $form) {
341+
// Exclude archived landing pages.
314342
if (isset($form->archived) && $form->archived) {
315343
continue;
316344
}
317345

346+
// Exclude non-hosted (i.e. forms).
318347
if ($form->type !== 'hosted') {
319348
continue;
320349
}

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_sequences() returns the expected data.
70119
*

0 commit comments

Comments
 (0)