Skip to content

Commit 3fb5257

Browse files
Fix. TRP. Correctly save TRP hashes. (#710)
* Fix. TRP. Correctly save TRP hashes. Do not set hashes only if license is inactive or manual moderation is force enabled in WP settings. * Fix. TRP. Cleantalk allowed moderation discussion state. * Fix. Comments protection. TRP meta logic fixed. * Fix. Comments protection. TRP meta logic fixed #2. * Fix. TRP. TRP meta checking fixed. * Fix. Code. Comment typo fixed. * Fix. Code. Code docblock for `addActionSetTRPHash` fixed. * Fix. Code. Psalm notice fixed. * Fix. TRP. TRP meta checking fixed #2. --------- Co-authored-by: Glomberg <bazz@bk.ru>
1 parent d8cb1f6 commit 3fb5257

4 files changed

Lines changed: 82 additions & 42 deletions

File tree

inc/cleantalk-public.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,20 +1039,6 @@ function ct_set_approved($approved, $_comment)
10391039
return 1;
10401040
}
10411041

1042-
/**
1043-
* Public action 'comment_post' - Store cleantalk hash in comment meta
1044-
*
1045-
* @psalm-suppress UnusedParam
1046-
* @return void
1047-
*/
1048-
function ct_set_real_user_badge_automod_hash($comment_id)
1049-
{
1050-
$hash1 = ct_hash();
1051-
if ( ! empty($hash1) ) {
1052-
update_comment_meta($comment_id, 'ct_real_user_badge_automod_hash', ct_hash());
1053-
}
1054-
}
1055-
10561042
/**
10571043
* Public filter 'pre_comment_approved' - Mark comment unapproved always
10581044
* @return string

inc/cleantalk-settings.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function apbct_settings_add_page()
6363
global $apbct;
6464

6565
$filtered = ($value === '1' || $value === 1) ? '1' : '0';
66+
//sync discussion and plugin settings
6667
if ($old_value !== $filtered) {
6768
$apbct->settings['cleantalk_allowed_moderation'] = $filtered;
6869
$apbct->saveSettings();
@@ -2108,7 +2109,7 @@ function apbct_discussion_settings__field__moderation()
21082109
name="cleantalk_allowed_moderation"
21092110
id="cleantalk_allowed_moderation"
21102111
value="1" ' .
2111-
checked('1', $apbct->settings['cleantalk_allowed_moderation'], false) .
2112+
checked('1', TT::toString($apbct->settings['cleantalk_allowed_moderation'], '0'), false) .
21122113
'/> ';
21132114
$output .= esc_html__('Skip manual approving for the very first comment if a comment has been allowed by CleanTalk Anti-Spam protection.', 'cleantalk-spam-protect');
21142115
$output .= '</label>';
@@ -2582,6 +2583,11 @@ function apbct_settings__validate($incoming_settings)
25822583
$incoming_settings['data__email_decoder_obfuscation_custom_text'] = ContactsEncoder::getDefaultReplacingText();
25832584
}
25842585

2586+
//sync discussion and plugin settings
2587+
if (isset($incoming_settings['cleantalk_allowed_moderation'])) {
2588+
update_option('cleantalk_allowed_moderation', TT::toString($incoming_settings['cleantalk_allowed_moderation'], '0'));
2589+
}
2590+
25852591
/**
25862592
* Triggered before returning the settings
25872593
*/

lib/Cleantalk/Antispam/Integrations/CleantalkPreprocessComment.php

Lines changed: 38 additions & 15 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;
@@ -212,12 +213,14 @@ public function doActionsBeforeAllowDeny($argument)
212213
/**
213214
* Do all the actions after and if request is allowed
214215
* @return void
216+
*
217+
* @psalm-suppress PossiblyUnusedMethod
215218
*/
216219
public function allow()
217220
{
218221
$wp_comment_moderation_enabled = get_option('comment_moderation') === '1';
219222
$wp_auto_approve_for_user_who_has_approved_comment = get_option('comment_previously_approved') === '1';
220-
$clentalk_option_skip_moderation_for_first_comment = get_option('cleantalk_allowed_moderation', 1) == 1;
223+
$cleantalk_option_skip_moderation_for_first_comment = self::firstCommentAutoModEnabled();
221224
$is_allowed_because_of_inactive_license = false;
222225

223226
$args = array(
@@ -250,7 +253,7 @@ public function allow()
250253
// if anu of options is disabled - standard WP recheck and exit
251254
if (
252255
!$wp_auto_approve_for_user_who_has_approved_comment ||
253-
!$clentalk_option_skip_moderation_for_first_comment
256+
!$cleantalk_option_skip_moderation_for_first_comment
254257
) {
255258
if (
256259
$this->rerunWPcheckCommentFunction()
@@ -259,6 +262,7 @@ public function allow()
259262
} else {
260263
$this->setCommentPreStatusAndModifyEmail('not_approved');
261264
}
265+
$this->addActionSetTRPHash();
262266
return;
263267
}
264268

@@ -271,6 +275,7 @@ public function allow()
271275
!$is_allowed_because_of_inactive_license
272276
) {
273277
$this->setCommentPreStatusAndModifyEmail('approved');
278+
$this->addActionSetTRPHash();
274279
} else {
275280
// moderation disabled - standard WP check
276281
if (
@@ -280,6 +285,7 @@ public function allow()
280285
} else {
281286
$this->setCommentPreStatusAndModifyEmail('not_approved');
282287
}
288+
// this is the only case when we do not set TRP hash!
283289
}
284290
} else {
285291
//not new author - standard WP check
@@ -290,6 +296,18 @@ public function allow()
290296
} else {
291297
$this->setCommentPreStatusAndModifyEmail('not_approved');
292298
}
299+
$this->addActionSetTRPHash();
300+
}
301+
}
302+
303+
/**
304+
* TRP hash should be set only if Cleantalk processed && auto-moderated the comment.
305+
*/
306+
private function addActionSetTRPHash()
307+
{
308+
$callback = array(CleantalkRealPerson::class, 'setTRPHash');
309+
if (has_action('comment_post', $callback) === false) {
310+
add_action('comment_post', $callback, 999, 2);
293311
}
294312
}
295313

@@ -540,24 +558,20 @@ private function rerunWPcheckCommentFunction()
540558
return $check_result;
541559
}
542560

561+
/**
562+
* @param $status
563+
*
564+
* @return void
565+
*/
543566
private function setCommentPreStatusAndModifyEmail($status)
544567
{
545568
if ($status !== 'approved' && $status !== 'not_approved') {
546569
return;
547570
}
548-
if ( $status === 'approved' ) {
549-
add_filter('pre_comment_approved', 'ct_set_approved', 999, 2);
550-
551-
// Always set hash for auto-moderated (approved) comments if cleantalk_allowed_moderation is enabled
552-
if (
553-
!empty($this->apbct->settings['cleantalk_allowed_moderation']) &&
554-
$this->apbct->settings['cleantalk_allowed_moderation'] == '1'
555-
) {
556-
add_action('comment_post', 'ct_set_real_user_badge_automod_hash', 999, 2);
557-
}
558-
} else {
559-
add_filter('pre_comment_approved', 'ct_set_not_approved', 999, 2);
560-
}
571+
$pre_comment_approved_callback_function = $status === 'approved'
572+
? 'ct_set_approved'
573+
: 'ct_set_not_approved';
574+
add_filter('pre_comment_approved', $pre_comment_approved_callback_function, 999, 2);
561575

562576
// Modify the email notification
563577
add_filter(
@@ -567,4 +581,13 @@ private function setCommentPreStatusAndModifyEmail($status)
567581
2
568582
); // Add two blacklist links: by email and IP
569583
}
584+
585+
/**
586+
* @return bool
587+
*/
588+
public static function firstCommentAutoModEnabled()
589+
{
590+
global $apbct;
591+
return !empty($apbct->settings['cleantalk_allowed_moderation']) && $apbct->settings['cleantalk_allowed_moderation'] == '1';
592+
}
570593
}

lib/Cleantalk/ApbctWP/CleantalkRealPerson.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Cleantalk\ApbctWP;
44

5+
use Cleantalk\Antispam\Integrations\CleantalkPreprocessComment;
6+
57
class CleantalkRealPerson
68
{
9+
public static $meta_hash_name__old = 'ct_real_user_badge_hash';
10+
public static $meta_hash_name__automod = 'ct_real_user_badge_automod_hash';
711
public static function getLocalizingData()
812
{
913
/** @psalm-suppress PossiblyUndefinedVariable */
@@ -44,23 +48,44 @@ public function publicCommentAddTrpClass($classes, $_css_class, $comment_id, $co
4448
}
4549

4650
// Logic for show TRP badge
47-
$show_trp = false;
4851
$the_real_person = !empty($apbct->settings['comments__the_real_person']) && $apbct->settings['comments__the_real_person'] == '1';
49-
$allowed_moderation = !empty($apbct->settings['cleantalk_allowed_moderation']) && $apbct->settings['cleantalk_allowed_moderation'] == '1';
50-
51-
if ($the_real_person && $allowed_moderation) {
52-
// Only for auto-moderated
53-
$automod_hash = get_comment_meta((int)$comment_id, 'ct_real_user_badge_automod_hash', true);
54-
$show_trp = $automod_hash && $comment->comment_author;
55-
} elseif ($the_real_person && !$allowed_moderation) {
56-
// Only for old
57-
$old_hash = get_comment_meta((int)$comment_id, 'ct_real_user_badge_hash', true);
58-
$show_trp = $old_hash && $comment->comment_author;
59-
}
52+
$show_trp = $the_real_person && self::isTRPHashExist($comment_id) && $comment->comment_author;
6053

6154
if ($show_trp || $show_trp_on_roles) {
6255
$classes[] = 'apbct-trp';
6356
}
6457
return $classes;
6558
}
59+
60+
/**
61+
* Check if TRP hash is saved for comment ID. Autodetect if cleantalk_allowed_moderation is enabled.
62+
* @param int|string $comment_id
63+
* @return bool
64+
*/
65+
public static function isTRPHashExist($comment_id)
66+
{
67+
$trp_hash =
68+
get_comment_meta((int)$comment_id, self::$meta_hash_name__automod, true) ||
69+
get_comment_meta((int)$comment_id, self::$meta_hash_name__old, true);
70+
71+
return $trp_hash;
72+
}
73+
74+
/**
75+
* Save TRP hash for comment ID. Autodetect if cleantalk_allowed_moderation is enabled.
76+
* @param int|string $comment_id
77+
*
78+
* @return void
79+
*/
80+
public static function setTRPHash($comment_id)
81+
{
82+
$hash = ct_hash();
83+
if ( ! empty($hash) ) {
84+
if (CleantalkPreprocessComment::firstCommentAutoModEnabled()) {
85+
update_comment_meta((int)$comment_id, self::$meta_hash_name__automod, $hash);
86+
} else {
87+
update_comment_meta((int)$comment_id, self::$meta_hash_name__old, $hash);
88+
}
89+
}
90+
}
6691
}

0 commit comments

Comments
 (0)