Skip to content

Commit 13ca0d9

Browse files
committed
Fix. TRP. Correctly save TRP hashes. Do not set hashes only if license is inactive or manual moderation is force enabled in WP settings.
1 parent d1930ea commit 13ca0d9

3 files changed

Lines changed: 69 additions & 37 deletions

File tree

inc/cleantalk-public.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,20 +1009,6 @@ function ct_set_approved($approved, $_comment)
10091009
return 1;
10101010
}
10111011

1012-
/**
1013-
* Public action 'comment_post' - Store cleantalk hash in comment meta
1014-
*
1015-
* @psalm-suppress UnusedParam
1016-
* @return void
1017-
*/
1018-
function ct_set_real_user_badge_automod_hash($comment_id)
1019-
{
1020-
$hash1 = ct_hash();
1021-
if ( ! empty($hash1) ) {
1022-
update_comment_meta($comment_id, 'ct_real_user_badge_automod_hash', ct_hash());
1023-
}
1024-
}
1025-
10261012
/**
10271013
* Public filter 'pre_comment_approved' - Mark comment unapproved always
10281014
* @return string

lib/Cleantalk/Antispam/Integrations/CleantalkPreprocessComment.php

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

33
namespace Cleantalk\Antispam\Integrations;
44

5+
use Cleantalk\ApbctWP\CleantalkRealPerson;
56
use Cleantalk\ApbctWP\Sanitize;
67
use Cleantalk\ApbctWP\Variables\AltSessions;
78
use Cleantalk\ApbctWP\Variables\Cookie;
@@ -259,6 +260,7 @@ public function allow()
259260
} else {
260261
$this->setCommentPreStatusAndModifyEmail('not_approved');
261262
}
263+
$this->addActionSetTRPHash();
262264
return;
263265
}
264266

@@ -271,6 +273,7 @@ public function allow()
271273
!$is_allowed_because_of_inactive_license
272274
) {
273275
$this->setCommentPreStatusAndModifyEmail('approved');
276+
$this->addActionSetTRPHash();
274277
} else {
275278
// moderation disabled - standard WP check
276279
if (
@@ -280,6 +283,7 @@ public function allow()
280283
} else {
281284
$this->setCommentPreStatusAndModifyEmail('not_approved');
282285
}
286+
// thi is the only case when we do not set TRP hash!
283287
}
284288
} else {
285289
//not new author - standard WP check
@@ -290,9 +294,18 @@ public function allow()
290294
} else {
291295
$this->setCommentPreStatusAndModifyEmail('not_approved');
292296
}
297+
$this->addActionSetTRPHash();
293298
}
294299
}
295300

301+
/**
302+
* TRP hash should be set anyway if Cleantalk processed.
303+
*/
304+
private function addActionSetTRPHash()
305+
{
306+
add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 2);
307+
}
308+
296309
public function doBlock($message)
297310
{
298311
$ct_result = $this->base_call_result['ct_result'];
@@ -536,24 +549,20 @@ private function rerunWPcheckCommentFunction()
536549
return $check_result;
537550
}
538551

552+
/**
553+
* @param $status
554+
*
555+
* @return void
556+
*/
539557
private function setCommentPreStatusAndModifyEmail($status)
540558
{
541559
if ($status !== 'approved' && $status !== 'not_approved') {
542560
return;
543561
}
544-
if ( $status === 'approved' ) {
545-
add_filter('pre_comment_approved', 'ct_set_approved', 999, 2);
546-
547-
// Always set hash for auto-moderated (approved) comments if cleantalk_allowed_moderation is enabled
548-
if (
549-
!empty($this->apbct->settings['cleantalk_allowed_moderation']) &&
550-
$this->apbct->settings['cleantalk_allowed_moderation'] == '1'
551-
) {
552-
add_action('comment_post', 'ct_set_real_user_badge_automod_hash', 999, 2);
553-
}
554-
} else {
555-
add_filter('pre_comment_approved', 'ct_set_not_approved', 999, 2);
556-
}
562+
$pre_comment_approved_callback_function = $status === 'approved'
563+
? 'ct_set_approved'
564+
: 'ct_set_not_approved';
565+
add_filter('pre_comment_approved', $pre_comment_approved_callback_function, 999, 2);
557566

558567
// Modify the email notification
559568
add_filter(

lib/Cleantalk/ApbctWP/CleantalkRealPerson.php

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class CleantalkRealPerson
66
{
7+
public static $meta_hash_name__old = 'ct_real_user_badge_hash';
8+
public static $meta_hash_name__automod = 'ct_real_user_badge_automod_hash';
79
public static function getLocalizingData()
810
{
911
/** @psalm-suppress PossiblyUndefinedVariable */
@@ -43,21 +45,56 @@ public function publicCommentAddTrpClass($classes, $_css_class, $comment_id, $co
4345
// Logic for show TRP badge
4446
$show_trp = false;
4547
$the_real_person = !empty($apbct->settings['comments__the_real_person']) && $apbct->settings['comments__the_real_person'] == '1';
46-
$allowed_moderation = !empty($apbct->settings['cleantalk_allowed_moderation']) && $apbct->settings['cleantalk_allowed_moderation'] == '1';
48+
$show_trp = $the_real_person && self::isTRPHashExist($comment_id) && $comment->comment_author;
4749

48-
if ($the_real_person && $allowed_moderation) {
50+
if ($show_trp || $show_trp_on_roles) {
51+
$classes[] = 'apbct-trp';
52+
}
53+
return $classes;
54+
}
55+
56+
/**
57+
* @return bool
58+
*/
59+
private static function isAutomodEnabled()
60+
{
61+
global $apbct;
62+
return !empty($apbct->settings['cleantalk_allowed_moderation']) && $apbct->settings['cleantalk_allowed_moderation'] == '1';
63+
}
64+
65+
/**
66+
* Check if TRP hash is saved for comment ID. Autodetect if cleantalk_allowed_moderation is enabled.
67+
* @param int|string $comment_id
68+
* @return bool
69+
*/
70+
public static function isTRPHashExist($comment_id)
71+
{
72+
if (self::isAutomodEnabled()) {
4973
// Only for auto-moderated
50-
$automod_hash = get_comment_meta((int)$comment_id, 'ct_real_user_badge_automod_hash', true);
51-
$show_trp = $automod_hash && $comment->comment_author;
52-
} elseif ($the_real_person && !$allowed_moderation) {
74+
$trp_hash = get_comment_meta((int)$comment_id, self::$meta_hash_name__automod, true);
75+
} else {
5376
// Only for old
54-
$old_hash = get_comment_meta((int)$comment_id, 'ct_real_user_badge_hash', true);
55-
$show_trp = $old_hash && $comment->comment_author;
77+
$trp_hash = get_comment_meta((int)$comment_id, self::$meta_hash_name__old, true);
5678
}
5779

58-
if ($show_trp || $show_trp_on_roles) {
59-
$classes[] = 'apbct-trp';
80+
return !empty($trp_hash);
81+
}
82+
83+
/**
84+
* Save TRP hash for comment ID. Autodetect if cleantalk_allowed_moderation is enabled.
85+
* @param int|string $comment_id
86+
*
87+
* @return void
88+
*/
89+
public static function setTRPHash($comment_id)
90+
{
91+
$hash = ct_hash();
92+
if ( ! empty($hash) ) {
93+
if (self::isAutomodEnabled()) {
94+
update_comment_meta((int)$comment_id, self::$meta_hash_name__automod, $hash);
95+
} else {
96+
update_comment_meta((int)$comment_id, self::$meta_hash_name__old, $hash);
97+
}
6098
}
61-
return $classes;
6299
}
63100
}

0 commit comments

Comments
 (0)