class-fl-builder-service-aweber.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * Helper class for the AWeber API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceAWeber extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'aweber';
  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 $auth_code A valid authorization code.
  26. * @return object The API instance.
  27. */
  28. public function get_api( $auth_code ) {
  29. if ( $this->api_instance ) {
  30. return $this->api_instance;
  31. }
  32. if ( ! class_exists( 'AWeberAPI' ) ) {
  33. require_once FL_BUILDER_DIR . 'includes/vendor/aweber/aweber_api.php';
  34. }
  35. list( $auth_key, $auth_token, $req_key, $req_token, $oauth ) = explode( '|', $auth_code );
  36. $this->api_instance = new AWeberAPI( $auth_key, $auth_token );
  37. $this->api_instance->user->requestToken = $req_key;
  38. $this->api_instance->user->tokenSecret = $req_token;
  39. $this->api_instance->user->verifier = $oauth;
  40. return $this->api_instance;
  41. }
  42. /**
  43. * Test the API connection.
  44. *
  45. * @since 1.5.4
  46. * @param array $fields {
  47. * @type string $auth_code A valid authorization code.
  48. * }
  49. * @return array{
  50. * @type bool|string $error The error message or false if no error.
  51. * @type array $data An array of data used to make the connection.
  52. * }
  53. */
  54. public function connect( $fields = array() ) {
  55. $response = array(
  56. 'error' => false,
  57. 'data' => array(),
  58. );
  59. // Make sure we have an authorization code.
  60. if ( ! isset( $fields['auth_code'] ) || empty( $fields['auth_code'] ) ) {
  61. $response['error'] = __( 'Error: You must provide an Authorization Code.', 'fl-builder' );
  62. } elseif ( 6 != count( explode( '|', $fields['auth_code'] ) ) ) {
  63. $response['error'] = __( 'Error: Please enter a valid Authorization Code.', 'fl-builder' );
  64. } // Try to connect and store the connection data.
  65. else {
  66. $api = $this->get_api( $fields['auth_code'] );
  67. // Get an access token from the API.
  68. try {
  69. list( $access_token, $access_token_secret ) = $api->getAccessToken();
  70. } catch ( AWeberException $e ) {
  71. $response['error'] = $e->getMessage();
  72. }
  73. // Make sure we can get the account.
  74. try {
  75. $account = $api->getAccount();
  76. } catch ( AWeberException $e ) {
  77. $response['error'] = $e->getMessage();
  78. }
  79. // Build the response data.
  80. if ( ! $response['error'] ) {
  81. $response['data'] = array(
  82. 'auth_code' => $fields['auth_code'],
  83. 'access_token' => $access_token,
  84. 'access_secret' => $access_token_secret,
  85. );
  86. }
  87. }
  88. return $response;
  89. }
  90. /**
  91. * Renders the markup for the connection settings.
  92. *
  93. * @since 1.5.4
  94. * @return string The connection settings markup.
  95. */
  96. public function render_connect_settings() {
  97. ob_start();
  98. FLBuilder::render_settings_field( 'auth_code', array(
  99. 'row_class' => 'fl-builder-service-connect-row',
  100. 'class' => 'fl-builder-service-connect-input',
  101. 'type' => 'text',
  102. 'label' => __( 'Authorization Code', 'fl-builder' ),
  103. 'description' => sprintf( __( 'Please register this website with AWeber to get your Authorization Code. <a%s>Register Now</a>', 'fl-builder' ), ' href="https://auth.aweber.com/1.0/oauth/authorize_app/baa1f131" target="_blank"' ),
  104. 'preview' => array(
  105. 'type' => 'none',
  106. ),
  107. ));
  108. return ob_get_clean();
  109. }
  110. /**
  111. * Render the markup for service specific fields.
  112. *
  113. * @since 1.5.4
  114. * @param string $account The name of the saved account.
  115. * @param object $settings Saved module settings.
  116. * @return array {
  117. * @type bool|string $error The error message or false if no error.
  118. * @type string $html The field markup.
  119. * }
  120. */
  121. public function render_fields( $account, $settings ) {
  122. $account_data = $this->get_account_data( $account );
  123. $api = $this->get_api( $account_data['auth_code'] );
  124. $response = array(
  125. 'error' => false,
  126. 'html' => '',
  127. );
  128. try {
  129. $account = $api->getAccount( $account_data['access_token'], $account_data['access_secret'] );
  130. $lists = $account->loadFromUrl( '/accounts/' . $account->id . '/lists' );
  131. $response['html'] = $this->render_list_field( $lists, $settings );
  132. $response['html'] .= $this->render_tags_field( $settings );
  133. } catch ( AWeberException $e ) {
  134. $response['error'] = $e->getMessage();
  135. }
  136. return $response;
  137. }
  138. /**
  139. * Render markup for the list field.
  140. *
  141. * @since 1.5.4
  142. * @param array $lists List data from the API.
  143. * @param object $settings Saved module settings.
  144. * @return string The markup for the list field.
  145. * @access private
  146. */
  147. private function render_list_field( $lists, $settings ) {
  148. ob_start();
  149. $options = array(
  150. '' => __( 'Choose...', 'fl-builder' ),
  151. );
  152. foreach ( $lists->data['entries'] as $list ) {
  153. $options[ $list['id'] ] = $list['name'];
  154. }
  155. FLBuilder::render_settings_field( 'list_id', array(
  156. 'row_class' => 'fl-builder-service-field-row',
  157. 'class' => 'fl-builder-service-list-select',
  158. 'type' => 'select',
  159. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  160. 'options' => $options,
  161. 'preview' => array(
  162. 'type' => 'none',
  163. ),
  164. ), $settings);
  165. return ob_get_clean();
  166. }
  167. /**
  168. * Render markup for the tags field.
  169. *
  170. * @since 1.8.8
  171. * @param object $settings Saved module settings.
  172. * @return string The markup for the tags field.
  173. * @access private
  174. */
  175. private function render_tags_field( $settings ) {
  176. ob_start();
  177. FLBuilder::render_settings_field( 'tags', array(
  178. 'row_class' => 'fl-builder-service-connect-row',
  179. 'class' => 'fl-builder-service-connect-input',
  180. 'type' => 'text',
  181. 'label' => _x( 'Tags', 'A comma separated list of tags.', 'fl-builder' ),
  182. 'help' => __( 'A comma separated list of tags.', 'fl-builder' ),
  183. 'preview' => array(
  184. 'type' => 'none',
  185. ),
  186. ),$settings);
  187. return ob_get_clean();
  188. }
  189. /**
  190. * Subscribe an email address to AWeber.
  191. *
  192. * @since 1.5.4
  193. * @param object $settings A module settings object.
  194. * @param string $email The email to subscribe.
  195. * @param string $name Optional. The full name of the person subscribing.
  196. * @return array {
  197. * @type bool|string $error The error message or false if no error.
  198. * }
  199. */
  200. public function subscribe( $settings, $email, $name = false ) {
  201. $account_data = $this->get_account_data( $settings->service_account );
  202. $response = array(
  203. 'error' => false,
  204. );
  205. if ( ! $account_data ) {
  206. $response['error'] = __( 'There was an error subscribing to AWeber. The account is no longer connected.', 'fl-builder' );
  207. } else {
  208. $api = $this->get_api( $account_data['auth_code'] );
  209. $data = array(
  210. 'email' => $email,
  211. );
  212. if ( isset( $settings->tags ) ) {
  213. $data['tags'] = array( $settings->tags );
  214. }
  215. if ( $name ) {
  216. $data['name'] = $name;
  217. }
  218. try {
  219. $account = $api->getAccount( $account_data['access_token'], $account_data['access_secret'] );
  220. $url = '/accounts/' . $account->id . '/lists/' . $settings->list_id . '/subscribers';
  221. // Try to check if subscribe already exists.
  222. $get_subscriber = $api->adapter->request( 'GET', $url,
  223. array(
  224. 'ws.op' => 'find',
  225. 'email' => $email,
  226. )
  227. );
  228. // Update
  229. if ( isset( $get_subscriber['entries'] ) && count( $get_subscriber['entries'] ) > 0 ) {
  230. $subscriber_id = $get_subscriber['entries'][0]['id'];
  231. if ( isset( $data['tags'] ) ) {
  232. $data['tags'] = array(
  233. 'add' => $data['tags'],
  234. );
  235. }
  236. try {
  237. $result = $api->adapter->request( 'PATCH', $url . '/' . $subscriber_id, $data, array(
  238. 'return' => 'headers',
  239. ) );
  240. if ( is_array( $result ) && isset( $result['Status-Code'] ) && 209 == $result['Status-Code'] ) {
  241. return $response;
  242. } else {
  243. $response['error'] = __( 'There was an error connecting to AWeber. Please try again.', 'fl-builder' );
  244. }
  245. } catch ( AWeberAPIException $e ) {
  246. $response['error'] = sprintf(
  247. __( 'There was an error subscribing to AWeber. %s', 'fl-builder' ),
  248. $e->getMessage()
  249. );
  250. }
  251. } else {
  252. $data['ws.op'] = 'create';
  253. $result = $api->adapter->request( 'POST', $url, $data, array(
  254. 'return' => 'headers',
  255. ) );
  256. if ( is_array( $result ) && isset( $result['Status-Code'] ) && 201 == $result['Status-Code'] ) {
  257. return $response;
  258. } else {
  259. $response['error'] = __( 'There was an error connecting to AWeber. Please try again.', 'fl-builder' );
  260. }
  261. }
  262. } catch ( AWeberAPIException $e ) {
  263. $response['error'] = sprintf(
  264. __( 'There was an error subscribing to AWeber. %s', 'fl-builder' ),
  265. $e->getMessage()
  266. );
  267. }
  268. }
  269. return $response;
  270. }
  271. }