class-fl-builder-service-convertkit.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Helper class for the ConvertKit API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceConvertKit extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'convertkit';
  15. /**
  16. * @since 1.5.4
  17. * @var object $api_instance
  18. * @access private
  19. */
  20. private $api_instance = null;
  21. /**
  22. * Get an instance of the API.
  23. *
  24. * @since 1.5.4
  25. * @param string $api_key A valid API key.
  26. * @return object The API instance.
  27. */
  28. public function get_api( $api_key ) {
  29. if ( $this->api_instance ) {
  30. return $this->api_instance;
  31. }
  32. if ( ! class_exists( 'ConvertKit' ) ) {
  33. require_once FL_BUILDER_DIR . 'includes/vendor/convertkit/ConvertKit.php';
  34. }
  35. $this->api_instance = new ConvertKit( $api_key );
  36. return $this->api_instance;
  37. }
  38. /**
  39. * Test the API connection.
  40. *
  41. * @since 1.5.4
  42. * @param array $fields {
  43. * @type string $api_key A valid API key.
  44. * }
  45. * @return array{
  46. * @type bool|string $error The error message or false if no error.
  47. * @type array $data An array of data used to make the connection.
  48. * }
  49. */
  50. public function connect( $fields = array() ) {
  51. $response = array(
  52. 'error' => false,
  53. 'data' => array(),
  54. );
  55. // Make sure we have an API key.
  56. if ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  57. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  58. } else {
  59. $api = $this->get_api( $fields['api_key'] );
  60. if ( $api->is_authenticated() ) {
  61. $response['data'] = array(
  62. 'api_key' => $fields['api_key'],
  63. );
  64. } else {
  65. $response['error'] = sprintf( __( 'Error: Please check your API key. %s', 'fl-builder' ), $api::$response['error_message'] );
  66. }
  67. }
  68. return $response;
  69. }
  70. /**
  71. * Renders the markup for the connection settings.
  72. *
  73. * @since 1.5.4
  74. * @return string The connection settings markup.
  75. */
  76. public function render_connect_settings() {
  77. ob_start();
  78. FLBuilder::render_settings_field( 'api_key', array(
  79. 'row_class' => 'fl-builder-service-connect-row',
  80. 'class' => 'fl-builder-service-connect-input',
  81. 'type' => 'text',
  82. 'label' => __( 'API Key', 'fl-builder' ),
  83. 'help' => __( 'Your API key can be found in your ConvertKit account under Account > Account Settings > API Key.', 'fl-builder' ),
  84. 'preview' => array(
  85. 'type' => 'none',
  86. ),
  87. ));
  88. return ob_get_clean();
  89. }
  90. /**
  91. * Render the markup for service specific fields.
  92. *
  93. * @since 1.5.4
  94. * @param string $account The name of the saved account.
  95. * @param object $settings Saved module settings.
  96. * @return array {
  97. * @type bool|string $error The error message or false if no error.
  98. * @type string $html The field markup.
  99. * }
  100. */
  101. public function render_fields( $account, $settings ) {
  102. $account_data = $this->get_account_data( $account );
  103. $api = $this->get_api( $account_data['api_key'] );
  104. $forms = $api->get_resources( 'forms' );
  105. $response = array(
  106. 'error' => false,
  107. 'html' => '',
  108. );
  109. if ( ! $forms ) {
  110. $response['error'] = __( 'Error: Please check your API key.', 'fl-builder' );
  111. } else {
  112. $response['html'] = $this->render_list_field( $forms, $settings );
  113. }
  114. return $response;
  115. }
  116. /**
  117. * Render markup for the list field.
  118. *
  119. * @since 1.5.4
  120. * @param array $lists List data from the API.
  121. * @param object $settings Saved module settings.
  122. * @return string The markup for the list field.
  123. * @access private
  124. */
  125. private function render_list_field( $forms, $settings ) {
  126. ob_start();
  127. $options = array(
  128. '' => __( 'Choose...', 'fl-builder' ),
  129. );
  130. if ( isset( $forms['forms'] ) ) {
  131. foreach ( $forms['forms'] as $form ) {
  132. $options[ $form['id'] ] = $form['name'];
  133. }
  134. }
  135. FLBuilder::render_settings_field( 'list_id', array(
  136. 'row_class' => 'fl-builder-service-field-row',
  137. 'class' => 'fl-builder-service-list-select',
  138. 'type' => 'select',
  139. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  140. 'options' => $options,
  141. 'preview' => array(
  142. 'type' => 'none',
  143. ),
  144. ), $settings);
  145. return ob_get_clean();
  146. }
  147. /**
  148. * Subscribe an email address to ConvertKit.
  149. *
  150. * @since 1.5.4
  151. * @param object $settings A module settings object.
  152. * @param string $email The email to subscribe.
  153. * @param string $name Optional. The full name of the person subscribing.
  154. * @return array {
  155. * @type bool|string $error The error message or false if no error.
  156. * }
  157. */
  158. public function subscribe( $settings, $email, $name = '' ) {
  159. $account_data = $this->get_account_data( $settings->service_account );
  160. $response = array(
  161. 'error' => false,
  162. );
  163. if ( ! $account_data ) {
  164. $response['error'] = __( 'There was an error subscribing to ConvertKit. The account is no longer connected.', 'fl-builder' );
  165. } else {
  166. $api = $this->get_api( $account_data['api_key'] );
  167. $data = array(
  168. 'email' => $email,
  169. );
  170. if ( ! empty( $name ) ) {
  171. $data['fname'] = $name;
  172. }
  173. $result = $api->form_subscribe( $settings->list_id, $data );
  174. if ( isset( $result->error ) ) {
  175. $message = isset( $result->message ) ? $result->message : '';
  176. $response['error'] = sprintf(
  177. __( 'There was an error subscribing to ConvertKit. Error: %s', 'fl-builder' ),
  178. $message
  179. );
  180. }
  181. }
  182. return $response;
  183. }
  184. }