class-fl-builder-service-enormail.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Helper class for the Enormail API.
  4. *
  5. * @since 1.9.5
  6. */
  7. final class FLBuilderServiceEnormail extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.9.5
  12. * @var string $id
  13. */
  14. public $id = 'enormail';
  15. /**
  16. * @since 1.9.5
  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.9.5
  25. * @param array $api_key A valid API key to authenticate.
  26. * @return object The API instance.
  27. */
  28. public function get_api( $api_key ) {
  29. if ( $this->api_instance ) {
  30. return $this->api_instance;
  31. }
  32. if ( ! class_exists( '\\Enormail\\ApiClient' ) ) {
  33. require_once FL_BUILDER_DIR . 'includes/vendor/enormail/autoload.php';
  34. }
  35. $this->api_instance = new \Enormail\ApiClient( $api_key );
  36. return $this->api_instance;
  37. }
  38. /**
  39. * Test the API connection.
  40. *
  41. * @since 1.9.5
  42. * @param array $fields {
  43. * @type string $api_host A valid Host.
  44. * @type string $api_key A valid API key.
  45. * }
  46. * @return array{
  47. * @type bool|string $error The error message or false if no error.
  48. * @type array $data An array of data used to make the connection.
  49. * }
  50. */
  51. public function connect( $fields = array() ) {
  52. $response = array(
  53. 'error' => false,
  54. 'data' => array(),
  55. );
  56. // Make sure we have an API key.
  57. if ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  58. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  59. } else {
  60. $api = $this->get_api( $fields['api_key'] );
  61. // Fetch account info
  62. $api_response = json_decode( $api->test() );
  63. if ( isset( $api_response->ping ) && 'hello' === $api_response->ping ) {
  64. $response['data'] = array(
  65. 'api_key' => $fields['api_key'],
  66. );
  67. } else {
  68. $response['error'] = sprintf(__( 'Error: Could not connect to Enormail. %s', 'fl-builder' ),
  69. '(' . $api_response->error->http_code . ': ' . $api_response->error->message . ')'
  70. );
  71. }
  72. }
  73. return $response;
  74. }
  75. /**
  76. * Renders the markup for the connection settings.
  77. *
  78. * @since 1.9.5
  79. * @return string The connection settings markup.
  80. */
  81. public function render_connect_settings() {
  82. ob_start();
  83. FLBuilder::render_settings_field( 'api_key', array(
  84. 'row_class' => 'fl-builder-service-connect-row',
  85. 'class' => 'fl-builder-service-connect-input',
  86. 'type' => 'text',
  87. 'label' => __( 'API Key', 'fl-builder' ),
  88. 'help' => __( 'Found in your Sendy application under Settings.', 'fl-builder' ),
  89. 'preview' => array(
  90. 'type' => 'none',
  91. ),
  92. ));
  93. return ob_get_clean();
  94. }
  95. /**
  96. * Render the markup for service specific fields.
  97. *
  98. * @since 1.9.5
  99. * @param string $account The name of the saved account.
  100. * @param object $settings Saved module settings.
  101. * @return array {
  102. * @type bool|string $error The error message or false if no error.
  103. * @type string $html The field markup.
  104. * }
  105. */
  106. public function render_fields( $account, $settings ) {
  107. $account_data = $this->get_account_data( $account );
  108. $api = $this->get_api( $account_data['api_key'] );
  109. $lists = json_decode( $api->lists->get() );
  110. $response = array(
  111. 'error' => false,
  112. 'html' => '',
  113. );
  114. if ( ! $lists ) {
  115. $response['error'] = __( 'Error: Please check your API key.', 'fl-builder' );
  116. } else {
  117. $response['html'] = $this->render_list_field( $lists, $settings );
  118. }
  119. return $response;
  120. }
  121. /**
  122. * Render markup for the list field.
  123. *
  124. * @since 1.9.5
  125. * @param array $lists List data from the API.
  126. * @param object $settings Saved module settings.
  127. * @return string The markup for the list field.
  128. * @access private
  129. */
  130. private function render_list_field( $lists, $settings ) {
  131. ob_start();
  132. $options = array(
  133. '' => __( 'Choose...', 'fl-builder' ),
  134. );
  135. foreach ( $lists as $list ) {
  136. if ( isset( $list->listid ) ) {
  137. $options[ $list->listid ] = $list->title;
  138. }
  139. }
  140. FLBuilder::render_settings_field( 'list_id', array(
  141. 'row_class' => 'fl-builder-service-field-row',
  142. 'class' => 'fl-builder-service-list-select',
  143. 'type' => 'select',
  144. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  145. 'options' => $options,
  146. 'preview' => array(
  147. 'type' => 'none',
  148. ),
  149. ), $settings);
  150. return ob_get_clean();
  151. }
  152. /**
  153. * Subscribe an email address to Sendy.
  154. *
  155. * @since 1.9.5
  156. * @param object $settings A module settings object.
  157. * @param string $email The email to subscribe.
  158. * @param string $name Optional. The full name of the person subscribing.
  159. * @return array {
  160. * @type bool|string $error The error message or false if no error.
  161. * }
  162. */
  163. public function subscribe( $settings, $email, $name = '' ) {
  164. $account_data = $this->get_account_data( $settings->service_account );
  165. $response = array(
  166. 'error' => false,
  167. );
  168. if ( ! $account_data ) {
  169. $response['error'] = __( 'There was an error subscribing to Enormail. The account is no longer connected.', 'fl-builder' );
  170. } else {
  171. $api = $this->get_api( $account_data['api_key'] );
  172. // Search user if already exists
  173. $contact = json_decode( $api->contacts->details( $settings->list_id, $email ) );
  174. // Name is required
  175. if ( empty( $name ) ) {
  176. $name = explode( '@', $email )[0];
  177. }
  178. // Add if not exists
  179. if ( -1 == $contact->code ) {
  180. $result = $api->contacts->add( $settings->list_id, $name, $email );
  181. } else {
  182. $result = $api->contacts->update( $settings->list_id, $name, $email );
  183. }
  184. $get_results = json_decode( $result );
  185. if ( isset( $get_results->status ) && 'error' === $get_results->status ) {
  186. $response['error'] = sprintf(__( 'There was an error subscribing to Enormail. %s', 'fl-builder' ),
  187. '(' . $get_results->code . ': ' . $get_results->message . ')'
  188. );
  189. }
  190. }
  191. return $response;
  192. }
  193. }