| @@ -9,8 +9,9 @@ | ||
| 9 | 9 | |
| 10 | 10 | use WP_Defender\DB; |
| 11 | 11 | use Calotes\Base\Model; |
| 12 | 12 | use WP_Defender\Traits\Formats; |
| 13 | +use WP_Defender\Traits\Country; | |
| 13 | 14 | use WP_Defender\Component\User_Agent; |
| 14 | 15 | use WP_Defender\Component\Table_Lockout; |
| 15 | 16 | use WP_Defender\Model\Setting\Blacklist_Lockout; |
| 16 | 17 | use WP_Defender\Model\Setting\User_Agent_Lockout; |
| @@ -20,8 +21,9 @@ | ||
| 20 | 21 | */ |
| 21 | 22 | class Lockout_Log extends DB { |
| 22 | 23 | |
| 23 | 24 | use Formats; |
| 25 | + use Country; | |
| 24 | 26 | |
| 25 | 27 | // The Firewall log hierarchy. Parent items: 'login', '404', 'bots', 'unlock' and 'custom'. |
| 26 | 28 | // Group of 'login' cases. |
| 27 | 29 | public const AUTH_FAIL = 'auth_fail', AUTH_LOCK = 'auth_lock'; |
| @@ -38,10 +40,8 @@ | ||
| 38 | 40 | public const IP_UNLOCK = 'ip_unlock'; |
| 39 | 41 | // Group of 'custom' cases. |
| 40 | 42 | public const LOCKOUT_IP_CUSTOM = 'custom_lockout'; |
| 41 | 43 | |
| 42 | - public const INFINITE_SCROLL_SIZE = 50; | |
| 43 | - | |
| 44 | 44 | /** |
| 45 | 45 | * Table name. |
| 46 | 46 | * |
| 47 | 47 | * @var string |
| @@ -110,8 +110,34 @@ | ||
| 110 | 110 | * @var string |
| 111 | 111 | * @defender_property |
| 112 | 112 | */ |
| 113 | 113 | public $country_iso_code; |
| 114 | + | |
| 115 | + /** | |
| 116 | + * Factory method to instantiate and populate a Lockout_Log model with request data. | |
| 117 | + * | |
| 118 | + * @param string $ip The IP address. | |
| 119 | + * @param string|null $tried The attempted URI, username, or user agent. | |
| 120 | + * | |
| 121 | + * @return self | |
| 122 | + */ | |
| 123 | + public static function create( string $ip, ?string $tried = null ): self { | |
| 124 | + $model = new self(); | |
| 125 | + $model->ip = $ip; | |
| 126 | + $user_agent = defender_get_data_from_request( 'HTTP_USER_AGENT', 's' ); | |
| 127 | + $model->user_agent = '' !== $user_agent ? User_Agent::fast_cleaning( $user_agent ) : ''; | |
| 128 | + $model->date = time(); | |
| 129 | + $model->tried = $tried ?? $model->user_agent; | |
| 130 | + $model->blog_id = get_current_blog_id(); | |
| 131 | + | |
| 132 | + $ip_to_country = $model->ip_to_country( $ip ); | |
| 133 | + | |
| 134 | + if ( isset( $ip_to_country['iso'] ) ) { | |
| 135 | + $model->country_iso_code = $ip_to_country['iso']; | |
| 136 | + } | |
| 137 | + | |
| 138 | + return $model; | |
| 139 | + } | |
| 114 | 140 | |
| 115 | 141 | /** |
| 116 | 142 | * Query the logs based on the provided filters and pagination settings. |
| 117 | 143 | * |