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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * Helper class for the iContact API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceIContact extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'icontact';
  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 array $data {
  26. * @type string $username A valid username.
  27. * @type string $app_id A valid app ID.
  28. * @type string $app_password A valid app password.
  29. * }
  30. * @return object The API instance.
  31. */
  32. public function get_api( $data ) {
  33. if ( $this->api_instance ) {
  34. return $this->api_instance;
  35. }
  36. if ( ! class_exists( 'iContactApi' ) ) {
  37. require_once FL_BUILDER_DIR . 'includes/vendor/icontact/iContactApi.php';
  38. }
  39. iContactApi::getInstance()->setConfig( $data );
  40. $this->api_instance = iContactApi::getInstance();
  41. return $this->api_instance;
  42. }
  43. /**
  44. * Test the API connection.
  45. *
  46. * @since 1.5.4
  47. * @param array $fields {
  48. * @type string $username A valid username.
  49. * @type string $app_id A valid app ID.
  50. * @type string $app_password A valid app password.
  51. * }
  52. * @return array{
  53. * @type bool|string $error The error message or false if no error.
  54. * @type array $data An array of data used to make the connection.
  55. * }
  56. */
  57. public function connect( $fields = array() ) {
  58. $response = array(
  59. 'error' => false,
  60. 'data' => array(),
  61. );
  62. // Make sure we have a username.
  63. if ( ! isset( $fields['username'] ) || empty( $fields['username'] ) ) {
  64. $response['error'] = __( 'Error: You must provide a username.', 'fl-builder' );
  65. } elseif ( ! isset( $fields['app_id'] ) || empty( $fields['app_id'] ) ) {
  66. $response['error'] = __( 'Error: You must provide a app ID.', 'fl-builder' );
  67. } // Make sure we have an app password.
  68. elseif ( ! isset( $fields['app_password'] ) || empty( $fields['app_password'] ) ) {
  69. $response['error'] = __( 'Error: You must provide a app password.', 'fl-builder' );
  70. } // Try to connect and store the connection data.
  71. else {
  72. $api = $this->get_api( array(
  73. 'apiUsername' => $fields['username'],
  74. 'appId' => $fields['app_id'],
  75. 'apiPassword' => $fields['app_password'],
  76. ));
  77. try {
  78. $api->getLists();
  79. $response['data'] = array(
  80. 'username' => $fields['username'],
  81. 'app_id' => $fields['app_id'],
  82. 'app_password' => $fields['app_password'],
  83. );
  84. } catch ( Exception $e ) {
  85. $errors = $api->getErrors();
  86. $response['error'] = sprintf( __( 'Error: Could not connect to iContact. %s', 'fl-builder' ), $errors[0] );
  87. }
  88. }
  89. return $response;
  90. }
  91. /**
  92. * Renders the markup for the connection settings.
  93. *
  94. * @since 1.5.4
  95. * @return string The connection settings markup.
  96. */
  97. public function render_connect_settings() {
  98. ob_start();
  99. FLBuilder::render_settings_field( 'username', array(
  100. 'row_class' => 'fl-builder-service-connect-row',
  101. 'class' => 'fl-builder-service-connect-input',
  102. 'type' => 'text',
  103. 'label' => __( 'Username', 'fl-builder' ),
  104. 'help' => __( 'Your iContact username.', 'fl-builder' ),
  105. 'preview' => array(
  106. 'type' => 'none',
  107. ),
  108. ));
  109. FLBuilder::render_settings_field( 'app_id', array(
  110. 'row_class' => 'fl-builder-service-connect-row',
  111. 'class' => 'fl-builder-service-connect-input',
  112. 'type' => 'text',
  113. 'label' => __( 'App ID', 'fl-builder' ),
  114. 'help' => __( 'Your iContact app ID.', 'fl-builder' ),
  115. 'preview' => array(
  116. 'type' => 'none',
  117. ),
  118. ));
  119. FLBuilder::render_settings_field( 'app_password', array(
  120. 'row_class' => 'fl-builder-service-connect-row',
  121. 'class' => 'fl-builder-service-connect-input',
  122. 'type' => 'text',
  123. 'label' => __( 'App Password', 'fl-builder' ),
  124. 'help' => __( 'Your iContact app password.', 'fl-builder' ),
  125. 'description' => sprintf( __( 'You must <a%1$s>create an app</a> in iContact to obtain an app ID and password. Please see <a%2$s>the iContact docs</a> for complete instructions.', 'fl-builder' ), ' href="https://app.icontact.com/icp/core/registerapp/" target="_blank"', ' href="http://www.icontact.com/developerportal/api-documentation/vocus-register-your-app/" target="_blank"' ),
  126. 'preview' => array(
  127. 'type' => 'none',
  128. ),
  129. ));
  130. return ob_get_clean();
  131. }
  132. /**
  133. * Render the markup for service specific fields.
  134. *
  135. * @since 1.5.4
  136. * @param string $account The name of the saved account.
  137. * @param object $settings Saved module settings.
  138. * @return array {
  139. * @type bool|string $error The error message or false if no error.
  140. * @type string $html The field markup.
  141. * }
  142. */
  143. public function render_fields( $account, $settings ) {
  144. $account_data = $this->get_account_data( $account );
  145. $api = $this->get_api( array(
  146. 'apiUsername' => $account_data['username'],
  147. 'appId' => $account_data['app_id'],
  148. 'apiPassword' => $account_data['app_password'],
  149. ));
  150. $response = array(
  151. 'error' => false,
  152. 'html' => '',
  153. );
  154. try {
  155. $lists = $api->getLists();
  156. $response['html'] = $this->render_list_field( $lists, $settings );
  157. } catch ( Exception $e ) {
  158. $errors = $api->getErrors();
  159. $response['error'] = sprintf( __( 'Error: Could not connect to iContact. %s', 'fl-builder' ), $errors[0] );
  160. }
  161. return $response;
  162. }
  163. /**
  164. * Render markup for the list field.
  165. *
  166. * @since 1.5.4
  167. * @param array $lists List data from the API.
  168. * @param object $settings Saved module settings.
  169. * @return string The markup for the list field.
  170. * @access private
  171. */
  172. private function render_list_field( $lists, $settings ) {
  173. ob_start();
  174. $options = array(
  175. '' => __( 'Choose...', 'fl-builder' ),
  176. );
  177. foreach ( $lists as $id => $list ) {
  178. // @codingStandardsIgnoreLine
  179. $options[ $list->listId ] = $list->name;
  180. }
  181. FLBuilder::render_settings_field( 'list_id', array(
  182. 'row_class' => 'fl-builder-service-field-row',
  183. 'class' => 'fl-builder-service-list-select',
  184. 'type' => 'select',
  185. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  186. 'options' => $options,
  187. 'preview' => array(
  188. 'type' => 'none',
  189. ),
  190. ), $settings);
  191. return ob_get_clean();
  192. }
  193. /**
  194. * Subscribe an email address to iContact.
  195. *
  196. * @since 1.5.4
  197. * @param object $settings A module settings object.
  198. * @param string $email The email to subscribe.
  199. * @param string $name Optional. The full name of the person subscribing.
  200. * @return array {
  201. * @type bool|string $error The error message or false if no error.
  202. * }
  203. */
  204. public function subscribe( $settings, $email, $name = false ) {
  205. $account_data = $this->get_account_data( $settings->service_account );
  206. $response = array(
  207. 'error' => false,
  208. );
  209. if ( ! $account_data ) {
  210. $response['error'] = __( 'There was an error subscribing to iContact. The account is no longer connected.', 'fl-builder' );
  211. } else {
  212. $data = array(
  213. 'email' => $email,
  214. );
  215. $api = $this->get_api( array(
  216. 'apiUsername' => $account_data['username'],
  217. 'appId' => $account_data['app_id'],
  218. 'apiPassword' => $account_data['app_password'],
  219. ));
  220. try {
  221. if ( $name ) {
  222. $names = explode( ' ', $name );
  223. $data['first_name'] = null;
  224. $data['last_name'] = null;
  225. if ( isset( $names[0] ) ) {
  226. $data['first_name'] = $names[0];
  227. }
  228. if ( isset( $names[1] ) ) {
  229. $data['last_name'] = $names[1];
  230. }
  231. $result = $api->addContact( $data['email'], 'normal', null, $data['first_name'], $data['last_name'] );
  232. } else {
  233. $result = $api->addContact( $data['email'] );
  234. }
  235. // @codingStandardsIgnoreLine
  236. $api->subscribeContactToList( $result->contactId, $settings->list_id );
  237. } catch ( Exception $e ) {
  238. $errors = $api->getErrors();
  239. $response['error'] = sprintf( __( 'There was an error subscribing to iContact. %s', 'fl-builder' ), $errors[0] );
  240. }
  241. }
  242. return $response;
  243. }
  244. }