Instantly share code, notes, and snippets.
Last active
March 23, 2021 18:50
-
Save mustardBees/e1fdfd53c86a428763f58f23678d7fd6 to your computer and use it in GitHub Desktop.
Prepopulate fields across forms with previously entered values.
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 | |
/** | |
* Plugin Name: Formidable Prepopulate from Cookie | |
* Plugin URI: https://www.iweb.co.uk/ | |
* Author: iWeb | |
* Author URI: https://www.iweb.co.uk/ | |
* Description: Prepopulate fields across forms with previously entered values. | |
* Version: 1.1.0 | |
* License: GPL2 | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
/** | |
* Class Iweb_Formidable_Prepopulate_From_Cookie | |
*/ | |
class Iweb_Formidable_Prepopulate_From_Cookie { | |
/** | |
* Initialize the plugin by hooking into Formidable | |
*/ | |
public function __construct() { | |
add_action( 'frm_field_options_form', array( $this, 'field_options' ) ); | |
add_filter( 'frm_update_field_options', array( $this, 'update_field_options' ), 10, 3 ); | |
add_action( 'frm_after_create_entry', array( $this, 'save_values' ), 30, 2 ); | |
add_filter( 'frm_get_default_value', array( $this, 'prepopulate' ), 10, 2 ); | |
} | |
/** | |
* Add cookie key field option. | |
* | |
* Add an additional option to each field. Allow the user to enter a cookie key. | |
* | |
* @param array $field | |
*/ | |
public function field_options( $field ) { ?> | |
<tr class="frm_options_heading"> | |
<td colspan="2"> | |
<div class="menu-settings"> | |
<h3 class="frm_no_bg">Prepopulate from Cookie</h3> | |
</div> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
<label>Cookie Key</label> | |
<span class="frm_help frm_icon_font frm_tooltip_icon" title="" data-original-title="If the user has entered a value for this key before, the field’s value will be prepopulated with the previously entered value."></span> | |
</td> | |
<td> | |
<input type="text" name="field_options[cookie_key_<?php echo $field['id'] ?>]" value="<?php echo esc_attr( $field['cookie_key'] ); ?>" /> | |
</td> | |
</tr> | |
<?php | |
} | |
/** | |
* Update the field options from the admin area | |
* | |
* @param $field_options | |
* @param $field | |
* @param $values | |
* | |
* @return mixed | |
*/ | |
public function update_field_options( $field_options, $field, $values ) { | |
$field_options['cookie_key'] = isset( $values['field_options'][ 'cookie_key_' . $field->id ] ) ? sanitize_text_field( $values['field_options'][ 'cookie_key_' . $field->id ] ) : false; | |
return $field_options; | |
} | |
/** | |
* Set a cookie to remember values for next time. | |
* | |
* Loop through each submitted field. Check each field to see if it has a cookie | |
* key. If the field has a cookie key, set a cookie to remember the value for | |
* next time. | |
* | |
* @param int $entry_id | |
* @param int $form_id | |
*/ | |
public function save_values( $entry_id, $form_id ) { | |
$cookie_keys = $this->get_cookie_keys_by_form( $form_id ); | |
if ( isset( $_POST['item_meta'] ) ) { | |
foreach ( $_POST['item_meta'] as $field_id => $value ) { | |
$field_id = intval( $field_id ); | |
// Skip this field if it doesn't have a cookie key. | |
if ( ! array_key_exists( $field_id, $cookie_keys ) ) { | |
continue; | |
} | |
setcookie( 'frm_prepopulate_' . $cookie_keys[ $field_id ], $value, time() + apply_filters( 'iweb_frm_prepopulate_cookie_expire', WEEK_IN_SECONDS ), COOKIEPATH, COOKIE_DOMAIN, false, true ); | |
} | |
} | |
} | |
/** | |
* Prepopulate field from cookie. | |
* | |
* @param $new_value | |
* @param $field | |
* | |
* @return mixed | |
*/ | |
public function prepopulate( $new_value, $field ) { | |
// Bail if we don't have a cookie key for this field. | |
if ( ! isset( $field->field_options['cookie_key'] ) || empty( $field->field_options['cookie_key'] ) ) { | |
return $new_value; | |
} | |
$new_value = isset( $_COOKIE[ 'frm_prepopulate_' . $field->field_options['cookie_key'] ] ) ? $_COOKIE[ 'frm_prepopulate_' . $field->field_options['cookie_key'] ] : $new_value; | |
return $new_value; | |
} | |
/** | |
* Get cookie keys by form. | |
* | |
* @param int $form_id | |
* | |
* @return array | |
*/ | |
public function get_cookie_keys_by_form( $form_id ) { | |
global $wpdb; | |
$cookie_keys = array(); | |
$sql = "SELECT id, field_options FROM {$wpdb->prefix}frm_fields WHERE form_id = %d"; | |
$sql = $wpdb->prepare( $sql, $form_id ); | |
$fields = $wpdb->get_results( $sql ); | |
if ( ! empty( $fields ) ) { | |
foreach ( $fields as $field ) { | |
$field_options = maybe_unserialize( $field->field_options ); | |
if ( isset( $field_options['cookie_key'] ) && ! empty( $field_options['cookie_key'] ) ) { | |
$cookie_keys[ $field->id ] = $field_options['cookie_key']; | |
} | |
} | |
} | |
return $cookie_keys; | |
} | |
} | |
new Iweb_Formidable_Prepopulate_From_Cookie(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment