PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.3
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 2.2.9 2.3.0 2.3.1 All 196 releases
← All changes | includes/integrations/forminator/class-convertkit-forminator.php +110 -11 2.3.13.4.3 View file →
@@ -70,26 +70,28 @@
70 70 * Settings.
71 71 *
72 72 * @since 2.3.0
73 73 *
74 - * @param array $entry Entry.
75 - * @param int $form_id Forminator Form ID.
76 - * @param array $form_data_array Forminator submitted data.
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 77 */
78 78 public function maybe_subscribe( $entry, $form_id, $form_data_array ) {
79 79
80 80 // Get ConvertKit Form ID mapped to this Forminator Form.
81 - $forminator_settings = new ConvertKit_Forminator_Settings();
82 - $convertkit_form_id = $forminator_settings->get_convertkit_form_id_by_forminator_form_id( $form_id );
81 + // We deliberately use the entry's form ID, as $form_id for a Quiz will point to a lead generation form, which
82 + // has a different Form ID.
83 + $forminator_settings = new ConvertKit_Forminator_Settings();
84 + $convertkit_subscribe_setting = $forminator_settings->get_convertkit_subscribe_setting_by_forminator_form_id( $entry->form_id );
83 85
84 - // If no ConvertKit Form is mapped to this Forminator Form, bail.
85 - if ( ! $convertkit_form_id ) {
86 + // If no ConvertKit subscribe setting is defined, bail.
87 + if ( ! $convertkit_subscribe_setting ) {
86 88 return;
87 89 }
88 90
89 91 // Bail if the API hasn't been configured.
90 92 $settings = new ConvertKit_Settings();
91 - if ( ! $settings->has_api_key_and_secret() ) {
93 + if ( ! $settings->has_access_and_refresh_token() ) {
92 94 return;
93 95 }
94 96
95 97 // Extract the name and email field values.
@@ -120,12 +122,109 @@
120 122 }
121 123
122 124 // If here, subscribe the user to the ConvertKit Form.
123 125 // Initialize the API.
124 - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'forminator' );
126 + $api = new ConvertKit_API_V4(
127 + CONVERTKIT_OAUTH_CLIENT_ID,
128 + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
129 + $settings->get_access_token(),
130 + $settings->get_refresh_token(),
131 + $settings->debug_enabled(),
132 + 'forminator'
133 + );
125 134
126 - // Send request.
127 - $api->form_subscribe( $convertkit_form_id, $email, $first_name );
135 + // If the resource setting is 'subscribe', create the subscriber in an active state and don't assign to a resource.
136 + if ( $convertkit_subscribe_setting === 'subscribe' ) {
137 + $api->create_subscriber( $email, $first_name );
138 + return;
139 + }
140 +
141 + // Determine the resource type and ID to assign to the subscriber.
142 + list( $resource_type, $resource_id ) = explode( ':', $convertkit_subscribe_setting );
143 +
144 + // Cast ID.
145 + $resource_id = absint( $resource_id );
146 +
147 + // Add the subscriber to the resource type (form, tag etc).
148 + switch ( $resource_type ) {
149 +
150 + /**
151 + * Form
152 + */
153 + case 'form':
154 + // Subscribe with inactive state.
155 + $subscriber = $api->create_subscriber( $email, $first_name, 'inactive' );
156 +
157 + // For Legacy Forms, a different endpoint is used.
158 + $forms = new ConvertKit_Resource_Forms();
159 + if ( $forms->is_legacy( $resource_id ) ) {
160 + return $api->add_subscriber_to_legacy_form( $resource_id, $subscriber['subscriber']['id'] );
161 + }
162 +
163 + // Add subscriber to form.
164 + return $api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'], $this->get_referrer_url() );
165 +
166 + /**
167 + * Sequence
168 + */
169 + case 'sequence':
170 + // Subscribe.
171 + $subscriber = $api->create_subscriber( $email, $first_name );
172 +
173 + // If an error occurred, don't attempt to add the subscriber to the Form, as it won't work.
174 + if ( is_wp_error( $subscriber ) ) {
175 + return;
176 + }
177 +
178 + // Add subscriber to sequence.
179 + return $api->add_subscriber_to_sequence( $resource_id, $subscriber['subscriber']['id'] );
180 +
181 + /**
182 + * Tag
183 + */
184 + case 'tag':
185 + // Subscribe.
186 + $subscriber = $api->create_subscriber( $email, $first_name );
187 +
188 + // If an error occurred, don't attempt to add the subscriber to the Form, as it won't work.
189 + if ( is_wp_error( $subscriber ) ) {
190 + return;
191 + }
192 +
193 + // Add subscriber to tag.
194 + return $api->tag_subscriber( $resource_id, $subscriber['subscriber']['id'] );
195 +
196 + }
197 +
198 + }
199 +
200 + /**
201 + * Gets the referrer URL to send to the `form_subscribe` API method.
202 + *
203 + * Falls back to the action's AJAX URL if the Post ID the form was
204 + * embedded in cannot be determined.
205 + *
206 + * @since 2.7.1
207 + *
208 + * @return string
209 + */
210 + private function get_referrer_url() {
211 +
212 + // If the request includes the HTTP referrer, return that URL
213 + // as it will include any UTM parameters.
214 + if ( filter_has_var( INPUT_POST, '_wp_http_referer' ) ) {
215 + // referrer is a relative path, so use home_url() to return a fully qualified URL.
216 + return esc_url( home_url( filter_input( INPUT_POST, '_wp_http_referer', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) );
217 + }
218 +
219 + // If the request includes the current_url, return that URL.
220 + // It won't include any UTM parameters, but is still an accurate URL.
221 + if ( filter_has_var( INPUT_POST, 'current_url' ) ) {
222 + return esc_url( filter_input( INPUT_POST, 'current_url', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
223 + }
224 +
225 + // Return the AJAX URL.
226 + return home_url( add_query_arg( null, null ) );
128 227
129 228 }
130 229
131 230 }