Skip to content

Commit 861c55f

Browse files
authored
Fix. Integration. Ninja forms getting form fields fixed. (#462)
* Fix. Integration. Ninja forms getting form fields fixed. * Fix. Code. Code style fixed. * Fix. Code. Psalm notice fixed. * Fix. Integration. Ninja forms new way form data collecting logic fixed. * Fix. Code. Psalm notices fixed. * Fix. Code. Psalm notices fixed #2. * Fix. Integration. Ninja Forms nickname detection improved. * Fix. Integration. New way getting fields improved. * Fix. Code. Code style and psalm notices fixed.
1 parent 9ba72e6 commit 861c55f

File tree

1 file changed

+86
-9
lines changed

1 file changed

+86
-9
lines changed

inc/cleantalk-public-integrations.php

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,15 +2168,12 @@ function apbct_form__ninjaForms__testSpam()
21682168

21692169
$checkjs = apbct_js_test(Sanitize::cleanTextField(Cookie::get('ct_checkjs')), true);
21702170

2171-
/**
2172-
* Filter for POST
2173-
*/
2174-
$input_array = apply_filters('apbct__filter_post', $_POST);
2175-
2176-
// Choosing between POST and GET
2177-
$params = ct_get_fields_any(
2178-
Get::get('ninja_forms_ajax_submit') || Get::get('nf_ajax_submit') ? $_GET : $input_array
2179-
);
2171+
try {
2172+
$params = apbct_form__ninjaForms__collect_fields_new();
2173+
} catch (\Exception $_e) {
2174+
// It is possible here check the reason if the new way collecting fields is not available.
2175+
$params = apbct_form__ninjaForms__collect_fields_old();
2176+
}
21802177

21812178
$sender_email = isset($params['email']) ? $params['email'] : '';
21822179
$sender_emails_array = isset($params['emails_array']) ? $params['emails_array'] : '';
@@ -2240,6 +2237,86 @@ function apbct_form__ninjaForms__testSpam()
22402237
}
22412238
}
22422239

2240+
/**
2241+
* Old way to collecting NF fields data.
2242+
*
2243+
* @return array
2244+
*/
2245+
function apbct_form__ninjaForms__collect_fields_old()
2246+
{
2247+
/**
2248+
* Filter for POST
2249+
*/
2250+
$input_array = apply_filters('apbct__filter_post', $_POST);
2251+
2252+
// Choosing between POST and GET
2253+
return ct_gfa(
2254+
Get::get('ninja_forms_ajax_submit') || Get::get('nf_ajax_submit') ? $_GET : $input_array
2255+
);
2256+
}
2257+
2258+
/**
2259+
* New way to collecting NF fields data - try to get username and email.
2260+
*
2261+
* @return array
2262+
* @throws Exception
2263+
* @psalm-suppress UndefinedClass
2264+
*/
2265+
function apbct_form__ninjaForms__collect_fields_new()
2266+
{
2267+
$form_data = json_decode(TT::toString(Post::get('formData')), true);
2268+
if ( ! $form_data ) {
2269+
$form_data = json_decode(stripslashes(TT::toString(Post::get('formData'))), true);
2270+
}
2271+
if ( ! isset($form_data['fields']) ) {
2272+
throw new Exception('No form data is provided');
2273+
}
2274+
if ( ! function_exists('Ninja_Forms') ) {
2275+
throw new Exception('No `Ninja_Forms` class exists');
2276+
}
2277+
$nf_form_info = Ninja_Forms()->form();
2278+
if ( ! ($nf_form_info instanceof NF_Abstracts_ModelFactory) ) {
2279+
throw new Exception('Getting NF form failed');
2280+
}
2281+
$nf_form_fields_info = $nf_form_info->get_fields();
2282+
if ( ! is_array($nf_form_fields_info) && count($nf_form_fields_info) === 0 ) {
2283+
throw new Exception('No fields are provided');
2284+
}
2285+
$nf_form_fields_info_array = [];
2286+
foreach ($nf_form_fields_info as $field) {
2287+
if ( $field instanceof NF_Database_Models_Field) {
2288+
$nf_form_fields_info_array[$field->get_id()] = [
2289+
'field_key' => TT::toString($field->get_setting('key')),
2290+
'field_type' => TT::toString($field->get_setting('type')),
2291+
'field_label' => TT::toString($field->get_setting('label')),
2292+
];
2293+
}
2294+
}
2295+
2296+
$nf_form_fields = $form_data['fields'];
2297+
$nickname = '';
2298+
$email = '';
2299+
$fields = [];
2300+
foreach ($nf_form_fields as $field) {
2301+
if ( isset($nf_form_fields_info_array[$field['id']]) ) {
2302+
$field_info = $nf_form_fields_info_array[$field['id']];
2303+
if ( isset($field_info['field_key'], $field_info['field_type']) ) {
2304+
$field_key = TT::toString($field_info['field_key']);
2305+
$field_type = TT::toString($field_info['field_type']);
2306+
$fields['nf-field-' . $field['id'] . '-' . $field_type] = $field['value'];
2307+
if ( stripos($field_key, 'name') !== false ) {
2308+
$nickname = $field['value'];
2309+
}
2310+
if ( stripos($field_key, 'email') !== false ) {
2311+
$email = $field['value'];
2312+
}
2313+
}
2314+
}
2315+
}
2316+
2317+
return ct_gfa($fields, $email, $nickname);
2318+
}
2319+
22432320
/**
22442321
* Inserts anti-spam hidden to ninja forms
22452322
*

0 commit comments

Comments
 (0)