class-fl-builder-service-campayn.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Helper class for the Campayn API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceCampayn extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'campayn';
  15. /**
  16. * The HTTP protocal
  17. *
  18. * @since 1.5.8
  19. * @access private
  20. * @var string $api_protocol
  21. */
  22. private $api_protocol = 'http';
  23. /**
  24. * The API version
  25. *
  26. * @since 1.5.8
  27. * @access private
  28. * @var string $api_version
  29. */
  30. private $api_version = 1;
  31. /**
  32. * Request data from the thir party API.
  33. *
  34. * @since 1.5.4
  35. * @param string $base_url Base URL where API is available
  36. * @param string $api_key API Key provided by this service
  37. * @param string $endpoint Method to request available from this service.
  38. * @param array $params Data to be passed to API
  39. * @return array|object The API response.
  40. */
  41. private function get_api_response( $base_url, $api_key, $endpoint, $params = array() ) {
  42. // Exclude http:// from the user's input
  43. $request_uri = $this->api_protocol . '://' . preg_replace( '#^https?://#', '', $base_url ) . '/api/v' . $this->api_version . $endpoint;
  44. $params['timeout'] = 60;
  45. $params['body'] = isset( $params['data'] ) && $params['data'] ? json_encode( $params['data'] ) : '';
  46. $params['headers'] = array(
  47. 'Authorization' => 'TRUEREST apikey=' . $api_key,
  48. );
  49. $response = wp_remote_get( $request_uri, $params );
  50. $response_code = wp_remote_retrieve_response_code( $response );
  51. $response_message = wp_remote_retrieve_response_message( $response );
  52. $get_response = json_decode( wp_remote_retrieve_body( $response ), true );
  53. if ( is_wp_error( $response ) || (200 != $response_code) ) {
  54. if ( is_wp_error( $response ) ) {
  55. $data['error'] = $response->get_error_message();
  56. } else {
  57. $data['error'] = isset( $get_response['msg'] ) ? $get_response['msg'] : $response_code . ' - ' . $response_message;
  58. }
  59. } else {
  60. if ( $get_response ) {
  61. $data = $get_response;
  62. } else {
  63. $data = $response;
  64. }
  65. }
  66. return $data;
  67. }
  68. /**
  69. * Test the API connection.
  70. *
  71. * @since 1.5.4
  72. * @param array $fields {
  73. * @type string $api_host A valid Host.
  74. * @type string $api_key A valid API key.
  75. * }
  76. * @return array{
  77. * @type bool|string $error The error message or false if no error.
  78. * @type array $data An array of data used to make the connection.
  79. * }
  80. */
  81. public function connect( $fields = array() ) {
  82. $response = array(
  83. 'error' => false,
  84. 'data' => array(),
  85. );
  86. // Make sure we have the Host.
  87. if ( ! isset( $fields['api_host'] ) || empty( $fields['api_host'] ) ) {
  88. $response['error'] = __( 'Error: You must provide a Host.', 'fl-builder' );
  89. } elseif ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  90. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  91. } // Try to connect and store the connection data.
  92. else {
  93. $result = $this->get_api_response( $fields['api_host'], $fields['api_key'], '/lists.json' );
  94. if ( ! isset( $result['error'] ) ) {
  95. $response['data'] = array(
  96. 'api_host' => $fields['api_host'],
  97. 'api_key' => $fields['api_key'],
  98. );
  99. } else {
  100. $response['error'] = sprintf( __( 'Error: Could not connect to Campayn. %s', 'fl-builder' ), $result['error'] );
  101. }
  102. }
  103. return $response;
  104. }
  105. /**
  106. * Renders the markup for the connection settings.
  107. *
  108. * @since 1.5.4
  109. * @return string The connection settings markup.
  110. */
  111. public function render_connect_settings() {
  112. ob_start();
  113. FLBuilder::render_settings_field( 'api_host', array(
  114. 'row_class' => 'fl-builder-service-connect-row',
  115. 'class' => 'fl-builder-service-connect-input',
  116. 'type' => 'text',
  117. 'label' => __( 'Host', 'fl-builder' ),
  118. 'help' => __( 'The host you chose when you signed up for your account. Check your welcome email if you forgot it. Please enter it without the initial http:// (for example: demo.campayn.com).', 'fl-builder' ),
  119. 'preview' => array(
  120. 'type' => 'none',
  121. ),
  122. ));
  123. FLBuilder::render_settings_field( 'api_key', array(
  124. 'row_class' => 'fl-builder-service-connect-row',
  125. 'class' => 'fl-builder-service-connect-input',
  126. 'type' => 'text',
  127. 'label' => __( 'API Key', 'fl-builder' ),
  128. 'help' => __( 'Your API key can be found in your Campayn account under Settings > API Key.', 'fl-builder' ),
  129. 'preview' => array(
  130. 'type' => 'none',
  131. ),
  132. ));
  133. return ob_get_clean();
  134. }
  135. /**
  136. * Render the markup for service specific fields.
  137. *
  138. * @since 1.5.4
  139. * @param string $account The name of the saved account.
  140. * @param object $settings Saved module settings.
  141. * @return array {
  142. * @type bool|string $error The error message or false if no error.
  143. * @type string $html The field markup.
  144. * }
  145. */
  146. public function render_fields( $account, $settings ) {
  147. $account_data = $this->get_account_data( $account );
  148. $results = $this->get_api_response( $account_data['api_host'], $account_data['api_key'], '/lists.json' );
  149. $response = array(
  150. 'error' => false,
  151. 'html' => '',
  152. );
  153. if ( isset( $results['error'] ) ) {
  154. $response['error'] = sprintf( __( 'Error: Please check your API key. %s', 'fl-builder' ), $results['error'] );
  155. } else {
  156. $response['html'] = $this->render_list_field( $results, $settings );
  157. }
  158. return $response;
  159. }
  160. /**
  161. * Render markup for the list field.
  162. *
  163. * @since 1.5.4
  164. * @param array $lists List data from the API.
  165. * @param object $settings Saved module settings.
  166. * @return string The markup for the list field.
  167. * @access private
  168. */
  169. private function render_list_field( $lists, $settings ) {
  170. ob_start();
  171. $options = array(
  172. '' => __( 'Choose...', 'fl-builder' ),
  173. );
  174. foreach ( $lists as $list ) {
  175. $options[ $list['id'] ] = $list['list_name'];
  176. }
  177. FLBuilder::render_settings_field( 'list_id', array(
  178. 'row_class' => 'fl-builder-service-field-row',
  179. 'class' => 'fl-builder-service-list-select',
  180. 'type' => 'select',
  181. 'label' => _x( 'List', 'An email list from third party provider.', 'fl-builder' ),
  182. 'options' => $options,
  183. 'preview' => array(
  184. 'type' => 'none',
  185. ),
  186. ), $settings);
  187. return ob_get_clean();
  188. }
  189. /**
  190. * Subscribe an email address to Campayn.
  191. *
  192. * @since 1.5.4
  193. * @param object $settings A module settings object.
  194. * @param string $email The email to subscribe.
  195. * @param string $name Optional. The full name of the person subscribing.
  196. * @return array {
  197. * @type bool|string $error The error message or false if no error.
  198. * }
  199. */
  200. public function subscribe( $settings, $email, $name = '' ) {
  201. $account_data = $this->get_account_data( $settings->service_account );
  202. $response = array(
  203. 'error' => false,
  204. );
  205. $contact_id = null;
  206. if ( ! $account_data ) {
  207. $response['error'] = __( 'There was an error subscribing to Campayn. The account is no longer connected.', 'fl-builder' );
  208. } else {
  209. // Build data array
  210. $data = array(
  211. 'email' => $email,
  212. );
  213. // Add the name to the data array if we have one.
  214. if ( $name ) {
  215. $names = explode( ' ', $name );
  216. if ( isset( $names[0] ) ) {
  217. $data['first_name'] = $names[0];
  218. }
  219. if ( isset( $names[1] ) ) {
  220. $data['last_name'] = $names[1];
  221. }
  222. }
  223. // Check if email already exists
  224. $result = $this->get_api_response( $account_data['api_host'], $account_data['api_key'],
  225. "/lists/{$settings->list_id}/contacts.json?filter[contact]=" . $email
  226. );
  227. // Already exists
  228. if ( ! isset( $result['error'] ) && (is_array( $result ) && isset( $result[0]['id'] )) ) {
  229. $contact_id = $result[0]['id'];
  230. }
  231. // Add the contact if it doesn't exist.
  232. if ( ! $contact_id ) {
  233. $endpoint = "/lists/{$settings->list_id}/contacts.json";
  234. $method = 'POST';
  235. } else {
  236. $endpoint = "/contacts/{$contact_id}.json";
  237. $method = 'PUT';
  238. $data['id'] = $contact_id;
  239. }
  240. $result = $this->get_api_response( $account_data['api_host'], $account_data['api_key'], $endpoint, array(
  241. 'data' => $data,
  242. 'method' => $method,
  243. ) );
  244. if ( isset( $result['error'] ) ) {
  245. $response['error'] = sprintf( __( 'There was an error subscribing to Campayn. %s', 'fl-builder' ), $result['error'] );
  246. }
  247. }
  248. return $response;
  249. }
  250. }