| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Contact Form 7 class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Contact Form 7 Integration |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_ContactForm7 { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor. Registers required hooks with Contact Form 7. |
| 19 |
* |
| 20 |
* @since 1.9.6 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
add_action( 'wpcf7_contact_form', array( $this, 'maybe_enqueue_creator_network_recommendations_script' ) ); |
| 25 |
add_action( 'wpcf7_submit', array( $this, 'handle_wpcf7_submit' ), 10, 2 ); |
| 26 |
|
| 27 |
} |
| 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 |
/** |
| 68 |
* Sends a Contact Form 7's Form Name and Email values through the ConvertKit API |
| 69 |
* if a ConvertKit Form is mapped to this Contact Form 7 Form in the ConvertKit |
| 70 |
* Settings. |
| 71 |
* |
| 72 |
* @since 1.9.6 |
| 73 |
* |
| 74 |
* @param WPCF7_ContactForm $contact_form Contact Form 7 Form. |
| 75 |
* @param array $result Submission Result. |
| 76 |
*/ |
| 77 |
public function handle_wpcf7_submit( $contact_form, $result ) { |
| 78 |
|
| 79 |
// If Demo Mode is enabled on the Contact Form 7 Form, don't send anything to ConvertKit. |
| 80 |
// @see https://contactform7.com/additional-settings/. |
| 81 |
if ( isset( $result['demo_mode'] ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
// If the form submission failed, don't send anything to ConvertKit. |
| 86 |
if ( $result['status'] !== 'mail_sent' ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Get ConvertKit Form ID mapped to this Contact Form 7 Form. |
| 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 |
|
| 94 |
// If no ConvertKit subscribe setting is defined, bail. |
| 95 |
if ( ! $convertkit_subscribe_setting ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
// Bail if the API hasn't been configured. |
| 100 |
$settings = new ConvertKit_Settings(); |
| 101 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
// Get Contact Form 7 Submission. |
| 106 |
$form_data = WPCF7_Submission::get_instance()->get_posted_data(); |
| 107 |
|
| 108 |
// Bail if the expected email form field does not exist. |
| 109 |
if ( ! isset( $form_data['your-email'] ) || empty( $form_data['your-email'] ) ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
// Get email and first name. |
| 114 |
$email = $form_data['your-email']; |
| 115 |
$first_name = ''; |
| 116 |
if ( isset( $form_data['your-name'] ) && ! empty( $form_data['your-name'] ) ) { |
| 117 |
$name = explode( ' ', $form_data['your-name'] ); |
| 118 |
$first_name = $name[0]; |
| 119 |
} |
| 120 |
|
| 121 |
// If here, subscribe the user to the ConvertKit Form. |
| 122 |
// Initialize the API. |
| 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 |
); |
| 131 |
|
| 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 |
} |
| 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 |
|
| 200 |
} |
| 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 |
|
| 225 |
} |
| 226 |
|
| 227 |
// Bootstrap. |
| 228 |
add_action( |
| 229 |
'convertkit_initialize_global', |
| 230 |
function () { |
| 231 |
|
| 232 |
new ConvertKit_ContactForm7(); |
| 233 |
|
| 234 |
} |
| 235 |
); |
| 236 |
|