class-fl-builder-service-mailchimp.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /**
  3. * Helper class for the MailChimp API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceMailChimp extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'mailchimp';
  15. /**
  16. * @since 1.5.4
  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.4
  25. * @param string $api_key A valid API key.
  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( 'Mailchimp' ) ) {
  33. require_once FL_BUILDER_DIR . 'includes/vendor/mailchimp/mailchimp.php';
  34. }
  35. $this->api_instance = new Mailchimp( $api_key );
  36. return $this->api_instance;
  37. }
  38. /**
  39. * Test the API connection.
  40. *
  41. * @since 1.5.4
  42. * @param array $fields {
  43. * @type string $api_key A valid API 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 API key.
  56. if ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  57. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  58. } else {
  59. try {
  60. $api = $this->get_api( $fields['api_key'] );
  61. $ping = $api->get( 'ping' );
  62. if ( ! isset( $ping['health_status'] ) && isset( $ping['title'] ) ) {
  63. $response['error'] = $ping['title'];
  64. } else {
  65. $response['data'] = array(
  66. 'api_key' => $fields['api_key'],
  67. );
  68. }
  69. } catch ( Exception $e ) {
  70. $response['error'] = $e->getMessage();
  71. }
  72. }
  73. return $response;
  74. }
  75. /**
  76. * Renders the markup for the connection settings.
  77. *
  78. * @since 1.5.4
  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' => __( 'Your API key can be found in your MailChimp account under Account > Extras > API Keys.', '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.5.4
  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. $post_data = FLBuilderModel::get_post_data();
  108. $account_data = $this->get_account_data( $account );
  109. $response = array(
  110. 'error' => false,
  111. 'html' => '',
  112. );
  113. // Lists field
  114. try {
  115. $api = $this->get_api( $account_data['api_key'] );
  116. if ( ! isset( $post_data['list_id'] ) ) {
  117. $lists = $api->getLists();
  118. $response['html'] .= $this->render_list_field( $lists, $settings );
  119. }
  120. } catch ( Exception $e ) {
  121. $response['error'] = $e->getMessage();
  122. }
  123. // Groups field
  124. try {
  125. if ( isset( $post_data['list_id'] ) || isset( $settings->list_id ) ) {
  126. if ( isset( $post_data['list_id'] ) ) {
  127. $list_id = $post_data['list_id'];
  128. } else {
  129. $list_id = $settings->list_id;
  130. }
  131. $groups = $api->interestGroupings( $list_id );
  132. $response['html'] .= $this->render_groups_field( $list_id, $groups, $settings );
  133. }
  134. } catch ( Exception $e ) {}
  135. return $response;
  136. }
  137. /**
  138. * Render markup for the list field.
  139. *
  140. * @since 1.5.4
  141. * @param array $lists List data from the API.
  142. * @param object $settings Saved module settings.
  143. * @return string The markup for the list field.
  144. * @access private
  145. */
  146. private function render_list_field( $lists, $settings ) {
  147. ob_start();
  148. $options = array(
  149. '' => __( 'Choose...', 'fl-builder' ),
  150. );
  151. if ( is_array( $lists ) && count( $lists ) > 0 ) {
  152. foreach ( $lists as $list ) {
  153. $options[ $list['id'] ] = $list['name'];
  154. }
  155. }
  156. FLBuilder::render_settings_field( 'list_id', array(
  157. 'row_class' => 'fl-builder-service-field-row',
  158. 'class' => 'fl-builder-service-list-select fl-builder-mailchimp-list-select',
  159. 'type' => 'select',
  160. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  161. 'options' => $options,
  162. 'preview' => array(
  163. 'type' => 'none',
  164. ),
  165. ), $settings);
  166. return ob_get_clean();
  167. }
  168. /**
  169. * Render markup for the groups field.
  170. *
  171. * @since 1.6.0
  172. * @param string $list_id The ID of the list for this groups.
  173. * @param array $groups An array of group data.
  174. * @param object $settings Saved module settings.
  175. * @return string The markup for the group field.
  176. * @access private
  177. */
  178. private function render_groups_field( $list_id, $groups, $settings ) {
  179. if ( ! is_array( $groups ) || 0 === count( $groups ) ) {
  180. return;
  181. }
  182. ob_start();
  183. $options = array(
  184. '' => __( 'No Group', 'fl-builder' ),
  185. );
  186. foreach ( $groups as $group ) {
  187. foreach ( $group['groups'] as $subgroup ) {
  188. $options[ $list_id . '_' . $group['id'] . '_' . $subgroup['id'] ] = $group['title'] . ' - ' . $subgroup['name'];
  189. }
  190. }
  191. FLBuilder::render_settings_field( 'groups', array(
  192. 'row_class' => 'fl-builder-service-field-row',
  193. 'class' => 'fl-builder-mailchimp-group-select',
  194. 'type' => 'select',
  195. 'label' => _x( 'Groups', 'MailChimp list group.', 'fl-builder' ),
  196. 'multi-select' => true,
  197. 'options' => $options,
  198. 'preview' => array(
  199. 'type' => 'none',
  200. ),
  201. ), $settings);
  202. return ob_get_clean();
  203. }
  204. /**
  205. * Subscribe an email address to MailChimp.
  206. *
  207. * @since 1.5.4
  208. * @param object $settings A module settings object.
  209. * @param string $email The email to subscribe.
  210. * @param string $name Optional. The full name of the person subscribing.
  211. * @return array {
  212. * @type bool|string $error The error message or false if no error.
  213. * }
  214. */
  215. public function subscribe( $settings, $email, $name = false ) {
  216. $account_data = $this->get_account_data( $settings->service_account );
  217. $response = array(
  218. 'error' => false,
  219. );
  220. if ( ! $account_data ) {
  221. $response['error'] = __( 'There was an error subscribing to MailChimp. The account is no longer connected.', 'fl-builder' );
  222. } else {
  223. try {
  224. $api = $this->get_api( $account_data['api_key'] );
  225. /**
  226. * Use this filter to enable double opt-ins for MailChimp integrations.
  227. * Returning true enables double opt-ins; returning false enables single opt-ins.
  228. * The default return value for this filter is false.
  229. * @see fl_builder_mailchimp_double_option
  230. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  231. */
  232. $double = apply_filters( 'fl_builder_mailchimp_double_option', false );
  233. $data = array(
  234. 'email' => $email,
  235. 'double_optin' => (bool) $double,
  236. );
  237. // Name
  238. if ( $name ) {
  239. $names = explode( ' ', $name );
  240. if ( isset( $names[0] ) ) {
  241. $data['FNAME'] = $names[0];
  242. $data['LNAME'] = ltrim( str_replace( $names[0], '', $name ) );
  243. }
  244. }
  245. // Groups
  246. if ( isset( $settings->groups ) && is_array( $settings->groups ) ) {
  247. $groups = array();
  248. // Build the array of saved group data.
  249. for ( $i = 0; $i < count( $settings->groups ); $i++ ) {
  250. if ( empty( $settings->groups[ $i ] ) ) {
  251. continue;
  252. }
  253. $group_data = explode( '_', $settings->groups[ $i ] );
  254. if ( $group_data[0] != $settings->list_id ) {
  255. continue;
  256. }
  257. if ( ! isset( $groups[ $group_data[1] ] ) ) {
  258. $groups[ $group_data[1] ] = array();
  259. }
  260. $groups[ $group_data[1] ][] = $group_data[2];
  261. }
  262. // Get the subgroup names from the API and add to the $data array.
  263. if ( count( $groups ) > 0 ) {
  264. $subgroup_ids = array();
  265. $groups_result = $api->interestGroupings( $settings->list_id );
  266. if ( is_array( $groups_result ) && count( $groups_result ) > 0 ) {
  267. foreach ( $groups_result as $group ) {
  268. if ( ! isset( $groups[ $group['id'] ] ) ) {
  269. continue;
  270. }
  271. foreach ( $group['groups'] as $subgroup ) {
  272. if ( in_array( $subgroup['id'], $groups[ $group['id'] ] ) ) {
  273. $subgroup_ids[ $subgroup['id'] ] = true;
  274. }
  275. }
  276. }
  277. }
  278. $data['groups'] = $subgroup_ids;
  279. }
  280. }
  281. $api->subscribe( $settings->list_id, $data );
  282. if ( $api->getLastError() ) {
  283. $response['error'] = sprintf(
  284. __( 'There was an error subscribing to MailChimp. %s', 'fl-builder' ),
  285. $api->getLastError()
  286. );
  287. }
  288. } catch ( Exception $e ) {
  289. $response['error'] = $e->getMessage();
  290. }// Try catch().
  291. }
  292. return $response;
  293. }
  294. }