Plugin Directory

Changeset 1021223


Ignore:
Timestamp:
11/06/2014 10:59:34 PM (11 years ago)
Author:
unclhos
Message:

Added ajax, no persist and changed variable names.

Location:
gravity-forms-data-persistence-add-on-reloaded/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • gravity-forms-data-persistence-add-on-reloaded/trunk/persistent_multipage_forms-reloaded.php

    r969698 r1021223  
    55  Description: This is a <a href="http://www.gravityforms.com/" target="_blank">Gravity Form</a> plugin. A big limitation with Gravity Form is, in case of big multipage forms, if you close or refresh the page during somewhere midle of some step. all the steps data will loose. this plugin solves that problem. This is an updated version of asthait's plugin.
    66  Author: Robert Iseley
    7   Version: 3.1.2
     7  Version: 3.2
    88  Author URI: http://www.robertiseley.com
    99  Orginal Plugin by: asthait
    1010 */
    1111
    12 
    13 
    14 add_action("gform_post_paging", "ri_page_changed", 10, 3);
    15 
    16 function ri_page_changed($form, $coming_from_page, $current_page) {
    17     if ($form['isPersistent']) {
    18         if (is_user_logged_in()) {
    19             $option_key = ri_getFormOptionKeyForGF($form);
    20             update_option($option_key, json_encode($_POST));
    21         }
    22     }
    23 }
    24 
     12define('GFDPVERSION', '3.2');
     13
     14add_action('wp_head', 'ri_gfdp_version_head');
     15function ri_gfdp_version_head() {
     16    echo '<!-- Gravity Forms Data Persistence Add-On Reloaded Version '.GFDPVERSION.' -->';
     17}
     18
     19// Register garlic script for local persistence
     20add_action('wp_enqueue_scripts', 'ri_gfdp_script_register');
     21function ri_gfdp_script_register() {
     22    wp_enqueue_script('jquery');
     23}
     24
     25// Render persistence data before form output
    2526add_filter("gform_pre_render", "ri_pre_populate_the_form");
    26 
    2727function ri_pre_populate_the_form($form) {
    28     if ($form['isPersistent']) {
     28    if ($form['isPersistent'] || $form['ri_gfdp_persist']) {
    2929        $current_page = GFFormDisplay::get_current_page($form["id"]);
    3030        if ($current_page == 1) {
     
    3535        }
    3636    }
    37 
    38 
     37       
    3938    return $form;
    4039}
    4140
     41// Updating data via ajax auto save
     42add_action( 'wp_ajax_gfdp_save', 'ri_gfdp_ajax' );
     43function ri_gfdp_ajax() {
     44    global $wpdb; // this is how you get access to the database
     45    parse_str($_POST['form'], $data);
     46    $form_id = isset($data['gform_submit']) ? $data["gform_submit"] : 0;
     47        if($form_id){
     48            $form_info = RGFormsModel::get_form($form_id);
     49            $is_valid_form = $form_info && $form_info->is_active;
     50            if($is_valid_form){
     51                $form = RGFormsModel::get_form_meta($form_id);
     52                $form = RGFormsModel::add_default_properties($form);
     53                ri_gfdp_ajax_save($form);
     54                echo "Saved";   
     55            } else {
     56                echo "Invalid Form";
     57            }
     58        } else {
     59            echo "Invalid Form";
     60        }
     61
     62    die(); // this is required to terminate immediately and return a proper response
     63}
     64
     65
     66//The js for ajax call
     67add_action('gform_enqueue_scripts', 'ri_gfdp_js_enqueue', 90, 2);
     68function ri_gfdp_js_enqueue($form, $is_ajax) {
     69    if($form['ri_gfdp_persist'] == 'ajax' && is_user_logged_in()) {
     70        add_action('wp_footer', 'ri_gfdp_js');
     71    }
     72}
     73function ri_gfdp_js() {
     74?>
     75        <script type="text/javascript" >
     76        var changed = false;
     77       
     78        function gfdp_events() {
     79            jQuery('form').on('change keyup', function() {
     80                    changed = true;
     81                })
     82        }
     83       
     84        jQuery(document).ready(gfdp_events);
     85        jQuery(document).ajaxComplete(gfdp_events);
     86       
     87       
     88        function gfdp_ajax($) {
     89            if(changed == true) {
     90                var data = {
     91                    'action': 'gfdp_save',
     92                    'form': jQuery('form').serialize()
     93                };
     94   
     95                jQuery.ajax({
     96                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
     97                    type: 'POST',
     98                    data: data,
     99                    success: function(response) {
     100                        changed = false;
     101                    }
     102                })
     103            }
     104        };
     105       
     106        jQuery(document).ready( setInterval(gfdp_ajax, 10000));
     107        </script> <?php
     108}
     109
     110// Saving data from ajax call
     111function ri_gfdp_ajax_save($form, $coming_from_page, $current_page) {
     112    if ($form['ri_gfdp_persist'] == 'ajax') {
     113        if (is_user_logged_in()) {
     114            $option_key = ri_getFormOptionKeyForGF($form);
     115            parse_str($_POST['form'], $data);
     116            $data = ri_gfdp_sanitize_data($data, $form);
     117            update_option($option_key, json_encode($data));
     118        }
     119    }
     120}
     121
     122function ri_gfdp_sanitize_data($data, $form) {
     123    foreach ($form['fields'] as $field) {
     124        if($field['ri_gfdp_no_persist']) {
     125            if(is_array($field['inputs'])) {
     126                foreach($field['inputs'] as $input) {
     127                    $data['input_'.str_replace('.','_',$input['id'])] = '';
     128                }
     129            } else
     130            $data['input_'.$field['id']] = ''; 
     131        }
     132    }
     133    return $data;   
     134}
     135
     136// Updating persistence data on page change
     137add_action("gform_post_paging", "ri_page_changed", 10, 3);
     138function ri_page_changed($form, $coming_from_page, $current_page) {
     139    if ($form['isPersistent'] || $form['ri_gfdp_persist']) {
     140        if (is_user_logged_in()) {
     141            $option_key = ri_getFormOptionKeyForGF($form);
     142            $data = ri_gfdp_sanitize_data($_POST, $form);
     143            update_option($option_key, json_encode($data));
     144        }
     145    }
     146}
     147
     148// Updating or clearning persistence data on form submission
    42149add_action("gform_post_submission", "ri_set_post_content", 10, 2);
    43 
    44150function ri_set_post_content($entry, $form) {
    45     if ($form['isPersistent']) {
     151    if ($form['isPersistent'] || $form['ri_gfdp_persist']) {
    46152        //Update form data in wp_options table
    47153        if (is_user_logged_in()) {
    48154            $option_key = ri_getFormOptionKeyForGF($form);
    49155           
    50             if($form['isEnablePersistentClear'])
     156            if($form['isEnablePersistentClear'] || $form['ri_gfdp_persist_clear'])
    51157                delete_option($option_key);
    52             else
    53                 update_option($option_key, json_encode($_POST));
     158            else {
     159                parse_str($_POST['form'], $data);
     160                $data = ri_gfdp_sanitize_data($data, $form);
     161                update_option($option_key, json_encode($data));
     162            }
    54163
    55164            $entry_option_key = ri_getEntryOptionKeyForGF($form);
    56165            if (get_option($entry_option_key)) {
    57166                //Delete old entry from GF tables
    58                 if (!$form['isEnableMulipleEntry']) {
     167                if (!$form['isEnableMulipleEntry'] || !$form['ri_gfdp_multiple_entries']) {
    59168                   RGFormsModel::delete_lead(get_option($entry_option_key));
    60                 }
    61                
     169                } 
    62170            }
    63171        }
    64172
    65173        //Update entry in wp_options table
    66 
    67174        update_option($entry_option_key, $entry['id']);
    68175    }
    69176}
    70177
     178// Create and return option table key for a form and user
    71179function ri_getFormOptionKeyForGF($form) {
    72180
     
    79187}
    80188
     189// Create and return option table key for user form entry
    81190function ri_getEntryOptionKeyForGF($form) {
    82191
     
    89198}
    90199
    91 //Add persistent checkbox to the form settings
     200//Add persistent settings to the form settings
    92201add_filter("gform_form_settings", "ri_persistency_settings", 50, 2);
    93 
    94202function ri_persistency_settings($form_settings, $form) {
    95203   
    96    
    97 
    98     //create settings on position 50 (right after Admin Label)
     204    // create settings on position 50 (right after Admin Label)
    99205    $tr_persistent = '
    100             <tr>
    101                 <th>Persistent</th>
    102             <td>
    103             <input type="checkbox" id="form_persist_value" onclick="SetFormPersistency();" /> Enable form persistence
    104             <label for="form_persist_value">             
    105                 <?php gform_tooltip("form_persist_tooltip") ?>
    106             </label>
    107             </td>
     206        <tr>
     207            <td colspan="2"><h4 class="gf_settings_subgroup_title">Persistence</h4></td>
    108208        </tr>
    109         <tr>
    110             <th></th>
    111             <td>
    112             <input type="checkbox" id="form_enable_multiple_entry_entry" onclick="SetFormMultipleEntry();" /> Enable multi entry from same user while form is persistent
    113             <label for="form_enable_multiple_entry">             
    114                 <?php gform_tooltip("form_enable_multiple_entry_tooltip") ?>
    115             </label>
    116             </td>
    117         </tr>
    118         <tr>
    119         <th></th>
    120             <td>
    121             <input type="checkbox" id="form_enable_persistent_clear" onclick="SetFormPersistentClear();" /> Clear persistence on submit
    122             <label for="form_enable_multiple_entry">             
    123                 <?php gform_tooltip("form_enable_persistent_clear_tooltip") ?>
    124             </label>
     209        <tr>
     210            <th>Persistence '. gform_tooltip('ri_gfdp_persist', '', true) .' </th>
     211            <td>
     212                <select name="ri_gfdp_persist" id="ri_gfdp_persist">
     213                    <option value="off" '.selected(rgar($form, "ri_gfdp_persist"), 'off', false).'>Off</option>
     214                    <option value="submit_only" '.selected(rgar($form, "ri_gfdp_persist"), 'submit_only', false).'>Save data on page change/submit only</option>
     215                    <option value="ajax" '.selected(rgar($form, "ri_gfdp_persist"), 'ajax', false).'>Save data with ajax</option>
     216                </select>
    125217            </td>
    126218        </tr>';
    127219       
     220    $tr_persistent .= '
     221        <tr>
     222            <th>Multiple Entries '. gform_tooltip("ri_gfdp_multiple_entries", '', true) .' </th>
     223            <td>
     224            <input type="checkbox" name="ri_gfdp_multiple_entries" id="ri_gfdp_multiple_entries" '.checked(rgar($form, "ri_gfdp_multiple_entries"),'1', false).'" value="1" />
     225            <label for="ri_gfdp_multiple_entries">Allow multiple entries</label>
     226            </td>
     227        </tr>';
     228       
     229    $tr_persistent .= '
     230        <tr>
     231            <th>Clear Persistence '. gform_tooltip("ri_gfdp_clear_persist", '', true) .' </th>
     232           
     233            <td>
     234            <input type="checkbox" name="ri_gfdp_persist_clear" id="ri_gfdp_persist_clear" '.checked(rgar($form, "ri_gfdp_persist_clear"), '1', false).'" value="1" />
     235            <label for="ri_gfdp_persist_clear"> Clear persistence on submit</label>
     236            </td>
     237        </tr>';
     238       
    128239        $form_settings["Form Options"]['persistent'] = $tr_persistent;
    129240       
     
    131242}
    132243
    133 //Action to inject supporting script to the form editor page
     244add_filter('gform_pre_form_settings_save', 'ri_gfdp_save_form_settings');
     245function ri_gfdp_save_form_settings($form) {
     246   
     247    //Remove old setting names
     248    unset($form['isPersistent']);
     249    unset($form['isEnableMulipleEntry']);
     250    unset($form['isEnablePersistentClear']);
     251   
     252    //update settings
     253    $form['ri_gfdp_persist'] = rgpost('ri_gfdp_persist');
     254    $form['ri_gfdp_multiple_entries'] = rgpost('ri_gfdp_multiple_entries');
     255    $form['ri_gfdp_persist_clear'] = rgpost('ri_gfdp_persist_clear');
     256   
     257    return $form;
     258   
     259};
     260
     261// Action to inject supporting script to the form editor page
    134262add_action("gform_advanced_settings", "ri_editor_script_persistency");
    135263function ri_editor_script_persistency() {
    136264    ?>
    137265    <script type='text/javascript'>
    138                
    139         function SetFormPersistency(){
    140             form.isPersistent = jQuery("#form_persist_value").is(":checked");
    141         }
    142         function SetFormMultipleEntry(){
    143             form.isEnableMulipleEntry = jQuery("#form_enable_multiple_entry_entry").is(":checked");
    144         }
    145         function SetFormPersistentClear(){
    146             form.isEnablePersistentClear = jQuery("#form_enable_persistent_clear").is(":checked");
    147         }
    148                
    149         jQuery("#form_persist_value").attr("checked", form.isPersistent);       
    150         jQuery("#form_enable_multiple_entry_entry").attr("checked", form.isEnableMulipleEntry);
    151         jQuery("#form_enable_persistent_clear").attr("checked", form.isEnablePersistentClear);   
    152        
     266        if(typeof form != 'undefined') {
     267            if(typeof form.isPersistent != 'undefined') {
     268                jQuery("#ri_gfdp_persist").val('submit_only');
     269            }
     270            if(typeof form.isEnableMulipleEntry != 'undefined') {
     271                jQuery("#ri_gfdp_multiple_entries").attr("checked", form.isEnableMulipleEntry);
     272            }
     273            if(typeof form.isEnablePersistentClear != 'undefined') {
     274                jQuery("#ri_gfdp_persist_clear").attr("checked", form.isEnablePersistentClear);
     275            }
     276        }
     277    </script>
     278    <?php
     279}
     280
     281add_action('gform_field_advanced_settings', 'ri_gfdp_advanced_settings', 10, 2);
     282function ri_gfdp_advanced_settings($position, $form_id) {
     283    if($position == 550) {
     284    ?> 
     285        <li class="field_ri_gfdp_no_persist_setting">
     286            <input type="checkbox" id="ri_gfdp_no_persist" name="ri_gfdp_no_persist" onclick="SetFieldProperty('ri_gfdp_no_persist', this.checked);" />
     287            <label for="ri_gfdp_no_persist" class="inline">
     288                <?php _e('Do not allow persistence', 'ri_gfdp'); ?>
     289                <?php gform_tooltip('ri_gfdp_no_persist'); ?>
     290            </label> 
     291        </li>
     292    <?php   
     293    }
     294}
     295
     296//Action to inject supporting script to the form editor page
     297add_action("gform_editor_js", "ri_gfdp_editor_script", 11);
     298function ri_gfdp_editor_script(){
     299    ?>
     300    <script type='text/javascript'>
     301        //adding setting to fields of type "text"
     302        //fieldSettings["text"] += ", .field_ri_gfdp_no_persist_setting";
     303
     304        //binding to the load field settings event to initialize the checkbox
     305        jQuery(document).bind("gform_load_field_settings", function(event, field, form){
     306            jQuery("#ri_gfdp_no_persist").attr("checked", field["ri_gfdp_no_persist"] == true);
     307        });
    153308    </script>
    154309    <?php
    155310}
    156311
    157 //Filter to add a new tooltip
     312// Filter to add a new tooltip
    158313add_filter('gform_tooltips', 'ri_add_persistency_tooltips');
    159 
    160314function ri_add_persistency_tooltips($tooltips) {
    161     $tooltips["form_persist_tooltip"] = "<h6>Persistency</h6>Check this box to make this form persistant";
    162     $tooltips["form_enable_multiple_entry_tooltip"] = "<h6>Persistency</h6>This will allow multiple entry from same user but, user can't edit their last";
     315    $tooltips["ri_gfdp_persist"] = "<h6>Persistency</h6>Select to save users progress with form so they may continue at another time. Must be a logged in user.";
     316    $tooltips["ri_gfdp_multiple_entries"] = "<h6>Multiple Entries Allowed</h6>This will allow multiple entry from same user. User can not edit their last and the previous entry not removed from the entry list";
     317    $tooltips['ri_gfdp_no_persist'] = '<h6>No Persist</h6>Checking this will removed this field(s) from the persistence data. User will have to re-enter information upon returning to the form. This does not affect the submission of an entry. Useful for sensitive information.';
     318    $tooltips['ri_gfdp_clear_persist'] = '<h6>Clear Persist</h6>This option will delete the persistence data when a form is submitted. Allow the user to return to a fresh blank form.';
    163319    return $tooltips;
    164320}
  • gravity-forms-data-persistence-add-on-reloaded/trunk/readme.txt

    r969698 r1021223  
    44Requires at least: 2.9.2
    55Tested up to: 3.9.1
    6 Stable tag: 3.1.1
     6Stable tag: 3.2.0
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4444== Changelog ==
    4545
     46= 3.2.0 =
     471. Added ajax save on 10 second timer. Will add custom timer in future. Sponsored by Letterquick.com
     482. Add a "No Persist" setting for form feilds. Found under the "Advanced" tab for each fields settings. Sponsored by Letterquick.com
     493. Changed variable names for uniformity. Still calling old variables for upgrade compatiblity.
     50
    4651= 3.1.1 =
    47 1. Added persistent data call has been added to the first page only.
     521. Persistent data call has been added to the first page only.
    4853
    4954= 3.1 =
Note: See TracChangeset for help on using the changeset viewer.