Skip to content

Commit 726f437

Browse files
authored
Upd. Integrations. Move wpforms integration to separate class. (#607)
* Upd. Integrations. Move wpforms integration to separate class. * fix psalm * Upd. Integrations. Improve nickname gathering. * fix psalm
1 parent 99882fa commit 726f437

6 files changed

Lines changed: 316 additions & 309 deletions

File tree

cleantalk.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -595,14 +595,6 @@ function apbct_write_js_errors($data)
595595
add_filter('happyforms_validate_submission', 'apbct_form_happyforms_test_spam', 1, 3);
596596
add_filter('happyforms_use_hash_protection', '__return_false');
597597

598-
// WPForms
599-
// Adding fields
600-
add_action('wpforms_frontend_output', 'apbct_form__WPForms__addField', 1000, 5);
601-
// Gathering data to validate
602-
add_filter('wpforms_process_before_filter', 'apbct_from__WPForms__gatherData', 100, 2);
603-
// Do spam check
604-
add_filter('wpforms_process_initial_errors', 'apbct_form__WPForms__showResponse', 100, 2);
605-
606598
// Formidable
607599
add_filter('frm_entries_before_create', 'apbct_form__formidable__testSpam', 999999, 2);
608600
add_action('frm_entries_footer_scripts', 'apbct_form__formidable__footerScripts', 20, 2);

inc/cleantalk-integrations-by-class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
'plugin_class' => '',
1515
'wp_includes' => true,
1616
),
17+
'WPForms' => array(
18+
'plugin_path' => ['wpforms-lite/wpforms.php', 'wpforms/wpforms.php'],
19+
'plugin_class' => 'WPForms',
20+
),
1721
);
1822

1923
add_action('plugins_loaded', function () use ($apbct_integrations_by_class) {

inc/cleantalk-public-integrations.php

Lines changed: 0 additions & 279 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,285 +2125,6 @@ function apbct_form__ninjaForms__changeMailNotification($message, $_data, $actio
21252125
return $message;
21262126
}
21272127

2128-
/**
2129-
* Inserts anti-spam hidden to WPForms
2130-
*
2131-
* @return void
2132-
* @global State $apbct
2133-
*/
2134-
function apbct_form__WPForms__addField($_form_data, $_some, $_title, $_description, $_errors)
2135-
{
2136-
global $apbct;
2137-
2138-
if ( $apbct->settings['forms__contact_forms_test'] == 1 && !is_user_logged_in() ) {
2139-
ct_add_hidden_fields('ct_checkjs_wpforms');
2140-
echo Honeypot::generateHoneypotField('wp_wpforms');
2141-
if ( $apbct->settings['trusted_and_affiliate__under_forms'] === '1' ) {
2142-
echo Escape::escKsesPreset(
2143-
apbct_generate_trusted_text_html('label_left'),
2144-
'apbct_public__trusted_text'
2145-
);
2146-
}
2147-
}
2148-
}
2149-
2150-
/**
2151-
* Gather fields data from submission and store it
2152-
*
2153-
* @param array $entry
2154-
* @param $form
2155-
*
2156-
* @return array
2157-
* @global State $apbct
2158-
*/
2159-
function apbct_from__WPForms__gatherData($entry, $form)
2160-
{
2161-
global $apbct;
2162-
$handled_result = array();
2163-
2164-
/**
2165-
* Filter for POST
2166-
*/
2167-
$input_array = apply_filters('apbct__filter_post', isset($entry['fields']) ? $entry['fields'] : array());
2168-
2169-
$entry_fields_data = $input_array ?: array();
2170-
$form_fields_info = $form['fields'] ?: array();
2171-
2172-
foreach ( $form_fields_info as $form_field ) {
2173-
$field_id = $form_field['id'];
2174-
$field_type = $form_field['type'];
2175-
if (array_key_exists('label', $form_field)) {
2176-
$field_label = $form_field['label'] ?: '';
2177-
} else {
2178-
$field_label = '';
2179-
}
2180-
if ( ! isset($entry_fields_data[$field_id]) ) {
2181-
continue;
2182-
}
2183-
$entry_field_value = $entry_fields_data[$field_id];
2184-
2185-
# search email field
2186-
if ( $field_type === 'email' ) {
2187-
if ( ! isset($handled_result['email']) || empty($handled_result['email']) ) {
2188-
$handled_result['email'] = $entry_field_value;
2189-
continue;
2190-
}
2191-
}
2192-
2193-
# search name
2194-
if ( $field_type === 'name' ) {
2195-
if ( is_array($entry_field_value) ) {
2196-
$handled_result['name'][] = implode(' ', array_slice($entry_field_value, 0, 3));
2197-
} else {
2198-
$handled_result['name'][] = $entry_field_value;
2199-
}
2200-
continue;
2201-
}
2202-
2203-
# search textarea
2204-
if ( $field_type === 'textarea' ) {
2205-
if ( is_array($entry_field_value) ) {
2206-
$handled_result["wpforms[fields][$field_id]"][] = implode(' ', array_slice($entry_field_value, 0, 3));
2207-
} else {
2208-
$handled_result["wpforms[fields][$field_id]"] = $entry_field_value;
2209-
}
2210-
continue;
2211-
}
2212-
2213-
# Add field label as key for result array
2214-
# add unique key if key exist
2215-
if ( $field_label ) {
2216-
$field_label = mb_strtolower(trim($field_label));
2217-
$field_label = str_replace(' ', '_', $field_label);
2218-
$field_label = preg_replace('/\W/u', '', $field_label);
2219-
2220-
if ( ! isset($handled_result[$field_label]) || empty($handled_result[$field_label]) ) {
2221-
$handled_result[$field_label] = $entry_field_value;
2222-
} else {
2223-
$handled_result[$field_label . rand(0, 100)] = $entry_field_value;
2224-
}
2225-
}
2226-
}
2227-
2228-
$apbct->form_data = $handled_result;
2229-
2230-
return $entry;
2231-
}
2232-
2233-
/**
2234-
* Adding error to form entry if message is spam
2235-
* Call spam test from here
2236-
*
2237-
* @param array $errors
2238-
* @param array $form_data
2239-
*
2240-
* @return array
2241-
*/
2242-
function apbct_form__WPForms__showResponse($errors, $form_data)
2243-
{
2244-
if (
2245-
empty($errors) ||
2246-
(isset($form_data['id'], $errors[$form_data['id']]) && ! count($errors[$form_data['id']]))
2247-
) {
2248-
$spam_comment = apbct_form__WPForms__testSpam();
2249-
2250-
if ( $spam_comment ) {
2251-
$field_id = 0;
2252-
if ( $form_data && ! empty($form_data['fields']) && is_array($form_data['fields']) ) {
2253-
foreach ( $form_data['fields'] as $key => $field ) {
2254-
if ( array_search('email', $field) === 'type' ) {
2255-
$field_id = $key;
2256-
break;
2257-
}
2258-
}
2259-
}
2260-
2261-
$field_id = ! $field_id && $form_data && ! empty($form_data['fields']) && is_array($form_data['fields'])
2262-
? key($form_data['fields'])
2263-
: $field_id;
2264-
2265-
if ( isset($form_data['id']) ) {
2266-
$errors[$form_data['id']][$field_id] = $spam_comment;
2267-
}
2268-
}
2269-
}
2270-
2271-
return $errors;
2272-
}
2273-
2274-
/**
2275-
* Test WPForms message for spam
2276-
* Doesn't hooked anywhere.
2277-
* Called directly from apbct_form__WPForms__showResponse()
2278-
*
2279-
* @return string|void
2280-
* @global State $apbct
2281-
*/
2282-
function apbct_form__WPForms__testSpam()
2283-
{
2284-
global $apbct;
2285-
2286-
if (
2287-
$apbct->settings['forms__contact_forms_test'] == 0 ||
2288-
($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users.
2289-
) {
2290-
do_action('apbct_skipped_request', __FILE__ . ' -> ' . __FUNCTION__ . '():' . __LINE__, $_POST);
2291-
2292-
return;
2293-
}
2294-
2295-
$checkjs = apbct_js_test(Sanitize::cleanTextField(Post::get('ct_checkjs_wpforms')));
2296-
2297-
$email = $apbct->form_data['email'] ?: null;
2298-
2299-
# Fixed if the 'Enable email address confirmation' option is enabled
2300-
if ( is_array($email) ) {
2301-
$email = reset($email);
2302-
}
2303-
2304-
$nickname = null;
2305-
$form_data = $apbct->form_data instanceof ArrayObject ? (array)$apbct->form_data : $apbct->form_data;
2306-
if (array_key_exists('name', $form_data)) {
2307-
$nickname = isset($form_data['name']) && is_array($form_data['name']) ? array_shift(
2308-
$form_data['name']
2309-
) : null;
2310-
}
2311-
2312-
if ( $email ) {
2313-
unset($form_data['email']);
2314-
}
2315-
if ( $nickname ) {
2316-
unset($form_data['name']);
2317-
}
2318-
2319-
$params = ct_gfa((array)$apbct->form_data, is_null($email) ? '' : $email, is_null($nickname) ? '' : $nickname);
2320-
2321-
if ( isset($params['nickname']) && is_array($params['nickname']) ) {
2322-
$params['nickname'] = implode(' ', $params['nickname']);
2323-
}
2324-
2325-
$sender_email = isset($params['email']) ? $params['email'] : '';
2326-
$sender_nickname = isset($params['nickname']) ? $params['nickname'] : '';
2327-
$subject = isset($params['subject']) ? $params['subject'] : '';
2328-
$message = isset($params['message']) ? $params['message'] : array();
2329-
if ( $subject !== '' ) {
2330-
$message = array_merge(array('subject' => $subject), $message);
2331-
}
2332-
2333-
$sender_info = [];
2334-
if ( ! empty($params['emails_array']) ) {
2335-
$sender_info['sender_emails_array'] = $params['emails_array'];
2336-
}
2337-
2338-
$base_call_result = apbct_base_call(
2339-
array(
2340-
'message' => $message,
2341-
'sender_email' => $sender_email,
2342-
'sender_nickname' => $sender_nickname,
2343-
'post_info' => array('comment_type' => 'contact_form_wordpress_wp_forms'),
2344-
'js_on' => $checkjs,
2345-
'sender_info' => $sender_info,
2346-
)
2347-
);
2348-
2349-
if ( isset($base_call_result['ct_result']) ) {
2350-
$ct_result = $base_call_result['ct_result'];
2351-
2352-
// Change mail notification if license is out of date
2353-
if ( $apbct->data['moderate'] == 0 &&
2354-
($ct_result->fast_submit == 1 || $ct_result->blacklisted == 1 || $ct_result->js_disabled == 1)
2355-
) {
2356-
$apbct->sender_email = $sender_email;
2357-
$apbct->sender_ip = Helper::ipGet('real');
2358-
add_filter('wpforms_email_message', 'apbct_form__WPForms__changeMailNotification', 100, 2);
2359-
}
2360-
2361-
if ( $ct_result->allow == 0 ) {
2362-
return $ct_result->comment;
2363-
}
2364-
}
2365-
2366-
return null;
2367-
}
2368-
2369-
/**
2370-
* Changes email notification for succes subscription for Ninja Forms
2371-
*
2372-
* @param string $message Body of email notification
2373-
* @param object $wpforms_email WPForms email class object
2374-
*
2375-
* @return string Body for email notification
2376-
*/
2377-
function apbct_form__WPForms__changeMailNotification($message, $_wpforms_email)
2378-
{
2379-
global $apbct;
2380-
2381-
$message = str_replace(array('</html>', '</body>'), '', $message);
2382-
$message .=
2383-
wpautop(
2384-
PHP_EOL
2385-
. '---'
2386-
. PHP_EOL
2387-
. __('CleanTalk Anti-Spam: This message could be spam.', 'cleantalk-spam-protect')
2388-
. PHP_EOL . __('CleanTalk\'s Anti-Spam database:', 'cleantalk-spam-protect')
2389-
//HANDLE LINK
2390-
. PHP_EOL . 'IP: ' . '<a href="https://cleantalk.org/blacklists/' . $apbct->sender_ip . '?utm_source=newsletter&utm_medium=email&utm_campaign=wpforms_spam_passed" target="_blank">' . $apbct->sender_ip . '</a>'
2391-
//HANDLE LINK
2392-
. PHP_EOL . 'Email: ' . '<a href="https://cleantalk.org/blacklists/' . $apbct->sender_email . '?utm_source=newsletter&utm_medium=email&utm_campaign=wpforms_spam_passed" target="_blank">' . $apbct->sender_email . '</a>'
2393-
. PHP_EOL
2394-
//HANDLE LINK
2395-
. sprintf(
2396-
__('If you want to be sure activate protection in your %sAnti-Spam Dashboard%s.', 'clentalk'),
2397-
'<a href="https://cleantalk.org/my/?cp_mode=antispam&utm_source=newsletter&utm_medium=email&utm_campaign=wpforms_activate_antispam" target="_blank">',
2398-
'</a>'
2399-
)
2400-
)
2401-
. '</body></html>';
2402-
2403-
return $message;
2404-
}
2405-
2406-
24072128
/**
24082129
* QuForms check spam
24092130
* works with single-paged forms

lib/Cleantalk/Antispam/IntegrationsByClass.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public function __construct($integrations)
6363
}
6464

6565
// Public work
66-
$skip_integration = $integration->isSkipIntegration();
67-
if ($skip_integration) {
66+
if ($integration->isSkipIntegration()) {
6867
continue;
6968
}
7069

@@ -74,6 +73,13 @@ public function __construct($integrations)
7473

7574
private function isPluginActive($plugin_path)
7675
{
76+
if (is_array($plugin_path)) {
77+
foreach ($plugin_path as $path) {
78+
if (in_array($path, $this->active_plugins) || in_array($path, $this->active_plugins_wpms)) {
79+
return true;
80+
}
81+
}
82+
}
7783
return in_array($plugin_path, $this->active_plugins) || in_array($plugin_path, $this->active_plugins_wpms);
7884
}
7985
}

0 commit comments

Comments
 (0)