class-fl-builder-service-sendy.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Helper class for the Sendy API.
  4. *
  5. * @since 1.5.4
  6. */
  7. final class FLBuilderServiceSendy extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.5.4
  12. * @var string $id
  13. */
  14. public $id = 'sendy';
  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 array $args A valid API authentication data.
  26. * @return object The API instance.
  27. */
  28. public function get_api( array $args ) {
  29. if ( $this->api_instance ) {
  30. return $this->api_instance;
  31. }
  32. if ( ! class_exists( '\\SendyPHP\\SendyPHP' ) ) {
  33. require_once FL_BUILDER_DIR . 'includes/vendor/sendy/SendyPHP.php';
  34. }
  35. $this->api_instance = new \SendyPHP\SendyPHP( $args );
  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_host A valid Host.
  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 the Host.
  57. if ( ! isset( $fields['api_host'] ) || empty( $fields['api_host'] ) ) {
  58. $response['error'] = __( 'Error: You must provide your Sendy installation URL.', 'fl-builder' );
  59. }
  60. // Make sure we have an API key.
  61. if ( ! isset( $fields['api_key'] ) || empty( $fields['api_key'] ) ) {
  62. $response['error'] = __( 'Error: You must provide an API key.', 'fl-builder' );
  63. }
  64. // Make sure we have the list ID.
  65. if ( ! isset( $fields['list_id'] ) || empty( $fields['list_id'] ) ) {
  66. $response['error'] = __( 'Error: You must provide a list ID.', 'fl-builder' );
  67. } else {
  68. $api = $this->get_api( array(
  69. 'installation_url' => $fields['api_host'],
  70. 'api_key' => $fields['api_key'],
  71. 'list_id' => $fields['list_id'],
  72. ) );
  73. // Send request for list ID validation
  74. $get_api_response = $api->subcount();
  75. if ( true === $get_api_response['status'] ) {
  76. $response['data'] = array(
  77. 'api_host' => $fields['api_host'],
  78. 'api_key' => $fields['api_key'],
  79. 'list_id' => $fields['list_id'],
  80. );
  81. } else {
  82. $response['error'] = sprintf( __( 'Error: Could not connect to Sendy. %s', 'fl-builder' ), $get_api_response['message'] );
  83. }
  84. }
  85. return $response;
  86. }
  87. /**
  88. * Renders the markup for the connection settings.
  89. *
  90. * @since 1.5.4
  91. * @return string The connection settings markup.
  92. */
  93. public function render_connect_settings() {
  94. ob_start();
  95. FLBuilder::render_settings_field( 'api_host', array(
  96. 'row_class' => 'fl-builder-service-connect-row',
  97. 'class' => 'fl-builder-service-connect-input',
  98. 'type' => 'text',
  99. 'label' => __( 'Installation URL', 'fl-builder' ),
  100. 'help' => __( 'The URL where your Sendy application is installed (e.g. http://mywebsite.com/sendy).', 'fl-builder' ),
  101. 'preview' => array(
  102. 'type' => 'none',
  103. ),
  104. ));
  105. FLBuilder::render_settings_field( 'api_key', array(
  106. 'row_class' => 'fl-builder-service-connect-row',
  107. 'class' => 'fl-builder-service-connect-input',
  108. 'type' => 'text',
  109. 'label' => __( 'API Key', 'fl-builder' ),
  110. 'help' => __( 'Found in your Sendy application under Settings.', 'fl-builder' ),
  111. 'preview' => array(
  112. 'type' => 'none',
  113. ),
  114. ));
  115. FLBuilder::render_settings_field( 'list_id', array(
  116. 'row_class' => 'fl-builder-service-connect-row',
  117. 'class' => 'fl-builder-service-connect-input',
  118. 'type' => 'text',
  119. 'label' => __( 'List ID', 'fl-builder' ),
  120. 'help' => __( 'The ID of the list you would like users to subscribe to. The ID of a list can be found under "View all lists" in the section named ID.', 'fl-builder' ),
  121. 'preview' => array(
  122. 'type' => 'none',
  123. ),
  124. ));
  125. return ob_get_clean();
  126. }
  127. /**
  128. * Render the markup for service specific fields.
  129. *
  130. * @since 1.5.4
  131. * @param string $account The name of the saved account.
  132. * @param object $settings Saved module settings.
  133. * @return array {
  134. * @type bool|string $error The error message or false if no error.
  135. * @type string $html The field markup.
  136. * }
  137. */
  138. public function render_fields( $account, $settings ) {
  139. $response = array(
  140. 'error' => false,
  141. 'html' => '',
  142. );
  143. return $response;
  144. }
  145. /**
  146. * Subscribe an email address to Sendy.
  147. *
  148. * @since 1.5.4
  149. * @param object $settings A module settings object.
  150. * @param string $email The email to subscribe.
  151. * @param string $name Optional. The full name of the person subscribing.
  152. * @return array {
  153. * @type bool|string $error The error message or false if no error.
  154. * }
  155. */
  156. public function subscribe( $settings, $email, $name = '' ) {
  157. $account_data = $this->get_account_data( $settings->service_account );
  158. $response = array(
  159. 'error' => false,
  160. );
  161. if ( ! $account_data ) {
  162. $response['error'] = __( 'There was an error subscribing to Sendy. The account is no longer connected.', 'fl-builder' );
  163. } else {
  164. $api = $this->get_api( array(
  165. 'installation_url' => $account_data['api_host'],
  166. 'api_key' => $account_data['api_key'],
  167. 'list_id' => $account_data['list_id'],
  168. ) );
  169. // Send request for list ID validation
  170. $get_api_response = $api->subscribe( array(
  171. 'name' => $name,
  172. 'email' => $email,
  173. ) );
  174. if ( false === $get_api_response['status'] ) {
  175. $response['error'] = sprintf( __( 'There was an error subscribing to Sendy. %s', 'fl-builder' ), $get_api_response['message'] );
  176. }
  177. }
  178. return $response;
  179. }
  180. }