-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclass-integrate-convertkit-wpforms.php
More file actions
176 lines (145 loc) · 5.51 KB
/
class-integrate-convertkit-wpforms.php
File metadata and controls
176 lines (145 loc) · 5.51 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Integrate ConvertKit and WPForms
*
* @package Integrate_ConvertKit_WPForms
* @since 1.0.0
* @copyright Copyright (c) 2017, Bill Erickson
* @license GPL-2.0+
*/
class Integrate_ConvertKit_WPForms {
/**
* Primary Class Constructor
*
*/
public function __construct() {
add_filter( 'wpforms_builder_settings_sections', array( $this, 'settings_section' ), 20, 2 );
add_filter( 'wpforms_form_settings_panel_content', array( $this, 'settings_section_content' ), 20 );
add_action( 'wpforms_process_complete', array( $this, 'send_data_to_convertkit' ), 10, 4 );
}
/**
* Add Settings Section
*
*/
function settings_section( $sections, $form_data ) {
$sections['be_convertkit'] = __( 'ConvertKit', 'integrate-convertkit-wpforms' );
return $sections;
}
/**
* ConvertKit Settings Content
*
*/
function settings_section_content( $instance ) {
echo '<div class="wpforms-panel-content-section wpforms-panel-content-section-be_convertkit">';
echo '<div class="wpforms-panel-content-section-title">' . __( 'ConvertKit', 'integrate-convertkit-wpforms' ) . '</div>';
if( empty( $instance->form_data['settings']['be_convertkit_api'] ) ) {
printf(
'<p>%s <a href="http://mbsy.co/convertkit/28981746" target="_blank" rel="noopener noreferrer">%s</a></p>',
__( 'Don\'t have an account?', 'integrate-convertkit-wpforms' ),
__( 'Sign up now!', 'integrate-convertkit-wpforms' )
);
}
wpforms_panel_field(
'text',
'settings',
'be_convertkit_api',
$instance->form_data,
__( 'ConvertKit API Key', 'integrate-convertkit-wpforms' )
);
wpforms_panel_field(
'text',
'settings',
'be_convertkit_form_id',
$instance->form_data,
__( 'ConvertKit Form ID', 'integrate-convertkit-wpforms' )
);
wpforms_panel_field(
'select',
'settings',
'be_convertkit_field_first_name',
$instance->form_data,
__( 'First Name', 'integrate-convertkit-wpforms' ),
array(
'field_map' => array( 'text', 'name' ),
'placeholder' => __( '-- Select Field --', 'integrate-convertkit-wpforms' ),
)
);
wpforms_panel_field(
'select',
'settings',
'be_convertkit_field_email',
$instance->form_data,
__( 'Email Address', 'integrate-convertkit-wpforms' ),
array(
'field_map' => array( 'email' ),
'placeholder' => __( '-- Select Field --', 'integrate-convertkit-wpforms' ),
)
);
echo '</div>';
}
/**
* Integrate WPForms with ConvertKit
*
*/
function send_data_to_convertkit( $fields, $entry, $form_data, $entry_id ) {
// Get API key and CK Form ID
$api_key = $ck_form_id = false;
if( !empty( $form_data['settings']['be_convertkit_api'] ) )
$api_key = esc_html( $form_data['settings']['be_convertkit_api'] );
if( !empty( $form_data['settings']['be_convertkit_form_id'] ) )
$ck_form_id = intval( $form_data['settings']['be_convertkit_form_id'] );
if( ! ( $api_key && $ck_form_id ) )
return;
$args = array(
'api_key' => $api_key
);
// Return early if no email
$email_field_id = $form_data['settings']['be_convertkit_field_email'];
if( empty( $email_field_id ) || empty( $fields[ $email_field_id ]['value'] ) )
return;
$args['email'] = $fields[ $email_field_id ]['value'];
$first_name_field_id = $form_data['settings']['be_convertkit_field_first_name'];
if( !empty( $first_name_field_id ) && !empty( $fields[ $first_name_field_id ]['value'] ) )
$args['first_name'] = $fields[ $first_name_field_id ]['value'];
// Custom Fields and tags
// @link https://www.billerickson.net/setup-convertkit-wordpress-form/#custom-fields-and-tags
foreach( $form_data['fields'] as $i => $field ) {
if( empty( $field['css'] ) )
continue;
$value = !empty( $fields[$i]['value_raw'] ) ? $fields[$i]['value_raw'] : $fields[$i]['value'];
$classes = explode( ' ', $field['css'] );
foreach( $classes as $class ) {
// Custom Fields
if( false !== strpos( $class, 'ck-custom-' ) ) {
$key = str_replace( 'ck-custom-', '', $class );
$args['fields'][ $key ] = $value;
}
// Tags
if( 'ck-tag' == $class ) {
$args['tags'][] = $value;
}
}
}
// Filter for customizing arguments
// @link https://www.billerickson.net/code/integrate-convertkit-wpforms-custom-fields/
$args = apply_filters( 'be_convertkit_form_args', $args, $fields, $form_data );
// Filter for limiting integration
// @link https://www.billerickson.net/code/integrate-convertkit-wpforms-conditional-processing/
if( ! apply_filters( 'be_convertkit_process_form', true, $fields, $form_data ) )
return;
// Submit to ConvertKit
$request = wp_remote_post( add_query_arg( $args, 'https://api.convertkit.com/v3/forms/' . $ck_form_id . '/subscribe' ) );
if ( function_exists( 'wpforms_log' ) ) {
wpforms_log(
'ConvertKit Response',
$request,
[
'type' => [ 'provider' ],
'parent' => $entry_id,
'form_id' => $form_data['id'],
]
);
}
}
}
new Integrate_ConvertKit_WPForms;