patilswapnilv /
Gravity-Pre-submission-Confirmation
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Plugin Name: Gravity Pre-submission Confirmation |
||
| 4 | * Plugin URI: http://github.com/patilswapnilv/pre-submission-confirmation/ |
||
| 5 | * Description: Adds a quick pre-submission confirmation page to your Graviy forms where users can preview their entered data before it is submitted. |
||
| 6 | * Author: patilswapnilv |
||
| 7 | * Version: 1.0.1 |
||
| 8 | * Author URI: http://swapnilpatil.in |
||
| 9 | * Domain Path: /languages/ |
||
| 10 | * |
||
| 11 | * Please read: <a href="https://wordpress.org/plugins/gravity-pre-submission-confirmation/installation/">Installation</a> |
||
| 12 | * and <a href="https://wordpress.org/plugins/gravity-pre-submission-confirmation/faq/">FAQ</a>. |
||
| 13 | * |
||
| 14 | * PHP version 5 |
||
| 15 | * |
||
| 16 | * LICENCE: GNU GENERAL PUBLIC LICENSE |
||
| 17 | * Version 3, 29 June 2007 |
||
| 18 | * Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/> |
||
| 19 | * Everyone is permitted to copy and distribute verbatim copies |
||
| 20 | * of this license document, but changing it is not allowed. |
||
| 21 | * |
||
| 22 | * Main file, contains the plugin metadata and activation processes |
||
| 23 | * |
||
| 24 | * @category Core |
||
| 25 | * @package gravity-pre-submission-confirmation |
||
| 26 | * @author Swapnil V. Patil <[email protected]> |
||
| 27 | * @license GPL-3.0+ https://www.gnu.org/licenses/gpl-3.0.en.html |
||
| 28 | * @version 1.0.0 |
||
| 29 | * @link https://github.com/patilswapnilv/gravity-pre-submission-confirmation |
||
| 30 | */ |
||
| 31 | |||
| 32 | class GFPreviewConfirmation { |
||
| 33 | |||
| 34 | private static $lead; |
||
| 35 | |||
| 36 | public static function init() { |
||
| 37 | add_filter( 'gform_pre_render', array( __class__, 'replace_merge_tags' ) ); |
||
| 38 | } |
||
| 39 | |||
| 40 | public static function replace_merge_tags( $form ) { |
||
| 41 | |||
| 42 | $current_page = isset(GFFormDisplay::$submission[$form['id']]) ? GFFormDisplay::$submission[$form['id']]['page_number'] : 1; |
||
| 43 | $fields = array(); |
||
| 44 | |||
| 45 | // get all HTML fields on the current page |
||
| 46 | foreach($form['fields'] as &$field) { |
||
| 47 | |||
| 48 | // skip all fields on the first page |
||
| 49 | if(rgar($field, 'pageNumber') <= 1) |
||
| 50 | continue; |
||
| 51 | |||
| 52 | $default_value = rgar($field, 'defaultValue'); |
||
| 53 | preg_match_all('/{.+}/', $default_value, $matches, PREG_SET_ORDER); |
||
| 54 | if(!empty($matches)) { |
||
| 55 | // if default value needs to be replaced but is not on current page, wait until on the current page to replace it |
||
| 56 | if(rgar($field, 'pageNumber') != $current_page) { |
||
| 57 | $field['defaultValue'] = ''; |
||
| 58 | } else { |
||
| 59 | $field['defaultValue'] = self::preview_replace_variables($default_value, $form); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | // only run 'content' filter for fields on the current page |
||
| 64 | if(rgar($field, 'pageNumber') != $current_page) |
||
| 65 | continue; |
||
| 66 | |||
| 67 | $html_content = rgar($field, 'content'); |
||
| 68 | preg_match_all('/{.+}/', $html_content, $matches, PREG_SET_ORDER); |
||
| 69 | if(!empty($matches)) { |
||
| 70 | $field['content'] = self::preview_replace_variables($html_content, $form); |
||
| 71 | } |
||
| 72 | |||
| 73 | } |
||
| 74 | |||
| 75 | return $form; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Adds special support for file upload, post image and multi input merge tags. |
||
| 80 | */ |
||
| 81 | public static function preview_special_merge_tags($value, $input_id, $merge_tag, $field) { |
||
| 82 | |||
| 83 | // added to prevent overriding :noadmin filter (and other filters that remove fields) |
||
| 84 | if( ! $value ) |
||
| 85 | return $value; |
||
| 86 | |||
| 87 | $input_type = RGFormsModel::get_input_type($field); |
||
| 88 | |||
| 89 | $is_upload_field = in_array( $input_type, array('post_image', 'fileupload') ); |
||
| 90 | $is_multi_input = is_array( rgar($field, 'inputs') ); |
||
| 91 | $is_input = intval( $input_id ) != $input_id; |
||
| 92 | |||
| 93 | if( !$is_upload_field && !$is_multi_input ) |
||
| 94 | return $value; |
||
| 95 | |||
| 96 | // if is individual input of multi-input field, return just that input value |
||
| 97 | if(!$is_input ) |
||
| 98 | return $value; |
||
| 99 | |||
| 100 | $form = RGFormsModel::get_form_meta($field['formId']); |
||
| 101 | $lead = self::create_lead($form); |
||
| 102 | $currency = GFCommon::get_currency(); |
||
| 103 | |||
| 104 | if(is_array(rgar($field, 'inputs'))) { |
||
| 105 | $value = RGFormsModel::get_lead_field_value($lead, $field); |
||
| 106 | return GFCommon::get_lead_field_display($field, $value, $currency); |
||
| 107 | } |
||
| 108 | |||
| 109 | switch($input_type) { |
||
| 110 | case 'fileupload': |
||
| 111 | $value = self::preview_image_value("input_{$field['id']}", $field, $form, $lead); |
||
| 112 | $value = self::preview_image_display($field, $form, $value); |
||
| 113 | break; |
||
| 114 | default: |
||
| 115 | $value = self::preview_image_value("input_{$field['id']}", $field, $form, $lead); |
||
| 116 | $value = GFCommon::get_lead_field_display($field, $value, $currency); |
||
| 117 | break; |
||
| 118 | } |
||
| 119 | |||
| 120 | return $value; |
||
| 121 | } |
||
| 122 | |||
| 123 | public static function preview_image_value($input_name, $field, $form, $lead) { |
||
| 124 | |||
| 125 | $field_id = $field['id']; |
||
| 126 | $file_info = RGFormsModel::get_temp_filename($form['id'], $input_name); |
||
| 127 | $source = RGFormsModel::get_upload_url($form['id']) . "/tmp/" . $file_info["temp_filename"]; |
||
| 128 | |||
| 129 | if(!$file_info) |
||
| 130 | return ''; |
||
| 131 | |||
| 132 | switch(RGFormsModel::get_input_type($field)){ |
||
| 133 | |||
| 134 | case "post_image": |
||
| 135 | list(,$image_title, $image_caption, $image_description) = explode("|:|", $lead[$field['id']]); |
||
| 136 | $value = !empty($source) ? $source . "|:|" . $image_title . "|:|" . $image_caption . "|:|" . $image_description : ""; |
||
| 137 | break; |
||
| 138 | |||
| 139 | case "fileupload" : |
||
| 140 | $value = $source; |
||
| 141 | break; |
||
| 142 | |||
| 143 | } |
||
| 144 | |||
| 145 | return $value; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 146 | } |
||
| 147 | |||
| 148 | public static function preview_image_display($field, $form, $value) { |
||
| 149 | |||
| 150 | // need to get the tmp $file_info to retrieve real uploaded filename, otherwise will display ugly tmp name |
||
| 151 | $input_name = "input_" . str_replace('.', '_', $field['id']); |
||
| 152 | $file_info = RGFormsModel::get_temp_filename($form['id'], $input_name); |
||
| 153 | |||
| 154 | $file_path = $value; |
||
| 155 | if(!empty($file_path)){ |
||
| 156 | $file_path = esc_attr(str_replace(" ", "%20", $file_path)); |
||
| 157 | $value = "<a href='$file_path' target='_blank' title='" . __("Click to view", "gravityforms") . "'>" . $file_info['uploaded_filename'] . "</a>"; |
||
| 158 | } |
||
| 159 | return $value; |
||
| 160 | |||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Retrieves $lead object from class if it has already been created; otherwise creates a new $lead object. |
||
| 165 | */ |
||
| 166 | public static function create_lead( $form ) { |
||
| 167 | |||
| 168 | if( empty( self::$lead ) ) { |
||
| 169 | self::$lead = GFFormsModel::create_lead( $form ); |
||
| 170 | self::clear_field_value_cache( $form ); |
||
| 171 | } |
||
| 172 | |||
| 173 | return self::$lead; |
||
| 174 | } |
||
| 175 | |||
| 176 | public static function preview_replace_variables( $content, $form ) { |
||
| 177 | |||
| 178 | $lead = self::create_lead($form); |
||
| 179 | |||
| 180 | // add filter that will handle getting temporary URLs for file uploads and post image fields (removed below) |
||
| 181 | // beware, the RGFormsModel::create_lead() function also triggers the gform_merge_tag_filter at some point and will |
||
| 182 | // result in an infinite loop if not called first above |
||
| 183 | add_filter('gform_merge_tag_filter', array('GFPreviewConfirmation', 'preview_special_merge_tags'), 10, 4); |
||
| 184 | |||
| 185 | $content = GFCommon::replace_variables($content, $form, $lead, false, false, false); |
||
| 186 | |||
| 187 | // remove filter so this function is not applied after preview functionality is complete |
||
| 188 | remove_filter('gform_merge_tag_filter', array('GFPreviewConfirmation', 'preview_special_merge_tags')); |
||
| 189 | |||
| 190 | return $content; |
||
| 191 | } |
||
| 192 | |||
| 193 | public static function clear_field_value_cache( $form ) { |
||
| 194 | |||
| 195 | if( ! class_exists( 'GFCache' ) ) |
||
| 196 | return; |
||
| 197 | |||
| 198 | foreach( $form['fields'] as &$field ) { |
||
| 199 | if( GFFormsModel::get_input_type( $field ) == 'total' ) |
||
| 200 | GFCache::delete( 'GFFormsModel::get_lead_field_value__' . $field['id'] ); |
||
| 201 | } |
||
| 202 | |||
| 203 | } |
||
| 204 | |||
| 205 | } |
||
| 206 | |||
| 207 | GFPreviewConfirmation::init(); |
||
| 208 |