class-fl-builder-service-mautic.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Helper class for the Mautic API.
  4. *
  5. * @since 1.10.6
  6. */
  7. final class FLBuilderServiceMautic extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.10.6
  12. * @var string $id
  13. */
  14. public $id = 'mautic';
  15. /**
  16. * @since 1.10.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.10.6
  25. * @param array $args A valid API authentication data.
  26. * @return object The API instance.
  27. */
  28. public function get_api( array $args ) {
  29. if ( $this->api_instance ) {
  30. return $this->api_instance;
  31. }
  32. if ( ! class_exists( 'MauticApi' ) ) {
  33. require_once FL_BUILDER_DIR . 'includes/vendor/mautic/MauticApi.php';
  34. }
  35. $settings = array(
  36. 'userName' => $args['api_username'], // The username - set up a new user for each external site
  37. 'password' => $args['api_password'], // Make this a Long passPhrase e.g. (Try: !wE4.And.*@ws4.Guess! )
  38. 'apiUrl' => $args['api_host'], // NOTE: Required for Unit tests; *must* contain a valid url
  39. );
  40. $this->api_instance = new MauticApi( $settings );
  41. return $this->api_instance;
  42. }
  43. /**
  44. * Test the API connection.
  45. *
  46. * @since 1.10.6
  47. * @param array $fields {
  48. * @type string $api_host A valid Host.
  49. * @type string $api_key A valid API key.
  50. * }
  51. * @return array{
  52. * @type bool|string $error The error message or false if no error.
  53. * @type array $data An array of data used to make the connection.
  54. * }
  55. */
  56. public function connect( $fields = array() ) {
  57. $response = array(
  58. 'error' => false,
  59. 'data' => array(),
  60. );
  61. // Make sure we have the Host.
  62. if ( ! isset( $fields['api_host'] ) || empty( $fields['api_host'] ) ) {
  63. $response['error'] = __( 'Error: You must provide your Mautic installation URL.', 'fl-builder' );
  64. }
  65. // Make sure we have a username
  66. if ( ! isset( $fields['api_username'] ) || empty( $fields['api_username'] ) ) {
  67. $response['error'] = __( 'Error: You must provide your Mautic app username.', 'fl-builder' );
  68. }
  69. // Make sure we have password
  70. if ( ! isset( $fields['api_password'] ) || empty( $fields['api_password'] ) ) {
  71. $response['error'] = __( 'Error: You must provide your Mautic app user password.', 'fl-builder' );
  72. } // Try to connect and store the connection data.
  73. else {
  74. $api = $this->get_api( array(
  75. 'api_host' => $fields['api_host'],
  76. 'api_username' => $fields['api_username'],
  77. 'api_password' => $fields['api_password'],
  78. ) );
  79. // Try sending request to verify credentials
  80. $get_response = $api->getSegments( array(
  81. 'limit' => 1,
  82. ) );
  83. $response_info = $api->getResponseInfo();
  84. if ( in_array( $response_info['http_code'], array( 200, 201 ) ) ) {
  85. $response['data'] = array(
  86. 'api_host' => $fields['api_host'],
  87. 'api_username' => $fields['api_username'],
  88. 'api_password' => $fields['api_password'],
  89. );
  90. } else {
  91. $error_message = $response_info['http_code'];
  92. if ( isset( $get_response['errors'] ) && count( $get_response['errors'] ) > 0 ) {
  93. $error_message = '[' . $get_response['errors'][0]['code'] . '] ' . $get_response['errors'][0]['message'];
  94. }
  95. $response['error'] = sprintf( __( 'Error: Could not connect to Mautic. %s', 'fl-builder' ), $error_message );
  96. }
  97. }
  98. return $response;
  99. }
  100. /**
  101. * Renders the markup for the connection settings.
  102. *
  103. * @since 1.10.6
  104. * @return string The connection settings markup.
  105. */
  106. public function render_connect_settings() {
  107. ob_start();
  108. FLBuilder::render_settings_field( 'api_host', array(
  109. 'row_class' => 'fl-builder-service-connect-row',
  110. 'class' => 'fl-builder-service-connect-input',
  111. 'type' => 'text',
  112. 'label' => __( 'Installation URL', 'fl-builder' ),
  113. 'help' => __( 'The URL where your Mautic application is installed (e.g. http://mautic.mywebsite.com).', 'fl-builder' ),
  114. 'description' => __( 'API should be enabled in your Mautic application.
  115. Go to Mautic Configuration / API Settings and set `API enabled` to `Yes`, set `Enable HTTP basic auth` to `Yes` . Save changes.', 'fl-builder' ),
  116. 'preview' => array(
  117. 'type' => 'none',
  118. ),
  119. ));
  120. FLBuilder::render_settings_field( 'api_username', array(
  121. 'row_class' => 'fl-builder-service-connect-row',
  122. 'class' => 'fl-builder-service-connect-input',
  123. 'type' => 'text',
  124. 'label' => __( 'Mautic Username', 'fl-builder' ),
  125. 'help' => __( 'Username from your Mautic application. Make sure it has `Full system access`. Best practice would be to set up a new user for each external site.', 'fl-builder' ),
  126. 'preview' => array(
  127. 'type' => 'none',
  128. ),
  129. ));
  130. FLBuilder::render_settings_field( 'api_password', array(
  131. 'row_class' => 'fl-builder-service-connect-row',
  132. 'class' => 'fl-builder-service-connect-input',
  133. 'type' => 'text',
  134. 'label' => __( 'Mautic Password', 'fl-builder' ),
  135. 'help' => __( 'Password associated with the username. Make this a Long passPhrase.', 'fl-builder' ),
  136. 'preview' => array(
  137. 'type' => 'none',
  138. ),
  139. ));
  140. return ob_get_clean();
  141. }
  142. /**
  143. * Render the markup for service specific fields.
  144. *
  145. * @since 1.10.6
  146. * @param string $account The name of the saved account.
  147. * @param object $settings Saved module settings.
  148. * @return array {
  149. * @type bool|string $error The error message or false if no error.
  150. * @type string $html The field markup.
  151. * }
  152. */
  153. public function render_fields( $account, $settings ) {
  154. $account_data = $this->get_account_data( $account );
  155. $api = $this->get_api( array(
  156. 'api_host' => $account_data['api_host'],
  157. 'api_username' => $account_data['api_username'],
  158. 'api_password' => $account_data['api_password'],
  159. ) );
  160. $lists = $api->getSegments();
  161. $response = array(
  162. 'error' => false,
  163. 'html' => '',
  164. );
  165. if ( ! isset( $lists['lists'] ) ) {
  166. $response['error'] = __( 'Error: Please check your API credentials.', 'fl-builder' );
  167. } else {
  168. $response['html'] = $this->render_list_field( $lists['lists'], $settings );
  169. }
  170. return $response;
  171. }
  172. /**
  173. * Render markup for the list field.
  174. *
  175. * @since 1.10.6
  176. * @param array $lists List data from the API.
  177. * @param object $settings Saved module settings.
  178. * @return string The markup for the list field.
  179. * @access private
  180. */
  181. private function render_list_field( $lists, $settings ) {
  182. ob_start();
  183. $options = array(
  184. '' => __( 'Choose...', 'fl-builder' ),
  185. );
  186. foreach ( $lists as $list ) {
  187. $options[ $list['id'] ] = $list['name'];
  188. }
  189. FLBuilder::render_settings_field( 'list_id', array(
  190. 'row_class' => 'fl-builder-service-field-row',
  191. 'class' => 'fl-builder-service-list-select',
  192. 'type' => 'select',
  193. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  194. 'options' => $options,
  195. 'preview' => array(
  196. 'type' => 'none',
  197. ),
  198. ), $settings);
  199. return ob_get_clean();
  200. }
  201. /**
  202. * Subscribe an email address to Mautic.
  203. *
  204. * @since 1.10.6
  205. * @param object $settings A module settings object.
  206. * @param string $email The email to subscribe.
  207. * @param string $name Optional. The full name of the person subscribing.
  208. * @return array {
  209. * @type bool|string $error The error message or false if no error.
  210. * }
  211. */
  212. public function subscribe( $settings, $email, $name = '' ) {
  213. $account_data = $this->get_account_data( $settings->service_account );
  214. $response = array(
  215. 'error' => false,
  216. );
  217. if ( ! $account_data ) {
  218. $response['error'] = __( 'There was an error subscribing to Mautic. The account is no longer connected.', 'fl-builder' );
  219. } else {
  220. $api = $this->get_api( array(
  221. 'api_host' => $account_data['api_host'],
  222. 'api_username' => $account_data['api_username'],
  223. 'api_password' => $account_data['api_password'],
  224. ) );
  225. $data = array(
  226. 'email' => $email,
  227. 'ipAddress' => $_SERVER['REMOTE_ADDR'],
  228. 'segmentId' => $settings->list_id,
  229. );
  230. if ( $name ) {
  231. $names = explode( ' ', $name );
  232. }
  233. if ( isset( $names[0] ) ) {
  234. $data['firstname'] = $names[0];
  235. }
  236. if ( isset( $names[1] ) ) {
  237. $data['lastname'] = $names[1];
  238. }
  239. // Add new contact
  240. $get_api_response = $api->subscribe( $data );
  241. $response_info = $api->getResponseInfo();
  242. if ( isset( $get_api_response['errors'] ) && count( $get_api_response['errors'] ) > 0 ) {
  243. $response['error'] = sprintf(
  244. __( 'There was an error subscribing to Mautic. %s', 'fl-builder' ),
  245. '[' . $get_api_response['errors'][0]['code'] . '] ' . $get_api_response['errors'][0]['message']
  246. );
  247. }
  248. }
  249. return $response;
  250. }
  251. }