class-fl-builder-service-getresponse.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * Helper class for the GetResponse API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceGetResponse extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'getresponse';
  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( 'GetResponse' ) ) {
  33. require_once FL_BUILDER_DIR . 'includes/vendor/getresponse/getresponse.php';
  34. }
  35. $this->api_instance = new GetResponse( $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. $api = $this->get_api( $fields['api_key'] );
  60. $ping = $api->ping();
  61. if ( ! $ping ) {
  62. $response['error'] = __( 'Error: Please check your API key.', 'fl-builder' );
  63. } else {
  64. $response['data'] = array(
  65. 'api_key' => $fields['api_key'],
  66. );
  67. }
  68. }
  69. return $response;
  70. }
  71. /**
  72. * Renders the markup for the connection settings.
  73. *
  74. * @since 1.5.4
  75. * @return string The connection settings markup.
  76. */
  77. public function render_connect_settings() {
  78. ob_start();
  79. FLBuilder::render_settings_field( 'api_key', array(
  80. 'row_class' => 'fl-builder-service-connect-row',
  81. 'class' => 'fl-builder-service-connect-input',
  82. 'type' => 'text',
  83. 'label' => __( 'API Key', 'fl-builder' ),
  84. 'help' => __( 'Your API key can be found in your GetResponse account under My Account > API & OAuth.', 'fl-builder' ),
  85. 'preview' => array(
  86. 'type' => 'none',
  87. ),
  88. ));
  89. return ob_get_clean();
  90. }
  91. /**
  92. * Render the markup for service specific fields.
  93. *
  94. * @since 1.5.4
  95. * @param string $account The name of the saved account.
  96. * @param object $settings Saved module settings.
  97. * @return array {
  98. * @type bool|string $error The error message or false if no error.
  99. * @type string $html The field markup.
  100. * }
  101. */
  102. public function render_fields( $account, $settings ) {
  103. $account_data = $this->get_account_data( $account );
  104. $api = $this->get_api( $account_data['api_key'] );
  105. $lists = $api->getCampaigns();
  106. $response = array(
  107. 'error' => false,
  108. 'html' => '',
  109. );
  110. if ( ! $lists ) {
  111. $response['error'] = __( 'Error: Please check your API key.', 'fl-builder' );
  112. } else {
  113. $response['html'] = $this->render_list_field( $lists, $settings );
  114. }
  115. return $response;
  116. }
  117. /**
  118. * Render markup for the list field.
  119. *
  120. * @since 1.5.4
  121. * @param array $lists List data from the API.
  122. * @param object $settings Saved module settings.
  123. * @return string The markup for the list field.
  124. * @access private
  125. */
  126. private function render_list_field( $lists, $settings ) {
  127. ob_start();
  128. $options = array(
  129. '' => __( 'Choose...', 'fl-builder' ),
  130. );
  131. foreach ( $lists as $id => $data ) {
  132. // @codingStandardsIgnoreLine
  133. $options[ $data->campaignId ] = $data->name;
  134. }
  135. FLBuilder::render_settings_field( 'list_id', array(
  136. 'row_class' => 'fl-builder-service-field-row',
  137. 'class' => 'fl-builder-service-list-select',
  138. 'type' => 'select',
  139. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  140. 'options' => $options,
  141. 'preview' => array(
  142. 'type' => 'none',
  143. ),
  144. ), $settings);
  145. FLBuilder::render_settings_field( 'cycle_day', array(
  146. 'row_class' => 'fl-builder-service-field-row',
  147. 'class' => 'fl-builder-service-cycleday-select',
  148. 'type' => 'select',
  149. 'label' => _x( 'Cycle Day', 'Day of autoresponder cycle.', 'fl-builder' ),
  150. 'help' => __( 'This should match the cycle day settings for the selected list\'s Autoresponder.', 'fl-builder' ),
  151. 'options' => range( 0, 30 ),
  152. 'preview' => array(
  153. 'type' => 'none',
  154. ),
  155. ), $settings);
  156. return ob_get_clean();
  157. }
  158. /**
  159. * Subscribe an email address to GetResponse.
  160. *
  161. * @since 1.5.4
  162. * @param object $settings A module settings object.
  163. * @param string $email The email to subscribe.
  164. * @param string $name Optional. The full name of the person subscribing.
  165. * @return array {
  166. * @type bool|string $error The error message or false if no error.
  167. * }
  168. */
  169. public function subscribe( $settings, $email, $name = '' ) {
  170. $account_data = $this->get_account_data( $settings->service_account );
  171. $response = array(
  172. 'error' => false,
  173. );
  174. if ( ! $account_data ) {
  175. $response['error'] = __( 'There was an error subscribing to GetResponse. The account is no longer connected.', 'fl-builder' );
  176. } else {
  177. $api = $this->get_api( $account_data['api_key'] );
  178. try {
  179. // Fix, name should not be empty
  180. if ( ! $name ) {
  181. $names = explode( '@', $email );
  182. $name = $names[0];
  183. }
  184. $cyle_day = isset( $settings->cycle_day ) ? $settings->cycle_day : 0;
  185. $data = array(
  186. 'email' => $email,
  187. 'name' => $name,
  188. 'campaign' => array(
  189. 'campaignId' => $settings->list_id,
  190. ),
  191. 'dayOfCycle' => $cyle_day,
  192. );
  193. // Check if email exists
  194. $get_contact = $api->getContacts(array(
  195. 'query' => array(
  196. 'email' => $email,
  197. 'campaignId' => $settings->list_id,
  198. ),
  199. 'fields' => 'name, email',
  200. ));
  201. // @codingStandardsIgnoreLine
  202. if ( $contact = (array) $get_contact ) {
  203. reset( $contact );
  204. $contact_id = $contact[0]->contactId;
  205. $result = $api->updateContact( $contact_id, $data );
  206. // New contact
  207. } else {
  208. $result = $api->addContact( $data );
  209. }
  210. } catch ( Exception $e ) {
  211. $response['error'] = sprintf(
  212. __( 'There was an error subscribing to GetResponse. %s', 'fl-builder' ),
  213. $e->getMessage()
  214. );
  215. }
  216. }
  217. return $response;
  218. }
  219. }