Skip to content

Commit 81cb3fa

Browse files
authoredApr 29, 2024
Merge pull request #58 from ConvertKit/v4-api-tests-wordpress-endpoint
v4 API: Tests: Run `wordpress` endpoint tests
2 parents 75fbe4a + 0a77f25 commit 81cb3fa

File tree

2 files changed

+55
-213
lines changed

2 files changed

+55
-213
lines changed
 

‎src/class-convertkit-api.php

+50-12
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ class ConvertKit_API {
2323
*/
2424
public const VERSION = '2.0.0';
2525

26-
/**
27-
* ConvertKit API Key
28-
*
29-
* @var bool|string
30-
*/
31-
protected $api_key = false;
32-
3326
/**
3427
* Redirect URI.
3528
*
@@ -690,7 +683,35 @@ public function get_post( $post_id ) {
690683
*/
691684
public function get_products() {
692685

693-
return $this->get( 'products' );
686+
$this->log( 'API: get_products()' );
687+
688+
$products = array();
689+
690+
$response = $this->get( 'products' );
691+
692+
// If an error occured, log and return it now.
693+
if ( is_wp_error( $response ) ) {
694+
$this->log( 'API: get_products(): Error: ' . $response->get_error_message() );
695+
return $response;
696+
}
697+
698+
// If the response isn't an array as we expect, log that no products exist and return a blank array.
699+
if ( ! is_array( $response['products'] ) ) {
700+
$this->log( 'API: get_products(): Error: No products exist in ConvertKit.' );
701+
return new WP_Error( 'convertkit_api_error', $this->get_error_message( 'response_type_unexpected' ) );
702+
}
703+
704+
// If no products exist, log that no products exist and return a blank array.
705+
if ( ! count( $response['products'] ) ) {
706+
$this->log( 'API: get_products(): Error: No products exist in ConvertKit.' );
707+
return $products;
708+
}
709+
710+
foreach ( $response['products'] as $product ) {
711+
$products[ $product['id'] ] = $product;
712+
}
713+
714+
return $products;
694715

695716
}
696717

@@ -856,22 +877,39 @@ public function profile( $signed_subscriber_id ) {
856877

857878
}
858879

880+
/**
881+
* Returns the recommendations script URL for this account from the API,
882+
* used to display the Creator Network modal when a form is submitted.
883+
*
884+
* @since 1.3.7
885+
*
886+
* @return WP_Error|array
887+
*/
888+
public function recommendations_script() {
889+
890+
$this->log( 'API: recommendations_script()' );
891+
892+
return $this->get( 'recommendations_script' );
893+
894+
}
895+
859896
/**
860897
* Get HTML from ConvertKit for the given Legacy Form ID.
861898
*
862899
* This isn't specifically an API function, but for now it's best suited here.
863900
*
864-
* @param int $id Form ID.
865-
* @return WP_Error|string HTML
901+
* @param int $id Form ID.
902+
* @param string $api_key API Key.
903+
* @return WP_Error|string HTML
866904
*/
867-
public function get_form_html( $id ) {
905+
public function get_form_html( $id, $api_key = '' ) {
868906

869907
$this->log( 'API: get_form_html(): [ id: ' . $id . ']' );
870908

871909
// Define Legacy Form URL.
872910
$url = add_query_arg(
873911
array(
874-
'k' => $this->api_key,
912+
'k' => $api_key,
875913
'v' => 2,
876914
),
877915
'https://api.convertkit.com/forms/' . $id . '/embed'

0 commit comments

Comments
 (0)