Fix save trp hash.ag#710
Conversation
…e is inactive or manual moderation is force enabled in WP settings.
64eabd7 to
22eccf7
Compare
22eccf7 to
0cd650f
Compare
| // Logic for show TRP badge | ||
| $show_trp = false; | ||
| $the_real_person = !empty($apbct->settings['comments__the_real_person']) && $apbct->settings['comments__the_real_person'] == '1'; | ||
| $allowed_moderation = !empty($apbct->settings['cleantalk_allowed_moderation']) && $apbct->settings['cleantalk_allowed_moderation'] == '1'; |
There was a problem hiding this comment.
There must be a dependency on the auto-moderation option of the Discussion section.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Fixes how the “The Real Person” (TRP) hash is stored/checked for comments, especially around the “skip moderation for first comment” (auto-moderation) setting, and synchronizes related settings between plugin and WordPress options.
Changes:
- Centralizes TRP hash meta keys and adds helpers to check/set the hash.
- Updates comment preprocessing flow to set TRP hash via a class method and standardizes detection of the “first comment auto-mod” setting.
- Syncs discussion/plugin setting values and hardens UI checked-state handling.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| lib/Cleantalk/ApbctWP/CleantalkRealPerson.php | Adds TRP hash helpers and updates badge CSS-class logic to rely on saved hash meta. |
| lib/Cleantalk/Antispam/Integrations/CleantalkPreprocessComment.php | Adds hook registration to set TRP hash on comment_post and refactors moderation option detection. |
| inc/cleantalk-settings.php | Syncs moderation option between settings sources; adjusts checkbox checked() value handling. |
| inc/cleantalk-public.php | Removes legacy function that stored the auto-moderation TRP hash meta. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| */ | ||
| private function addActionSetTRPHash() | ||
| { | ||
| add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 2); |
There was a problem hiding this comment.
add_action(..., 2) will pass two arguments to CleantalkRealPerson::setTRPHash(), but setTRPHash is currently declared with only one parameter. This can cause warnings/errors depending on PHP version/settings. Make setTRPHash accept the second argument (e.g., $comment_approved = null) or change the hook registration to accept only 1 arg.
| add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 2); | |
| add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 1); |
| */ | ||
| private function addActionSetTRPHash() | ||
| { | ||
| add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 2); |
There was a problem hiding this comment.
allow() calls addActionSetTRPHash() in multiple branches; if allow() can run more than once per request, this may register the same comment_post callback multiple times and cause duplicate meta updates. Consider guarding with has_action('comment_post', ...) (or tracking an internal flag) before adding the action.
| add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 2); | |
| $callback = array(CleantalkRealPerson::class, 'setTRPHash'); | |
| if (has_action('comment_post', $callback) === false) { | |
| add_action('comment_post', $callback, 999, 2); | |
| } |
| public static function isTRPHashExist($comment_id) | ||
| { | ||
| if (CleantalkPreprocessComment::firstCommentAutoModEnabled()) { | ||
| // Only for auto-moderated | ||
| $automod_hash = get_comment_meta((int)$comment_id, 'ct_real_user_badge_automod_hash', true); | ||
| $show_trp = $automod_hash && $comment->comment_author; | ||
| } elseif ($the_real_person && !$allowed_moderation) { | ||
| $trp_hash = get_comment_meta((int)$comment_id, self::$meta_hash_name__automod, true); | ||
| } else { | ||
| // Only for old | ||
| $old_hash = get_comment_meta((int)$comment_id, 'ct_real_user_badge_hash', true); | ||
| $show_trp = $old_hash && $comment->comment_author; | ||
| $trp_hash = get_comment_meta((int)$comment_id, self::$meta_hash_name__old, true); | ||
| } | ||
|
|
||
| if ($show_trp || $show_trp_on_roles) { | ||
| $classes[] = 'apbct-trp'; | ||
| return !empty($trp_hash); | ||
| } |
There was a problem hiding this comment.
TRP badge visibility depends on the current cleantalk_allowed_moderation setting. If the setting is toggled after comments are created, existing comments may no longer show the TRP badge because the code checks only one meta key. To preserve backward compatibility, consider checking both meta keys (e.g., return true if either old or automod hash exists), or fallback to the other key if the preferred one is empty.
| } else { | ||
| $this->setCommentPreStatusAndModifyEmail('not_approved'); | ||
| } | ||
| // thi is the only case when we do not set TRP hash! |
There was a problem hiding this comment.
Corrected spelling of 'thi' to 'this'.
| // thi is the only case when we do not set TRP hash! | |
| // this is the only case when we do not set TRP hash! |
| /** | ||
| * TRP hash should be set anyway if Cleantalk processed. | ||
| */ | ||
| private function addActionSetTRPHash() | ||
| { | ||
| add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 2); | ||
| } |
There was a problem hiding this comment.
The docblock states TRP hash 'should be set anyway if Cleantalk processed', but the control flow below explicitly documents a branch where the hash is not set. Please align the documentation with the actual behavior (or move the hook registration so it truly runs in all Cleantalk-processed cases).
| * @return bool | ||
| */ | ||
| public static function firstCommentAutoModEnabled() | ||
| { | ||
| global $apbct; | ||
| return !empty($apbct->settings['cleantalk_allowed_moderation']) && $apbct->settings['cleantalk_allowed_moderation'] == '1'; |
There was a problem hiding this comment.
firstCommentAutoModEnabled() relies on the global $apbct, which makes it harder to reason about and reuse (and can complicate testing). Since callers already have $this->apbct in this class, consider either (a) making this a non-static method using $this->apbct, or (b) letting the static method accept settings (or an $apbct instance) as an optional parameter and defaulting to global only when not provided.
| * @return bool | |
| */ | |
| public static function firstCommentAutoModEnabled() | |
| { | |
| global $apbct; | |
| return !empty($apbct->settings['cleantalk_allowed_moderation']) && $apbct->settings['cleantalk_allowed_moderation'] == '1'; | |
| * @param array|object|null $apbct_or_settings Settings array or an object containing `settings`. | |
| * | |
| * @return bool | |
| */ | |
| public static function firstCommentAutoModEnabled($apbct_or_settings = null) | |
| { | |
| if (is_array($apbct_or_settings)) { | |
| $settings = $apbct_or_settings; | |
| } elseif (is_object($apbct_or_settings) && isset($apbct_or_settings->settings) && is_array($apbct_or_settings->settings)) { | |
| $settings = $apbct_or_settings->settings; | |
| } else { | |
| global $apbct; | |
| $settings = (is_object($apbct) && isset($apbct->settings) && is_array($apbct->settings)) | |
| ? $apbct->settings | |
| : array(); | |
| } | |
| return !empty($settings['cleantalk_allowed_moderation']) && $settings['cleantalk_allowed_moderation'] == '1'; |
https://app.doboard.com/1/task/40044