PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.5.0
WP 2FA – Two-factor authentication for WordPress v2.5.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 / Methods / Traits / class-login-attempts.php
wp-2fa / includes / classes / Admin / Methods / Traits Last commit date
class-login-attempts.php 2 years ago index.php 2 years ago
class-login-attempts.php
146 lines
1 <?php
2 /**
3 * Responsible for the plugin login attempts
4 *
5 * @package wp2fa
6 * @subpackage admin_controllers
7 * @copyright %%YEAR%% Melapress
8 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
9 * @link https://wordpress.org/plugins/wp-2fa/
10 */
11
12 namespace WP2FA\Admin\Methods\Traits;
13
14 use WP2FA\Admin\Helpers\User_Helper;
15
16 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
17 /**
18 * Responsible for the login attempts
19 *
20 * @since 2.4.1
21 */
22 trait Login_Attempts {
23
24 /**
25 * Holds the number of allowed attempts to login
26 *
27 * @var integer
28 *
29 * @since 2.4.1
30 */
31 private static $number_of_allowed_attempts = 3;
32
33 /**
34 * Increasing login attempts for User
35 *
36 * @since 2.4.1
37 *
38 * @param \WP_User $user - the WP User.
39 *
40 * @return void
41 */
42 public static function increase_login_attempts( \WP_User $user ) {
43 $attempts = self::get_login_attempts( $user );
44 if ( '' === $attempts ) {
45 $attempts = 0;
46 }
47 User_Helper::set_meta( self::$logging_attempts_meta_key, ++$attempts, $user );
48 }
49
50 /**
51 * Returns the number of unsuccessful attempts for the User
52 *
53 * @since 2.4.1
54 *
55 * @param \WP_User $user - the WP User.
56 *
57 * @return integer
58 */
59 public static function get_login_attempts( \WP_User $user ): int {
60 return (int) User_Helper::get_meta( self::$logging_attempts_meta_key, $user );
61 }
62
63 /**
64 * Clearing login attempts for User
65 *
66 * @since 2.4.1
67 *
68 * @param \WP_User $user - the WP User.
69 *
70 * @return void
71 */
72 public static function clear_login_attempts( \WP_User $user ) {
73 User_Helper::remove_meta( self::$logging_attempts_meta_key, $user );
74 }
75
76 /**
77 * Returns the number of allowed login attempts
78 *
79 * @return integer
80 *
81 * @since 2.4.1
82 */
83 public static function get_allowed_login_attempts(): int {
84 return self::$number_of_allowed_attempts;
85 }
86
87 /**
88 * Sets the number of allowed attempts
89 *
90 * @param integer $number - The number of the allowed attempts.
91 *
92 * @return integer
93 *
94 * @since 2.4.1
95 */
96 public static function set_number_of_login_attempts( int $number ): int {
97 self::$number_of_allowed_attempts = $number;
98
99 return self::$number_of_allowed_attempts;
100 }
101
102 /**
103 * Returns the name of the meta key holding the login attempts for the user
104 *
105 * @return string
106 *
107 * @since 2.4.1
108 */
109 public static function get_meta_key(): string {
110
111 return self::$logging_attempts_meta_key;
112 }
113
114 /**
115 * Sets the login attempts meta key
116 *
117 * @param string $logging_attempts_meta_key - The name of the meta.
118 *
119 * @return string
120 *
121 * @since 2.4.1
122 */
123 public static function set_meta_key( string $logging_attempts_meta_key ): string {
124 self::$logging_attempts_meta_key = $logging_attempts_meta_key;
125
126 return self::$logging_attempts_meta_key;
127 }
128
129 /**
130 * Checks the number of login attempts
131 *
132 * @param \WP_User $user - The user we have to check for.
133 *
134 * @return boolean
135 *
136 * @since 2.4.1
137 */
138 public static function check_number_of_attempts( \WP_User $user ): bool {
139 if ( self::get_allowed_login_attempts() < self::get_login_attempts( $user ) ) {
140 return false;
141 }
142
143 return true;
144 }
145 }
146