class-fl-builder-service-icontact-pro.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * Helper class for the iContact PRO API.
  4. *
  5. * @since 1.10.6
  6. */
  7. final class FLBuilderServiceIContactPro extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.10.6
  12. * @var string $id
  13. */
  14. public $id = 'icontact-pro';
  15. /**
  16. * @since 1.10.6
  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.10.6
  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. * @type string $company_id A valid iContact Pro Company ID.
  30. * @type string $profile_id A valid iContact Pro Profile ID.
  31. * }
  32. * @return object The API instance.
  33. */
  34. public function get_api( $data ) {
  35. if ( $this->api_instance ) {
  36. return $this->api_instance;
  37. }
  38. if ( ! class_exists( 'iContactProApi' ) ) {
  39. require_once FL_BUILDER_DIR . 'includes/vendor/icontact/iContactProApi.php';
  40. }
  41. iContactProApi::getInstance()->setConfig( $data );
  42. $this->api_instance = iContactProApi::getInstance();
  43. return $this->api_instance;
  44. }
  45. /**
  46. * Test the API connection.
  47. *
  48. * @since 1.10.6
  49. * @param array $fields {
  50. * @type string $username A valid username.
  51. * @type string $app_id A valid app ID.
  52. * @type string $app_password A valid app password.
  53. * @type string $company_id A valid iContact Pro Company ID.
  54. * @type string $profile_id A valid iContact Pro Profile ID.
  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 a username.
  67. if ( ! isset( $fields['username'] ) || empty( $fields['username'] ) ) {
  68. $response['error'] = __( 'Error: You must provide a username.', '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. } // Make sure we have an app password.
  72. elseif ( ! isset( $fields['app_password'] ) || empty( $fields['app_password'] ) ) {
  73. $response['error'] = __( 'Error: You must provide an app password.', 'fl-builder' );
  74. } // Make sure we have a company ID
  75. elseif ( ! isset( $fields['company_id'] ) || empty( $fields['company_id'] ) ) {
  76. $response['error'] = __( 'Error: You must provide a company ID.', 'fl-builder' );
  77. } // Make sure we have a profile ID
  78. elseif ( ! isset( $fields['profile_id'] ) || empty( $fields['profile_id'] ) ) {
  79. $response['error'] = __( 'Error: You must provide a profile ID.', 'fl-builder' );
  80. } // Try to connect and store the connection data.
  81. else {
  82. $api = $this->get_api( array(
  83. 'apiUsername' => $fields['username'],
  84. 'appId' => $fields['app_id'],
  85. 'apiPassword' => $fields['app_password'],
  86. 'companyId' => $fields['company_id'],
  87. 'profileId' => $fields['profile_id'],
  88. ));
  89. try {
  90. $api->getLists();
  91. $response['data'] = array(
  92. 'username' => $fields['username'],
  93. 'app_id' => $fields['app_id'],
  94. 'app_password' => $fields['app_password'],
  95. 'company_id' => $fields['company_id'],
  96. 'profile_id' => $fields['profile_id'],
  97. );
  98. } catch ( Exception $e ) {
  99. $errors = $api->getErrors();
  100. $response['error'] = sprintf( __( 'Error: Could not connect to iContact Pro. %s', 'fl-builder' ), $errors[0] );
  101. }
  102. }
  103. return $response;
  104. }
  105. /**
  106. * Renders the markup for the connection settings.
  107. *
  108. * @since 1.10.6
  109. * @return string The connection settings markup.
  110. */
  111. public function render_connect_settings() {
  112. ob_start();
  113. FLBuilder::render_settings_field( 'username', array(
  114. 'row_class' => 'fl-builder-service-connect-row',
  115. 'class' => 'fl-builder-service-connect-input',
  116. 'type' => 'text',
  117. 'label' => __( 'Username', 'fl-builder' ),
  118. 'help' => __( 'Your iContact Pro username.', 'fl-builder' ),
  119. 'preview' => array(
  120. 'type' => 'none',
  121. ),
  122. ));
  123. FLBuilder::render_settings_field( 'app_id', array(
  124. 'row_class' => 'fl-builder-service-connect-row',
  125. 'class' => 'fl-builder-service-connect-input',
  126. 'type' => 'text',
  127. 'label' => __( 'App ID', 'fl-builder' ),
  128. 'help' => __( 'Your iContact Pro app ID.', 'fl-builder' ),
  129. 'preview' => array(
  130. 'type' => 'none',
  131. ),
  132. ));
  133. FLBuilder::render_settings_field( 'app_password', array(
  134. 'row_class' => 'fl-builder-service-connect-row',
  135. 'class' => 'fl-builder-service-connect-input',
  136. 'type' => 'text',
  137. 'label' => __( 'App Password', 'fl-builder' ),
  138. 'help' => __( 'Your iContact Pro app password.', 'fl-builder' ),
  139. 'description' => sprintf( __( 'You must <a%1$s>create an app</a> in iContact Pro to obtain an app ID and password. Please see <a%2$s>the iContact docs</a> for complete instructions.', 'fl-builder' ), ' href="https://app.icontactpro.com/MKT/Settings/Api?returnUrl=/MKT/Settings" target="_blank"', ' href="http://www.icontact.com/developerportal/api-documentation/vocus-register-your-app/" target="_blank"' ),
  140. 'preview' => array(
  141. 'type' => 'none',
  142. ),
  143. ));
  144. FLBuilder::render_settings_field( 'company_id', array(
  145. 'row_class' => 'fl-builder-service-connect-row',
  146. 'class' => 'fl-builder-service-connect-input',
  147. 'type' => 'text',
  148. 'label' => __( 'Company ID', 'fl-builder' ),
  149. 'help' => __( 'Your iContact Pro Company ID.', 'fl-builder' ),
  150. 'preview' => array(
  151. 'type' => 'none',
  152. ),
  153. ));
  154. FLBuilder::render_settings_field( 'profile_id', array(
  155. 'row_class' => 'fl-builder-service-connect-row',
  156. 'class' => 'fl-builder-service-connect-input',
  157. 'type' => 'text',
  158. 'label' => __( 'Profile ID', 'fl-builder' ),
  159. 'help' => __( 'Your iContact Pro Profile ID.', 'fl-builder' ),
  160. 'description' => sprintf( __( 'Your Company and Profile ID can also be found in the <a%1$s>iContact Pro API settings</a> under Account Information.', 'fl-builder' ), ' href="https://app.icontactpro.com/MKT/Settings/Api?returnUrl=/MKT/Settings" target="_blank"' ),
  161. 'preview' => array(
  162. 'type' => 'none',
  163. ),
  164. ));
  165. return ob_get_clean();
  166. }
  167. /**
  168. * Render the markup for service specific fields.
  169. *
  170. * @since 1.10.6
  171. * @param string $account The name of the saved account.
  172. * @param object $settings Saved module settings.
  173. * @return array {
  174. * @type bool|string $error The error message or false if no error.
  175. * @type string $html The field markup.
  176. * }
  177. */
  178. public function render_fields( $account, $settings ) {
  179. $account_data = $this->get_account_data( $account );
  180. $api = $this->get_api( array(
  181. 'apiUsername' => $account_data['username'],
  182. 'appId' => $account_data['app_id'],
  183. 'apiPassword' => $account_data['app_password'],
  184. 'companyId' => $account_data['company_id'],
  185. 'profileId' => $account_data['profile_id'],
  186. ));
  187. $response = array(
  188. 'error' => false,
  189. 'html' => '',
  190. );
  191. try {
  192. $lists = $api->getLists();
  193. $response['html'] = $this->render_list_field( $lists, $settings );
  194. } catch ( Exception $e ) {
  195. $errors = $api->getErrors();
  196. $response['error'] = sprintf( __( 'Error: Could not connect to iContact Pro. %s', 'fl-builder' ), $errors[0] );
  197. }
  198. return $response;
  199. }
  200. /**
  201. * Render markup for the list field.
  202. *
  203. * @since 1.10.6
  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 ( $lists as $id => $list ) {
  215. $options[ $list->listId ] = $list->name; // @codingStandardsIgnoreLine
  216. }
  217. FLBuilder::render_settings_field( 'list_id', array(
  218. 'row_class' => 'fl-builder-service-field-row',
  219. 'class' => 'fl-builder-service-list-select',
  220. 'type' => 'select',
  221. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  222. 'options' => $options,
  223. 'preview' => array(
  224. 'type' => 'none',
  225. ),
  226. ), $settings);
  227. return ob_get_clean();
  228. }
  229. /**
  230. * Subscribe an email address to iContact.
  231. *
  232. * @since 1.10.6
  233. * @param object $settings A module settings object.
  234. * @param string $email The email to subscribe.
  235. * @param string $name Optional. The full name of the person subscribing.
  236. * @return array {
  237. * @type bool|string $error The error message or false if no error.
  238. * }
  239. */
  240. public function subscribe( $settings, $email, $name = false ) {
  241. $account_data = $this->get_account_data( $settings->service_account );
  242. $response = array(
  243. 'error' => false,
  244. );
  245. if ( ! $account_data ) {
  246. $response['error'] = __( 'There was an error subscribing to iContact. The account is no longer connected.', 'fl-builder' );
  247. } else {
  248. $data = array(
  249. 'email' => $email,
  250. );
  251. $api = $this->get_api( array(
  252. 'apiUsername' => $account_data['username'],
  253. 'appId' => $account_data['app_id'],
  254. 'apiPassword' => $account_data['app_password'],
  255. 'companyId' => $account_data['company_id'],
  256. 'profileId' => $account_data['profile_id'],
  257. ));
  258. try {
  259. if ( $name ) {
  260. $names = explode( ' ', $name );
  261. $data['first_name'] = null;
  262. $data['last_name'] = null;
  263. if ( isset( $names[0] ) ) {
  264. $data['first_name'] = $names[0];
  265. }
  266. if ( isset( $names[1] ) ) {
  267. $data['last_name'] = $names[1];
  268. }
  269. $result = $api->addContact( $data['email'], 'normal', null, $data['first_name'], $data['last_name'] );
  270. } else {
  271. $result = $api->addContact( $data['email'] );
  272. }
  273. $api->subscribeContactToList( $result->contactId, $settings->list_id ); // @codingStandardsIgnoreLine
  274. } catch ( Exception $e ) {
  275. $errors = $api->getErrors();
  276. $response['error'] = sprintf( __( 'There was an error subscribing to iContact Pro. %s', 'fl-builder' ), $errors[0] );
  277. }
  278. }
  279. return $response;
  280. }
  281. }