class-fl-builder-service-mailpoet.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Helper class for MailPoet.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceMailPoet extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'mailpoet';
  15. /**
  16. * Test the API connection.
  17. *
  18. * @since 1.5.4
  19. * @param array $fields
  20. * @return array{
  21. * @type bool|string $error The error message or false if no error.
  22. * @type array $data An array of data used to make the connection.
  23. * }
  24. */
  25. public function connect( $fields = array() ) {
  26. $response = array(
  27. 'error' => false,
  28. 'data' => array(),
  29. );
  30. return $response;
  31. }
  32. /**
  33. * Renders the markup for the connection settings.
  34. *
  35. * @since 1.5.4
  36. * @return string The connection settings markup.
  37. */
  38. public function render_connect_settings() {
  39. return '';
  40. }
  41. /**
  42. * Render the markup for service specific fields.
  43. *
  44. * @since 1.5.4
  45. * @param string $account The name of the saved account.
  46. * @param object $settings Saved module settings.
  47. * @return array {
  48. * @type bool|string $error The error message or false if no error.
  49. * @type string $html The field markup.
  50. * }
  51. */
  52. public function render_fields( $account, $settings ) {
  53. $response = array(
  54. 'error' => false,
  55. 'html' => '',
  56. );
  57. $lists = array();
  58. try {
  59. // MailPoet 2+
  60. if ( class_exists( 'WYSIJA' ) ) {
  61. $list_model = WYSIJA::get( 'list', 'model' );
  62. $lists = $list_model->get( array( 'name', 'list_id' ), array(
  63. 'is_enabled' => 1,
  64. ) );
  65. // MailPoet 3.0
  66. } elseif ( defined( 'MAILPOET_INITIALIZED' ) && true === MAILPOET_INITIALIZED ) {
  67. $listing = new MailPoet\Listing\Handler( '\MailPoet\Models\Segment' );
  68. $listing_data = $listing->get();
  69. if ( isset( $listing_data['items'] ) ) {
  70. foreach ( $listing_data['items'] as $segment ) {
  71. $lists[] = array(
  72. 'list_id' => $segment->id,
  73. 'name' => $segment->name,
  74. );
  75. }
  76. }
  77. }
  78. $response['html'] = self::render_list_field( $lists, $settings );
  79. } catch ( Exception $e ) {
  80. $response['error'] = __( 'There was an error retrieveing your lists.', 'fl-builder' );
  81. }
  82. return $response;
  83. }
  84. /**
  85. * Render markup for the list field.
  86. *
  87. * @since 1.5.4
  88. * @param array $lists List data from the API.
  89. * @param object $settings Saved module settings.
  90. * @return string The markup for the list field.
  91. * @access private
  92. */
  93. private function render_list_field( $lists, $settings ) {
  94. ob_start();
  95. $options = array(
  96. '' => __( 'Choose...', 'fl-builder' ),
  97. );
  98. foreach ( $lists as $list ) {
  99. $options[ $list['list_id'] ] = $list['name'];
  100. }
  101. FLBuilder::render_settings_field( 'list_id', array(
  102. 'row_class' => 'fl-builder-service-field-row',
  103. 'class' => 'fl-builder-service-list-select',
  104. 'type' => 'select',
  105. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  106. 'options' => $options,
  107. 'preview' => array(
  108. 'type' => 'none',
  109. ),
  110. ), $settings);
  111. return ob_get_clean();
  112. }
  113. /**
  114. * Subscribe an email address to MailPoet.
  115. *
  116. * @since 1.5.4
  117. * @param object $settings A module settings object.
  118. * @param string $email The email to subscribe.
  119. * @param string $name Optional. The full name of the person subscribing.
  120. * @return array {
  121. * @type bool|string $error The error message or false if no error.
  122. * }
  123. */
  124. public function subscribe( $settings, $email, $name = false ) {
  125. $response = array(
  126. 'error' => false,
  127. );
  128. $user = array(
  129. 'email' => $email,
  130. );
  131. if ( ! class_exists( 'WYSIJA' )
  132. && ( ! defined( 'MAILPOET_INITIALIZED' ) || ( defined( 'MAILPOET_INITIALIZED' ) && false === MAILPOET_INITIALIZED ) )
  133. ) {
  134. $response['error'] = __( 'There was an error subscribing. MailPoet is not installed.', 'fl-builder' );
  135. } else {
  136. if ( $name ) {
  137. $names = explode( ' ', $name );
  138. }
  139. // MailPoet 2+
  140. if ( class_exists( 'WYSIJA' ) ) {
  141. if ( $names && isset( $names[0] ) ) {
  142. $user['firstname'] = $names[0];
  143. }
  144. if ( $names && isset( $names[1] ) ) {
  145. $user['lastname'] = $names[1];
  146. }
  147. $helper = WYSIJA::get( 'user', 'helper' );
  148. $helper->addSubscriber( array(
  149. 'user' => $user,
  150. 'user_list' => array(
  151. 'list_ids' => array( $settings->list_id ),
  152. ),
  153. ));
  154. // MailPoet 3.0
  155. } elseif ( defined( 'MAILPOET_INITIALIZED' ) && true === MAILPOET_INITIALIZED ) {
  156. $subscriber = new MailPoet\Models\Subscriber();
  157. if ( $names && isset( $names[0] ) ) {
  158. $user['first_name'] = $names[0];
  159. }
  160. if ( $names && isset( $names[1] ) ) {
  161. $user['last_name'] = $names[1];
  162. }
  163. $subscribed = $subscriber::subscribe( $user, array( $settings->list_id ) );
  164. $errors = $subscribed->getErrors();
  165. if ( false !== $errors ) {
  166. $response['error'] = sprintf( __( 'There was an error subscribing to MailPoet. %s', 'fl-builder' ), $errors[0] );
  167. }
  168. }
  169. }
  170. return $response;
  171. }
  172. }