• if ( ! class_exists( 'Jeba_Limit_Login_Attempts' ) ) {
        class Jeba_Limit_Login_Attempts {
    
            var $failed_login_limit = "3";                    //Number of authentification accepted
            var $lockout_duration   = "1800";                 //Stop authentification process for 30 minutes: 60*30 = 1800
            var $transient_name     = 'attempted_login';    //Transient used
    
            public function __construct() {
                add_filter( 'authenticate', array( $this, 'check_attempted_login' ), 30, 3 );
                add_action( 'wp_login_failed', array( $this, 'login_failed' ), 10, 1 );
            }
            public function check_attempted_login( $user, $username, $password ) {
                if ( get_transient( $this->transient_name ) ) {
                    $datas = get_transient( $this->transient_name );
    
                    if ( $datas['tried'] >= $this->failed_login_limit ) {
                        $until = get_option( '_transient_timeout_' . $this->transient_name );
                        $time = $this->when( $until );
    
                        //Display error message to the user when limit is reached
                        return new WP_Error( 'too_many_tried', sprintf( __( '<strong>ERROR</strong>: You have reached authentification limit, you will be able to try again in %1$s.' ) , $time ) );
                    }
                }
    
                return $user;
            }
            public function login_failed( $username ) {
                if ( get_transient( $this->transient_name ) ) {
                    $datas = get_transient( $this->transient_name );
                    $datas['tried']++;
    
                    if ( $datas['tried'] <= $this->failed_login_limit )
                        set_transient( $this->transient_name, $datas , $this->lockout_duration );
                } else {
                    $datas = array(
                        'tried'     => 1
                    );
                    set_transient( $this->transient_name, $datas , $this->lockout_duration );
                }
            }
    
            private function when( $time ) {
                if ( ! $time )
                    return;
    
                $right_now = time();
    
                $diff = abs( $right_now - $time );
    
                $second = 1;
                $minute = $second * 60;
                $hour = $minute * 60;
                $day = $hour * 24;
    
                if ( $diff < $minute )
                    return floor( $diff / $second ) . ' secondes';
    
                if ( $diff < $minute * 2 )
                    return "about 1 minute ago";
    
                if ( $diff < $hour )
                    return floor( $diff / $minute ) . ' minutes';
    
                if ( $diff < $hour * 2 )
                    return 'about 1 hour';
    
                return floor( $diff / $hour ) . ' hours';
            }
        }
    }
    new Jeba_Limit_Login_Attempts();
Viewing 3 replies - 1 through 3 (of 3 total)
  • Do you have a question? Which plugin is this regarding?

    Thread Starter Md Jahed

    (@jahed)

    I want to use setting api for my own plugin. just how can I get value from setting page option field for var $failed_login_limit = “3”;
    var $lockout_duration = “1800”; ?

    On that case, I will move this thread to “Hacks” forum – people there can generally help with this kind of question. “Plugins” is generally only for specific plugin sub-forum questions – other threads aren’t likely to be replied to.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Changing $failed_login_limit and $lockout_duration value from wp setting api’ is closed to new replies.