class-fl-builder-service-campaign-monitor.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Helper class for the Campaign Monitor API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceCampaignMonitor extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'campaign-monitor';
  15. /**
  16. * Constructor function.
  17. *
  18. * @since 1.5.4
  19. * @return void
  20. */
  21. public function __construct() {
  22. if ( ! class_exists( 'CS_REST_General' ) ) {
  23. require_once FL_BUILDER_DIR . 'includes/vendor/campaign-monitor/csrest_general.php';
  24. require_once FL_BUILDER_DIR . 'includes/vendor/campaign-monitor/csrest_clients.php';
  25. require_once FL_BUILDER_DIR . 'includes/vendor/campaign-monitor/csrest_lists.php';
  26. require_once FL_BUILDER_DIR . 'includes/vendor/campaign-monitor/csrest_subscribers.php';
  27. }
  28. }
  29. /**
  30. * Test the API connection.
  31. *
  32. * @since 1.5.4
  33. * @param array $fields {
  34. * @type string $api_key A valid API key.
  35. * }
  36. * @return array{
  37. * @type bool|string $error The error message or false if no error.
  38. * @type array $data An array of data used to make the connection.
  39. * }
  40. */
  41. public function connect( $fields = array() ) {
  42. $response = array(
  43. 'error' => false,
  44. 'data' => array(),
  45. );
  46. // Make sure we have an API key.
  47. if ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  48. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  49. } else {
  50. $api = new CS_REST_General( array(
  51. 'api_key' => $fields['api_key'],
  52. ) );
  53. $result = $api->get_clients();
  54. if ( $result->was_successful() ) {
  55. $response['data'] = array(
  56. 'api_key' => $fields['api_key'],
  57. );
  58. } else {
  59. $response['error'] = __( 'Error: Please check your API key.', 'fl-builder' );
  60. }
  61. }
  62. return $response;
  63. }
  64. /**
  65. * Renders the markup for the connection settings.
  66. *
  67. * @since 1.5.4
  68. * @return string The connection settings markup.
  69. */
  70. public function render_connect_settings() {
  71. ob_start();
  72. FLBuilder::render_settings_field( 'api_key', array(
  73. 'row_class' => 'fl-builder-service-connect-row',
  74. 'class' => 'fl-builder-service-connect-input',
  75. 'type' => 'text',
  76. 'label' => __( 'API Key', 'fl-builder' ),
  77. 'help' => __( 'Your API key can be found in your Campaign Monitor account under Account Settings > API Key.', 'fl-builder' ),
  78. 'preview' => array(
  79. 'type' => 'none',
  80. ),
  81. ));
  82. return ob_get_clean();
  83. }
  84. /**
  85. * Render the markup for service specific fields.
  86. *
  87. * @since 1.5.4
  88. * @param string $account The name of the saved account.
  89. * @param object $settings Saved module settings.
  90. * @return array {
  91. * @type bool|string $error The error message or false if no error.
  92. * @type string $html The field markup.
  93. * }
  94. */
  95. public function render_fields( $account, $settings ) {
  96. $post_data = FLBuilderModel::get_post_data();
  97. $account_data = $this->get_account_data( $account );
  98. $api = new CS_REST_General( $account_data );
  99. $result = $api->get_clients();
  100. $response = array(
  101. 'error' => false,
  102. 'html' => '',
  103. );
  104. if ( $result->was_successful() ) {
  105. if ( ! isset( $post_data['client'] ) ) {
  106. $response['html'] .= $this->render_client_field( $result, $settings );
  107. }
  108. $response['html'] .= $this->render_list_field( $account_data, $settings );
  109. } else {
  110. $response['error'] = __( 'Error: Please check your API key.', 'fl-builder' );
  111. }
  112. return $response;
  113. }
  114. /**
  115. * Render markup for the client field.
  116. *
  117. * @since 1.5.4
  118. * @param array $clients Client data from the API.
  119. * @param object $settings Saved module settings.
  120. * @return string The markup for the list field.
  121. * @access private
  122. */
  123. private function render_client_field( $clients, $settings ) {
  124. ob_start();
  125. $options = array(
  126. '' => __( 'Choose...', 'fl-builder' ),
  127. );
  128. foreach ( $clients->response as $client ) {
  129. // @codingStandardsIgnoreLine
  130. $options[ $client->ClientID ] = $client->Name;
  131. }
  132. FLBuilder::render_settings_field( 'client_id', array(
  133. 'row_class' => 'fl-builder-service-field-row',
  134. 'class' => 'fl-builder-campaign-monitor-client-select',
  135. 'type' => 'select',
  136. 'label' => _x( 'Client', 'A client account in Campaign Monitor.', 'fl-builder' ),
  137. 'options' => $options,
  138. 'preview' => array(
  139. 'type' => 'none',
  140. ),
  141. ), $settings);
  142. return ob_get_clean();
  143. }
  144. /**
  145. * Render markup for the list field.
  146. *
  147. * @since 1.5.4
  148. * @param array $account_data Saved account data.
  149. * @param object $settings Saved module settings.
  150. * @return string The markup for the list field.
  151. * @access private
  152. */
  153. private function render_list_field( $account_data, $settings ) {
  154. $post_data = FLBuilderModel::get_post_data();
  155. // Get the client ID. Return an empty string if we don't have one yet.
  156. if ( isset( $post_data['client'] ) ) {
  157. $client_id = $post_data['client'];
  158. } elseif ( isset( $settings->client_id ) ) {
  159. $client_id = $settings->client_id;
  160. } else {
  161. return '';
  162. }
  163. // Get the list data.
  164. $api = new CS_REST_Clients( $client_id, $account_data );
  165. $lists = $api->get_lists();
  166. // Render the list field.
  167. ob_start();
  168. $options = array(
  169. '' => __( 'Choose...', 'fl-builder' ),
  170. );
  171. foreach ( $lists->response as $list ) {
  172. // @codingStandardsIgnoreLine
  173. $options[ $list->ListID ] = $list->Name;
  174. }
  175. FLBuilder::render_settings_field( 'list_id', array(
  176. 'row_class' => 'fl-builder-service-field-row',
  177. 'class' => 'fl-builder-service-list-select',
  178. 'type' => 'select',
  179. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  180. 'options' => $options,
  181. 'preview' => array(
  182. 'type' => 'none',
  183. ),
  184. ), $settings);
  185. return ob_get_clean();
  186. }
  187. /**
  188. * Subscribe an email address to Campaign Monitor.
  189. *
  190. * @since 1.5.4
  191. * @param object $settings A module settings object.
  192. * @param string $email The email to subscribe.
  193. * @param string $name Optional. The full name of the person subscribing.
  194. * @return array {
  195. * @type bool|string $error The error message or false if no error.
  196. * }
  197. */
  198. public function subscribe( $settings, $email, $name = false ) {
  199. $account_data = $this->get_account_data( $settings->service_account );
  200. $response = array(
  201. 'error' => false,
  202. );
  203. if ( ! $account_data ) {
  204. $response['error'] = __( 'There was an error subscribing to Campaign Monitor. The account is no longer connected.', 'fl-builder' );
  205. } else {
  206. $api = new CS_Rest_Subscribers( $settings->list_id, $account_data );
  207. $data = array(
  208. 'EmailAddress' => $email,
  209. 'Resubscribe' => true,
  210. );
  211. if ( $name ) {
  212. $data['Name'] = $name;
  213. }
  214. $result = $api->add( $data );
  215. if ( ! $result->was_successful() ) {
  216. $response['error'] = __( 'There was an error subscribing to Campaign Monitor.', 'fl-builder' );
  217. }
  218. }
  219. return $response;
  220. }
  221. }