From b453bf9ef1f11cbcfdcc7ca38faab81ea8895dc6 Mon Sep 17 00:00:00 2001 From: AntonV1211 Date: Thu, 24 Apr 2025 15:16:43 +0700 Subject: [PATCH 1/3] Ref. WPSearch. Class integration for WP Search --- inc/cleantalk-integrations-by-class.php | 5 + inc/cleantalk-public-integrations.php | 47 ---------- inc/cleantalk-public.php | 5 - .../Antispam/IntegrationsByClass.php | 14 +-- .../IntegrationsByClass/WPSearchForm.php | 94 +++++++++++++++++++ .../PublicIntegrationsTest.php | 20 ++-- 6 files changed, 118 insertions(+), 67 deletions(-) create mode 100644 lib/Cleantalk/Antispam/IntegrationsByClass/WPSearchForm.php diff --git a/inc/cleantalk-integrations-by-class.php b/inc/cleantalk-integrations-by-class.php index 0b18480da..807ea173f 100755 --- a/inc/cleantalk-integrations-by-class.php +++ b/inc/cleantalk-integrations-by-class.php @@ -9,6 +9,11 @@ 'plugin_path' => 'woocommerce/woocommerce.php', 'plugin_class' => 'WooCommerce', ), + 'WPSearchForm' => array( + 'plugin_path' => '', + 'plugin_class' => '', + 'wp_includes' => true, + ), ); add_action('plugins_loaded', function () use ($apbct_integrations_by_class) { diff --git a/inc/cleantalk-public-integrations.php b/inc/cleantalk-public-integrations.php index 92be87b6b..49db604c3 100644 --- a/inc/cleantalk-public-integrations.php +++ b/inc/cleantalk-public-integrations.php @@ -3404,53 +3404,6 @@ function apbct_form_happyforms_test_spam($is_valid, $request, $_form) return $is_valid; } -/** - * Prepare data to add honeypot to the WordPress default search form. - * Fires ct_add_honeypot_field() on hook get_search_form when: - * - method of the form is post - * - spam test of search form is enabled - * - * @param string $form_html - * @return string - */ -function apbct_form_search__add_fields($form_html) -{ - global $apbct; - - if ( !empty($form_html) && is_string($form_html) && $apbct->settings['forms__search_test'] == 1 ) { - // extract method of the form with DOMDocument - if ( class_exists('DOMDocument') ) { - libxml_use_internal_errors(true); - $dom = new DOMDocument(); - if ( @$dom->loadHTML($form_html) ) { - $search_form_dom = $dom->getElementById('searchform'); - if ( !empty($search_form_dom) ) { - $method = empty($search_form_dom->getAttribute('method')) - //default method is get for any form if no method specified - ? 'get' - : $search_form_dom->getAttribute('method'); - } - } - libxml_clear_errors(); - unset($dom); - } - - // retry extract method of the form with regex - if ( empty($method) ) { - preg_match('/form.*method="(.*?)"/', $form_html, $matches); - $method = empty($matches[1]) - ? 'get' - : trim($matches[1]); - } - - $form_method = strtolower($method); - - return str_replace('', Honeypot::generateHoneypotField('search_form', $form_method) . '', $form_html); - } - - return $form_html; -} - /** * Advanced Classifieds & Directory Pro * diff --git a/inc/cleantalk-public.php b/inc/cleantalk-public.php index b41fc600b..7bc908e84 100644 --- a/inc/cleantalk-public.php +++ b/inc/cleantalk-public.php @@ -47,11 +47,6 @@ function apbct_init() add_filter('rocket_delay_js_exclusions', 'apbct_rocket_delay_js_exclusions'); } - //Search form hook init - if ( $apbct->settings['forms__search_test'] ) { - add_filter('get_search_form', 'apbct_form_search__add_fields', 999); - } - //fix for EPM registration form if ( Post::get('reg_email') && shortcode_exists('epm_registration_form') ) { unset($_POST['ct_checkjs_register_form']); diff --git a/lib/Cleantalk/Antispam/IntegrationsByClass.php b/lib/Cleantalk/Antispam/IntegrationsByClass.php index a77a0d8f5..96e6c46a1 100755 --- a/lib/Cleantalk/Antispam/IntegrationsByClass.php +++ b/lib/Cleantalk/Antispam/IntegrationsByClass.php @@ -27,13 +27,15 @@ public function __construct($integrations) foreach ($this->integrations as $integration_name => $integration_info) { // pre-check to skip integration by plugin path - if ( isset($integration_info['plugin_path']) && !$this->isPluginActive($integration_info['plugin_path']) ) { - continue; - } + if (!isset($integration_info['wp_includes'])) { + if ( isset($integration_info['plugin_path']) && !$this->isPluginActive($integration_info['plugin_path']) ) { + continue; + } - // pre-check to skip integration by plugin class - if ( isset($integration_info['plugin_class']) && !class_exists($integration_info['plugin_class']) ) { - continue; + // pre-check to skip integration by plugin class + if ( isset($integration_info['plugin_class']) && !class_exists($integration_info['plugin_class']) ) { + continue; + } } $class = '\\Cleantalk\\Antispam\\IntegrationsByClass\\' . $integration_name; diff --git a/lib/Cleantalk/Antispam/IntegrationsByClass/WPSearchForm.php b/lib/Cleantalk/Antispam/IntegrationsByClass/WPSearchForm.php new file mode 100644 index 000000000..089e0070e --- /dev/null +++ b/lib/Cleantalk/Antispam/IntegrationsByClass/WPSearchForm.php @@ -0,0 +1,94 @@ +settings['forms__search_test'] ) { + add_filter('get_search_form', array($this, 'apbctFormSearchAddFields'), 999); + } + } + + /** + * @return void + * @psalm-suppress PossiblyUnusedMethod + */ + public function doAdminWork() + { + } + + /** + * Prepare data to add honeypot to the WordPress default search form. + * Fires ct_add_honeypot_field() on hook get_search_form when: + * - method of the form is post + * - spam test of search form is enabled + * + * @param string $form_html + * @return string + */ + public function apbctFormSearchAddFields($form_html) + { + global $apbct; + + if ( !empty($form_html) && is_string($form_html) && $apbct->settings['forms__search_test'] == 1 ) { + // extract method of the form with DOMDocument + if ( class_exists('DOMDocument') ) { + libxml_use_internal_errors(true); + $dom = new DOMDocument(); + if ( @$dom->loadHTML($form_html) ) { + $search_form_dom = $dom->getElementById('searchform'); + if ( !empty($search_form_dom) ) { + $method = empty($search_form_dom->getAttribute('method')) + //default method is get for any form if no method specified + ? 'get' + : $search_form_dom->getAttribute('method'); + } + } + libxml_clear_errors(); + unset($dom); + } + + // retry extract method of the form with regex + if ( empty($method) ) { + preg_match('/form.*method="(.*?)"/', $form_html, $matches); + $method = empty($matches[1]) + ? 'get' + : trim($matches[1]); + } + $form_method = strtolower($method); + + $resalt = str_replace('', Honeypot::generateHoneypotField('search_form', $form_method) . '', $form_html); + return $resalt; + } + + return $form_html; + } +} diff --git a/tests/StandaloneFunctions/PublicIntegrationsTest.php b/tests/StandaloneFunctions/PublicIntegrationsTest.php index f0fd10ee6..bee6c53e3 100644 --- a/tests/StandaloneFunctions/PublicIntegrationsTest.php +++ b/tests/StandaloneFunctions/PublicIntegrationsTest.php @@ -2,7 +2,6 @@ use PHPUnit\Framework\TestCase; use Cleantalk\ApbctWP\State; - class PublicIntegrationsTest extends TestCase { /** @@ -14,6 +13,7 @@ public function test_apbct_form_search__add_fields() global $apbct; $apbct = new State( 'cleantalk', array('settings', 'data', 'errors', 'remote_calls', 'stats', 'fw_stats') ); $apbct->settings['data__honeypot_field'] = 1; + $wp_search_form = new \Cleantalk\Antispam\IntegrationsByClass\WPSearchForm(); //define sample form test $sample_form = ''; - $this->assertNotFalse(simplexml_load_string(apbct_form_search__add_fields($sample_form)), - 'TEST DATA:['.$sample_form.']'); + $this->assertNotFalse( + simplexml_load_string($wp_search_form->apbctFormSearchAddFields($sample_form)), + 'TEST DATA:[' . $sample_form . ']' + ); //define empty string test - $this->assertEmpty(apbct_form_search__add_fields(''), + $this->assertEmpty($wp_search_form->apbctFormSearchAddFields(''), 'TEST DATA:[EMPTY STRING]'); //define values that will be returned as is $test_data_invalid[] = array('form'=>''); @@ -36,18 +38,18 @@ public function test_apbct_form_search__add_fields() $test_data_invalid[] = new Exception(); //and walk them foreach ($test_data_invalid as $test){ - $this->assertEquals(apbct_form_search__add_fields($test),$test); + $this->assertEquals($wp_search_form->apbctFormSearchAddFields($test),$test); } //fails if signature not found in changed form - $success = (preg_match('/class=".*?apbct__email_id__search_form/', apbct_form_search__add_fields($sample_form)) || - preg_match('/id=".*?apbct_event_id/', apbct_form_search__add_fields($sample_form)) + $success = (preg_match('/class=".*?apbct__email_id__search_form/', $wp_search_form->apbctFormSearchAddFields($sample_form)) || + preg_match('/id=".*?apbct_event_id/', $wp_search_form->apbctFormSearchAddFields($sample_form)) )&& - preg_match('/class=".*?apbct_special_field/', apbct_form_search__add_fields($sample_form)); + preg_match('/class=".*?apbct_special_field/', $wp_search_form->apbctFormSearchAddFields($sample_form)); $this->assertNotFalse($success); //form won`t be changed if search form checking is not set in settings $apbct->settings['forms__search_test'] = 0; - $this->assertEquals(apbct_form_search__add_fields($sample_form),$sample_form); + $this->assertEquals($wp_search_form->apbctFormSearchAddFields($sample_form),$sample_form); } } From 524ebca8ef107011b9b71625a575236b8cdf7f8f Mon Sep 17 00:00:00 2001 From: alexandergull Date: Wed, 30 Apr 2025 13:25:17 +0500 Subject: [PATCH 2/3] Upd. Search forms. Integration by class. Meta and cpam check method moved to the class. --- cleantalk.php | 4 -- inc/cleantalk-public-integrations.php | 64 ----------------- .../Antispam/IntegrationsByClass.php | 5 ++ .../IntegrationsByClass/WPSearchForm.php | 71 +++++++++++++++++++ 4 files changed, 76 insertions(+), 68 deletions(-) diff --git a/cleantalk.php b/cleantalk.php index bf17a125e..646dc7cf0 100644 --- a/cleantalk.php +++ b/cleantalk.php @@ -592,10 +592,6 @@ function apbct_write_js_errors($data) // Public actions if ( ! is_admin() && ! apbct_is_ajax() && ! apbct_is_customize_preview() ) { - // Default search - add_filter('get_search_query', 'apbct_forms__search__testSpam'); - add_action('wp_head', 'apbct_search_add_noindex', 1); - if (apbct_is_plugin_active('fluentformpro/fluentformpro.php') && apbct_is_in_uri('ff_landing=')) { add_action('wp_head', function () { echo '