From f21163369f5c85b66a28dee874b03e3a79df8626 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Thu, 9 Mar 2023 15:36:05 +0000 Subject: [PATCH] Added `get_forms()` and `get_landing_pages()` API functions --- src/ConvertKit_API.php | 39 +++++++++++++++++++++++++---- tests/ConvertKitAPITest.php | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 9c7e861..fa1f425 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -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'); + } + /** * Gets all sequences * @@ -266,9 +290,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, ] ); @@ -291,12 +313,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; @@ -309,12 +337,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; } diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 398bb42..b175259 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -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_sequences() returns the expected data. *