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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * Helper class for the Ontraport API.
  4. *
  5. * @since 2.1
  6. */
  7. final class FLBuilderServiceOntraport extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 2.1
  12. * @var string $id
  13. */
  14. public $id = 'ontraport';
  15. /**
  16. * @since 2.1
  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 2.1
  25. * @param string $app_id A valid APP ID.
  26. * @param string $api_key A valid API key.
  27. * @return object The API instance.
  28. */
  29. public function get_api( $app_id, $api_key ) {
  30. if ( $this->api_instance ) {
  31. return $this->api_instance;
  32. }
  33. if ( ! class_exists( '\\OntraportAPI\\Ontraport' ) ) {
  34. require_once FL_BUILDER_DIR . 'includes/vendor/ontraport/Ontraport.php';
  35. }
  36. $this->api_instance = new \OntraportAPI\Ontraport( $app_id, $api_key );
  37. return $this->api_instance;
  38. }
  39. /**
  40. * Test the API connection.
  41. *
  42. * @since 2.1
  43. * @param array $fields {
  44. * @type string $api_key A valid API key.
  45. * }
  46. * @return array{
  47. * @type bool|string $error The error message or false if no error.
  48. * @type array $data An array of data used to make the connection.
  49. * }
  50. */
  51. public function connect( $fields = array() ) {
  52. $response = array(
  53. 'error' => false,
  54. 'data' => array(),
  55. );
  56. // Make sure we have an API token.
  57. if ( ! isset( $fields['app_id'] ) || empty( $fields['app_id'] ) ) {
  58. $response['error'] = __( 'Error: You must provide an APP ID.', 'fl-builder' );
  59. } // Make sure we have an Account ID.
  60. elseif ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  61. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  62. } // Try to connect and store the connection data.
  63. else {
  64. $api = $this->get_api( $fields['app_id'], $fields['api_key'] );
  65. // Try to request something to authenticate the validity of APP ID and API Key
  66. $search = json_decode( $api->contact()->retrieveMultiple( array(
  67. 'range' => 1,
  68. )));
  69. $status_code = $api->getLastStatusCode();
  70. if ( 200 === $status_code ) {
  71. $response['data'] = array(
  72. 'api_key' => $fields['api_key'],
  73. 'app_id' => $fields['app_id'],
  74. );
  75. } else {
  76. $response['error'] = sprintf(
  77. __( 'Error: Please check your API token. Code: %s', 'fl-builder' ),
  78. $status_code
  79. );
  80. }
  81. }
  82. return $response;
  83. }
  84. /**
  85. * Renders the markup for the connection settings.
  86. *
  87. * @since 2.1
  88. * @return string The connection settings markup.
  89. */
  90. public function render_connect_settings() {
  91. ob_start();
  92. FLBuilder::render_settings_field( 'app_id', array(
  93. 'row_class' => 'fl-builder-service-connect-row',
  94. 'class' => 'fl-builder-service-connect-input',
  95. 'type' => 'text',
  96. 'label' => __( 'APP ID', 'fl-builder' ),
  97. 'help' => __( 'Your APP ID can be found in your Ontraport account.', 'fl-builder' ),
  98. 'preview' => array(
  99. 'type' => 'none',
  100. ),
  101. ));
  102. FLBuilder::render_settings_field( 'api_key', array(
  103. 'row_class' => 'fl-builder-service-connect-row',
  104. 'class' => 'fl-builder-service-connect-input',
  105. 'type' => 'text',
  106. 'label' => __( 'API Key', 'fl-builder' ),
  107. 'help' => __( 'Your API key can be found in your Ontraport account.', 'fl-builder' ),
  108. 'preview' => array(
  109. 'type' => 'none',
  110. ),
  111. ));
  112. return ob_get_clean();
  113. }
  114. /**
  115. * Render the markup for service specific fields.
  116. *
  117. * @since 2.1
  118. * @param string $account The name of the saved account.
  119. * @param object $settings Saved module settings.
  120. * @return array {
  121. * @type bool|string $error The error message or false if no error.
  122. * @type string $html The field markup.
  123. * }
  124. */
  125. public function render_fields( $account, $settings ) {
  126. $account_data = $this->get_account_data( $account );
  127. $api = $this->get_api( $account_data['app_id'], $account_data['api_key'] );
  128. $campaigns = json_decode( $api->campaignbuilder()->retrieveMultiplePaginated( array(
  129. 'listFields' => 'id, name',
  130. 'start' => 0,
  131. 'range' => 50,
  132. )));
  133. $campaigns_list = array();
  134. if ( $campaigns ) {
  135. foreach ( $campaigns as $obj ) {
  136. if ( isset( $obj->data ) && count( $obj->data ) > 0 ) {
  137. $campaigns_list = array_merge( $campaigns_list, $obj->data );
  138. }
  139. }
  140. }
  141. $response = array(
  142. 'error' => false,
  143. 'html' => $this->render_campaigns_field( $campaigns_list, $settings ),
  144. );
  145. return $response;
  146. }
  147. /**
  148. * Render markup for the campaign field.
  149. *
  150. * @since 2.1
  151. * @param array $campaigns Campaigns data from the API.
  152. * @param object $settings Saved module settings.
  153. * @return string The markup for the campaign field.
  154. * @access private
  155. */
  156. private function render_campaigns_field( $campaigns, $settings ) {
  157. ob_start();
  158. $options = array(
  159. '0' => __( 'Choose...', 'fl-builder' ),
  160. );
  161. if ( $campaigns > 0 ) {
  162. foreach ( $campaigns as $campaign ) {
  163. $options[ $campaign->id ] = $campaign->name;
  164. }
  165. }
  166. FLBuilder::render_settings_field( 'campaign_id', array(
  167. 'row_class' => 'fl-builder-service-field-row',
  168. 'class' => 'fl-builder-service-list-select',
  169. 'type' => 'select',
  170. 'label' => _x( 'Campaign', 'An email campaign from your Ontraport account.', 'fl-builder' ),
  171. 'options' => $options,
  172. 'default' => 0,
  173. 'preview' => array(
  174. 'type' => 'none',
  175. ),
  176. ), $settings);
  177. return ob_get_clean();
  178. }
  179. /**
  180. * Subscribe an email address to Ontraport.
  181. *
  182. * @since 2.1
  183. * @param object $settings A module settings object.
  184. * @param string $email The email to subscribe.
  185. * @param string $name Optional. The full name of the person subscribing.
  186. * @return array {
  187. * @type bool|string $error The error message or false if no error.
  188. * }
  189. */
  190. public function subscribe( $settings, $email, $name = '' ) {
  191. $account_data = $this->get_account_data( $settings->service_account );
  192. $response = array(
  193. 'error' => false,
  194. );
  195. if ( ! $account_data ) {
  196. $response['error'] = __( 'There was an error subscribing to Ontraport. The account is no longer connected.', 'fl-builder' );
  197. } else {
  198. $api = $this->get_api( $account_data['app_id'], $account_data['api_key'] );
  199. $args = array(
  200. 'email' => $email,
  201. 'updateCampaign' => $settings->campaign_id,
  202. );
  203. // Add the name to the data array if we have one.
  204. if ( $name ) {
  205. $names = explode( ' ', $name );
  206. if ( isset( $names[0] ) ) {
  207. $args['firstname'] = $names[0];
  208. }
  209. if ( isset( $names[1] ) ) {
  210. $args['lastname'] = $names[1];
  211. }
  212. }
  213. // Save or update subscriber.
  214. $result = $api->contact()->saveOrUpdate( $args );
  215. if ( 200 !== $api->getLastStatusCode() ) {
  216. $response['error'] = sprintf(
  217. __( 'There was an error subscribing to Ontraport. Code: %s', 'fl-builder' ),
  218. $api->getLastStatusCode()
  219. );
  220. }
  221. }
  222. return $response;
  223. }
  224. }