class-fl-builder-service-infusionsoft.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Helper class for the Infusionsoft API.
  4. *
  5. * @since 1.5.7
  6. */
  7. final class FLBuilderServiceInfusionsoft extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.7
  12. * @var string $id
  13. */
  14. public $id = 'infusionsoft';
  15. /**
  16. * @since 1.5.7
  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.7
  25. * @param string $app_id A valid app ID.
  26. * @param string $api_key A valid API key.
  27. * @return object The API instance.
  28. */
  29. public function get_api( $app_id, $api_key ) {
  30. if ( $this->api_instance ) {
  31. return $this->api_instance;
  32. }
  33. if ( ! class_exists( 'iSDK' ) ) {
  34. require_once FL_BUILDER_DIR . 'includes/vendor/infusionsoft/isdk.php';
  35. }
  36. try {
  37. $this->api_instance = new iSDK();
  38. $this->api_instance->cfgCon( $app_id, $api_key, 'throw' );
  39. } catch ( iSDKException $e ) {
  40. $this->api_instance = new stdClass();
  41. $this->api_instance->error = sprintf(
  42. __( 'There was an error connecting to Infusionsoft. %s', 'fl-builder' ),
  43. $e->getMessage()
  44. );
  45. }
  46. return $this->api_instance;
  47. }
  48. /**
  49. * Test the API connection.
  50. *
  51. * @since 1.5.7
  52. * @param array $fields {
  53. * @type string $app_id A valid app ID.
  54. * @type string $api_key A valid API key.
  55. * }
  56. * @return array{
  57. * @type bool|string $error The error message or false if no error.
  58. * @type array $data An array of data used to make the connection.
  59. * }
  60. */
  61. public function connect( $fields = array() ) {
  62. $response = array(
  63. 'error' => false,
  64. 'data' => array(),
  65. );
  66. // Make sure we have an API key.
  67. if ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  68. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  69. } elseif ( ! isset( $fields['app_id'] ) || empty( $fields['app_id'] ) ) {
  70. $response['error'] = __( 'Error: You must provide an app ID.', 'fl-builder' );
  71. } // Try to connect and store the connection data.
  72. else {
  73. $api = $this->get_api( $fields['app_id'], $fields['api_key'] );
  74. if ( isset( $api->error ) ) {
  75. $response['error'] = $api->error;
  76. } else {
  77. $response['data'] = array(
  78. 'app_id' => $fields['app_id'],
  79. 'api_key' => $fields['api_key'],
  80. );
  81. }
  82. }
  83. return $response;
  84. }
  85. /**
  86. * Renders the markup for the connection settings.
  87. *
  88. * @since 1.5.7
  89. * @return string The connection settings markup.
  90. */
  91. public function render_connect_settings() {
  92. ob_start();
  93. FLBuilder::render_settings_field( 'app_id', array(
  94. 'row_class' => 'fl-builder-service-connect-row',
  95. 'class' => 'fl-builder-service-connect-input',
  96. 'type' => 'text',
  97. 'label' => __( 'App ID', 'fl-builder' ),
  98. 'help' => __( 'Your App ID can be found in the URL for your account. For example, if the URL for your account is myaccount.infusionsoft.com, your App ID would be <strong>myaccount</strong>.', 'fl-builder' ),
  99. 'preview' => array(
  100. 'type' => 'none',
  101. ),
  102. ));
  103. FLBuilder::render_settings_field( 'api_key', array(
  104. 'row_class' => 'fl-builder-service-connect-row',
  105. 'class' => 'fl-builder-service-connect-input',
  106. 'type' => 'text',
  107. 'label' => __( 'API Key', 'fl-builder' ),
  108. 'help' => __( 'Your API key can be found in your Infusionsoft account under Admin > Settings > Application > API > Encrypted Key.', 'fl-builder' ),
  109. 'preview' => array(
  110. 'type' => 'none',
  111. ),
  112. ));
  113. return ob_get_clean();
  114. }
  115. /**
  116. * Render the markup for service specific fields.
  117. *
  118. * @since 1.5.7
  119. * @param string $account The name of the saved account.
  120. * @param object $settings Saved module settings.
  121. * @return array {
  122. * @type bool|string $error The error message or false if no error.
  123. * @type string $html The field markup.
  124. * }
  125. */
  126. public function render_fields( $account, $settings ) {
  127. $account_data = $this->get_account_data( $account );
  128. $api = $this->get_api( $account_data['app_id'], $account_data['api_key'] );
  129. $page = 0;
  130. $lists = array();
  131. $response = array(
  132. 'error' => false,
  133. 'html' => '',
  134. );
  135. if ( isset( $api->error ) ) {
  136. $response['error'] = $api->error;
  137. } else {
  138. while ( true ) {
  139. $result = $api->dsQuery(
  140. 'ContactGroup',
  141. 1000,
  142. $page,
  143. array(
  144. 'Id' => '%',
  145. ),
  146. array( 'Id', 'GroupName' )
  147. );
  148. $lists = array_merge( $lists, $result );
  149. if ( count( $result ) < 1000 ) {
  150. break;
  151. }
  152. $page ++;
  153. }
  154. $response['html'] = $this->render_list_field( $lists, $settings );
  155. }
  156. return $response;
  157. }
  158. /**
  159. * Render markup for the list field.
  160. *
  161. * @since 1.5.7
  162. * @param array $lists List data from the API.
  163. * @param object $settings Saved module settings.
  164. * @return string The markup for the list field.
  165. * @access private
  166. */
  167. private function render_list_field( $lists, $settings ) {
  168. ob_start();
  169. $options = array(
  170. '' => __( 'Choose...', 'fl-builder' ),
  171. );
  172. foreach ( $lists as $list ) {
  173. $options[ $list['Id'] ] = $list['GroupName'];
  174. }
  175. FLBuilder::render_settings_field( 'list_id', array(
  176. 'row_class' => 'fl-builder-service-field-row',
  177. 'class' => 'fl-builder-service-list-select',
  178. 'type' => 'select',
  179. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  180. 'options' => $options,
  181. 'preview' => array(
  182. 'type' => 'none',
  183. ),
  184. ), $settings);
  185. return ob_get_clean();
  186. }
  187. /**
  188. * Subscribe an email address to Infusionsoft.
  189. *
  190. * @since 1.5.7
  191. * @param object $settings A module settings object.
  192. * @param string $email The email to subscribe.
  193. * @param string $name Optional. The full name of the person subscribing.
  194. * @return array {
  195. * @type bool|string $error The error message or false if no error.
  196. * }
  197. */
  198. public function subscribe( $settings, $email, $name = false ) {
  199. $account_data = $this->get_account_data( $settings->service_account );
  200. $response = array(
  201. 'error' => false,
  202. );
  203. $data = array();
  204. if ( ! $account_data ) {
  205. $response['error'] = __( 'There was an error subscribing to Infusionsoft. The account is no longer connected.', 'fl-builder' );
  206. } else {
  207. $api = $this->get_api( $account_data['app_id'], $account_data['api_key'] );
  208. if ( isset( $api->error ) ) {
  209. $response['error'] = $api->error;
  210. } else {
  211. if ( $name ) {
  212. $names = explode( ' ', $name );
  213. if ( isset( $names[0] ) && isset( $names[1] ) ) {
  214. $data = array(
  215. 'FirstName' => $names[0],
  216. 'LastName' => $names[1],
  217. 'Email' => $email,
  218. );
  219. } else {
  220. $data = array(
  221. 'FirstName' => $name,
  222. 'Email' => $email,
  223. );
  224. }
  225. } else {
  226. $data = array(
  227. 'Email' => $email,
  228. );
  229. }
  230. try {
  231. $contact = $api->findByEmail( $email, array( 'Id' ) );
  232. if ( isset( $contact[0] ) && ! empty( $contact[0]['Id'] ) ) {
  233. $contact_id = $contact[0]['Id'];
  234. $api->updateCon( $contact_id, $data );
  235. $group = $api->grpAssign( $contact[0]['Id'], $settings->list_id );
  236. } else {
  237. $contact_id = $api->addCon( $data );
  238. $group_add = $api->grpAssign( $contact_id, $settings->list_id );
  239. }
  240. } catch ( iSDKException $e ) {
  241. $response['error'] = sprintf(
  242. __( 'There was an error subscribing to Infusionsoft. %s', 'fl-builder' ),
  243. $e->getMessage()
  244. );
  245. }
  246. }
  247. }
  248. return $response;
  249. }
  250. }