Files
ogre-captcha/inc/location/class-comment-form.php
T
2026-06-09 12:53:19 -05:00

63 lines
1.7 KiB
PHP

<?php
/**
* @package ogre-captcha
* @author cleverogre
* @copyright 2026 CleverOgre
* @license GLP-3.0-or-later
* @version 1.0.0
* @since 1.0.0
*/
namespace Ogre\Captcha\Location;
use Override;
use Ogre\Captcha\Location;
use Ogre\Captcha\Settings;
use Ogre\Captcha\Widget;
use WP_Error;
defined('ABSPATH') || exit;
final class CommentForm extends Location {
protected function __construct() {
parent::__construct(Settings::LOCATION_COMMENT_FORM);
}
#[Override]
public function activate(): void
{
add_action('comment_form_after_fields', [$this, 'render'], 1);
add_filter('comment_form_submit_button', [$this, 'submit_button'], 10, 2);
add_filter('preprocess_comment', [$this, 'process_form']);
}
public function submit_button(string $submit_button, array $args):string {
if (Settings::is_widget_floating()) {
$submit_button = Widget::set_button_floating($submit_button);
}
return $submit_button;
}
public function process_form(array $commentdata):array {
// Don't process CAPTCHA for comment replies inside admin menu
if (isset($_REQUEST['action']) && 'replyto-comment' == $_REQUEST['action'] && (check_ajax_referer('replyto-comment', '_ajax_nonce', false) || check_ajax_referer('replyto-comment', '_ajax_nonce-replyto-comment', false))) {
return $comment;
}
// Don't do CAPTCHA for pingback/trackback
if ('' != $comment['comment_type'] && 'comment' != $comment['comment_type'] && 'review' != $comment['comment_type']) {
return $comment;
}
$errors = new WP_Error();
$this->process($errors);
$this->print_errors($errors);
return $commentdata;
}
}
CommentForm::instance();