Last active
August 29, 2015 13:57
-
-
Save hiranthi/9364703 to your computer and use it in GitHub Desktop.
The "Better Pre-submission Confirmation" from Gravity Wiz, with a few adjustments (support for Gravitate Encryption and a little adjustment on the Order (products) table).
This file contains 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 | |
/** | |
* Better Pre-submission Confirmation | |
* http://gravitywiz.com/2012/08/04/better-pre-submission-confirmation/ | |
*/ | |
class GWPreviewConfirmation { | |
private static $lead; | |
function init() { | |
add_filter( 'gform_pre_render', array( 'GWPreviewConfirmation', 'replace_merge_tags' ) ); | |
add_filter( 'gform_replace_merge_tags', array( 'GWPreviewConfirmation', 'product_summary_merge_tag' ), 10, 3 ); | |
#--> Hiranthi: get all field values before printing them | |
add_filter( 'gform_get_field_value', array( 'GWPreviewConfirmation', 'get_field_value' ), 10, 3 ); | |
} | |
/** | |
* Decrypt the value if the GDS_Encryption_Class exists, otherwise just return the given value | |
* | |
* @author Hiranthi Molhoek-Herlaar, Onexa | |
* | |
* @var $value (str) - the value to decrypt | |
* | |
* @return $value (str) - the decrypted valud | |
**/ | |
public static function gravitate_decrypt_value( $value ) | |
{ | |
return ( class_exists( 'GDS_Encryption_Class' ) ) ? GDS_Encryption_Class::decrypt( $value ) : $value; | |
} // end gravitate_decrypt_value | |
/** | |
* Use the field value, right before displaying it | |
* | |
* @author Hiranthi Molhoek-Herlaar, Onexa | |
* | |
* @var $value - The value of the field (str or array, depending on the type of field) | |
* @var $lead - Lead Object, the current entry | |
* @var $field - Field Object | |
* | |
* @return $value - the (decrypted if needed) value of the field | |
**/ | |
public static function get_field_value( $value, $lead, $field ) | |
{ | |
if ( is_array( $value ) ) | |
{ | |
foreach( $value as $key => $input_value ) | |
$value[$key] = GWPreviewConfirmation::gravitate_decrypt_value( $input_value ); | |
return $value; | |
} | |
return GWPreviewConfirmation::gravitate_decrypt_value( $value ); | |
} // end get_field_value | |
public static function replace_merge_tags($form) { | |
$current_page = isset(GFFormDisplay::$submission[$form['id']]) ? GFFormDisplay::$submission[$form['id']]['page_number'] : 1; | |
$fields = array(); | |
// get all HTML fields on the current page | |
foreach($form['fields'] as &$field) { | |
// skip all fields on the first page | |
if(rgar($field, 'pageNumber') <= 1) | |
continue; | |
$default_value = rgar($field, 'defaultValue'); | |
preg_match_all('/{.+}/', $default_value, $matches, PREG_SET_ORDER); | |
if(!empty($matches)) { | |
// if default value needs to be replaced but is not on current page, wait until on the current page to replace it | |
if(rgar($field, 'pageNumber') != $current_page) { | |
$field['defaultValue'] = ''; | |
} else { | |
$field['defaultValue'] = self::preview_replace_variables($default_value, $form); | |
} | |
} | |
// only run 'content' filter for fields on the current page | |
if(rgar($field, 'pageNumber') != $current_page) | |
continue; | |
$html_content = rgar($field, 'content'); | |
preg_match_all('/{.+}/', $html_content, $matches, PREG_SET_ORDER); | |
if(!empty($matches)) { | |
$field['content'] = self::preview_replace_variables($html_content, $form); | |
} | |
} | |
return $form; | |
} | |
/** | |
* Adds special support for file upload, post image and multi input merge tags. | |
*/ | |
public static function preview_special_merge_tags($value, $input_id, $merge_tag, $field) { | |
// added to prevent overriding :noadmin filter (and other filters that remove fields) | |
if( !$value ) | |
return $value; | |
$input_type = RGFormsModel::get_input_type($field); | |
$is_upload_field = in_array( $input_type, array('post_image', 'fileupload') ); | |
$is_multi_input = is_array( rgar($field, 'inputs') ); | |
$is_input = intval( $input_id ) != $input_id; | |
if( !$is_upload_field && !$is_multi_input ) | |
return $value; | |
// if is individual input of multi-input field, return just that input value | |
if( $is_input ) | |
return $value; | |
$form = RGFormsModel::get_form_meta($field['formId']); | |
$lead = self::create_lead($form); | |
$currency = GFCommon::get_currency(); | |
if(is_array(rgar($field, 'inputs'))) { | |
$value = RGFormsModel::get_lead_field_value($lead, $field); | |
return GFCommon::get_lead_field_display($field, $value, $currency); | |
} | |
switch($input_type) { | |
case 'fileupload': | |
$value = self::preview_image_value("input_{$field['id']}", $field, $form, $lead); | |
$value = self::preview_image_display($field, $form, $value); | |
break; | |
default: | |
$value = self::preview_image_value("input_{$field['id']}", $field, $form, $lead); | |
$value = GFCommon::get_lead_field_display($field, $value, $currency); | |
break; | |
} | |
return $value; | |
} | |
public static function preview_image_value($input_name, $field, $form, $lead) { | |
$field_id = $field['id']; | |
$file_info = RGFormsModel::get_temp_filename($form['id'], $input_name); | |
$source = RGFormsModel::get_upload_url($form['id']) . "/tmp/" . $file_info["temp_filename"]; | |
if(!$file_info) | |
return ''; | |
switch(RGFormsModel::get_input_type($field)){ | |
case "post_image": | |
list(,$image_title, $image_caption, $image_description) = explode("|:|", $lead[$field['id']]); | |
$value = !empty($source) ? $source . "|:|" . $image_title . "|:|" . $image_caption . "|:|" . $image_description : ""; | |
break; | |
case "fileupload" : | |
$value = $source; | |
break; | |
} | |
return $value; | |
} | |
public static function preview_image_display($field, $form, $value) { | |
// need to get the tmp $file_info to retrieve real uploaded filename, otherwise will display ugly tmp name | |
$input_name = "input_" . str_replace('.', '_', $field['id']); | |
$file_info = RGFormsModel::get_temp_filename($form['id'], $input_name); | |
$file_path = $value; | |
if(!empty($file_path)){ | |
$file_path = esc_attr(str_replace(" ", "%20", $file_path)); | |
$value = "<a href='$file_path' target='_blank' title='" . __("Click to view", "gravityforms") . "'>" . $file_info['uploaded_filename'] . "</a>"; | |
} | |
return $value; | |
} | |
/** | |
* Retrieves $lead object from class if it has already been created; otherwise creates a new $lead object. | |
*/ | |
public static function create_lead( $form ) { | |
if( empty( self::$lead ) ) { | |
self::$lead = RGFormsModel::create_lead( $form ); | |
self::clear_field_value_cache( $form ); | |
} | |
return self::$lead; | |
} | |
public static function preview_replace_variables($content, $form) { | |
$lead = self::create_lead($form); | |
// add filter that will handle getting temporary URLs for file uploads and post image fields (removed below) | |
// beware, the RGFormsModel::create_lead() function also triggers the gform_merge_tag_filter at some point and will | |
// result in an infinite loop if not called first above | |
add_filter('gform_merge_tag_filter', array('GWPreviewConfirmation', 'preview_special_merge_tags'), 10, 4); | |
$content = GFCommon::replace_variables($content, $form, $lead, false, false, false); | |
// remove filter so this function is not applied after preview functionality is complete | |
remove_filter('gform_merge_tag_filter', array('GWPreviewConfirmation', 'preview_special_merge_tags')); | |
#--> Hiranthi: Added because the table the Order stuff was didn't look as if it was a part of the rest of the data | |
return str_replace('<table>', '<table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">', $content ); | |
} | |
public static function product_summary_merge_tag($text, $form, $lead) { | |
if(empty($lead)) | |
$lead = self::create_lead($form); | |
$remove = array("<tr bgcolor=\"#EAF2FA\">\n <td colspan=\"2\">\n <font style=\"font-family: sans-serif; font-size:12px;\"><strong>Order</strong></font>\n </td>\n </tr>\n <tr bgcolor=\"#FFFFFF\">\n <td width=\"20\"> </td>\n <td>\n ", "\n </td>\n </tr>"); | |
$product_summary = str_replace($remove, '', GFCommon::get_submitted_pricing_fields($form, $lead, 'html')); | |
return str_replace('{product_summary}', $product_summary, $text); | |
} | |
public static function clear_field_value_cache( $form ) { | |
if( ! class_exists( 'GFCache' ) ) | |
return; | |
foreach( $form['fields'] as &$field ) { | |
if( GFFormsModel::get_input_type( $field ) == 'total' ) | |
GFCache::delete( 'GFFormsModel::get_lead_field_value__' . $field['id'] ); | |
} | |
} | |
} | |
GWPreviewConfirmation::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment