class-fl-builder-service-mailerlite.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Helper class for the A API.
  4. *
  5. * @since 1.9
  6. */
  7. final class FLBuilderServiceMailerLite extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.9
  12. * @var string $id
  13. */
  14. public $id = 'mailerlite';
  15. /**
  16. * The API URL
  17. *
  18. * @since 1.9
  19. * @var string $api_url
  20. */
  21. public $api_url = 'https://app.mailerlite.com/api/v2/';
  22. /**
  23. * @since 1.9
  24. * @var object $api_instance
  25. * @access private
  26. */
  27. private $api_instance = null;
  28. /**
  29. * Get an instance of the API.
  30. *
  31. * @since 1.9
  32. * @param string $api_key A valid API token.
  33. * @return object The API instance.
  34. */
  35. public function get_api( $api_key ) {
  36. if ( $this->api_instance ) {
  37. return $this->api_instance;
  38. }
  39. if ( ! class_exists( 'FL_ML_Rest' ) ) {
  40. require_once FL_BUILDER_DIR . 'includes/vendor/mailerlite/FL_ML_Rest.php';
  41. }
  42. $this->api_instance = new FL_ML_Rest( $api_key );
  43. $this->api_instance->setUrl( $this->api_url );
  44. return $this->api_instance;
  45. }
  46. /**
  47. * Test the API connection.
  48. *
  49. * @since 1.9
  50. * @param array $fields {
  51. * @type string $api_key A valid API key.
  52. * }
  53. * @return array{
  54. * @type bool|string $error The error message or false if no error.
  55. * @type array $data An array of data used to make the connection.
  56. * }
  57. */
  58. public function connect( $fields = array() ) {
  59. $response = array(
  60. 'error' => false,
  61. 'data' => array(),
  62. );
  63. // Make sure we have an API token.
  64. if ( ! isset( $fields['api_key'] ) ) {
  65. $response['error'] = __( 'Error: You must provide an API token.', 'fl-builder' );
  66. } else {
  67. $api = $this->get_api( $fields['api_key'] );
  68. $api->setPath( 'groups' );
  69. $api->getAll();
  70. $get_api_response = $api->getResponseInfo();
  71. if ( 200 === $get_api_response['http_code'] ) {
  72. $response['data'] = array(
  73. 'api_key' => $fields['api_key'],
  74. );
  75. } else {
  76. $response['error'] = sprintf( __( 'Error: Could not connect to MailerLite. %s', 'fl-builder' ), $get_api_response['http_code'] );
  77. }
  78. }
  79. return $response;
  80. }
  81. /**
  82. * Renders the markup for the connection settings.
  83. *
  84. * @since 1.9
  85. * @return string The connection settings markup.
  86. */
  87. public function render_connect_settings() {
  88. ob_start();
  89. FLBuilder::render_settings_field( 'api_key', array(
  90. 'row_class' => 'fl-builder-service-connect-row',
  91. 'class' => 'fl-builder-service-connect-input',
  92. 'type' => 'text',
  93. 'label' => __( 'API Key', 'fl-builder' ),
  94. 'help' => __( 'Found in your MailerLite account under Integrations > Developer API.', 'fl-builder' ),
  95. 'preview' => array(
  96. 'type' => 'none',
  97. ),
  98. ));
  99. return ob_get_clean();
  100. }
  101. /**
  102. * Render the markup for service specific fields.
  103. *
  104. * @since 1.9
  105. * @param string $account The name of the saved account.
  106. * @param object $settings Saved module settings.
  107. * @return array {
  108. * @type bool|string $error The error message or false if no error.
  109. * @type string $html The field markup.
  110. * }
  111. */
  112. public function render_fields( $account, $settings ) {
  113. $account_data = $this->get_account_data( $account );
  114. $api = $this->get_api( $account_data['api_key'] );
  115. $api->setPath( 'groups' );
  116. $get_lists = json_decode( $api->getAll() );
  117. $lists = array();
  118. if ( $get_lists && count( $get_lists ) > 0 ) {
  119. $lists = $get_lists;
  120. }
  121. $response = array(
  122. 'error' => false,
  123. 'html' => $this->render_list_field( $lists, $settings ),
  124. );
  125. return $response;
  126. }
  127. /**
  128. * Render markup for the list field.
  129. *
  130. * @since 1.9
  131. * @param array $lists List data from the API.
  132. * @param object $settings Saved module settings.
  133. * @return string The markup for the list field.
  134. * @access private
  135. */
  136. private function render_list_field( $lists, $settings ) {
  137. ob_start();
  138. $options = array(
  139. '' => __( 'Choose...', 'fl-builder' ),
  140. );
  141. if ( $lists ) {
  142. foreach ( $lists as $list ) {
  143. $options[ $list->id ] = $list->name;
  144. }
  145. }
  146. FLBuilder::render_settings_field( 'list_id', array(
  147. 'row_class' => 'fl-builder-service-field-row',
  148. 'class' => 'fl-builder-service-list-select',
  149. 'type' => 'select',
  150. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  151. 'options' => $options,
  152. 'preview' => array(
  153. 'type' => 'none',
  154. ),
  155. ), $settings);
  156. return ob_get_clean();
  157. }
  158. /**
  159. * Subscribe an email address to Drip.
  160. *
  161. * @since 1.9
  162. * @param object $settings A module settings object.
  163. * @param string $email The email to subscribe.
  164. * @param string $name Optional. The full name of the person subscribing.
  165. * @return array {
  166. * @type bool|string $error The error message or false if no error.
  167. * }
  168. */
  169. public function subscribe( $settings, $email, $name = '' ) {
  170. $account_data = $this->get_account_data( $settings->service_account );
  171. $response = array(
  172. 'error' => false,
  173. );
  174. if ( ! $account_data ) {
  175. $response['error'] = __( 'There was an error subscribing to MailerLite. The account is no longer connected.', 'fl-builder' );
  176. } else {
  177. $api = $this->get_api( $account_data['api_key'] );
  178. $data['email'] = $email;
  179. // Add the name to the data array if we have one.
  180. if ( $name ) {
  181. $names = explode( ' ', $name );
  182. if ( isset( $names[0] ) ) {
  183. $data['name'] = $names[0];
  184. }
  185. if ( isset( $names[1] ) ) {
  186. $data['last_name'] = $names[1];
  187. }
  188. }
  189. // Search if it's an existing subscriber
  190. /*
  191. $api->setPath('subscribers/search?query='. $email);
  192. $search_results = json_decode($api->getAll());
  193. if ( $search_results && isset($search_results[0]->id) ) {
  194. echo 'existing email...';
  195. // Update subscriber
  196. $api->setPath('subscribers/'. $search_results[0]->id);
  197. $response = $api->put($data);
  198. }*/
  199. // Add new
  200. $api->setPath( 'groups/' . $settings->list_id . '/subscribers' );
  201. $api->add( $data );
  202. $result = $api->getResponseInfo();
  203. if ( 200 !== $result['http_code'] ) {
  204. $response['error'] = sprintf( __( 'There was an error subscribing to MailerLite. Code: %s', 'fl-builder' ), $result['http_code'] );
  205. }
  206. }
  207. return $response;
  208. }
  209. }