-
-
Save johnpbloch/04943d6e99370542b38a to your computer and use it in GitHub Desktop.
Hide If Logged In/Out -- Gravity Forms
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 | |
/** | |
* Filter out the html for fields if they should be hidden from users logged in or out. | |
*/ | |
function _avm_dym_gform_field_content( $content, $field, $value, $zero, $form_id ) { | |
if( is_admin() ) | |
return $content; | |
if( !empty( $field['hideIfLoggedIn'] ) && is_user_logged_in() ) | |
return ''; | |
if( !empty( $field['hideIfLoggedOut'] ) && !is_user_logged_in() ) | |
return ''; | |
return $content; | |
} | |
/** | |
* Determine if there are any hidden fields that need to be filled in. | |
*/ | |
function _avm_gforms_filter_for_validation() { | |
if( isset( $_POST['gform_submit'] ) ) { | |
$form = RGFormsModel::get_form_meta( $_POST['gform_submit'] ); | |
$form = RGFormsModel::add_default_properties( $form ); | |
if( !isset( $form['fields'] ) || !is_array( $form['fields'] ) || empty( $form['fields'] ) ) | |
return; | |
foreach( $form['fields'] as $field ) { | |
if( !$field['isRequired'] || ( empty( $field['hideIfLoggedIn'] ) && empty( $field['hideIfLoggedOut'] ) ) ) | |
continue; | |
if( !empty( $field['hideIfLoggedIn'] ) && is_user_logged_in() ) | |
_avm_validate_logged_in( $field ); | |
elseif( !empty( $field['hideIfLoggedOut'] ) && !is_user_logged_in() ) | |
_avm_validate_logged_out( $field ); | |
} | |
} | |
} | |
/** | |
* Fill in fields for logged in users | |
*/ | |
function _avm_validate_logged_in( $field ) { | |
$user = wp_get_current_user(); | |
$_POST['input_' . $field['id']] = '-'; | |
switch( RGFormsModel::get_input_type( $field ) ) { | |
case 'name': | |
if( $field["nameFormat"] != "simple" ) { | |
$_POST['input_' . $field['id'] . '_3'] = empty( $user->first_name ) ? '-' : $user->first_name; | |
$_POST['input_' . $field['id'] . '_6'] = empty( $user->last_name ) ? '-' : $user->last_name; | |
} | |
break; | |
case 'email': | |
$_POST['input_' . $field['id']] = $user->user_email; | |
if( rgget( "emailConfirmEnabled", $field ) ) | |
$_POST['input_' . $field['id'] . '_2'] = $user->user_email; | |
break; | |
} | |
if( !empty( $field['defaultValue'] ) ) | |
$_POST['input_' . $field['id']] = GFCommon::replace_variables_prepopulate( $field['defaultValue'] ); | |
} | |
/** | |
* Fill in fields for logged out users | |
*/ | |
function _avm_validate_logged_out( $field ) { | |
$_POST['input_' . $field['id']] = '-'; | |
switch( RGFormsModel::get_input_type( $field ) ) { | |
case 'name': | |
if( $field["nameFormat"] != "simple" ) { | |
$_POST['input_' . $field['id'] . '_3'] = '-'; | |
$_POST['input_' . $field['id'] . '_6'] = '-'; | |
} | |
break; | |
case 'email': | |
$_POST['input_' . $field['id']] = 'n' . time() . rand( 0, 99 ) . '@example.com'; | |
if( rgget( "emailConfirmEnabled", $field ) ) | |
$_POST['input_' . $field['id'] . '_2'] = $_POST['input_' . $field['id']]; | |
break; | |
} | |
if( !empty( $field['defaultValue'] ) ) | |
$_POST['input_' . $field['id']] = GFCommon::replace_variables_prepopulate( $field['defaultValue'] ); | |
} | |
/** | |
* Add checkboxes to the 'advanced' settings tab for each field in the gravity forms admin area. | |
*/ | |
function _avm_gform_field_advanced_settings( $id, $form ) { | |
if( $id != -1 ) | |
return; | |
?> | |
<li> | |
<label><input type="checkbox" value="1" id="avmHideIfLoggedIn" onclick="SetFieldProperty('hideIfLoggedIn',this.checked);" />Hide this field if the user is logged in</label> | |
<label><input type="checkbox" value="1" id="avmHideIfLoggedOut" onclick="SetFieldProperty('hideIfLoggedOut',this.checked);" />Hide this field if the user is logged out</label> | |
</li> | |
<?php | |
add_action( 'admin_footer', '_avm_handle_field_settings' ); | |
} | |
/** | |
* Add javascript to hook into gravity forms' form update code and update the hide if logged in/out values. | |
*/ | |
function _avm_handle_field_settings() { | |
?> | |
<script> | |
jQuery(document).ready(function($){ | |
$(document).bind('gform_load_field_settings',function(event, field, form){ | |
var hili = (undefined === field.hideIfLoggedIn) ? false : (true === field.hideIfLoggedIn), | |
hilo = (undefined === field.hideIfLoggedOut) ? false : (true === field.hideIfLoggedOut); | |
$('#avmHideIfLoggedIn')[0].checked = hili; | |
$('#avmHideIfLoggedOut')[0].checked = hilo; | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_filter( 'gform_field_content', '_avm_dym_gform_field_content', 10, 5 ); | |
add_action( 'init', '_avm_gforms_filter_for_validation' ); | |
if( is_admin() ) | |
add_action( 'gform_field_advanced_settings', '_avm_gform_field_advanced_settings', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment