class-fl-builder-service-hatchbuck.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Helper class for the Hatchbuck API.
  4. *
  5. * @since 1.5.8
  6. */
  7. final class FLBuilderServiceHatchbuck extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.8
  12. * @var string $id
  13. */
  14. public $id = 'hatchbuck';
  15. /**
  16. * The API url for this service.
  17. *
  18. * @since 1.5.8
  19. * @access private
  20. * @var string $api_url
  21. */
  22. private $api_url = 'https://api.hatchbuck.com/api/v1/contact/';
  23. /**
  24. * Test the API connection.
  25. *
  26. * @since 1.5.8
  27. * @param array $fields {
  28. * @type string $api_key A valid API key.
  29. * }
  30. * @return array{
  31. * @type bool|string $error The error message or false if no error.
  32. * @type array $data An array of data used to make the connection.
  33. * }
  34. */
  35. public function connect( $fields = array() ) {
  36. $response = array(
  37. 'error' => false,
  38. 'data' => array(),
  39. );
  40. // Make sure we have an API key.
  41. if ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  42. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  43. } else {
  44. $result = wp_remote_post( $this->api_url . 'search?api_key=' . $fields['api_key'], array(
  45. 'method' => 'POST',
  46. 'timeout' => 60,
  47. 'headers' => array(
  48. 'Content-Type' => 'application/json',
  49. ),
  50. 'body' => array(),
  51. ) );
  52. if ( 401 == $result['response']['code'] ) {
  53. $response['error'] = __( 'Error: Please check your API key.', 'fl-builder' );
  54. } else {
  55. $response['data'] = array(
  56. 'api_key' => $fields['api_key'],
  57. );
  58. }
  59. }
  60. return $response;
  61. }
  62. /**
  63. * Renders the markup for the connection settings.
  64. *
  65. * @since 1.5.8
  66. * @return string The connection settings markup.
  67. */
  68. public function render_connect_settings() {
  69. ob_start();
  70. FLBuilder::render_settings_field( 'api_key', array(
  71. 'row_class' => 'fl-builder-service-connect-row',
  72. 'class' => 'fl-builder-service-connect-input',
  73. 'type' => 'text',
  74. 'label' => __( 'API Key', 'fl-builder' ),
  75. 'help' => __( 'Your API key can be found in your Hatchbuck account under Account Settings > Web API.', 'fl-builder' ),
  76. 'preview' => array(
  77. 'type' => 'none',
  78. ),
  79. ));
  80. return ob_get_clean();
  81. }
  82. /**
  83. * Render the markup for service specific fields.
  84. *
  85. * @since 1.5.8
  86. * @param string $account The name of the saved account.
  87. * @param object $settings Saved module settings.
  88. * @return array {
  89. * @type bool|string $error The error message or false if no error.
  90. * @type string $html The field markup.
  91. * }
  92. */
  93. public function render_fields( $account, $settings ) {
  94. $response = array(
  95. 'error' => false,
  96. 'html' => $this->render_tag_field( $settings ),
  97. );
  98. return $response;
  99. }
  100. /**
  101. * Render markup for the tag field.
  102. *
  103. * @since 1.5.8
  104. * @param object $settings Saved module settings.
  105. * @return string The markup for the tag field.
  106. * @access private
  107. */
  108. private function render_tag_field( $settings ) {
  109. ob_start();
  110. FLBuilder::render_settings_field( 'list_id', array(
  111. 'row_class' => 'fl-builder-service-field-row',
  112. 'class' => 'fl-builder-service-list-select',
  113. 'type' => 'text',
  114. 'label' => _x( 'Tag', 'A tag to add to contacts in Hatchbuck when they subscribe.', 'fl-builder' ),
  115. 'preview' => array(
  116. 'type' => 'none',
  117. ),
  118. ), $settings);
  119. return ob_get_clean();
  120. }
  121. /**
  122. * Subscribe an email address to Hatchbuck.
  123. *
  124. * @since 1.5.8
  125. * @param object $settings A module settings object.
  126. * @param string $email The email to subscribe.
  127. * @param string $name Optional. The full name of the person subscribing.
  128. * @return array {
  129. * @type bool|string $error The error message or false if no error.
  130. * }
  131. */
  132. public function subscribe( $settings, $email, $name = false ) {
  133. $contact_id = null;
  134. $account_data = $this->get_account_data( $settings->service_account );
  135. $response = array(
  136. 'error' => false,
  137. );
  138. if ( ! $account_data ) {
  139. $response['error'] = __( 'There was an error subscribing to Hatchbuck. The account is no longer connected.', 'fl-builder' );
  140. } else {
  141. // Build the data array.
  142. $data = array(
  143. 'emails' => array(
  144. array(
  145. 'address' => $email,
  146. 'type' => 'Work',
  147. ),
  148. ),
  149. 'status' => array(
  150. 'name' => 'Lead',
  151. ),
  152. );
  153. // Check if the contact exists.
  154. $result = wp_remote_post( $this->api_url . 'search?api_key=' . $account_data['api_key'], array(
  155. 'method' => 'POST',
  156. 'timeout' => 60,
  157. 'headers' => array(
  158. 'Content-Type' => 'application/json',
  159. ),
  160. 'body' => json_encode( $data ),
  161. ) );
  162. // Return if we have an API key error.
  163. if ( 401 == $result['response']['code'] ) {
  164. $response['error'] = __( 'There was an error subscribing to Hatchbuck. The API key is invalid.', 'fl-builder' );
  165. return $response; // Invalid API key.
  166. } elseif ( 200 == $result['response']['code'] ) {
  167. $result_data = json_decode( $result['body'] );
  168. $contact_id = $result_data[0]->contactId;
  169. } // Generic error. Contact not found should be 400.
  170. elseif ( 400 != $result['response']['code'] ) {
  171. $response['error'] = __( 'There was an error subscribing to Hatchbuck.', 'fl-builder' );
  172. return $response;
  173. }
  174. // Add the contact if it doesn't exist.
  175. if ( ! $contact_id ) {
  176. // Add the name to the data array if we have one.
  177. if ( $name ) {
  178. $names = explode( ' ', $name );
  179. if ( isset( $names[0] ) ) {
  180. $data['firstName'] = $names[0];
  181. }
  182. if ( isset( $names[1] ) ) {
  183. $data['lastName'] = $names[1];
  184. }
  185. }
  186. // Add the contact to Hatchbuck.
  187. $result = wp_remote_post( $this->api_url . '?api_key=' . $account_data['api_key'], array(
  188. 'method' => 'POST',
  189. 'timeout' => 60,
  190. 'headers' => array(
  191. 'Content-Type' => 'application/json',
  192. ),
  193. 'body' => json_encode( $data ),
  194. ) );
  195. // Return if we have an error.
  196. if ( 200 != $result['response']['code'] ) {
  197. $response['error'] = __( 'There was an error subscribing to Hatchbuck.', 'fl-builder' );
  198. return $response;
  199. }
  200. // Get the result data that contains the new contact ID.
  201. $result_data = json_decode( $result['body'] );
  202. // @codingStandardsIgnoreLine
  203. $contact_id = $result_data->contactId;
  204. }
  205. // Add the tag to the contact.
  206. $result = wp_remote_post( $this->api_url . $contact_id . '/Tags?api_key=' . $account_data['api_key'], array(
  207. 'method' => 'POST',
  208. 'timeout' => 60,
  209. 'headers' => array(
  210. 'Content-Type' => 'application/json',
  211. ),
  212. 'body' => json_encode( array(
  213. array(
  214. 'name' => $settings->list_id,
  215. ),
  216. ) ),
  217. ) );
  218. }
  219. return $response;
  220. }
  221. }