From 83a278db6a7177a0e6a7f5bf6e9237e148207137 Mon Sep 17 00:00:00 2001 From: svfcode Date: Sat, 21 Dec 2024 14:20:27 +0300 Subject: [PATCH 1/3] New. SpamProtection. Added shortcode for easily third party integration. --- cleantalk.php | 4 ++ lib/Cleantalk/Antispam/ProtectByShortcode.php | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/Cleantalk/Antispam/ProtectByShortcode.php diff --git a/cleantalk.php b/cleantalk.php index d51abdf4..73be2f7a 100644 --- a/cleantalk.php +++ b/cleantalk.php @@ -11,6 +11,7 @@ Domain Path: /i18n */ +use Cleantalk\Antispam\ProtectByShortcode; use Cleantalk\ApbctWP\Activator; use Cleantalk\ApbctWP\AdminNotices; use Cleantalk\ApbctWP\Antispam\EmailEncoder; @@ -366,6 +367,9 @@ function apbct_alt_session__save__WP_AJAX() require_once(CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-integrations-by-hook.php'); require_once(CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-integrations-by-class.php'); +// Form protection by shortcode +new ProtectByShortcode(); + // WP Delicious integration add_filter('delicious_recipes_process_registration_errors', 'apbct_wp_delicious', 10, 4); diff --git a/lib/Cleantalk/Antispam/ProtectByShortcode.php b/lib/Cleantalk/Antispam/ProtectByShortcode.php new file mode 100644 index 00000000..a6987729 --- /dev/null +++ b/lib/Cleantalk/Antispam/ProtectByShortcode.php @@ -0,0 +1,49 @@ + false, + 'message' => '', + ]; + + $input_array = apply_filters('apbct__filter_post', $data); + $data = ct_gfa($input_array); + + $base_call_data = array( + 'message' => ! empty($data['message']) ? json_encode($data['message']) : '', + 'sender_email' => ! empty($data['email']) ? $data['email'] : '', + 'sender_nickname' => ! empty($data['nickname']) ? $data['nickname'] : '', + 'event_token' => ! empty($data['event_token']) ? $data['event_token'] : '', + 'post_info' => array( + 'post_url' => Server::get('HTTP_REFERER'), + ), + ); + $result = apbct_base_call($base_call_data, false); + $ct_result = isset($result['ct_result']) ? $result['ct_result'] : null; + + $is_spam = $ct_result !== null && $ct_result->allow !== 1; + + if (isset($options['redirect_to_block_page']) && $options['redirect_to_block_page'] && $is_spam) { + wp_die(isset($ct_result->comment) ? $ct_result->comment : __('Blocked by CleanTalk'), __('Forbidden'), array('response' => 403)); + } + + if ($is_spam) { + $output = [ + 'is_spam' => true, + 'message' => $ct_result->comment, + ]; + } + + return $output; + } +} From 10d4fd981241d89cf237d0e832a134c049eef0f6 Mon Sep 17 00:00:00 2001 From: svfcode Date: Mon, 23 Dec 2024 08:12:02 +0300 Subject: [PATCH 2/3] fix phpcs --- lib/Cleantalk/Antispam/ProtectByShortcode.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/Cleantalk/Antispam/ProtectByShortcode.php b/lib/Cleantalk/Antispam/ProtectByShortcode.php index a6987729..3572b7c1 100644 --- a/lib/Cleantalk/Antispam/ProtectByShortcode.php +++ b/lib/Cleantalk/Antispam/ProtectByShortcode.php @@ -4,12 +4,14 @@ use Cleantalk\ApbctWP\Variables\Server; -class ProtectByShortcode { - public function __construct() { - add_filter('ct_wordpress_protect_from_spam', array($this, 'protect_by_shortcode'), 10, 2); +class ProtectByShortcode +{ + public function __construct() + { + add_filter('ct_wordpress_protect_from_spam', array($this, 'protectByShortcode'), 10, 2); } - public function protect_by_shortcode($data, $options = []) + public function protectByShortcode($data, $options = []) { $output = [ 'is_spam' => false, @@ -34,7 +36,7 @@ public function protect_by_shortcode($data, $options = []) $is_spam = $ct_result !== null && $ct_result->allow !== 1; if (isset($options['redirect_to_block_page']) && $options['redirect_to_block_page'] && $is_spam) { - wp_die(isset($ct_result->comment) ? $ct_result->comment : __('Blocked by CleanTalk'), __('Forbidden'), array('response' => 403)); + wp_die(isset($ct_result->comment) ? $ct_result->comment : __('Blocked by CleanTalk'), __('Forbidden'), array('response' => 403)); } if ($is_spam) { From 4adad9df8758b83c63aa98958e478354d0c94c08 Mon Sep 17 00:00:00 2001 From: AntonV1211 Date: Tue, 14 Jan 2025 19:54:58 +0700 Subject: [PATCH 3/3] Upd. ProtectByShortcode. Edits based on the review --- lib/Cleantalk/Antispam/ProtectByShortcode.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/Cleantalk/Antispam/ProtectByShortcode.php b/lib/Cleantalk/Antispam/ProtectByShortcode.php index 3572b7c1..46de7de1 100644 --- a/lib/Cleantalk/Antispam/ProtectByShortcode.php +++ b/lib/Cleantalk/Antispam/ProtectByShortcode.php @@ -8,9 +8,22 @@ class ProtectByShortcode { public function __construct() { - add_filter('ct_wordpress_protect_from_spam', array($this, 'protectByShortcode'), 10, 2); + global $apbct; + + if ((int)$apbct->settings['forms__contact_forms_test'] === 1) { + add_filter('ct_wordpress_protect_from_spam', array($this, 'protectByShortcode'), 10, 2); + } } + /** + * A function to protect the data of $_POST, $_GET custom forms by the ct_wordpress_protect_from_spam hook. + * Returns an array with the result of the check. Also, if $options['redirect_to_block_page'] = 1 is passed, + * a redirect will be made to the blocking page. + * @param array $data + * @param array $options + * @return array + * @psalm-suppress PossiblyUnusedReturnValue + */ public function protectByShortcode($data, $options = []) { $output = [