class-fl-builder-service-godaddy-email-marketing.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Helper class for the GoDaddy Email Marketing.
  4. *
  5. * @since 1.10.5
  6. */
  7. final class FLBuilderServiceGoDaddyEmailMarketing extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.10.5
  12. * @var string $id
  13. */
  14. public $id = 'godaddy-email-marketing';
  15. /**
  16. * @since 1.10.5
  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.10.5
  25. * @param string $api_username The email address associated with the API key.
  26. * @param string $api_key A valid API key.
  27. * @return object The API instance.
  28. */
  29. public function get_api( $api_username, $api_key ) {
  30. if ( $this->api_instance ) {
  31. return $this->api_instance;
  32. }
  33. if ( ! class_exists( 'GoDaddyEM' ) ) {
  34. require_once FL_BUILDER_DIR . 'includes/vendor/godaddy-email-marketing/class-godaddy-em.php';
  35. }
  36. $this->api_instance = new GoDaddyEM( $api_username, $api_key );
  37. return $this->api_instance;
  38. }
  39. /**
  40. * Test the API connection.
  41. *
  42. * @since 1.10.5
  43. * @param array $fields {
  44. * @type string $api_username A valid API username
  45. * @type string $api_key A valid API key.
  46. * }
  47. * @return array{
  48. * @type bool|string $error The error message or false if no error.
  49. * @type array $data An array of data used to make the connection.
  50. * }
  51. */
  52. public function connect( $fields = array() ) {
  53. $response = array(
  54. 'error' => false,
  55. 'data' => array(),
  56. );
  57. // Make sure we have an email address.
  58. if ( ! isset( $fields['api_username'] ) || empty( $fields['api_username'] ) ) {
  59. $response['error'] = __( 'Error: You must provide an API username.', 'fl-builder' );
  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['api_username'], $fields['api_key'] );
  65. if ( ! $api::is_account_ok() ) {
  66. $response['error'] = __( 'Unable to connect to GoDaddy Email Marketing. Please check your credentials.', 'fl-builder' );
  67. } else {
  68. $response['data'] = array(
  69. 'api_username' => $fields['api_username'],
  70. 'api_key' => $fields['api_key'],
  71. );
  72. }
  73. }
  74. return $response;
  75. }
  76. /**
  77. * Renders the markup for the connection settings.
  78. *
  79. * @since 1.10.5
  80. * @return string The connection settings markup.
  81. */
  82. public function render_connect_settings() {
  83. ob_start();
  84. FLBuilder::render_settings_field( 'api_username', array(
  85. 'row_class' => 'fl-builder-service-connect-row',
  86. 'class' => 'fl-builder-service-connect-input',
  87. 'type' => 'text',
  88. 'label' => __( 'API Username', 'fl-builder' ),
  89. 'help' => __( 'The username associated with your GoDaddy Email Marketing account.', 'fl-builder' ),
  90. 'preview' => array(
  91. 'type' => 'none',
  92. ),
  93. ));
  94. FLBuilder::render_settings_field( 'api_key', array(
  95. 'row_class' => 'fl-builder-service-connect-row',
  96. 'class' => 'fl-builder-service-connect-input',
  97. 'type' => 'text',
  98. 'label' => __( 'API Key', 'fl-builder' ),
  99. 'help' => __( 'Your API key from your GoDaddy Email Marketing account.', 'fl-builder' ),
  100. 'description' => sprintf( __( '<a%1$s>Sign in</a> to get your username and API key. <a%2$s>Signup</a> if you don\'t have a GoDaddy Email Marketing account.', 'fl-builder' ), ' href="https://gem.godaddy.com/mwp/accounts" target="_blank"', ' href="https://sso.godaddy.com/account/create?path=/wordpress_plugin&app=gem&realm=idp&ssoreturnpath=/%3Fpath%3D%2Fwordpress_plugin%26app%3Dgem%26realm%3Didp" target="_blank"' ),
  101. 'preview' => array(
  102. 'type' => 'none',
  103. ),
  104. ));
  105. return ob_get_clean();
  106. }
  107. /**
  108. * Render the markup for service specific fields.
  109. *
  110. * @since 1.10.5
  111. * @param string $account The name of the saved account.
  112. * @param object $settings Saved module settings.
  113. * @return array {
  114. * @type bool|string $error The error message or false if no error.
  115. * @type string $html The field markup.
  116. * }
  117. */
  118. public function render_fields( $account, $settings ) {
  119. $account_data = $this->get_account_data( $account );
  120. $api = $this->get_api( $account_data['api_username'], $account_data['api_key'] );
  121. $response = array(
  122. 'error' => false,
  123. 'html' => '',
  124. );
  125. $result = $api::get_forms();
  126. if ( ! $result ) {
  127. $response['error'] = __( 'There was a problem retrieving your lists. Please check your API credentials.', 'fl-builder' );
  128. } else {
  129. $response['html'] = $this->render_list_field( $result, $settings );
  130. }
  131. return $response;
  132. }
  133. /**
  134. * Render markup for the list field.
  135. *
  136. * @since 1.10.5
  137. * @param array $forms GoDaddy Signup Forms data from the API.
  138. * @param object $settings Saved module settings.
  139. * @return string The markup for the list field.
  140. * @access private
  141. */
  142. private function render_list_field( $forms, $settings ) {
  143. ob_start();
  144. $options = array(
  145. '' => __( 'Choose...', 'fl-builder' ),
  146. );
  147. if ( ! empty( $forms->signups ) ) {
  148. foreach ( $forms->signups as $form ) {
  149. $options[ $form->id ] = $form->name;
  150. }
  151. }
  152. FLBuilder::render_settings_field( 'form_id', array(
  153. 'row_class' => 'fl-builder-service-field-row',
  154. 'class' => 'fl-builder-service-list-select',
  155. 'type' => 'select',
  156. 'label' => _x( 'Form', 'A signup form from your GoDaddy Email Marketing account.', 'fl-builder' ),
  157. 'options' => $options,
  158. 'preview' => array(
  159. 'type' => 'none',
  160. ),
  161. ), $settings);
  162. return ob_get_clean();
  163. }
  164. /**
  165. * Subscribe an email address to GoDaddy Email Marketing.
  166. *
  167. * @since 1.10.5
  168. * @param object $settings A module settings object.
  169. * @param string $email The email to subscribe.
  170. * @param string $name Optional. The full name of the person subscribing.
  171. * @return array {
  172. * @type bool|string $error The error message or false if no error.
  173. * }
  174. */
  175. public function subscribe( $settings, $email, $name = false ) {
  176. $account_data = $this->get_account_data( $settings->service_account );
  177. $response = array(
  178. 'error' => false,
  179. );
  180. if ( ! $account_data ) {
  181. $response['error'] = __( 'There was an error subscribing to GoDaddy Email Marketing. The account is no longer connected.', 'fl-builder' );
  182. } else {
  183. $api = $this->get_api( $account_data['api_username'], $account_data['api_key'] );
  184. $data = array(
  185. 'email' => $email,
  186. 'form_id' => $settings->form_id,
  187. );
  188. if ( $name ) {
  189. $names = explode( ' ', $name );
  190. if ( isset( $names[0] ) ) {
  191. $data['first_name'] = $names[0];
  192. }
  193. if ( isset( $names[1] ) ) {
  194. $data['last_name'] = $names[1];
  195. }
  196. }
  197. $result = $api->add_subscriber( $data );
  198. if ( ! $result ) {
  199. $response['error'] = __( 'There was an error subscribing to GoDaddy Email Marketing. The account is no longer connected.', 'fl-builder' );
  200. }
  201. }
  202. return $response;
  203. }
  204. }