class-fl-builder-service-email-address.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Helper class for adding an email address as a service.
  4. *
  5. * @since 1.6.0
  6. */
  7. final class FLBuilderServiceEmailAddress extends FLBuilderService {
  8. /**
  9. * The ID for this service.
  10. *
  11. * @since 1.6.0
  12. * @var string $id
  13. */
  14. public $id = 'email-address';
  15. /**
  16. * Test the API connection.
  17. *
  18. * @since 1.6.0
  19. * @param array $fields {
  20. * @type string $email A valid email address.
  21. * }
  22. * @return array{
  23. * @type bool|string $error The error message or false if no error.
  24. * @type array $data An array of data used to make the connection.
  25. * }
  26. */
  27. public function connect( $fields = array() ) {
  28. $response = array(
  29. 'error' => false,
  30. 'data' => array(),
  31. );
  32. // Make sure we have an email address.
  33. if ( ! isset( $fields['email'] ) || empty( $fields['email'] ) ) {
  34. $response['error'] = __( 'Error: You must provide an email address.', 'fl-builder' );
  35. } else {
  36. $response['data'] = array(
  37. 'email' => $fields['email'],
  38. );
  39. }
  40. return $response;
  41. }
  42. /**
  43. * Renders the markup for the connection settings.
  44. *
  45. * @since 1.6.0
  46. * @return string The connection settings markup.
  47. */
  48. public function render_connect_settings() {
  49. ob_start();
  50. FLBuilder::render_settings_field( 'email', array(
  51. 'row_class' => 'fl-builder-service-connect-row',
  52. 'class' => 'fl-builder-service-connect-input',
  53. 'type' => 'text',
  54. 'label' => __( 'Email Address', 'fl-builder' ),
  55. 'preview' => array(
  56. 'type' => 'none',
  57. ),
  58. ));
  59. return ob_get_clean();
  60. }
  61. /**
  62. * Render the markup for service specific fields.
  63. *
  64. * @since 1.6.0
  65. * @param string $account The name of the saved account.
  66. * @param object $settings Saved module settings.
  67. * @return array {
  68. * @type bool|string $error The error message or false if no error.
  69. * @type string $html The field markup.
  70. * }
  71. */
  72. public function render_fields( $account, $settings ) {
  73. $response = array(
  74. 'error' => false,
  75. 'html' => '',
  76. );
  77. return $response;
  78. }
  79. /**
  80. * Send the subscription info to the saved email address.
  81. *
  82. * @since 1.6.0
  83. * @param object $settings A module settings object.
  84. * @param string $email The email to subscribe.
  85. * @param string $name Optional. The full name of the person subscribing.
  86. * @return array {
  87. * @type bool|string $error The error message or false if no error.
  88. * }
  89. */
  90. public function subscribe( $settings, $email, $name = false ) {
  91. $account_data = $this->get_account_data( $settings->service_account );
  92. $response = array(
  93. 'error' => false,
  94. );
  95. if ( ! $account_data ) {
  96. $response['error'] = __( 'There was an error subscribing. The account is no longer connected.', 'fl-builder' );
  97. } else {
  98. $subject = __( 'Subscribe Form Signup', 'fl-builder' );
  99. if ( $settings->custom_subject ) {
  100. $subject = $settings->custom_subject;
  101. }
  102. $message = __( 'Email', 'fl-builder' ) . ': ' . $email;
  103. if ( $name ) {
  104. $message .= "\n" . __( 'Name', 'fl-builder' ) . ': ' . $name;
  105. }
  106. $result = wp_mail( $account_data['email'], $subject, $message );
  107. if ( ! $result ) {
  108. $response['error'] = __( 'There was an error subscribing. Please try again.', 'fl-builder' );
  109. }
  110. }
  111. return $response;
  112. }
  113. }