Created
August 21, 2012 12:24
-
-
Save thomasgriffin/3415027 to your computer and use it in GitHub Desktop.
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 | |
add_filter( 'gform_shortcode_form', 'tgm_filter_data', 10, 3 ); | |
function tgm_filter_data( $form, $atts, $content ) { | |
/** Get the saved data from the current user (or return early if there is none to format) */ | |
$data = json_decode( get_user_meta( get_current_user_id(), 'casting_form_data', true ) ); | |
if ( empty( $data ) ) | |
return $form; | |
/** Also go ahead and grab any file data as well */ | |
$files = json_decode( $data->gform_uploaded_files_1 ); | |
/** Require the simple DOM parser library */ | |
require get_stylesheet_directory() . '/lib/php/simple_html_dom.php'; | |
/** Get the HTML from the form string */ | |
$html = str_get_html( $form ); | |
/** Loop through the form input fields and update values */ | |
foreach ( $html->find('input') as $obj ) { | |
foreach ( $data as $key => $val ) { | |
if ( ( $key == $obj->attr['id'] ) && ! empty( $val ) ) { | |
if ( preg_match( '#^choice#', $key ) ) | |
$obj->attr['checked'] = $val; | |
else | |
$obj->attr['value'] = $val; | |
} | |
} | |
} | |
/** Loop through the form input file fields and update values */ | |
foreach ( $html->find('input[type="file"]') as $obj ) { | |
foreach ( (array) $files as $key => $val ) { | |
if ( ( $key == $obj->attr['name'] ) && ! empty( $val ) ) { | |
$obj->attr['value'] = $val; | |
} | |
} | |
} | |
/** Loop through the form select fields and update values */ | |
foreach ( $html->find('select') as $obj ) { | |
foreach ( $data as $key => $val ) { | |
if ( ( $key == $obj->attr['id'] ) && ! empty( $val ) ) { | |
foreach ( $obj->children as $option ) { | |
if ( isset( $option->attr['value'] ) && $val == $option->attr['value'] ) | |
$option->attr['selected'] = 'selected'; | |
} | |
} | |
} | |
} | |
/** Loop through the form textareas and update values */ | |
foreach ( $html->find('textarea') as $obj ) { | |
foreach ( $data as $key => $val ) { | |
if ( ( $key == $obj->attr['id'] ) && ! empty( $val ) ) { | |
$obj->innertext = $val; | |
} | |
} | |
} | |
/** Return the Simple DOM HTML instead of the regular HTML */ | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment