Skip to content

Instantly share code, notes, and snippets.

@mattneal-stafflink
Last active May 26, 2021 04:19
Show Gist options
  • Save mattneal-stafflink/5d7b40dcbdbccf3d989051ff9d994eb5 to your computer and use it in GitHub Desktop.
Save mattneal-stafflink/5d7b40dcbdbccf3d989051ff9d994eb5 to your computer and use it in GitHub Desktop.
User registration functions to sign up agency staff. Place all functions in functions.php of the hubs' theme.
<?php
function is_valid_email_domain($login, $email, $errors ){
$valid_email_domains = array("ocre.com.au","stafflink.com.au");// whitelist email domain lists
$valid = false;
foreach( $valid_email_domains as $d ){
$d_length = strlen( $d );
$current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
if( $current_email_domain == strtolower($d) ){
$valid = true;
break;
}
}
// if invalid, return error message
if( $valid === false ){
$errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: Sorry, your email address is not on the approved list. You must use a valid Ouwens Casserly email address.' ));
}
}
add_action('register_post', 'is_valid_email_domain',10,3 );
// output the form field
add_action('register_form', 'ad_register_fields');
function ad_register_fields() {
?>
<p>
<label for="firstname"><?php _e('First Name') ?><br />
<input type="firstname" name="firstname" id="firstname" class="input" value="<?php echo esc_attr($_POST['firstname']); ?>" size="25" tabindex="20" />
</label>
</p>
<?php
}
// save new first name
add_filter('pre_user_first_name', 'add_user_firstname');
function add_user_firstname($firstname) {
if (isset($_POST['firstname'])) {
$firstname = $_POST['firstname'];
}
return $firstname;
}
add_filter( 'gform_user_registration_prepared_value', 'serialize_user_type', 10, 5 );
function serialize_user_type($value, $field, $input_id, $entry, $is_username ) {
if ( $field->id == '7' && $field->type == 'select' ) {
// if ($value == 'property_manager') { // This method works.
// $value = [$value]; // this needs ot happen for it to store correctly. Not sure who to point the finger at here.
// }
$value = [$value]; // this needs ot happen for it to store correctly. Not sure who to point the finger at here.
}
return $value;
}
add_action( 'gform_user_registered', 'update_user_groups', 10, 4 );
// Add user to LearnDash Group if purchases specific WooCommerce product
function update_user_groups( $user_id, $feed, $entry, $user_pass ) {
// Add group ID from LearnDash here
$ld_group_id = 1658;
ld_update_group_access( $user_id, $ld_group_id, false );
}
/**
* Bypass Force Login to allow for exceptions. Used to push everyone to the login page, except if they are registering.
*
* @param bool $bypass Whether to disable Force Login. Default false.
* @param string $visited_url The visited URL.
* @return bool
*/
function my_forcelogin_bypass( $bypass, $visited_url ) {
// Allow all single posts
if ( is_single() ) {
$bypass = true;
}
// Allow these absolute URLs
$allowed = array(
home_url( '/register/' )
);
if ( ! $bypass ) {
$bypass = in_array( $visited_url, $allowed );
}
return $bypass;
}
add_filter( 'v_forcelogin_bypass', 'my_forcelogin_bypass', 10, 2 );
/*
* Gravity Wiz // Gravity Forms // Email Domain Validator // Used by STAFFLINK on the Gravity Forms user rego form.
*
* This snippets allows you to exclude a list of invalid domains or include a list of valid domains for your Gravity Form Email fields.
*
* @version 1.4
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/banlimit-email-domains-for-gravity-form-email-fields/
*/
class GW_Email_Domain_Validator {
private $_args;
function __construct($args) {
$this->_args = wp_parse_args( $args, [
'form_id' => false,
'field_id' => false,
'domains' => false,
'validation_message' => __( 'Sorry, <strong>%s</strong> email accounts are not eligible for this form.' ),
'mode' => 'ban' // also accepts "limit"
] );
// convert field ID to an array for consistency, it can be passed as an array or a single ID
if($this->_args['field_id'] && !is_array($this->_args['field_id']))
$this->_args['field_id'] = array($this->_args['field_id']);
$form_filter = $this->_args['form_id'] ? "_{$this->_args['form_id']}" : '';
add_filter("gform_validation{$form_filter}", array($this, 'validate'));
}
function validate($validation_result) {
$form = $validation_result['form'];
foreach($form['fields'] as &$field) {
// if this is not an email field, skip
if(RGFormsModel::get_input_type($field) != 'email')
continue;
// if field ID was passed and current field is not in that array, skip
if($this->_args['field_id'] && !in_array($field['id'], $this->_args['field_id']))
continue;
$page_number = GFFormDisplay::get_source_page( $form['id'] );
if( $page_number > 0 && $field->pageNumber != $page_number ) {
continue;
}
if( GFFormsModel::is_field_hidden( $form, $field, array() ) ) {
continue;
}
$domain = $this->get_email_domain($field);
// if domain is valid OR if the email field is empty, skip
if($this->is_domain_valid($domain) || empty($domain))
continue;
$validation_result['is_valid'] = false;
$field['failed_validation'] = true;
$field['validation_message'] = sprintf($this->_args['validation_message'], $domain);
}
$validation_result['form'] = $form;
return $validation_result;
}
function get_email_domain( $field ) {
$email = explode( '@', rgpost( "input_{$field['id']}" ) );
return trim( rgar( $email, 1 ) );
}
function is_domain_valid( $domain ) {
$mode = $this->_args['mode'];
$domain = strtolower( $domain );
foreach( $this->_args['domains'] as $_domain ) {
$_domain = strtolower( $_domain );
$full_match = $domain == $_domain;
$suffix_match = strpos( $_domain, '.' ) === 0 && $this->str_ends_with( $domain, $_domain );
$has_match = $full_match || $suffix_match;
if( $mode == 'ban' && $has_match ) {
return false;
} else if( $mode == 'limit' && $has_match ) {
return true;
}
}
return $mode == 'limit' ? false : true;
}
function str_ends_with( $string, $text ) {
$length = strlen( $string );
$text_length = strlen( $text );
if( $text_length > $length ) {
return false;
}
return substr_compare( $string, $text, $length - $text_length, $text_length ) === 0;
}
}
/*
* Valid email addresses and options for validation.
*/
class GWEmailDomainControl extends GW_Email_Domain_Validator { }
new GW_Email_Domain_Validator( [
'form_id' => 1,
'field_id' => 2,
'domains' => ['ocre.com.au', 'stafflink.com.au'],
'validation_message' => __( 'Sorry! <strong>%s</strong> email accounts are not eligible for this form. Please use a valid email address.' ),
'mode' => 'limit'
] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment