class-fl-builder-service-activecampaign.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * Helper class for the ActiveCampaign API.
  4. *
  5. * @since 1.6.0
  6. */
  7. final class FLBuilderServiceActiveCampaign extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.6.0
  12. * @var string $id
  13. */
  14. public $id = 'activecampaign';
  15. /**
  16. * @since 1.6.0
  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.6.0
  25. * @param string $api_url A valid API url.
  26. * @param string $api_key A valid API key.
  27. * @return object The API instance.
  28. */
  29. public function get_api( $api_url, $api_key ) {
  30. if ( $this->api_instance ) {
  31. return $this->api_instance;
  32. }
  33. if ( ! class_exists( 'ActiveCampaign' ) ) {
  34. require_once FL_BUILDER_DIR . 'includes/vendor/activecampaign/ActiveCampaign.class.php';
  35. }
  36. $this->api_instance = new ActiveCampaign( $api_url, $api_key );
  37. return $this->api_instance;
  38. }
  39. /**
  40. * Test the API connection.
  41. *
  42. * @since 1.6.0
  43. * @param array $fields {
  44. * @type string $api_url A valid API url.
  45. * @type string $api_key A valid API key.
  46. * }
  47. * @return array{
  48. * @type bool|string $error The error message or false if no error.
  49. * @type array $data An array of data used to make the connection.
  50. * }
  51. */
  52. public function connect( $fields = array() ) {
  53. $response = array(
  54. 'error' => false,
  55. 'data' => array(),
  56. );
  57. // Make sure we have an API url.
  58. if ( ! isset( $fields['api_url'] ) || empty( $fields['api_url'] ) ) {
  59. $response['error'] = __( 'Error: You must provide an API URL.', 'fl-builder' );
  60. } elseif ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  61. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  62. } // Try to connect and store the connection data.
  63. else {
  64. $api = $this->get_api( $fields['api_url'], $fields['api_key'] );
  65. if ( ! (int) $api->credentials_test() ) {
  66. $response['error'] = __( 'Error: Please check your API URL and API key.', 'fl-builder' );
  67. } else {
  68. $response['data'] = array(
  69. 'api_url' => $fields['api_url'],
  70. 'api_key' => $fields['api_key'],
  71. );
  72. }
  73. }
  74. return $response;
  75. }
  76. /**
  77. * Renders the markup for the connection settings.
  78. *
  79. * @since 1.6.0
  80. * @return string The connection settings markup.
  81. */
  82. public function render_connect_settings() {
  83. ob_start();
  84. FLBuilder::render_settings_field( 'api_url', array(
  85. 'row_class' => 'fl-builder-service-connect-row',
  86. 'class' => 'fl-builder-service-connect-input',
  87. 'type' => 'text',
  88. 'label' => __( 'API URL', 'fl-builder' ),
  89. 'help' => __( 'Your API url can be found in your ActiveCampaign account under My Settings > Developer > API.', 'fl-builder' ),
  90. 'preview' => array(
  91. 'type' => 'none',
  92. ),
  93. ));
  94. FLBuilder::render_settings_field( 'api_key', array(
  95. 'row_class' => 'fl-builder-service-connect-row',
  96. 'class' => 'fl-builder-service-connect-input',
  97. 'type' => 'text',
  98. 'label' => __( 'API Key', 'fl-builder' ),
  99. 'help' => __( 'Your API key can be found in your ActiveCampaign account under My Settings > Developer > API.', 'fl-builder' ),
  100. 'preview' => array(
  101. 'type' => 'none',
  102. ),
  103. ));
  104. return ob_get_clean();
  105. }
  106. /**
  107. * Render the markup for service specific fields.
  108. *
  109. * @since 1.6.0
  110. * @param string $account The name of the saved account.
  111. * @param object $settings Saved module settings.
  112. * @return array {
  113. * @type bool|string $error The error message or false if no error.
  114. * @type string $html The field markup.
  115. * }
  116. */
  117. public function render_fields( $account, $settings ) {
  118. $post_data = FLBuilderModel::get_post_data();
  119. $account_data = $this->get_account_data( $account );
  120. $api = $this->get_api( $account_data['api_url'], $account_data['api_key'] );
  121. $response = array(
  122. 'error' => false,
  123. 'html' => '',
  124. );
  125. if ( ! isset( $post_data['list_type'] ) ) {
  126. $response['html'] = $this->render_list_type_field( $settings );
  127. }
  128. $lists = $api->api( 'list/list?ids=all' );
  129. $render_type_html = $this->render_list_field( $lists, $settings );
  130. if ( isset( $post_data['list_type'] ) || isset( $settings->list_type ) ) {
  131. $list_type = isset( $post_data['list_type'] ) ? $post_data['list_type'] : $settings->list_type;
  132. if ( ! empty( $list_type ) && 'form' == $list_type ) {
  133. $forms = $api->api( 'form/getforms' );
  134. $render_type_html = $this->render_form_field( $forms, $settings );
  135. }
  136. }
  137. $response['html'] .= $render_type_html;
  138. if ( ! isset( $post_data['list_type'] ) ) {
  139. $response['html'] .= $this->render_tags_field( $settings );
  140. }
  141. return $response;
  142. }
  143. /**
  144. * Render markup for the list type.
  145. *
  146. * @since 1.8.3
  147. * @param object $settings Saved module settings.
  148. * @return string The markup for the list field.
  149. * @access private
  150. */
  151. private function render_list_type_field( $settings ) {
  152. ob_start();
  153. FLBuilder::render_settings_field( 'list_type', array(
  154. 'row_class' => 'fl-builder-service-field-row',
  155. 'class' => 'fl-builder-activecampaign-list_type-select',
  156. 'type' => 'select',
  157. 'label' => _x( 'Type', 'Select the list type.', 'fl-builder' ),
  158. 'default' => 'list',
  159. 'options' => array(
  160. 'list' => __( 'List', 'fl-builder' ),
  161. 'form' => __( 'Form', 'fl-builder' ),
  162. ),
  163. 'preview' => array(
  164. 'type' => 'none',
  165. ),
  166. ), $settings);
  167. return ob_get_clean();
  168. }
  169. /**
  170. * Render markup for the form field
  171. *
  172. * @since 1.8.3
  173. * @param array $forms Form data from the API.
  174. * @param object $settings Saved module settings.
  175. * @return string The markup for the form field.
  176. * @access private
  177. */
  178. private function render_form_field( $forms, $settings ) {
  179. ob_start();
  180. $options = array(
  181. '' => __( 'Choose...', 'fl-builder' ),
  182. );
  183. foreach ( (array) $forms as $form ) {
  184. if ( is_object( $form ) && isset( $form->id ) ) {
  185. $options[ $form->id ] = $form->name;
  186. }
  187. }
  188. FLBuilder::render_settings_field( 'form_id', array(
  189. 'row_class' => 'fl-builder-service-field-row',
  190. 'class' => 'fl-builder-service-list-select',
  191. 'type' => 'select',
  192. 'label' => _x( 'Form', 'Select a form a ActiveCampaign.', 'fl-builder' ),
  193. 'options' => $options,
  194. 'preview' => array(
  195. 'type' => 'none',
  196. ),
  197. ), $settings);
  198. return ob_get_clean();
  199. }
  200. /**
  201. * Render markup for the list field.
  202. *
  203. * @since 1.6.0
  204. * @param array $lists List data from the API.
  205. * @param object $settings Saved module settings.
  206. * @return string The markup for the list field.
  207. * @access private
  208. */
  209. private function render_list_field( $lists, $settings ) {
  210. ob_start();
  211. $options = array(
  212. '' => __( 'Choose...', 'fl-builder' ),
  213. );
  214. foreach ( (array) $lists as $list ) {
  215. if ( is_object( $list ) && isset( $list->id ) ) {
  216. $options[ $list->id ] = $list->name;
  217. }
  218. }
  219. FLBuilder::render_settings_field( 'list_id', array(
  220. 'row_class' => 'fl-builder-service-field-row',
  221. 'class' => 'fl-builder-service-list-select',
  222. 'type' => 'select',
  223. 'label' => _x( 'List', 'An email list from ActiveCampaign.', 'fl-builder' ),
  224. 'options' => $options,
  225. 'preview' => array(
  226. 'type' => 'none',
  227. ),
  228. ), $settings);
  229. return ob_get_clean();
  230. }
  231. /**
  232. * Render markup for the tags field.
  233. *
  234. * @since 1.8.8
  235. * @param object $settings Saved module settings.
  236. * @return string The markup for the tags field.
  237. * @access private
  238. */
  239. private function render_tags_field( $settings ) {
  240. ob_start();
  241. FLBuilder::render_settings_field( 'tags', array(
  242. 'row_class' => 'fl-builder-service-connect-row',
  243. 'class' => 'fl-builder-service-connect-input',
  244. 'type' => 'text',
  245. 'default' => '',
  246. 'label' => _x( 'Tags', 'A comma separated list of tags.', 'fl-builder' ),
  247. 'help' => __( 'A comma separated list of tags.', 'fl-builder' ),
  248. 'preview' => array(
  249. 'type' => 'none',
  250. ),
  251. ),$settings);
  252. return ob_get_clean();
  253. }
  254. /**
  255. * Subscribe an email address to ActiveCampaign.
  256. *
  257. * @since 1.6.0
  258. * @since 1.8.6 Changed contact_add method to contact_sync
  259. * @param object $settings A module settings object.
  260. * @param string $email The email to subscribe.
  261. * @param string $name Optional. The full name of the person subscribing.
  262. * @return array {
  263. * @type bool|string $error The error message or false if no error.
  264. * }
  265. */
  266. public function subscribe( $settings, $email, $name = false ) {
  267. $account_data = $this->get_account_data( $settings->service_account );
  268. $response = array(
  269. 'error' => false,
  270. );
  271. if ( ! $account_data ) {
  272. $response['error'] = __( 'There was an error subscribing to ActiveCampaign. The account is no longer connected.', 'fl-builder' );
  273. } else {
  274. $api = $this->get_api( $account_data['api_url'], $account_data['api_key'] );
  275. $data['email'] = $email;
  276. if ( isset( $settings->list_type ) && 'form' == $settings->list_type ) {
  277. $data['form'] = $settings->form_id;
  278. } else {
  279. $data['p'] = array( $settings->list_id );
  280. $data['status'] = array(
  281. $settings->list_id => 1,
  282. );
  283. $data['instantresponders'] = array(
  284. $settings->list_id => 1,
  285. );
  286. }
  287. // Name
  288. if ( $name ) {
  289. $names = explode( ' ', $name );
  290. if ( isset( $names[0] ) ) {
  291. $data['first_name'] = $names[0];
  292. }
  293. if ( isset( $names[1] ) ) {
  294. $data['last_name'] = $names[1];
  295. }
  296. }
  297. // Tags
  298. if ( isset( $settings->tags ) && ! empty( $settings->tags ) ) {
  299. $data['tags'] = $settings->tags;
  300. }
  301. // Subscribe
  302. $result = $api->api( 'contact/sync', $data );
  303. if ( ! $result->success && isset( $result->error ) ) {
  304. if ( stristr( $result->error, 'access' ) ) {
  305. $response['error'] = __( 'Error: Invalid API data.', 'fl-builder' );
  306. } else {
  307. $response['error'] = $result->error;
  308. }
  309. }
  310. }
  311. return $response;
  312. }
  313. }