@@ -23,13 +23,6 @@ class ConvertKit_API {
23
23
*/
24
24
public const VERSION = '2.0.0 ' ;
25
25
26
- /**
27
- * ConvertKit API Key
28
- *
29
- * @var bool|string
30
- */
31
- protected $ api_key = false ;
32
-
33
26
/**
34
27
* Redirect URI.
35
28
*
@@ -690,7 +683,35 @@ public function get_post( $post_id ) {
690
683
*/
691
684
public function get_products () {
692
685
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 ;
694
715
695
716
}
696
717
@@ -856,22 +877,39 @@ public function profile( $signed_subscriber_id ) {
856
877
857
878
}
858
879
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
+
859
896
/**
860
897
* Get HTML from ConvertKit for the given Legacy Form ID.
861
898
*
862
899
* This isn't specifically an API function, but for now it's best suited here.
863
900
*
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
866
904
*/
867
- public function get_form_html ( $ id ) {
905
+ public function get_form_html ( $ id, $ api_key = '' ) {
868
906
869
907
$ this ->log ( 'API: get_form_html(): [ id: ' . $ id . '] ' );
870
908
871
909
// Define Legacy Form URL.
872
910
$ url = add_query_arg (
873
911
array (
874
- 'k ' => $ this -> api_key ,
912
+ 'k ' => $ api_key ,
875
913
'v ' => 2 ,
876
914
),
877
915
'https://api.convertkit.com/forms/ ' . $ id . '/embed '
0 commit comments