Skip to content

Commit a59a296

Browse files
committed
Fix. After tests. Removed data rotation because the meta will be deleted on user deletion.
1 parent c376524 commit a59a296

6 files changed

Lines changed: 19 additions & 143 deletions

File tree

cleantalk.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
function apbct_wp_login_actions($_user_login, $wp_user)
182182
{
183183
global $apbct;
184-
$apbct->login_ip_keeper->hookSaveLoggedInUserData($wp_user);
184+
$apbct->login_ip_keeper->addUserIP($wp_user);
185185
apbct_add_admin_ip_to_swf_whitelist($wp_user);
186186
}
187187

lib/Cleantalk/ApbctWP/FindSpam/ListTable/BadUsers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function column_ct_username($item) // phpcs:ignore PSR1.Methods.CamelCaps
9696
$column_content .= '<br/>';
9797

9898
// IP
99-
$ip_from_keeper = $apbct->login_ip_keeper->getMetaRecordValue($user_obj->ID);
99+
$ip_from_keeper = $apbct->login_ip_keeper->getIP($user_obj->ID);
100100
$ip_from_keeper = null !== $ip_from_keeper
101101
? $ip_from_keeper
102102
: null;

lib/Cleantalk/ApbctWP/FindSpam/ListTable/Users.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function column_ct_username($item) // phpcs:ignore PSR1.Methods.CamelCaps
121121
$column_content .= '<br/>';
122122

123123
// IP
124-
$ip_from_keeper = $apbct->login_ip_keeper->getMetaRecordValue($user_obj->ID);
124+
$ip_from_keeper = $apbct->login_ip_keeper->getIP($user_obj->ID);
125125
$ip_from_keeper = null !== $ip_from_keeper
126126
? $ip_from_keeper
127127
: null;

lib/Cleantalk/ApbctWP/FindSpam/LoginIPKeeper.php

Lines changed: 8 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Cleantalk\ApbctWP\FindSpam;
44

5+
use Cleantalk\Common\Helper;
56
use Cleantalk\Common\TT;
67

78
/**
@@ -16,24 +17,6 @@ class LoginIPKeeper
1617
* @var string WordPress option name for storing user IP data.
1718
*/
1819
private static $wp_meta_name = '_cleantalk_ip_keeper_data';
19-
/**
20-
* @var int Time in seconds after which inactive users are deleted (30 days by default).
21-
*/
22-
private static $user_inactive_time_to_being_deleted = 86400 * 30;
23-
24-
/**
25-
* Hook action.
26-
*
27-
* @param \WP_User $wp_user WordPress user object.
28-
* @return void
29-
*/
30-
public function hookSaveLoggedInUserData($wp_user)
31-
{
32-
// run rotation on every login event
33-
$this->rotateData();
34-
// run record adding to user meta
35-
$this->addRecord($wp_user);
36-
}
3720

3821
/**
3922
* Save the provided in user's IP and last login from session_tokens.
@@ -42,19 +25,16 @@ public function hookSaveLoggedInUserData($wp_user)
4225
*
4326
* @return void
4427
*/
45-
public function addRecord($wp_user)
28+
public function addUserIP($wp_user)
4629
{
4730
// run record adding to user meta
4831
if ($wp_user instanceof \WP_User && 0 !== $wp_user->ID) {
4932
$session_tokens = get_user_meta($wp_user->ID, 'session_tokens', true);
5033
$data = reset($session_tokens);
51-
$record = array();
5234
if ($data) {
53-
$record['ip'] = TT::getArrayValueAsString($data, 'ip');
54-
$record['last_login'] = TT::getArrayValueAsString($data, 'login');
55-
$record = json_encode($record);
56-
if (false !== $record) {
57-
$this->updateMetaRecord($wp_user->ID, $record);
35+
$ip = TT::getArrayValueAsString($data, 'ip');
36+
if ( Helper::ipValidate($ip) ) {
37+
update_user_meta($wp_user->ID, self::$wp_meta_name, $ip);
5838
}
5939
}
6040
}
@@ -64,90 +44,14 @@ public function addRecord($wp_user)
6444
* Retrieves data from user meta of a user by his user ID.
6545
*
6646
* @param int|string $user_id User ID to search for.
67-
* @param string $property ip|last_login, default is ip
6847
*
6948
* @return string|null The selected record property value of user meta data.
7049
* @psalm-suppress PossiblyUnusedMethod
7150
*/
72-
public function getMetaRecordValue($user_id, $property = 'ip')
51+
public function getIP($user_id)
7352
{
74-
$result = null;
7553
$user_id = TT::toInt($user_id);
76-
$meta = get_user_meta($user_id, self::$wp_meta_name, true);
77-
$meta = json_decode($meta, true);
78-
if (!empty($meta) && !empty($meta[$property])) {
79-
$result = $meta[$property];
80-
}
81-
return $result;
82-
}
83-
84-
/**
85-
* Remove inactive users from the stored data.
86-
*
87-
* @return void
88-
* @psalm-suppress PossiblyUnusedMethod
89-
*/
90-
private function rotateData()
91-
{
92-
global $wpdb;
93-
$meta_values = $wpdb->get_results(
94-
$wpdb->prepare(
95-
"SELECT user_id, meta_value
96-
FROM {$wpdb->usermeta}
97-
WHERE meta_key = %s",
98-
self::$wp_meta_name
99-
),
100-
ARRAY_A
101-
);
102-
foreach ($meta_values as $_meta => $data) {
103-
$user_id = isset($data['user_id'])
104-
? $data['user_id']
105-
: false;
106-
$user_exists = !empty($user_id)
107-
? !empty(get_user_by('ID', $user_id))
108-
: false;
109-
if ($user_id && (!$user_exists || !$this->isUserAlive($user_id))) {
110-
$this->deleteUserMetaRecord($user_id);
111-
}
112-
}
113-
}
114-
115-
/**
116-
* @param int|string $user_id
117-
*
118-
* @return void
119-
*/
120-
public function deleteUserMetaRecord($user_id)
121-
{
122-
$user_id = TT::toInt($user_id);
123-
delete_user_meta($user_id, self::$wp_meta_name);
124-
}
125-
126-
/**
127-
* Adds a new record to the keeper data.
128-
*
129-
* @param int $user_id User ID.
130-
* @param string $record JSON of meta record to add.
131-
* @return void
132-
*/
133-
private function updateMetaRecord($user_id, $record)
134-
{
135-
update_user_meta($user_id, self::$wp_meta_name, $record);
136-
}
137-
138-
/**
139-
* Check if IP record meta last login value is lesser than inactivity time.
140-
* @param int|string $user_id
141-
*
142-
* @return bool False if no record found or last login is more that inactivity time, true otherwise.
143-
*/
144-
private function isUserAlive($user_id)
145-
{
146-
$last_login = $this->getMetaRecordValue($user_id, 'last_login');
147-
if ($last_login) {
148-
$last_login = TT::toInt($last_login);
149-
return time() - $last_login < self::$user_inactive_time_to_being_deleted;
150-
}
151-
return false;
54+
$ip = get_user_meta($user_id, self::$wp_meta_name, true);
55+
return Helper::ipValidate($ip) ? $ip : null;
15256
}
15357
}

lib/Cleantalk/ApbctWP/FindSpam/UsersChecker.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private static function removeUsersWithoutIPEmail(array $users)
170170
continue;
171171
}
172172

173-
$ip_from_keeper = $apbct->login_ip_keeper->getMetaRecordValue($user->ID);
173+
$ip_from_keeper = $apbct->login_ip_keeper->getIP($user->ID);
174174
$ip_from_keeper = null !== $ip_from_keeper
175175
? $ip_from_keeper
176176
: false;
@@ -510,7 +510,7 @@ public static function ctGetCsvFile()
510510
$u = get_users($params);
511511
foreach ( $u as $iValue ) {
512512
// gain IP from keeper
513-
$ip_from_keeper = $apbct->login_ip_keeper->getMetaRecordValue($iValue->ID);
513+
$ip_from_keeper = $apbct->login_ip_keeper->getIP($iValue->ID);
514514
$ip_from_keeper = null !== $ip_from_keeper
515515
? $ip_from_keeper
516516
: 'N/A';
@@ -590,7 +590,7 @@ public static function ctAjaxInsertUsers()
590590
}
591591

592592
update_user_meta($curr_user->ID, 'session_tokens', array($rnd => array('ip' => $ips[$i])));
593-
$apbct->login_ip_keeper->addRecord($curr_user);
593+
$apbct->login_ip_keeper->addUserIP($curr_user);
594594
if ( is_int($user_id) ) {
595595
$inserted++;
596596
}
@@ -624,7 +624,6 @@ public static function ctAjaxDeleteAllUsers($count_all = 0)
624624
if ( $users ) {
625625
foreach ( $users as $user ) {
626626
wp_delete_user($user->ID);
627-
$apbct->login_ip_keeper->deleteUserMetaRecord($user->ID);
628627
usleep(5000);
629628
}
630629
}

tests/ApbctWP/LoginIPKeeperTest.php

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,17 @@ public function testAddRecord()
2525
$session_tokens = [
2626
'token1' => [
2727
'ip' => '192.168.1.1',
28-
'login' => time(),
2928
],
3029
];
3130
update_user_meta($wp_user->ID, 'session_tokens', $session_tokens);
3231

3332
// Call the method
34-
$this->loginIPKeeper->addRecord($wp_user);
33+
$this->loginIPKeeper->addUserIP($wp_user);
3534

3635
// Assert that the meta record was updated
37-
$meta_data = get_user_meta($wp_user->ID, '_cleantalk_ip_keeper_data', true);
38-
$meta_data = json_decode($meta_data, true);
36+
$ip = get_user_meta($wp_user->ID, '_cleantalk_ip_keeper_data', true);
3937

40-
$this->assertEquals('192.168.1.1', $meta_data['ip']);
41-
$this->assertNotEmpty($meta_data['last_login']);
38+
$this->assertEquals('192.168.1.1', $ip);
4239
}
4340

4441
public function testGetMetaRecordValue()
@@ -47,36 +44,12 @@ public function testGetMetaRecordValue()
4744
$user_id = wp_create_user('testuser2', 'password', 'testuser2@example.com');
4845

4946
// Set meta data
50-
$meta_data = json_encode(['ip' => '192.168.1.1', 'last_login' => 1000000]);
51-
update_user_meta($user_id, '_cleantalk_ip_keeper_data', $meta_data);
47+
update_user_meta($user_id, '_cleantalk_ip_keeper_data', '192.168.1.1');
5248

5349
// Call the method
54-
$result = $this->loginIPKeeper->getMetaRecordValue($user_id, 'ip');
50+
$result = $this->loginIPKeeper->getIP($user_id);
5551

5652
// Assert the result
5753
$this->assertEquals('192.168.1.1', $result);
58-
59-
// Call the method
60-
$result = $this->loginIPKeeper->getMetaRecordValue($user_id, 'last_login');
61-
62-
// Assert the result
63-
$this->assertEquals('1000000', $result);
64-
}
65-
66-
public function testRotateData()
67-
{
68-
// Create a WordPress user
69-
$user_id = wp_create_user('testuser3', 'password', 'testuser3@example.com');
70-
71-
// Set meta data with an too old last login
72-
$meta_data = json_encode(['ip' => '192.168.1.1', 'last_login' => time() - (86400 * 31)]);
73-
update_user_meta($user_id, '_cleantalk_ip_keeper_data', $meta_data);
74-
75-
// Call the method with rotation inside
76-
$this->loginIPKeeper->hookSaveLoggedInUserData(null);
77-
78-
// Assert that the meta record was deleted
79-
$meta_data = get_user_meta($user_id, '_cleantalk_ip_keeper_data', true);
80-
$this->assertEmpty($meta_data);
8154
}
8255
}

0 commit comments

Comments
 (0)