class-fl-builder-service-mailrelay.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Helper class for the Mailrelay API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceMailrelay extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'mailrelay';
  15. /**
  16. * The API url suffix for this service.
  17. *
  18. * @since 1.5.8
  19. * @access private
  20. * @var string $api_url
  21. */
  22. private $api_url = '/ccm/admin/api/version/2/&type=json';
  23. /**
  24. * Request data from the thir party API.
  25. *
  26. * @since 1.5.4
  27. * @param string $base_url Base URL where API is available
  28. * @param string $method Method to request available from this service.
  29. * @param array $params Data to be passed to API
  30. * @return array|object The API response.
  31. */
  32. private function get_api_response( $base_url, $params ) {
  33. // Exclude http:// for the specific service
  34. $base_url = preg_replace( '#^https?://#', '', $base_url );
  35. $response = wp_remote_post( 'https://' . $base_url . $this->api_url, array(
  36. 'timeout' => 60,
  37. 'body' => $params,
  38. ) );
  39. if ( is_wp_error( $response ) || (isset( $response->status ) && 0 == $response->status ) ) {
  40. if ( isset( $response->status ) ) {
  41. $data = json_decode( $response, true );
  42. } else {
  43. $data['error'] = $response->get_error_message();
  44. }
  45. } else {
  46. $data = json_decode( wp_remote_retrieve_body( $response ), true );
  47. }
  48. return $data;
  49. }
  50. /**
  51. * Test the API connection.
  52. *
  53. * @since 1.5.4
  54. * @param array $fields {
  55. * @type string $api_host A valid Host.
  56. * @type string $api_key A valid API key.
  57. * }
  58. * @return array{
  59. * @type bool|string $error The error message or false if no error.
  60. * @type array $data An array of data used to make the connection.
  61. * }
  62. */
  63. public function connect( $fields = array() ) {
  64. $response = array(
  65. 'error' => false,
  66. 'data' => array(),
  67. );
  68. // Make sure we have the Host.
  69. if ( ! isset( $fields['api_host'] ) || empty( $fields['api_host'] ) ) {
  70. $response['error'] = __( 'Error: You must provide a Host.', 'fl-builder' );
  71. } elseif ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  72. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  73. } // Try to connect and store the connection data.
  74. else {
  75. $result = $this->get_api_response( $fields['api_host'], array(
  76. 'function' => 'getGroups',
  77. 'apiKey' => $fields['api_key'],
  78. 'offset' => 0,
  79. 'count' => 1,
  80. ) );
  81. if ( ! isset( $result['error'] ) ) {
  82. $response['data'] = array(
  83. 'api_host' => $fields['api_host'],
  84. 'api_key' => $fields['api_key'],
  85. );
  86. } else {
  87. $response['error'] = sprintf( __( 'Error: Could not connect to Mailrelay. %s', 'fl-builder' ), $result['error'] );
  88. }
  89. }
  90. return $response;
  91. }
  92. /**
  93. * Renders the markup for the connection settings.
  94. *
  95. * @since 1.5.4
  96. * @return string The connection settings markup.
  97. */
  98. public function render_connect_settings() {
  99. ob_start();
  100. FLBuilder::render_settings_field( 'api_host', array(
  101. 'row_class' => 'fl-builder-service-connect-row',
  102. 'class' => 'fl-builder-service-connect-input',
  103. 'type' => 'text',
  104. 'label' => __( 'Host', 'fl-builder' ),
  105. 'help' => __( 'The host you chose when you signed up for your account. Check your welcome email if you forgot it. Please enter it without the initial http:// (e.g. demo.ip-zone.com).', 'fl-builder' ),
  106. 'preview' => array(
  107. 'type' => 'none',
  108. ),
  109. ));
  110. FLBuilder::render_settings_field( 'api_key', array(
  111. 'row_class' => 'fl-builder-service-connect-row',
  112. 'class' => 'fl-builder-service-connect-input',
  113. 'type' => 'text',
  114. 'label' => __( 'API Key', 'fl-builder' ),
  115. 'help' => __( 'Your API key can be found in your Mailrelay account under Menu > Settings > API access.', 'fl-builder' ),
  116. 'preview' => array(
  117. 'type' => 'none',
  118. ),
  119. ));
  120. return ob_get_clean();
  121. }
  122. /**
  123. * Render the markup for service specific fields.
  124. *
  125. * @since 1.5.4
  126. * @param string $account The name of the saved account.
  127. * @param object $settings Saved module settings.
  128. * @return array {
  129. * @type bool|string $error The error message or false if no error.
  130. * @type string $html The field markup.
  131. * }
  132. */
  133. public function render_fields( $account, $settings ) {
  134. $account_data = $this->get_account_data( $account );
  135. $result = $this->get_api_response( $account_data['api_host'], array(
  136. 'function' => 'getGroups',
  137. 'apiKey' => $account_data['api_key'],
  138. ) );
  139. $response = array(
  140. 'error' => false,
  141. 'html' => '',
  142. );
  143. if ( isset( $result['error'] ) ) {
  144. $response['error'] = sprintf( __( 'Error: Please check your API key. %s', 'fl-builder' ), $result['error'] );
  145. } else {
  146. $response['html'] = $this->render_list_field( $result['data'], $settings );
  147. }
  148. return $response;
  149. }
  150. /**
  151. * Render markup for the list field.
  152. *
  153. * @since 1.5.4
  154. * @param array $lists List data from the API.
  155. * @param object $settings Saved module settings.
  156. * @return string The markup for the list field.
  157. * @access private
  158. */
  159. private function render_list_field( $groups, $settings ) {
  160. ob_start();
  161. $options = array(
  162. '' => __( 'Choose...', 'fl-builder' ),
  163. );
  164. foreach ( $groups as $group ) {
  165. $options[ $group['id'] ] = $group['name'];
  166. }
  167. FLBuilder::render_settings_field( 'list_id', array(
  168. 'row_class' => 'fl-builder-service-field-row',
  169. 'class' => 'fl-builder-service-list-select',
  170. 'type' => 'select',
  171. 'multi-select' => true,
  172. 'label' => _x( 'Group', 'A list of subscribers group from a Mailrelay account.', 'fl-builder' ),
  173. 'options' => $options,
  174. 'preview' => array(
  175. 'type' => 'none',
  176. ),
  177. ), $settings);
  178. return ob_get_clean();
  179. }
  180. /**
  181. * Subscribe an email address to Mailrelay.
  182. *
  183. * @since 1.5.4
  184. * @param object $settings A module settings object.
  185. * @param string $email The email to subscribe.
  186. * @param string $name Optional. The full name of the person subscribing.
  187. * @return array {
  188. * @type bool|string $error The error message or false if no error.
  189. * }
  190. */
  191. public function subscribe( $settings, $email, $name = '' ) {
  192. $account_data = $this->get_account_data( $settings->service_account );
  193. $response = array(
  194. 'error' => false,
  195. );
  196. if ( ! $account_data ) {
  197. $response['error'] = __( 'There was an error subscribing to Mailrelay. The account is no longer connected.', 'fl-builder' );
  198. } else {
  199. $result = $this->get_api_response( $account_data['api_host'], array(
  200. 'function' => 'addSubscriber',
  201. 'apiKey' => $account_data['api_key'],
  202. 'email' => $email,
  203. 'name' => $name,
  204. 'groups' => $settings->list_id,
  205. ) );
  206. if ( isset( $result['error'] ) ) {
  207. $response['error'] = sprintf( __( 'There was an error subscribing to Mailrelay. %s', 'fl-builder' ), $result['error'] );
  208. }
  209. }
  210. return $response;
  211. }
  212. }