PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.6
3.4.6 3.4.5 3.4.4 3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 All 199 releases
convertkit / includes / integrations / forminator / class-convertkit-forminator.php

class-convertkit-forminator.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.6, at includes/integrations/forminator/class-convertkit-forminator.php

260 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Forminator class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Forminator Integration
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Forminator {
16
17 /**
18 * Constructor. Registers required hooks with Forminator.
19 *
20 * @since 2.3.0
21 */
22 public function __construct() {
23
24 add_action( 'forminator_before_form_render', array( $this, 'maybe_enqueue_creator_network_recommendations_script' ) );
25 add_action( 'forminator_custom_form_submit_before_set_fields', array( $this, 'maybe_subscribe' ), 10, 3 );
26
27 }
28
29 /**
30 * Enqueues the Creator Network Recommendations script, if the Forminator Form
31 * has the 'Enable Creator Network Recommendations' setting enabled at Settings >
32 * ConvertKit > Forminator.
33 *
34 * @since 2.3.0
35 *
36 * @param int $form_id Forminator Form ID.
37 */
38 public function maybe_enqueue_creator_network_recommendations_script( $form_id ) {
39
40 // Don't enqueue if this is a WordPress Admin screen request.
41 if ( is_admin() ) {
42 return;
43 }
44
45 // Initialize classes.
46 $creator_network_recommendations = new ConvertKit_Resource_Creator_Network_Recommendations( 'forminator' );
47 $forminator_settings = new ConvertKit_Forminator_Settings();
48
49 // Bail if Creator Network Recommendations are not enabled for this form.
50 if ( ! $forminator_settings->get_creator_network_recommendations_enabled_by_forminator_form_id( $form_id ) ) {
51 return;
52 }
53
54 // Get script.
55 $script_url = $creator_network_recommendations->get();
56
57 // Bail if no script exists (i.e. the Creator Network Recommendations is not enabled on the ConvertKit account).
58 if ( ! $script_url ) {
59 return;
60 }
61
62 // Enqueue script.
63 wp_enqueue_script( 'convertkit-creator-network-recommendations', $script_url, array(), CONVERTKIT_PLUGIN_VERSION, true );
64
65 }
66
67 /**
68 * Sends a Forminator's Form Name and Email values through the ConvertKit API
69 * if a ConvertKit Form is mapped to this Forminator Form in the ConvertKit
70 * Settings.
71 *
72 * @since 2.3.0
73 *
74 * @param Forminator_Form_Entry_Model $entry Entry.
75 * @param int $form_id Forminator Form ID.
76 * @param array $form_data_array Forminator submitted data.
77 */
78 public function maybe_subscribe( $entry, $form_id, $form_data_array ) {
79
80 // Bail if the entry is spam, a draft or an abandoned form.
81 if ( ! empty( $entry->is_spam ) || $entry->status !== 'active' ) {
82 return;
83 }
84
85 // Get ConvertKit Form ID mapped to this Forminator Form.
86 // We deliberately use the entry's form ID, as $form_id for a Quiz will point to a lead generation form, which
87 // has a different Form ID.
88 $forminator_settings = new ConvertKit_Forminator_Settings();
89 $convertkit_subscribe_setting = $forminator_settings->get_convertkit_subscribe_setting_by_forminator_form_id( $entry->form_id );
90
91 // If no ConvertKit subscribe setting is defined, bail.
92 if ( ! $convertkit_subscribe_setting ) {
93 return;
94 }
95
96 // Bail if the API hasn't been configured.
97 $settings = new ConvertKit_Settings();
98 if ( ! $settings->has_access_and_refresh_token() ) {
99 return;
100 }
101
102 // Extract the name and email field values.
103 $first_name = false;
104 $email = false;
105 foreach ( $form_data_array as $form_field ) {
106 // Skip field if it doesn't have a type - it's likely an IP address value.
107 if ( ! array_key_exists( 'field_type', $form_field ) ) {
108 continue;
109 }
110
111 // Extract the name / email address, depending on the field type.
112 switch ( $form_field['field_type'] ) {
113 case 'name':
114 // If the Name field uses multiple fields (prefix, first, middle and last name), use the first name.
115 if ( is_array( $form_field['value'] ) ) {
116 $first_name = isset( $form_field['value']['first-name'] ) ? $form_field['value']['first-name'] : false;
117 break;
118 }
119
120 $name = explode( ' ', $form_field['value'] );
121 $first_name = $name[0];
122 break;
123
124 case 'email':
125 $email = $form_field['value'];
126 break;
127 }
128 }
129
130 // Bail if no email address could be found.
131 if ( ! $email ) {
132 return;
133 }
134
135 // If here, subscribe the user to the ConvertKit Form.
136 // Initialize the API.
137 $api = new ConvertKit_API_V4(
138 CONVERTKIT_OAUTH_CLIENT_ID,
139 CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
140 $settings->get_access_token(),
141 $settings->get_refresh_token(),
142 $settings->debug_enabled(),
143 'forminator'
144 );
145
146 // If the resource setting is 'subscribe', create the subscriber in an active state and don't assign to a resource.
147 if ( $convertkit_subscribe_setting === 'subscribe' ) {
148 $api->create_subscriber( $email, $first_name );
149 return;
150 }
151
152 // Determine the resource type and ID to assign to the subscriber.
153 list( $resource_type, $resource_id ) = explode( ':', $convertkit_subscribe_setting );
154
155 // Cast ID.
156 $resource_id = absint( $resource_id );
157
158 // Add the subscriber to the resource type (form, tag etc).
159 switch ( $resource_type ) {
160
161 /**
162 * Form
163 */
164 case 'form':
165 // Subscribe with inactive state.
166 $subscriber = $api->create_subscriber( $email, $first_name, 'inactive' );
167
168 // If an error occurred, don't attempt to add the subscriber to the Form, as it won't work.
169 if ( is_wp_error( $subscriber ) ) {
170 return;
171 }
172
173 // For Legacy Forms, a different endpoint is used.
174 $forms = new ConvertKit_Resource_Forms();
175 if ( $forms->is_legacy( $resource_id ) ) {
176 return $api->add_subscriber_to_legacy_form( $resource_id, $subscriber['subscriber']['id'] );
177 }
178
179 // Add subscriber to form.
180 return $api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'], $this->get_referrer_url() );
181
182 /**
183 * Sequence
184 */
185 case 'sequence':
186 // Subscribe.
187 $subscriber = $api->create_subscriber( $email, $first_name );
188
189 // If an error occurred, don't attempt to add the subscriber to the Form, as it won't work.
190 if ( is_wp_error( $subscriber ) ) {
191 return;
192 }
193
194 // Add subscriber to sequence.
195 return $api->add_subscriber_to_sequence( $resource_id, $subscriber['subscriber']['id'] );
196
197 /**
198 * Tag
199 */
200 case 'tag':
201 // Subscribe.
202 $subscriber = $api->create_subscriber( $email, $first_name );
203
204 // If an error occurred, don't attempt to add the subscriber to the Form, as it won't work.
205 if ( is_wp_error( $subscriber ) ) {
206 return;
207 }
208
209 // Add subscriber to tag.
210 return $api->tag_subscriber( $resource_id, $subscriber['subscriber']['id'] );
211
212 }
213
214 }
215
216 /**
217 * Gets the referrer URL to send to the `form_subscribe` API method.
218 *
219 * Falls back to the action's AJAX URL if the Post ID the form was
220 * embedded in cannot be determined.
221 *
222 * @since 2.7.1
223 *
224 * @return string
225 */
226 private function get_referrer_url() {
227
228 // If the request includes the HTTP referrer, return that URL
229 // as it will include any UTM parameters.
230 if ( filter_has_var( INPUT_POST, '_wp_http_referer' ) ) {
231 // referrer is relative to the domain and includes any subdirectory, so prefix it with the site's scheme, host and port.
232 $home = wp_parse_url( home_url() );
233 return esc_url_raw(
234 $home['scheme'] . '://' . $home['host'] . ( isset( $home['port'] ) ? ':' . $home['port'] : '' ) . filter_input( INPUT_POST, '_wp_http_referer', FILTER_UNSAFE_RAW )
235 );
236 }
237
238 // If the request includes the current_url, return that URL.
239 // It won't include any UTM parameters, but is still an accurate URL.
240 if ( filter_has_var( INPUT_POST, 'current_url' ) ) {
241 return esc_url_raw( filter_input( INPUT_POST, 'current_url', FILTER_UNSAFE_RAW ) );
242 }
243
244 // Return the AJAX URL.
245 return home_url( add_query_arg( null, null ) );
246
247 }
248
249 }
250
251 // Bootstrap.
252 add_action(
253 'convertkit_initialize_global',
254 function () {
255
256 new ConvertKit_Forminator();
257
258 }
259 );
260