Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Created March 13, 2017 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panoslyrakis/a802a223b5ca52dcd707d733e5788b43 to your computer and use it in GitHub Desktop.
Save panoslyrakis/a802a223b5ca52dcd707d733e5788b43 to your computer and use it in GitHub Desktop.
Remove default filter from 'the_content' for the password reset page and add a new one to the login hook. This way the rest content of the page will not be lost.
<?php
add_action( 'wp_head', function(){
global $wp_filter, $post;
if( ! class_exists( 'MS_Model_Pages' ) || $post->post_name != MS_Model_Pages::MS_PAGE_ACCOUNT || ! isset( $_GET['action'] ) || $_GET['action'] != 'rp' ){
return;
}
// Originally uses the_content filter in MS_Controller_Frontend::user_account_manager()
// Lets unset it so it displays the login's shortcode MS_View_Shortcode_Login::to_html()
// So then we can hook in the ms_compact_code filter
$hook = 'the_content';
if( empty( $hook ) || !isset( $wp_filter[$hook] ) ){
return;
}
foreach( $wp_filter[ $hook ] as $priority => $actions ) {
foreach( $actions as $key => $data ) {
//Looking for the 'MS_Controller_Frontend'
if ( is_a( $data['function'][0], 'MS_Controller_Frontend' ) ) {
//unset( $wp_filter[ $hook ][ $priority ][$key] );
unset( $wp_filter[ $hook ][ $priority ] );
}
}
}
}, 10 );
add_filter( 'ms_compact_code', function( $html, $data ){
global $post;
if( ! class_exists( 'MS_Model_Pages' ) || $post->post_name != MS_Model_Pages::MS_PAGE_ACCOUNT || ! isset( $_GET['action'] ) || $_GET['action'] != 'rp' ){
return $html;
}
static $Reset_Result = null;
if ( null === $Reset_Result ) {
lib3()->array->equip_get( 'login', 'key' );
lib3()->array->equip_post( 'pass1', 'pass2' );
$rp_login = wp_unslash( $_GET['login'] );
$rp_key = wp_unslash( $_GET['key'] );
$err_msg = new WP_Error();
$fatal_error = false;
lib3()->array->strip_slashes( $_POST, 'pass1', 'pass2' );
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
// Get the user object and validate the key.
if ( $rp_login && $rp_key ) {
$user = check_password_reset_key( $rp_key, $rp_login );
} else {
$user = false;
}
if ( ! $user || is_wp_error( $user ) ) {
// If the user was not found then show an error message.
if ( $user && 'expired_key' == $user->get_error_code() ) {
$fatal_error = true;
$err_msg->add(
'password_expired_key',
__( 'Sorry, this reset-key is not valid anymore. Please request a new reset email and try again.', 'membership2' )
);
} else {
$fatal_error = true;
$err_msg->add(
'password_invalid_key',
__( 'Sorry, we did not find a valid reset-key. Please request a new reset email and try again.', 'membership2' )
);
}
} else {
// If the user provided a new password, then check it now.
if ( $pass1 && $pass1 != $pass2 ) {
$pass1 = false;
$err_msg->add(
'password_reset_mismatch',
__( 'The passwords do not match, try again.', 'membership2' )
);
}
}
if ( $fatal_error && count( $err_msg->errors ) ) {
$url = esc_url_raw(
add_query_arg(
array( 'show' => 'lostpass' ),
remove_query_arg( array( 'action', 'key', 'login' ) )
)
);
$Reset_Result = sprintf(
'[ms-note type="warning"]%s[/ms-note]<a href="%s">%s</a>',
$err_msg->get_error_message(),
$url,
__( 'Request a new password-reset key', 'membership2' )
);
} elseif ( $pass1 ) {
// This action is documented in wp-login.php
do_action( 'validate_password_reset', $err_msg, $user );
reset_password( $user, $_POST['pass1'] );
// All done! Show success message and link to login form
$url = esc_url_raw(
remove_query_arg( array( 'action', 'key', 'login' ) )
);
$Reset_Result = sprintf(
'[ms-note type="info"]%s[/ms-note]<a href="%s">%s</a>',
__( 'Your Password has been reset.', 'membership2' ),
$url,
__( 'Login with your new password', 'membership2' )
);
} else {
// This action is documented in wp-login.php
do_action( 'validate_password_reset', $err_msg, $user );
wp_enqueue_script( 'utils' );
wp_enqueue_script( 'user-profile' );
ob_start();
if ( count( $err_msg->errors ) ) {
printf(
'[ms-note type="warning"]%s[/ms-note]',
implode( '<br>', $err_msg->get_error_messages() )
);
}
?>
<form name="resetpassform" id="resetpassform"
action="" method="post" autocomplete="off" class="ms-form">
<input type="hidden" id="user_login"
value="<?php echo esc_attr( $rp_login ); ?>" autocomplete="off"/>
<p class="user-pass1-wrap">
<label for="pass1"><?php _e( 'New password' ) ?></label><br />
<div class="wp-pwd">
<span class="password-input-wrapper">
<input type="password" data-reveal="1" data-pw="<?php echo esc_attr( wp_generate_password( 16 ) ); ?>" name="pass1" id="pass1" class="input" size="20" value="" autocomplete="off" aria-describedby="pass-strength-result" />
</span>
<div id="pass-strength-result" class="hide-if-no-js" aria-live="polite"><?php _e( 'Strength indicator' ); ?></div>
</div>
</p>
<p class="user-pass2-wrap">
<label for="pass2"><?php _e( 'Confirm new password' ) ?></label><br />
<input type="password" name="pass2" id="pass2" class="input" size="20" value="" autocomplete="off" />
</p>
<p class="description indicator-hint"><?php echo wp_get_password_hint(); ?></p>
<br class="clear"/>
<?php
// This action is documented in wp-login.php
do_action( 'resetpass_form', $user );
?>
<p class="submit">
<input type="hidden" name="rp_key" value="<?php echo esc_attr( $rp_key ); ?>" />
<button type="submit" name="wp-submit" id="wp-submit"
class="button button-primary button-large">
<?php _e( 'Reset Password', 'membership2' ); ?>
</button>
</p>
</form>
<?php
$html = ob_get_clean();
$Reset_Result = apply_filters( 'wpmudev_ms_compact_code', $html );
}
$Reset_Result = do_shortcode( $Reset_Result );
}
return $Reset_Result;
},10 , 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment