diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 83c2a94..9ea9c4a 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'); + } + /** * List subscriptions to a form * @@ -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, ] ); @@ -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; @@ -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; } diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 51f25fd..a8bdeee 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_form_subscriptions() returns the expected data * when a valid Form ID is specified.