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