Last active
June 21, 2022 18:17
-
-
Save crstauf/5b6da3b818221ec0b408ae70a1cca232 to your computer and use it in GitHub Desktop.
Populate Gravity Forms with dynamic population via JavaScript.
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 declare( strict_types=1 ); | |
/** | |
* Plugin name: CSSLLC Gravity Forms JavaScript Prepopulation | |
* Plugin URI: https://gist.github.com/crstauf/5b6da3b818221ec0b408ae70a1cca232 | |
* Description: Populate Gravity Forms with dynamic population via JavaScript. | |
* Author URI: https://develop.calebstauffer.com | |
* Author: Caleb Stauffer | |
* Version: 1.0 | |
*/ | |
class CSSLLC_GravityForms_JS_Prepopulation { | |
/** | |
* @var string Input field attribute name. | |
*/ | |
const ATTRIBUTE_NAME = 'data-gform-js-prepop'; | |
/** | |
* @var bool Check if there's at least one field with dynamic population. | |
*/ | |
protected $has_prepop = false; | |
/** | |
* Initialize. | |
* | |
* @return void | |
*/ | |
public static function init() : void { | |
static $once = false; | |
if ( $once ) { | |
return; | |
} | |
new self; | |
$once = true; | |
} | |
/** | |
* Construct. | |
*/ | |
protected function __construct() { | |
add_filter( 'gform_field_content', array( $this, 'filter__gform_field_content' ), 10, 2 ); | |
add_action( 'gform_register_init_scripts', array( $this, 'action__gform_register_init_scripts' ) ); | |
} | |
/** | |
* Filter: gform_field_content | |
* | |
* Add attribute to input field. | |
* | |
* @param string $markup | |
* @param object $field | |
* | |
* @return string | |
*/ | |
public function filter__gform_field_content( $markup, $field ) : string { | |
if ( ! $field->allowsPrepopulate || empty( $field->inputName ) ) { | |
return $markup; | |
} | |
$this->has_prepop = true; | |
$attribute = sprintf( '%s="%s"', self::ATTRIBUTE_NAME, esc_attr( $field->inputName ) ); | |
$markup = str_replace( ' name=', ' ' . $attribute . ' name=', $markup ); | |
return $markup; | |
} | |
/** | |
* Action: gform_register_init_script | |
* | |
* Print JavaScript to populate the fields. | |
* | |
* @param array $form | |
* | |
* @return void | |
*/ | |
public function action__gform_register_init_scripts( $form ) : void { | |
static $once = false; | |
if ( ! $this->has_prepop || $once ) { | |
return; | |
} | |
ob_start(); | |
?> | |
<script> | |
( function() { | |
const attribute_name = <?php echo json_encode( self::ATTRIBUTE_NAME ) ?>; | |
const query_string = window.location.search; | |
const query_params = new URLSearchParams( query_string ); | |
const prepop_fields = document.querySelectorAll( 'input[' + attribute_name + ']' ); | |
if ( ! prepop_fields.length ) { | |
return; | |
} | |
var field_id, field_prepop_parameter, query_param_value; | |
prepop_fields.forEach( function( field ) { | |
if ( ! field.hasAttribute( attribute_name ) ) { | |
return; | |
} | |
field_prepop_parameter = field.getAttribute( attribute_name ); | |
if ( ! query_params.has( field_prepop_parameter ) ) { | |
return; | |
} | |
query_param_value = query_params.get( field_prepop_parameter ); | |
if ( ! query_param_value.length ) { | |
return; | |
} | |
document.querySelector( 'input#' + field.getAttribute( 'id' ) ).value = query_param_value; | |
} ); | |
} () ); | |
</script> | |
<?php | |
$script = ob_get_clean(); | |
$script = str_replace( array( '<script>', '</script>' ), '', $script ); | |
$once = true; | |
GFFormDisplay::add_init_script( $form['id'], __CLASS__, GFFormDisplay::ON_PAGE_RENDER, $script ); | |
} | |
} | |
CSSLLC_GravityForms_JS_Prepopulation::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment