class-fl-builder-service-madmimi.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Helper class for the Mad Mimi API.
  4. *
  5. * @since 1.5.2
  6. */
  7. final class FLBuilderServiceMadMimi extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.2
  12. * @var string $id
  13. */
  14. public $id = 'madmimi';
  15. /**
  16. * @since 1.5.2
  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.2
  25. * @param string $api_email The email address associated with the API key.
  26. * @param string $api_key A valid API key.
  27. * @return object The API instance.
  28. */
  29. public function get_api( $api_email, $api_key ) {
  30. if ( $this->api_instance ) {
  31. return $this->api_instance;
  32. }
  33. if ( ! class_exists( 'MadMimi' ) ) {
  34. require_once FL_BUILDER_DIR . 'includes/vendor/madmimi/MadMimi.class.php';
  35. }
  36. $this->api_instance = new MadMimi( $api_email, $api_key );
  37. return $this->api_instance;
  38. }
  39. /**
  40. * Test the API connection.
  41. *
  42. * @since 1.5.2
  43. * @param array $fields {
  44. * @type string $api_email A valid email address.
  45. * @type string $api_key A valid API key.
  46. * }
  47. * @return array{
  48. * @type bool|string $error The error message or false if no error.
  49. * @type array $data An array of data used to make the connection.
  50. * }
  51. */
  52. public function connect( $fields = array() ) {
  53. $response = array(
  54. 'error' => false,
  55. 'data' => array(),
  56. );
  57. // Make sure we have an email address.
  58. if ( ! isset( $fields['api_email'] ) || empty( $fields['api_email'] ) ) {
  59. $response['error'] = __( 'Error: You must provide an email address.', 'fl-builder' );
  60. } elseif ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  61. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  62. } // Try to connect and store the connection data.
  63. else {
  64. $api = $this->get_api( $fields['api_email'], $fields['api_key'] );
  65. libxml_use_internal_errors( true );
  66. if ( ! simplexml_load_string( $api->Lists() ) ) {
  67. $response['error'] = __( 'Unable to connect to Mad Mimi. Please check your credentials.', 'fl-builder' );
  68. } else {
  69. $response['data'] = array(
  70. 'api_email' => $fields['api_email'],
  71. 'api_key' => $fields['api_key'],
  72. );
  73. }
  74. }
  75. return $response;
  76. }
  77. /**
  78. * Renders the markup for the connection settings.
  79. *
  80. * @since 1.5.2
  81. * @return string The connection settings markup.
  82. */
  83. public function render_connect_settings() {
  84. ob_start();
  85. FLBuilder::render_settings_field( 'api_email', array(
  86. 'row_class' => 'fl-builder-service-connect-row',
  87. 'class' => 'fl-builder-service-connect-input',
  88. 'type' => 'text',
  89. 'label' => __( 'Email Address', 'fl-builder' ),
  90. 'help' => __( 'The email address associated with your Mad Mimi account.', 'fl-builder' ),
  91. 'preview' => array(
  92. 'type' => 'none',
  93. ),
  94. ));
  95. FLBuilder::render_settings_field( 'api_key', array(
  96. 'row_class' => 'fl-builder-service-connect-row',
  97. 'class' => 'fl-builder-service-connect-input',
  98. 'type' => 'text',
  99. 'label' => __( 'API Key', 'fl-builder' ),
  100. 'help' => __( 'Your API key can be found in your Mad Mimi account under Account > Settings &amp; Billing > API.', 'fl-builder' ),
  101. 'preview' => array(
  102. 'type' => 'none',
  103. ),
  104. ));
  105. return ob_get_clean();
  106. }
  107. /**
  108. * Render the markup for service specific fields.
  109. *
  110. * @since 1.5.2
  111. * @param string $account The name of the saved account.
  112. * @param object $settings Saved module settings.
  113. * @return array {
  114. * @type bool|string $error The error message or false if no error.
  115. * @type string $html The field markup.
  116. * }
  117. */
  118. public function render_fields( $account, $settings ) {
  119. $account_data = $this->get_account_data( $account );
  120. $api = $this->get_api( $account_data['api_email'], $account_data['api_key'] );
  121. $response = array(
  122. 'error' => false,
  123. 'html' => '',
  124. );
  125. libxml_use_internal_errors( true );
  126. $result = simplexml_load_string( $api->Lists() );
  127. if ( ! $result ) {
  128. $response['error'] = __( 'There was a problem retrieving your lists. Please check your API credentials.', 'fl-builder' );
  129. } else {
  130. $response['html'] = $this->render_list_field( $result, $settings );
  131. }
  132. return $response;
  133. }
  134. /**
  135. * Render markup for the list field.
  136. *
  137. * @since 1.5.2
  138. * @param array $lists List data from the API.
  139. * @param object $settings Saved module settings.
  140. * @return string The markup for the list field.
  141. * @access private
  142. */
  143. private function render_list_field( $lists, $settings ) {
  144. ob_start();
  145. $options = array(
  146. '' => __( 'Choose...', 'fl-builder' ),
  147. );
  148. foreach ( $lists->list as $list ) {
  149. $options[ (string) $list['id'] ] = (string) $list['name'];
  150. }
  151. FLBuilder::render_settings_field( 'list_id', array(
  152. 'row_class' => 'fl-builder-service-field-row',
  153. 'class' => 'fl-builder-service-list-select',
  154. 'type' => 'select',
  155. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  156. 'options' => $options,
  157. 'preview' => array(
  158. 'type' => 'none',
  159. ),
  160. ), $settings);
  161. return ob_get_clean();
  162. }
  163. /**
  164. * Subscribe an email address to Mad Mimi.
  165. *
  166. * @since 1.5.2
  167. * @param object $settings A module settings object.
  168. * @param string $email The email to subscribe.
  169. * @param string $name Optional. The full name of the person subscribing.
  170. * @return array {
  171. * @type bool|string $error The error message or false if no error.
  172. * }
  173. */
  174. public function subscribe( $settings, $email, $name = false ) {
  175. $account_data = $this->get_account_data( $settings->service_account );
  176. $response = array(
  177. 'error' => false,
  178. );
  179. if ( ! $account_data ) {
  180. $response['error'] = __( 'There was an error subscribing to Mad Mimi. The account is no longer connected.', 'fl-builder' );
  181. } else {
  182. $api = $this->get_api( $account_data['api_email'], $account_data['api_key'] );
  183. $data = array(
  184. 'email' => $email,
  185. 'add_list' => $settings->list_id,
  186. );
  187. if ( $name ) {
  188. $names = explode( ' ', $name );
  189. if ( isset( $names[0] ) ) {
  190. $data['firstName'] = $names[0];
  191. }
  192. if ( isset( $names[1] ) ) {
  193. $data['lastName'] = $names[1];
  194. }
  195. }
  196. ob_start();
  197. $api->AddUser( $data, true );
  198. $request = ob_get_clean();
  199. if ( stristr( $request, 'Unable to authenticate' ) ) {
  200. $response['error'] = __( 'There was an error subscribing to Mad Mimi. The account is no longer connected.', 'fl-builder' );
  201. }
  202. }
  203. return $response;
  204. }
  205. }