← All changes
|
includes/integrations/contactform7/class-convertkit-contactform7.php
+104
-9
2.2.8
→
3.4.4
View file →
| @@ -87,19 +87,19 @@ | ||
| 87 | 87 | return; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | // Get ConvertKit Form ID mapped to this Contact Form 7 Form. |
| 91 | - $contact_form_7_settings = new ConvertKit_ContactForm7_Settings(); | |
| 92 | - $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() ); | |
| 93 | 93 | |
| 94 | - // If no ConvertKit Form is mapped to this Contact Form 7 Form, bail. | |
| 95 | - if ( ! $convertkit_form_id ) { | |
| 94 | + // If no ConvertKit subscribe setting is defined, bail. | |
| 95 | + if ( ! $convertkit_subscribe_setting ) { | |
| 96 | 96 | return; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | // Bail if the API hasn't been configured. |
| 100 | 100 | $settings = new ConvertKit_Settings(); |
| 101 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 101 | + if ( ! $settings->has_access_and_refresh_token() ) { | |
| 102 | 102 | return; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // Get Contact Form 7 Submission. |
| @@ -119,21 +119,116 @@ | ||
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | // If here, subscribe the user to the ConvertKit Form. |
| 122 | 122 | // Initialize the API. |
| 123 | - $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 | + ); | |
| 124 | 131 | |
| 125 | - // Send request. | |
| 126 | - $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 | + } | |
| 127 | 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 | + | |
| 128 | 200 | } |
| 129 | 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 | + | |
| 130 | 225 | } |
| 131 | 226 | |
| 132 | 227 | // Bootstrap. |
| 133 | 228 | add_action( |
| 134 | 229 | 'convertkit_initialize_global', |
| 135 | - function() { | |
| 230 | + function () { | |
| 136 | 231 | |
| 137 | 232 | new ConvertKit_ContactForm7(); |
| 138 | 233 | |
| 139 | 234 | } |