PageRenderTime 78ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/beaver-builder-lite-version/classes/class-fl-builder-service-aweber.php

https://bitbucket.org/madiha303/clickitplugins-web
PHP | 277 lines | 156 code | 37 blank | 84 comment | 16 complexity | 9f1a96d6e5450f1da9d629e418c87566 MD5 | raw file
  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. } // End if().
  63. elseif ( 6 != count( explode( '|', $fields['auth_code'] ) ) ) {
  64. $response['error'] = __( 'Error: Please enter a valid Authorization Code.', 'fl-builder' );
  65. } // Try to connect and store the connection data.
  66. else {
  67. $api = $this->get_api( $fields['auth_code'] );
  68. // Get an access token from the API.
  69. try {
  70. list( $access_token, $access_token_secret ) = $api->getAccessToken();
  71. } catch ( AWeberException $e ) {
  72. $response['error'] = $e->getMessage();
  73. }
  74. // Make sure we can get the account.
  75. try {
  76. $account = $api->getAccount();
  77. } catch ( AWeberException $e ) {
  78. $response['error'] = $e->getMessage();
  79. }
  80. // Build the response data.
  81. if ( ! $response['error'] ) {
  82. $response['data'] = array(
  83. 'auth_code' => $fields['auth_code'],
  84. 'access_token' => $access_token,
  85. 'access_secret' => $access_token_secret,
  86. );
  87. }
  88. }
  89. return $response;
  90. }
  91. /**
  92. * Renders the markup for the connection settings.
  93. *
  94. * @since 1.5.4
  95. * @return string The connection settings markup.
  96. */
  97. public function render_connect_settings() {
  98. ob_start();
  99. FLBuilder::render_settings_field( 'auth_code', array(
  100. 'row_class' => 'fl-builder-service-connect-row',
  101. 'class' => 'fl-builder-service-connect-input',
  102. 'type' => 'text',
  103. 'label' => __( 'Authorization Code', 'fl-builder' ),
  104. '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"' ),
  105. 'preview' => array(
  106. 'type' => 'none',
  107. ),
  108. ));
  109. return ob_get_clean();
  110. }
  111. /**
  112. * Render the markup for service specific fields.
  113. *
  114. * @since 1.5.4
  115. * @param string $account The name of the saved account.
  116. * @param object $settings Saved module settings.
  117. * @return array {
  118. * @type bool|string $error The error message or false if no error.
  119. * @type string $html The field markup.
  120. * }
  121. */
  122. public function render_fields( $account, $settings ) {
  123. $account_data = $this->get_account_data( $account );
  124. $api = $this->get_api( $account_data['auth_code'] );
  125. $response = array(
  126. 'error' => false,
  127. 'html' => '',
  128. );
  129. try {
  130. $account = $api->getAccount( $account_data['access_token'], $account_data['access_secret'] );
  131. $lists = $account->loadFromUrl( '/accounts/' . $account->id . '/lists' );
  132. $response['html'] = $this->render_list_field( $lists, $settings );
  133. $response['html'] .= $this->render_tags_field( $settings );
  134. } catch ( AWeberException $e ) {
  135. $response['error'] = $e->getMessage();
  136. }
  137. return $response;
  138. }
  139. /**
  140. * Render markup for the list field.
  141. *
  142. * @since 1.5.4
  143. * @param array $lists List data from the API.
  144. * @param object $settings Saved module settings.
  145. * @return string The markup for the list field.
  146. * @access private
  147. */
  148. private function render_list_field( $lists, $settings ) {
  149. ob_start();
  150. $options = array(
  151. '' => __( 'Choose...', 'fl-builder' ),
  152. );
  153. foreach ( $lists->data['entries'] as $list ) {
  154. $options[ $list['id'] ] = $list['name'];
  155. }
  156. FLBuilder::render_settings_field( 'list_id', array(
  157. 'row_class' => 'fl-builder-service-field-row',
  158. 'class' => 'fl-builder-service-list-select',
  159. 'type' => 'select',
  160. 'label' => _x( 'List', 'An email list from a third party provider.', 'fl-builder' ),
  161. 'options' => $options,
  162. 'preview' => array(
  163. 'type' => 'none',
  164. ),
  165. ), $settings);
  166. return ob_get_clean();
  167. }
  168. /**
  169. * Render markup for the tags field.
  170. *
  171. * @since 1.8.8
  172. * @param object $settings Saved module settings.
  173. * @return string The markup for the tags field.
  174. * @access private
  175. */
  176. private function render_tags_field( $settings ) {
  177. ob_start();
  178. FLBuilder::render_settings_field( 'tags', array(
  179. 'row_class' => 'fl-builder-service-connect-row',
  180. 'class' => 'fl-builder-service-connect-input',
  181. 'type' => 'text',
  182. 'label' => _x( 'Tags', 'A comma separated list of tags.', 'fl-builder' ),
  183. 'help' => __( 'A comma separated list of tags.', 'fl-builder' ),
  184. 'preview' => array(
  185. 'type' => 'none',
  186. ),
  187. ),$settings);
  188. return ob_get_clean();
  189. }
  190. /**
  191. * Subscribe an email address to AWeber.
  192. *
  193. * @since 1.5.4
  194. * @param object $settings A module settings object.
  195. * @param string $email The email to subscribe.
  196. * @param string $name Optional. The full name of the person subscribing.
  197. * @return array {
  198. * @type bool|string $error The error message or false if no error.
  199. * }
  200. */
  201. public function subscribe( $settings, $email, $name = false ) {
  202. $account_data = $this->get_account_data( $settings->service_account );
  203. $response = array(
  204. 'error' => false,
  205. );
  206. if ( ! $account_data ) {
  207. $response['error'] = __( 'There was an error subscribing to AWeber. The account is no longer connected.', 'fl-builder' );
  208. } else {
  209. $api = $this->get_api( $account_data['auth_code'] );
  210. $data = array(
  211. 'ws.op' => 'create',
  212. 'email' => $email,
  213. );
  214. if ( isset( $settings->tags ) ) {
  215. $data['tags'] = array( $settings->tags );
  216. }
  217. if ( $name ) {
  218. $data['name'] = $name;
  219. }
  220. try {
  221. $account = $api->getAccount( $account_data['access_token'], $account_data['access_secret'] );
  222. $url = '/accounts/' . $account->id . '/lists/' . $settings->list_id . '/subscribers';
  223. $result = $api->adapter->request( 'POST', $url, $data, array(
  224. 'return' => 'headers',
  225. ) );
  226. if ( is_array( $result ) && isset( $result['Status-Code'] ) && 201 == $result['Status-Code'] ) {
  227. return $response;
  228. } else {
  229. $response['error'] = __( 'There was an error connecting to AWeber. Please try again.', 'fl-builder' );
  230. }
  231. } catch ( AWeberAPIException $e ) {
  232. $response['error'] = sprintf(
  233. __( 'There was an error subscribing to AWeber. %s', 'fl-builder' ),
  234. $e->getMessage()
  235. );
  236. }
  237. }// End if().
  238. return $response;
  239. }
  240. }