class-fl-builder-service-drip.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * Helper class for the Drip API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceDrip extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'drip';
  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 token.
  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( 'Drip_Api' ) ) {
  33. require_once FL_BUILDER_DIR . 'includes/vendor/drip/Drip_API.class.php';
  34. }
  35. $this->api_instance = new Drip_Api( $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 token.
  56. if ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  57. $response['error'] = __( 'Error: You must provide an API token.', 'fl-builder' );
  58. } elseif ( ! isset( $fields['api_account_id'] ) || empty( $fields['api_account_id'] ) ) {
  59. $response['error'] = __( 'Error: You must provide an Account ID.', 'fl-builder' );
  60. } // Try to connect and store the connection data.
  61. else {
  62. try {
  63. $api = $this->get_api( $fields['api_key'] );
  64. try {
  65. $account = $api->fetch_account( $fields['api_account_id'] );
  66. $error_message = $api->get_error_message();
  67. if ( ! empty( $error_message ) ) {
  68. $response['error'] = $error_message;
  69. } else {
  70. $response['data'] = array(
  71. 'api_key' => $fields['api_key'],
  72. 'api_account_id' => $fields['api_account_id'],
  73. );
  74. }
  75. } catch ( Exception $e ) {
  76. $response['error'] = sprintf(
  77. __( 'Error: Please check your Account ID. %s', 'fl-builder' ),
  78. $e->getMessage()
  79. );
  80. }
  81. } catch ( Exception $e ) {
  82. $response['error'] = sprintf(
  83. __( 'Error: Please check your API token. %s', 'fl-builder' ),
  84. $e->getMessage()
  85. );
  86. }
  87. }
  88. return $response;
  89. }
  90. /**
  91. * Renders the markup for the connection settings.
  92. *
  93. * @since 1.5.4
  94. * @return string The connection settings markup.
  95. */
  96. public function render_connect_settings() {
  97. ob_start();
  98. FLBuilder::render_settings_field( 'api_key', array(
  99. 'row_class' => 'fl-builder-service-connect-row',
  100. 'class' => 'fl-builder-service-connect-input',
  101. 'type' => 'text',
  102. 'label' => __( 'API Token', 'fl-builder' ),
  103. 'description' => sprintf( __( 'Your API Token can be found in your Drip account under Settings > My User Settings. Or, you can click this <a%s>direct link</a>.', 'fl-builder' ), ' href="https://www.getdrip.com/user/edit" target="_blank"' ),
  104. 'preview' => array(
  105. 'type' => 'none',
  106. ),
  107. ));
  108. FLBuilder::render_settings_field( 'api_account_id', array(
  109. 'row_class' => 'fl-builder-service-connect-row',
  110. 'class' => 'fl-builder-service-connect-input',
  111. 'type' => 'text',
  112. 'label' => __( 'Account ID', 'fl-builder' ),
  113. 'help' => __( 'Your Account ID can be found in your Drip account under Settings > Site Setup.', 'fl-builder' ),
  114. 'preview' => array(
  115. 'type' => 'none',
  116. ),
  117. ));
  118. return ob_get_clean();
  119. }
  120. /**
  121. * Render the markup for service specific fields.
  122. *
  123. * @since 1.5.4
  124. * @param string $account The name of the saved account.
  125. * @param object $settings Saved module settings.
  126. * @return array {
  127. * @type bool|string $error The error message or false if no error.
  128. * @type string $html The field markup.
  129. * }
  130. */
  131. public function render_fields( $account, $settings ) {
  132. $account_data = $this->get_account_data( $account );
  133. $api = $this->get_api( $account_data['api_key'] );
  134. $campaigns = $api->get_campaigns( array(
  135. 'account_id' => $account_data['api_account_id'],
  136. ) );
  137. $response = array(
  138. 'error' => false,
  139. 'html' => $this->render_campaigns_field( $campaigns, $settings ) . $this->render_tag_field( $settings ),
  140. );
  141. return $response;
  142. }
  143. /**
  144. * Render markup for the campaign field.
  145. *
  146. * @since 1.10.5
  147. * @param array $campaigns Campaigns data from the API.
  148. * @param object $settings Saved module settings.
  149. * @return string The markup for the campaign field.
  150. * @access private
  151. */
  152. private function render_campaigns_field( $campaigns, $settings ) {
  153. ob_start();
  154. $options = array(
  155. '' => __( 'Choose...', 'fl-builder' ),
  156. );
  157. foreach ( $campaigns as $campaign ) {
  158. $options[ $campaign['id'] ] = $campaign['name'];
  159. }
  160. FLBuilder::render_settings_field( 'campaign_id', array(
  161. 'row_class' => 'fl-builder-service-field-row',
  162. 'class' => 'fl-builder-service-campaign-select',
  163. 'type' => 'select',
  164. 'label' => _x( 'Campaign', 'An email campaign from your GetDrip account.', 'fl-builder' ),
  165. 'options' => $options,
  166. 'preview' => array(
  167. 'type' => 'none',
  168. ),
  169. ), $settings);
  170. return ob_get_clean();
  171. }
  172. /**
  173. * Render markup for the tag field.
  174. *
  175. * @since 1.5.4
  176. * @param object $settings Saved module settings.
  177. * @return string The markup for the tag field.
  178. * @access private
  179. */
  180. private function render_tag_field( $settings ) {
  181. ob_start();
  182. FLBuilder::render_settings_field( 'list_id', array(
  183. 'row_class' => 'fl-builder-service-field-row',
  184. 'class' => 'fl-builder-service-list-select',
  185. 'type' => 'text',
  186. 'label' => _x( 'Tags', 'A tag to add to contacts in Drip when they subscribe.', 'fl-builder' ),
  187. 'help' => __( 'For multiple tags, separate with comma.', 'fl-builder' ),
  188. 'preview' => array(
  189. 'type' => 'none',
  190. ),
  191. ), $settings);
  192. return ob_get_clean();
  193. }
  194. /**
  195. * Subscribe an email address to Drip.
  196. *
  197. * @since 1.5.4
  198. * @param object $settings A module settings object.
  199. * @param string $email The email to subscribe.
  200. * @param string $name Optional. The full name of the person subscribing.
  201. * @return array {
  202. * @type bool|string $error The error message or false if no error.
  203. * }
  204. */
  205. public function subscribe( $settings, $email, $name = '' ) {
  206. $account_data = $this->get_account_data( $settings->service_account );
  207. $response = array(
  208. 'error' => false,
  209. );
  210. $subscriber_id = null;
  211. if ( ! $account_data ) {
  212. $response['error'] = __( 'There was an error subscribing to Drip. The account is no longer connected.', 'fl-builder' );
  213. } else {
  214. $api = $this->get_api( $account_data['api_key'] );
  215. $args = array(
  216. 'account_id' => $account_data['api_account_id'],
  217. 'email' => $email,
  218. );
  219. // Check if the contact already exists
  220. try {
  221. $result = $api->fetch_subscriber( $args );
  222. if ( $result && isset( $result['id'] ) ) {
  223. $subscriber_id = $result['id'];
  224. }
  225. } catch ( Exception $e ) {
  226. $response['error'] = sprintf(
  227. __( 'There was an error searching contact from Drip. %s', 'fl-builder' ),
  228. $e->getMessage()
  229. );
  230. return $response;
  231. }
  232. if ( $subscriber_id ) {
  233. $args['user_id'] = $subscriber_id;
  234. }
  235. if ( $settings->list_id ) {
  236. $args['tags'] = explode( ',', $settings->list_id );
  237. }
  238. if ( $name ) {
  239. $args['custom_fields'] = array(
  240. 'name' => $name,
  241. );
  242. }
  243. // Create or update contact
  244. try {
  245. $result = $api->create_or_update_subscriber( $args );
  246. if ( isset( $result['id'] ) && isset( $settings->campaign_id ) ) {
  247. $args['campaign_id'] = $settings->campaign_id;
  248. $args['double_optin'] = false;
  249. $get_res = $api->subscribe_subscriber( $args );
  250. }
  251. } catch ( Exception $e ) {
  252. $response['error'] = sprintf(
  253. __( 'There was an error subscribing to Drip. %s', 'fl-builder' ),
  254. $e->getMessage()
  255. );
  256. }
  257. }
  258. return $response;
  259. }
  260. }