Skip to content

Instantly share code, notes, and snippets.

@gicolek
Created May 15, 2015 11:18
Show Gist options
  • Save gicolek/f02ade5b51bc796f69de to your computer and use it in GitHub Desktop.
Save gicolek/f02ade5b51bc796f69de to your computer and use it in GitHub Desktop.
Gravity Forms Password Recovery Second Form
<?php
// remember, the hook suffix, should contain the form id!
add_filter( 'gform_field_validation_1', 'wp_doin_validation_1', 10, 4 );
/**
* Custom GF validation function used for pagination and required fields
*
* @return string
*/
function wp_doin_validation_1($result, $value, $form, $field) {
global $pass;
$result['is_valid'] = true;
$classes = explode( ' ', $field['cssClass'] );
// lets store the new-pass in a global variable to make sure it'd be
// recursively available next time
if ( in_array( 'new-pass', $classes ) ) {
$pass = $value;
}
// lets check if the password fields are equal
if ( in_array( 'repeat-pass', $classes ) ) {
// if these two dont match make sure the fields are not valid
if ( $pass != $value ) {
$result['is_valid'] = false;
$result['message'] = 'Password mismatch.';
}
}
return $result;
}
add_action( "gform_pre_submission_1", "wp_doin_pre_submission_1" );
function wp_doin_pre_submission_1($form) {
// we'll need the data created before to update the correct user
global $gf_reset_user;
list( $rp_path ) = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) );
$rp_cookie = 'wp-resetpass-' . COOKIEHASH;
// get the old and new pass values
$pass = $_POST['input_1'];
$pass_confirm = $_POST['input_2'];
// if we're doing a cron job let's forget about it
if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) )
return;
// let's check if a user with given name exists
// we're already doing that in the form validation, but this gives us another bridge of safety
$user_id = username_exists( $gf_reset_user->ID );
// let's validate the email and the user
if ( !$user_id ) {
// let's add another safety check to make sure that the passwords remain unchanged
if ( !empty( $pass ) and ! empty( $pass_confirm ) and $pass === $pass_confirm ) {
reset_password( $gf_reset_user, $pass );
setcookie( $rp_cookie, ' ', time() - YEAR_IN_SECONDS, $rp_path, COOKIE_DOMAIN, is_ssl(), true );
wp_logout();
}
} else {
// validation failed
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment