PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.4
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 2.2.9 2.3.0 All 197 releases
← All changes | includes/integrations/contactform7/class-convertkit-contactform7.php +143 -9 2.2.0 → 3.4.4 View file →
@@ -20,13 +20,52 @@
20 20 * @since 1.9.6
21 21 */
22 22 public function __construct() {
23 23
24 + add_action( 'wpcf7_contact_form', array( $this, 'maybe_enqueue_creator_network_recommendations_script' ) );
24 25 add_action( 'wpcf7_submit', array( $this, 'handle_wpcf7_submit' ), 10, 2 );
25 26
26 27 }
27 28
28 29 /**
30 + * Enqueues the Creator Network Recommendations script, if the Contact Form 7 Form
31 + * has the 'Enable Creator Network Recommendations' setting enabled at Settings >
32 + * ConvertKit > Contact Form 7.
33 + *
34 + * @since 2.2.7
35 + *
36 + * @param WPCF7_ContactForm $contact_form Contact Form 7 Form.
37 + */
38 + public function maybe_enqueue_creator_network_recommendations_script( $contact_form ) {
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( 'contact_form_7' );
47 + $contact_form_7_settings = new ConvertKit_ContactForm7_Settings();
48 +
49 + // Bail if Creator Network Recommendations are not enabled for this form.
50 + if ( ! $contact_form_7_settings->get_creator_network_recommendations_enabled_by_cf7_form_id( $contact_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 + /**
29 68 * Sends a Contact Form 7's Form Name and Email values through the ConvertKit API
30 69 * if a ConvertKit Form is mapped to this Contact Form 7 Form in the ConvertKit
31 70 * Settings.
32 71 *
@@ -48,19 +87,19 @@
48 87 return;
49 88 }
50 89
51 90 // Get ConvertKit Form ID mapped to this Contact Form 7 Form.
52 - $contact_form_7_settings = new ConvertKit_ContactForm7_Settings();
53 - $convertkit_form_id = $contact_form_7_settings->get_convertkit_form_id_by_cf7_form_id( $contact_form->id() );
91 + $contact_form_7_settings = new ConvertKit_ContactForm7_Settings();
92 + $convertkit_subscribe_setting = $contact_form_7_settings->get_convertkit_subscribe_setting_by_cf7_form_id( $contact_form->id() );
54 93
55 - // If no ConvertKit Form is mapped to this Contact Form 7 Form, bail.
56 - if ( ! $convertkit_form_id ) {
94 + // If no ConvertKit subscribe setting is defined, bail.
95 + if ( ! $convertkit_subscribe_setting ) {
57 96 return;
58 97 }
59 98
60 99 // Bail if the API hasn't been configured.
61 100 $settings = new ConvertKit_Settings();
62 - if ( ! $settings->has_api_key_and_secret() ) {
101 + if ( ! $settings->has_access_and_refresh_token() ) {
63 102 return;
64 103 }
65 104
66 105 // Get Contact Form 7 Submission.
@@ -80,21 +119,116 @@
80 119 }
81 120
82 121 // If here, subscribe the user to the ConvertKit Form.
83 122 // Initialize the API.
84 - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'contact_form_7' );
123 + $api = new ConvertKit_API_V4(
124 + CONVERTKIT_OAUTH_CLIENT_ID,
125 + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
126 + $settings->get_access_token(),
127 + $settings->get_refresh_token(),
128 + $settings->debug_enabled(),
129 + 'contact_form_7'
130 + );
85 131
86 - // Send request.
87 - $api->form_subscribe( $convertkit_form_id, $email, $first_name );
132 + // If the resource setting is 'subscribe', create the subscriber in an active state and don't assign to a resource.
133 + if ( $convertkit_subscribe_setting === 'subscribe' ) {
134 + $api->create_subscriber( $email, $first_name );
135 + return;
136 + }
88 137
138 + // Determine the resource type and ID to assign to the subscriber.
139 + list( $resource_type, $resource_id ) = explode( ':', $convertkit_subscribe_setting );
140 +
141 + // Cast ID.
142 + $resource_id = absint( $resource_id );
143 +
144 + // Add the subscriber to the resource type (form, tag etc).
145 + switch ( $resource_type ) {
146 +
147 + /**
148 + * Form
149 + */
150 + case 'form':
151 + // Subscribe with inactive state.
152 + $subscriber = $api->create_subscriber( $email, $first_name, 'inactive' );
153 +
154 + // If an error occurred, don't attempt to add the subscriber to the Form, as it won't work.
155 + if ( is_wp_error( $subscriber ) ) {
156 + return;
157 + }
158 +
159 + // For Legacy Forms, a different endpoint is used.
160 + $forms = new ConvertKit_Resource_Forms();
161 + if ( $forms->is_legacy( $resource_id ) ) {
162 + return $api->add_subscriber_to_legacy_form( $resource_id, $subscriber['subscriber']['id'] );
163 + }
164 +
165 + // Add subscriber to form.
166 + return $api->add_subscriber_to_form( $resource_id, $subscriber['subscriber']['id'], $this->get_referrer_url() );
167 +
168 + /**
169 + * Sequence
170 + */
171 + case 'sequence':
172 + // Subscribe.
173 + $subscriber = $api->create_subscriber( $email, $first_name );
174 +
175 + // If an error occurred, don't attempt to add the subscriber to the Form, as it won't work.
176 + if ( is_wp_error( $subscriber ) ) {
177 + return;
178 + }
179 +
180 + // Add subscriber to sequence.
181 + return $api->add_subscriber_to_sequence( $resource_id, $subscriber['subscriber']['id'] );
182 +
183 + /**
184 + * Tag
185 + */
186 + case 'tag':
187 + // Subscribe.
188 + $subscriber = $api->create_subscriber( $email, $first_name );
189 +
190 + // If an error occurred, don't attempt to add the subscriber to the Form, as it won't work.
191 + if ( is_wp_error( $subscriber ) ) {
192 + return;
193 + }
194 +
195 + // Add subscriber to tag.
196 + return $api->tag_subscriber( $resource_id, $subscriber['subscriber']['id'] );
197 +
198 + }
199 +
89 200 }
90 201
202 + /**
203 + * Gets the referrer URL to send to the `form_subscribe` API method.
204 + *
205 + * Falls back to the action's AJAX URL if the Post ID the form was
206 + * embedded in cannot be determined.
207 + *
208 + * @since 2.7.1
209 + *
210 + * @return string
211 + */
212 + private function get_referrer_url() {
213 +
214 + // If the request includes the Post ID the form was embedded in,
215 + // return that URL.
216 + if ( filter_has_var( INPUT_POST, '_wpcf7_container_post' ) ) {
217 + return get_permalink( absint( filter_input( INPUT_POST, '_wpcf7_container_post', FILTER_SANITIZE_NUMBER_INT ) ) );
218 + }
219 +
220 + // Return the AJAX URL.
221 + return home_url( add_query_arg( null, null ) );
222 +
223 + }
224 +
91 225 }
92 226
93 227 // Bootstrap.
94 228 add_action(
95 229 'convertkit_initialize_global',
96 - function() {
230 + function () {
97 231
98 232 new ConvertKit_ContactForm7();
99 233
100 234 }