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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Helper class for the SendinBlue API.
  4. *
  5. * @since 1.5.6
  6. */
  7. final class FLBuilderServiceSendinBlue extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.6
  12. * @var string $id
  13. */
  14. public $id = 'sendinblue';
  15. /**
  16. * @since 1.5.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.5.6
  25. * @param string $access_key A valid access key.
  26. * @return object The API instance.
  27. */
  28. public function get_api( $access_key ) {
  29. if ( $this->api_instance ) {
  30. return $this->api_instance;
  31. }
  32. if ( ! class_exists( 'Mailin_Rest' ) ) {
  33. require_once FL_BUILDER_DIR . 'includes/vendor/sendinblue/Mailin.php';
  34. }
  35. $this->api_instance = new Mailin_Rest( 'https://api.sendinblue.com/v2.0', $access_key );
  36. return $this->api_instance;
  37. }
  38. /**
  39. * Test the API connection.
  40. *
  41. * @since 1.5.6
  42. * @param array $fields {
  43. * @type string $access_key A valid access key.
  44. * }
  45. * @return array{
  46. * @type bool|string $error The error message or false if no error.
  47. * @type array $data An array of data used to make the connection.
  48. * }
  49. */
  50. public function connect( $fields = array() ) {
  51. $response = array(
  52. 'error' => false,
  53. 'data' => array(),
  54. );
  55. // Make sure we have an access key.
  56. if ( ! isset( $fields['access_key'] ) || empty( $fields['access_key'] ) ) {
  57. $response['error'] = __( 'Error: You must provide an Access Key.', 'fl-builder' );
  58. } else {
  59. $api = $this->get_api( $fields['access_key'] );
  60. $result = $api->get_account();
  61. if ( ! is_array( $result ) ) {
  62. $response['error'] = __( 'There was an error connecting to SendinBlue. Please try again.', 'fl-builder' );
  63. } elseif ( isset( $result['code'] ) && 'failure' == $result['code'] ) {
  64. $response['error'] = sprintf( __( 'Error: Could not connect to SendinBlue. %s', 'fl-builder' ), $result['message'] );
  65. } else {
  66. $response['data'] = array(
  67. 'access_key' => $fields['access_key'],
  68. );
  69. }
  70. }
  71. return $response;
  72. }
  73. /**
  74. * Renders the markup for the connection settings.
  75. *
  76. * @since 1.5.6
  77. * @return string The connection settings markup.
  78. */
  79. public function render_connect_settings() {
  80. ob_start();
  81. FLBuilder::render_settings_field( 'access_key', array(
  82. 'row_class' => 'fl-builder-service-connect-row',
  83. 'class' => 'fl-builder-service-connect-input',
  84. 'type' => 'text',
  85. 'label' => __( 'Access Key', 'fl-builder' ),
  86. 'help' => __( 'Your Access Key can be found in your SendinBlue account under API & Integration > Manager Your Keys > Version 2.0 > Access Key.', 'fl-builder' ),
  87. 'preview' => array(
  88. 'type' => 'none',
  89. ),
  90. ));
  91. return ob_get_clean();
  92. }
  93. /**
  94. * Render the markup for service specific fields.
  95. *
  96. * @since 1.5.6
  97. * @param string $account The name of the saved account.
  98. * @param object $settings Saved module settings.
  99. * @return array {
  100. * @type bool|string $error The error message or false if no error.
  101. * @type string $html The field markup.
  102. * }
  103. */
  104. public function render_fields( $account, $settings ) {
  105. $account_data = $this->get_account_data( $account );
  106. $api = $this->get_api( $account_data['access_key'] );
  107. $response = array(
  108. 'error' => false,
  109. 'html' => '',
  110. );
  111. $result = $api->get_lists( 1, 50 );
  112. if ( ! is_array( $result ) ) {
  113. $response['error'] = __( 'There was an error connecting to SendinBlue. Please try again.', 'fl-builder' );
  114. } elseif ( isset( $result['code'] ) && 'failure' == $result['code'] ) {
  115. $response['error'] = sprintf( __( 'Error: Could not connect to SendinBlue. %s', 'fl-builder' ), $result['message'] );
  116. } else {
  117. $response['html'] = $this->render_list_field( $result['data']['lists'], $settings );
  118. }
  119. return $response;
  120. }
  121. /**
  122. * Render markup for the list field.
  123. *
  124. * @since 1.5.6
  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. $options[ $list['id'] ] = $list['name'];
  137. }
  138. FLBuilder::render_settings_field( 'list_id', array(
  139. 'row_class' => 'fl-builder-service-field-row',
  140. 'class' => 'fl-builder-service-list-select',
  141. 'type' => 'select',
  142. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  143. 'options' => $options,
  144. 'preview' => array(
  145. 'type' => 'none',
  146. ),
  147. ), $settings);
  148. return ob_get_clean();
  149. }
  150. /**
  151. * Subscribe an email address to SendinBlue.
  152. *
  153. * @since 1.5.6
  154. * @param object $settings A module settings object.
  155. * @param string $email The email to subscribe.
  156. * @param string $name Optional. The full name of the person subscribing.
  157. * @return array {
  158. * @type bool|string $error The error message or false if no error.
  159. * }
  160. */
  161. public function subscribe( $settings, $email, $name = false ) {
  162. $account_data = $this->get_account_data( $settings->service_account );
  163. $response = array(
  164. 'error' => false,
  165. );
  166. if ( ! $account_data ) {
  167. $response['error'] = __( 'There was an error subscribing to SendinBlue. The account is no longer connected.', 'fl-builder' );
  168. } else {
  169. $api = $this->get_api( $account_data['access_key'] );
  170. $data = array();
  171. if ( $name ) {
  172. $names = explode( ' ', $name );
  173. if ( isset( $names[0] ) ) {
  174. $data['NAME'] = $names[0];
  175. }
  176. if ( isset( $names[1] ) ) {
  177. $data['SURNAME'] = $names[1];
  178. }
  179. }
  180. $result = $api->create_update_user( $email, $data, 0, array( $settings->list_id ), array(), 0 );
  181. if ( ! is_array( $result ) ) {
  182. $response['error'] = __( 'There was an error subscribing to SendinBlue. Please try again.', 'fl-builder' );
  183. } elseif ( isset( $result['code'] ) && 'failure' == $result['code'] ) {
  184. $response['error'] = sprintf( __( 'Error: Could not subscribe to SendinBlue. %s', 'fl-builder' ), $result['message'] );
  185. }
  186. }
  187. return $response;
  188. }
  189. }