Last active
October 10, 2015 14:58
-
-
Save spivurno/3708519 to your computer and use it in GitHub Desktop.
Gravity Wiz // Conditional Confirmations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Conditional Confirmations (with Merge Tag Support) | |
* http://gravitywiz.com/2012/08/18/conditional-confirmations/ | |
* | |
* Provides the ability to register conditional confirmations. To register a new conditional confirmation | |
* use the GWConditionalConfirmations::add_conditional_confirmation() function. | |
* | |
* GWConditionalConfirmations::add_conditional_confirmation($form_id, $field_id, $operator, $value, $confirmation); | |
* | |
* @param mixed $form_id The ID of the form for which you would like to register a conditional confirmation. | |
* @param mixed $field_id The field ID of the field for which you would like to base the confirmation condition. | |
* @param mixed $operator The operator which will be used to compare the submitted field value against the specified value | |
* passed in the $value parameter. Accepted values are "is", "isnot", "greater_than", "less_than", "contains", "starts_with", "ends_with" | |
* @param mixed $value | |
* @param mixed $confirmation Accepted values are: | |
* array('redirect' => 'http:://yoururl.com') | |
* array('page' => 12) | |
* 'Plain text confirmation!' | |
* @param $do_init Defaults to true. Will automatically run the init which will trigger the conditional functionality when the | |
* form is submitted. If you would like to init on your own, you can pass false here and call the GWConditionalConfirmation::init() | |
* function anytime before the gform_confirmation hook is called. | |
*/ | |
class GWConditionalConfirmations { | |
public static $init = false; | |
public static $confirmations = array(); | |
public static function init() { | |
add_filter('gform_confirmation', array( __class__, 'get_conditional_confirmation'), 10, 3); | |
} | |
public static function add_conditional_confirmation($form_id, $field_id, $operator, $value, $confirmation, $do_init = true) { | |
if(!self::$init && $do_init) { | |
GWConditionalConfirmations::init(); | |
self::$init = true; | |
} | |
if(!isset(self::$confirmations[$form_id])) | |
self::$confirmations[$form_id] = array(); | |
$confirmation['gwConditionalLogic'] = array('field_id' => $field_id, 'value' => $value, 'operator' => $operator); | |
array_push(self::$confirmations[$form_id], $confirmation); | |
} | |
public static function get_conditional_confirmation($confirmation, $form, $lead) { | |
if(!isset(self::$confirmations[$form['id']])) | |
return $confirmation; | |
foreach(self::$confirmations[$form['id']] as $conf) { | |
if(self::is_condition_met($form, $conf['gwConditionalLogic'])) | |
return self::convert_confirmation($conf, $form, $lead); | |
} | |
return $confirmation; | |
} | |
public static function is_condition_met($form, $condition) { | |
$field = RGFormsModel::get_field($form, $condition['field_id']); | |
$is_visible = !RGFormsModel::is_field_hidden($form, $field, array()); | |
$field_value = apply_filters('gw_condition_field_value', RGFormsModel::get_field_value($field, array()) ); | |
$is_value_match = RGFormsModel::is_value_match($field_value, $condition['value'], $condition['operator']); | |
return $is_value_match && $is_visible; | |
} | |
public static function convert_confirmation($confirmation, $form, $lead) { | |
// if redirect is set, return as is | |
if(isset($confirmation['redirect'])) { | |
$confirmation['redirect'] = GFCommon::replace_variables( trim( $confirmation['redirect'] ), $form, $lead, true ); | |
return $confirmation; | |
// if page is set, return as redirect with permalink | |
} else if(isset($confirmation['page'])) { | |
return array('redirect' => get_permalink($confirmation['page'])); | |
// if nothing else, assume it is text and wrap in the confirmation HTML | |
} else { | |
return "<div id='preview_form_container'>" . GFCommon::replace_variables($confirmation, $form, $lead, true) . "</div>"; | |
} | |
} | |
} | |
// example for form ID 7 where the confirmation will redirect the user to http://google.com if the value of field ID 3 is less than 10 | |
GWConditionalConfirmations::add_conditional_confirmation(7, 3, 'less_than', 10, array('redirect' => 'http://google.com')); | |
// example for form ID 5 where a text confirmation will be displayed if field ID 2 is equal to "Virginia" | |
GWConditionalConfirmations::add_conditional_confirmation(5, 2, 'is', 'Virginia', 'Confirmed! You are from Virginia!'); | |
// example for form ID 11 where the confirmation will redirect to the WordPress page ID 12 if the value of field ID 4 is greater than 500 | |
GWConditionalConfirmations::add_conditional_confirmation(11, 4, 'greater_than', 500, array('page' => 12)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment