Skip to content

Fix save trp hash.ag#710

Open
alexandergull wants to merge 6 commits intodevfrom
fix_save_trp_hash.ag
Open

Fix save trp hash.ag#710
alexandergull wants to merge 6 commits intodevfrom
fix_save_trp_hash.ag

Conversation

@alexandergull
Copy link
Copy Markdown
Member

@alexandergull alexandergull commented Jan 5, 2026

// 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';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be a dependency on the auto-moderation option of the Discussion section.

@alexandergull alexandergull changed the base branch from fix to dev February 16, 2026 09:03
Copilot AI review requested due to automatic review settings May 1, 2026 11:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 2);
add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 1);

Copilot uses AI. Check for mistakes.
*/
private function addActionSetTRPHash()
{
add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 2);
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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);
}

Copilot uses AI. Check for mistakes.
Comment on lines +65 to +76
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);
}
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
} else {
$this->setCommentPreStatusAndModifyEmail('not_approved');
}
// thi is the only case when we do not set TRP hash!
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'thi' to 'this'.

Suggested change
// thi is the only case when we do not set TRP hash!
// this is the only case when we do not set TRP hash!

Copilot uses AI. Check for mistakes.
Comment on lines +301 to +307
/**
* TRP hash should be set anyway if Cleantalk processed.
*/
private function addActionSetTRPHash()
{
add_action('comment_post', array(CleantalkRealPerson::class, 'setTRPHash'), 999, 2);
}
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Comment on lines +581 to +586
* @return bool
*/
public static function firstCommentAutoModEnabled()
{
global $apbct;
return !empty($apbct->settings['cleantalk_allowed_moderation']) && $apbct->settings['cleantalk_allowed_moderation'] == '1';
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
* @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';

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants