PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.1.0
WP 2FA – Two-factor authentication for WordPress v2.1.0
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Admin / Controllers / class-login-attempts.php
wp-2fa / includes / classes / Admin / Controllers Last commit date
class-login-attempts.php 4 years ago class-settings.php 4 years ago
class-login-attempts.php
174 lines
1 <?php
2 /**
3 * Responsible for the plugin login attempts
4 *
5 * @package wp2fa
6 * @subpackage login-attempts
7 */
8
9 namespace WP2FA\Admin\Controllers;
10
11 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
12
13 /**
14 * Login Attempts class
15 */
16 if ( ! class_exists( '\WP2FA\Admin\Controllers\Login_Attempts' ) ) {
17
18 /**
19 * Responsible for the login attempts
20 *
21 * @since 2.0.0
22 */
23 class Login_Attempts {
24
25 /**
26 * Holds the name of the meta key
27 *
28 * @var string
29 *
30 * @since 2.0.0
31 */
32 private $meta_key = WP_2FA_PREFIX . 'login-attempts';
33
34 /**
35 * Holds the number of allowed attempts to login
36 *
37 * @var integer
38 *
39 * @since 2.0.0
40 */
41 private $number_of_allowed_attempts = 3;
42
43 /**
44 * Default constructor
45 *
46 * @param string $meta_key - The meta key name.
47 * @param integer $attempts - Number of the allowed login attempts.
48 *
49 * @since 2.0.0
50 */
51 public function __construct( string $meta_key = '', int $attempts = 0 ) {
52 if ( '' !== trim( $meta_key ) ) {
53 $this->meta_key = $meta_key;
54 }
55 if ( 0 !== $attempts ) {
56 $this->number_of_allowed_attempts = $attempts;
57 }
58 }
59
60 /**
61 * Increasing login attempts for User
62 *
63 * @since 2.0.0
64 *
65 * @param \WP_User $user - the WP User.
66 *
67 * @return void
68 */
69 public function increase_login_attempts( \WP_User $user ) {
70 $attempts = $this->get_login_attempts( $user );
71 if ( '' === $attempts ) {
72 $attempts = 0;
73 }
74 \update_user_meta( $user->ID, $this->meta_key, ++$attempts );
75 }
76
77 /**
78 * Returns the number of unsuccessful attempts for the User
79 *
80 * @since 2.0.0
81 *
82 * @param \WP_User $user - the WP User.
83 *
84 * @return integer
85 */
86 public function get_login_attempts( \WP_User $user ): int {
87 return (int) \get_user_meta( $user->ID, $this->meta_key, true );
88 }
89
90 /**
91 * Clearing login attempts for User
92 *
93 * @since 2.0.0
94 *
95 * @param \WP_User $user - the WP User.
96 *
97 * @return void
98 */
99 public function clear_login_attempts( \WP_User $user ) {
100 \delete_user_meta( $user->ID, $this->meta_key );
101 }
102
103 /**
104 * Returns the number of allowed login attempts
105 *
106 * @return integer
107 *
108 * @since 2.0.0
109 */
110 public function get_allowed_login_attempts(): int {
111 return $this->number_of_allowed_attempts;
112 }
113
114 /**
115 * Sets the number of allowed attempts
116 *
117 * @param integer $number - The number of the allowed attempts.
118 *
119 * @return integer
120 *
121 * @since 2.0.0
122 */
123 public function set_number_of_login_attempts( int $number ): int {
124 $this->number_of_allowed_attempts = $number;
125
126 return $this->number_of_allowed_attempts;
127 }
128
129 /**
130 * Returns the name of the meta key holding the login attempts for the user
131 *
132 * @return string
133 *
134 * @since 2.0.0
135 */
136 public function get_meta_key(): string {
137
138 return $this->meta_key;
139 }
140
141 /**
142 * Sets the login attempts meta key
143 *
144 * @param string $meta_key - The name of the meta.
145 *
146 * @return string
147 *
148 * @since 2.0.0
149 */
150 public function set_meta_key( string $meta_key ): string {
151 $this->meta_key = $meta_key;
152
153 return $this->meta_key;
154 }
155
156 /**
157 * Checks the number of login attempts
158 *
159 * @param \WP_User $user - The user we have to check for.
160 *
161 * @return boolean
162 *
163 * @since 2.0.0
164 */
165 public function check_number_of_attempts( \WP_User $user ):bool {
166 if ( $this->get_allowed_login_attempts() < $this->get_login_attempts( $user ) ) {
167 return false;
168 }
169
170 return true;
171 }
172 }
173 }
174