| 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 |
// Get ConvertKit Form ID mapped to this Forminator Form. |
| 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 ); |
| 85 |
|
| 86 |
// If no ConvertKit subscribe setting is defined, bail. |
| 87 |
if ( ! $convertkit_subscribe_setting ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// Bail if the API hasn't been configured. |
| 92 |
$settings = new ConvertKit_Settings(); |
| 93 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
// Extract the name and email field values. |
| 98 |
$first_name = false; |
| 99 |
$email = false; |
| 100 |
foreach ( $form_data_array as $form_field ) { |
| 101 |
// Skip field if it doesn't have a type - it's likely an IP address value. |
| 102 |
if ( ! array_key_exists( 'field_type', $form_field ) ) { |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
// Extract the name / email address, depending on the field type. |
| 107 |
switch ( $form_field['field_type'] ) { |
| 108 |
case 'name': |
| 109 |
$name = explode( ' ', $form_field['value'] ); |
| 110 |
$first_name = $name[0]; |
| 111 |
break; |
| 112 |
|
| 113 |
case 'email': |
| 114 |
$email = $form_field['value']; |
| 115 |
break; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Bail if no email address could be found. |
| 120 |
if ( ! $email ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
// If here, subscribe the user to the ConvertKit Form. |
| 125 |
// Initialize the API. |
| 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 |
); |
| 134 |
|
| 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 ) ); |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
// Bootstrap. |
| 233 |
add_action( |
| 234 |
'convertkit_initialize_global', |
| 235 |
function () { |
| 236 |
|
| 237 |
new ConvertKit_Forminator(); |
| 238 |
|
| 239 |
} |
| 240 |
); |
| 241 |
|