HarunRRayhan

Gravity Forms Pre-Submission

Jul 26th, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.72 KB | None | 0 0
  1. <?php
  2. /**
  3. * Better Pre-submission Confirmation
  4. * http://gravitywiz.com/2012/08/04/better-pre-submission-confirmation/
  5. */
  6. class GWPreviewConfirmation {
  7.  
  8.     private static $lead;
  9.  
  10.     public static function init() {
  11.         add_filter( 'gform_pre_render', array( __class__, 'replace_merge_tags' ) );
  12.     }
  13.  
  14.     public static function replace_merge_tags( $form ) {
  15.  
  16.         $current_page = isset(GFFormDisplay::$submission[$form['id']]) ? GFFormDisplay::$submission[$form['id']]['page_number'] : 1;
  17.         $fields = array();
  18.  
  19.         // get all HTML fields on the current page
  20.         foreach($form['fields'] as &$field) {
  21.  
  22.             // skip all fields on the first page
  23.             if(rgar($field, 'pageNumber') <= 1)
  24.                 continue;
  25.  
  26.             $default_value = rgar($field, 'defaultValue');
  27.             preg_match_all('/{.+}/', $default_value, $matches, PREG_SET_ORDER);
  28.             if(!empty($matches)) {
  29.                 // if default value needs to be replaced but is not on current page, wait until on the current page to replace it
  30.                 if(rgar($field, 'pageNumber') != $current_page) {
  31.                     $field['defaultValue'] = '';
  32.                 } else {
  33.                     $field['defaultValue'] = self::preview_replace_variables($default_value, $form);
  34.                 }
  35.             }
  36.  
  37.             // only run 'content' filter for fields on the current page
  38.             if(rgar($field, 'pageNumber') != $current_page)
  39.                 continue;
  40.  
  41.             $html_content = rgar($field, 'content');
  42.             preg_match_all('/{.+}/', $html_content, $matches, PREG_SET_ORDER);
  43.             if(!empty($matches)) {
  44.                 $field['content'] = self::preview_replace_variables($html_content, $form);
  45.             }
  46.  
  47.         }
  48.  
  49.         return $form;
  50.     }
  51.  
  52.     /**
  53.     * Adds special support for file upload, post image and multi input merge tags.
  54.     */
  55.     public static function preview_special_merge_tags($value, $input_id, $merge_tag, $field) {
  56.        
  57.         // added to prevent overriding :noadmin filter (and other filters that remove fields)
  58.         if( ! $value )
  59.             return $value;
  60.        
  61.         $input_type = RGFormsModel::get_input_type($field);
  62.        
  63.         $is_upload_field = in_array( $input_type, array('post_image', 'fileupload') );
  64.         $is_multi_input = is_array( rgar($field, 'inputs') );
  65.         $is_input = intval( $input_id ) != $input_id;
  66.        
  67.         if( !$is_upload_field && !$is_multi_input )
  68.             return $value;
  69.  
  70.         // if is individual input of multi-input field, return just that input value
  71.         if( $is_input )
  72.             return $value;
  73.            
  74.         $form = RGFormsModel::get_form_meta($field['formId']);
  75.         $lead = self::create_lead($form);
  76.         $currency = GFCommon::get_currency();
  77.  
  78.         if(is_array(rgar($field, 'inputs'))) {
  79.             $value = RGFormsModel::get_lead_field_value($lead, $field);
  80.             return GFCommon::get_lead_field_display($field, $value, $currency);
  81.         }
  82.  
  83.         switch($input_type) {
  84.         case 'fileupload':
  85.             $value = self::preview_image_value("input_{$field['id']}", $field, $form, $lead);
  86.             $value = self::preview_image_display($field, $form, $value);
  87.             break;
  88.         default:
  89.             $value = self::preview_image_value("input_{$field['id']}", $field, $form, $lead);
  90.             $value = GFCommon::get_lead_field_display($field, $value, $currency);
  91.             break;
  92.         }
  93.  
  94.         return $value;
  95.     }
  96.  
  97.     public static function preview_image_value($input_name, $field, $form, $lead) {
  98.  
  99.         $field_id = $field['id'];
  100.         $file_info = RGFormsModel::get_temp_filename($form['id'], $input_name);
  101.         $source = RGFormsModel::get_upload_url($form['id']) . "/tmp/" . $file_info["temp_filename"];
  102.  
  103.         if(!$file_info)
  104.             return '';
  105.  
  106.         switch(RGFormsModel::get_input_type($field)){
  107.  
  108.             case "post_image":
  109.                 list(,$image_title, $image_caption, $image_description) = explode("|:|", $lead[$field['id']]);
  110.                 $value = !empty($source) ? $source . "|:|" . $image_title . "|:|" . $image_caption . "|:|" . $image_description : "";
  111.                 break;
  112.  
  113.             case "fileupload" :
  114.                 $value = $source;
  115.                 break;
  116.  
  117.         }
  118.  
  119.         return $value;
  120.     }
  121.  
  122.     public static function preview_image_display($field, $form, $value) {
  123.  
  124.         // need to get the tmp $file_info to retrieve real uploaded filename, otherwise will display ugly tmp name
  125.         $input_name = "input_" . str_replace('.', '_', $field['id']);
  126.         $file_info = RGFormsModel::get_temp_filename($form['id'], $input_name);
  127.  
  128.         $file_path = $value;
  129.         if(!empty($file_path)){
  130.             $file_path = esc_attr(str_replace(" ", "%20", $file_path));
  131.             $value = "<a href='$file_path' target='_blank' title='" . __("Click to view", "gravityforms") . "'>" . $file_info['uploaded_filename'] . "</a>";
  132.         }
  133.         return $value;
  134.  
  135.     }
  136.  
  137.     /**
  138.     * Retrieves $lead object from class if it has already been created; otherwise creates a new $lead object.
  139.     */
  140.     public static function create_lead( $form ) {
  141.        
  142.         if( empty( self::$lead ) ) {
  143.             self::$lead = GFFormsModel::create_lead( $form );
  144.             self::clear_field_value_cache( $form );
  145.         }
  146.        
  147.         return self::$lead;
  148.     }
  149.  
  150.     public static function preview_replace_variables( $content, $form ) {
  151.  
  152.         $lead = self::create_lead($form);
  153.  
  154.         // add filter that will handle getting temporary URLs for file uploads and post image fields (removed below)
  155.         // beware, the RGFormsModel::create_lead() function also triggers the gform_merge_tag_filter at some point and will
  156.         // result in an infinite loop if not called first above
  157.         add_filter('gform_merge_tag_filter', array('GWPreviewConfirmation', 'preview_special_merge_tags'), 10, 4);
  158.  
  159.         $content = GFCommon::replace_variables($content, $form, $lead, false, false, false);
  160.  
  161.         // remove filter so this function is not applied after preview functionality is complete
  162.         remove_filter('gform_merge_tag_filter', array('GWPreviewConfirmation', 'preview_special_merge_tags'));
  163.  
  164.         return $content;
  165.     }
  166.    
  167.     public static function clear_field_value_cache( $form ) {
  168.        
  169.         if( ! class_exists( 'GFCache' ) )
  170.             return;
  171.            
  172.         foreach( $form['fields'] as &$field ) {
  173.             if( GFFormsModel::get_input_type( $field ) == 'total' )
  174.                 GFCache::delete( 'GFFormsModel::get_lead_field_value__' . $field['id'] );
  175.         }
  176.        
  177.     }
  178.  
  179. }
  180.  
  181. GWPreviewConfirmation::init();
Add Comment
Please, Sign In to add comment