From a270b79b2b9edb160880262b51f1c2d050da9b95 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 23 Aug 2024 18:28:03 +0300 Subject: [PATCH 01/42] Restrict comment_author to name fields --- classes/models/FrmEntryValidate.php | 1 - 1 file changed, 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index f66e104b1c..1454be7f6c 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -620,7 +620,6 @@ private static function is_akismet_guest_info_value( $key, $value, $field_id, $n // If there is name field in the form, we should always use it as author name. return in_array( $field_id, $name_field_ids, true ); } - return strlen( $value ) < 200; } return false; From 036c38f9c3c61062081b7bd843c8fc57607964b2 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:30:07 +0300 Subject: [PATCH 02/42] Check field name patterns for building comment_author for non-name fields --- classes/models/FrmEntryValidate.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 1454be7f6c..ba07f8a080 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -582,7 +582,7 @@ private static function recursive_add_akismet_guest_info( &$datas, $values, $cus $field_id = ! is_null( $custom_index ) ? $custom_index : $index; foreach ( $datas['missing_keys'] as $key_index => $key ) { - $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] ); + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values ); if ( $found ) { $datas[ $key ] = $value; $datas['frm_duplicated'][] = $field_id; @@ -603,7 +603,7 @@ private static function recursive_add_akismet_guest_info( &$datas, $values, $cus * @param array $name_field_ids Name field IDs. * @return bool */ - private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) { + private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) { if ( ! $value || is_numeric( $value ) ) { return false; } @@ -616,6 +616,20 @@ private static function is_akismet_guest_info_value( $key, $value, $field_id, $n return 0 === strpos( $value, 'http' ); case 'comment_author': + if ( ! $name_field_ids ) { + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); + $fields = FrmField::get_all_for_form( $form_id ); + foreach ( $fields as $index => $field ) { + if ( false === stripos( $field->name, 'name' ) ) { + continue; + } + if ( isset( $fields[ $index + 1 ] ) && false !== stripos( $fields[ $index + 1 ]->name, 'last' ) ) { + $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; + return true; + } + } + } + if ( $name_field_ids ) { // If there is name field in the form, we should always use it as author name. return in_array( $field_id, $name_field_ids, true ); From 7fd8c2be51d8f9cebe710ed0cca6d57f53d4e094 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:49:32 +0300 Subject: [PATCH 03/42] Optimize code a bit --- classes/models/FrmEntryValidate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index ba07f8a080..949ed75998 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -618,7 +618,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ case 'comment_author': if ( ! $name_field_ids ) { $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); - $fields = FrmField::get_all_for_form( $form_id ); + $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); foreach ( $fields as $index => $field ) { if ( false === stripos( $field->name, 'name' ) ) { continue; From 2c36fe019e44a00d7b6cb91dcfc8862b9d2041ad Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:53:38 +0300 Subject: [PATCH 04/42] Use strict comparison for field names --- classes/models/FrmEntryValidate.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 949ed75998..54d89f60ab 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -601,6 +601,8 @@ private static function recursive_add_akismet_guest_info( &$datas, $values, $cus * @param string $value Value to check. * @param int $field_id Field ID. * @param array $name_field_ids Name field IDs. + * @param array $values Array of posted values. + * * @return bool */ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) { @@ -620,10 +622,10 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); foreach ( $fields as $index => $field ) { - if ( false === stripos( $field->name, 'name' ) ) { + if ( __( 'Name', 'formidable' ) !== $field->name ) { continue; } - if ( isset( $fields[ $index + 1 ] ) && false !== stripos( $fields[ $index + 1 ]->name, 'last' ) ) { + if ( isset( $fields[ $index + 1 ] ) && __( 'Last', 'formidable' ) === $fields[ $index + 1 ]->name ) { $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; return true; } From ba417494fb3ccb65c38c1592c460285c456d650a Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:59:51 +0300 Subject: [PATCH 05/42] Merge if..else block --- classes/models/FrmEntryValidate.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 54d89f60ab..3fae47ab92 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -630,9 +630,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return true; } } - } - - if ( $name_field_ids ) { + } else { // If there is name field in the form, we should always use it as author name. return in_array( $field_id, $name_field_ids, true ); } From fe5344de1309c77b801428d4f1d173d9b9ee7760 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 27 Aug 2024 15:01:17 +0300 Subject: [PATCH 06/42] Shuffle if..else conditions --- classes/models/FrmEntryValidate.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 3fae47ab92..88300afb4a 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -618,7 +618,10 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return 0 === strpos( $value, 'http' ); case 'comment_author': - if ( ! $name_field_ids ) { + if ( $name_field_ids ) { + // If there is name field in the form, we should always use it as author name. + return in_array( $field_id, $name_field_ids, true ); + } else { $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); foreach ( $fields as $index => $field ) { @@ -630,9 +633,6 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return true; } } - } else { - // If there is name field in the form, we should always use it as author name. - return in_array( $field_id, $name_field_ids, true ); } } From 606906c2bf4825e5890f2463d27c17584f39f79a Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 27 Aug 2024 15:05:08 +0300 Subject: [PATCH 07/42] Correct if condition --- classes/models/FrmEntryValidate.php | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 88300afb4a..7e42b4070d 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -618,20 +618,19 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return 0 === strpos( $value, 'http' ); case 'comment_author': - if ( $name_field_ids ) { + if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { // If there is name field in the form, we should always use it as author name. - return in_array( $field_id, $name_field_ids, true ); - } else { - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); - $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); - foreach ( $fields as $index => $field ) { - if ( __( 'Name', 'formidable' ) !== $field->name ) { - continue; - } - if ( isset( $fields[ $index + 1 ] ) && __( 'Last', 'formidable' ) === $fields[ $index + 1 ]->name ) { - $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; - return true; - } + return true; + } + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); + $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); + foreach ( $fields as $index => $field ) { + if ( __( 'Name', 'formidable' ) !== $field->name ) { + continue; + } + if ( isset( $fields[ $index + 1 ] ) && __( 'Last', 'formidable' ) === $fields[ $index + 1 ]->name ) { + $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; + return true; } } } From c675db97252c2c3c2ae6b21780997c7e992de99e Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 27 Aug 2024 15:07:14 +0300 Subject: [PATCH 08/42] Fix PHPCS error --- classes/models/FrmEntryValidate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 7e42b4070d..448d287b9d 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -633,7 +633,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return true; } } - } + }//end switch return false; } From ac56f10c9b641faed60243cb2670cf6ebf70a46b Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:47:07 +0300 Subject: [PATCH 09/42] Debugging unit test --- classes/models/FrmEntryValidate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 448d287b9d..30026e9391 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -618,7 +618,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return 0 === strpos( $value, 'http' ); case 'comment_author': - if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { + if ( $name_field_ids && in_array( $field_id, $name_field_ids ) ) { // If there is name field in the form, we should always use it as author name. return true; } From 6ca7338907755cab72deb62e2f75294dfd006f93 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:52:57 +0300 Subject: [PATCH 10/42] Debugging unit test --- classes/models/FrmEntryValidate.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 30026e9391..375bc57be7 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -618,7 +618,11 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return 0 === strpos( $value, 'http' ); case 'comment_author': - if ( $name_field_ids && in_array( $field_id, $name_field_ids ) ) { + var_dump( $field_id ); + var_dump( $name_field_ids ); + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); + var_dump( $form_id ); + if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { // If there is name field in the form, we should always use it as author name. return true; } From 493ef348263d55b87b8f092e9aedb895d30e6431 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 10:01:14 +0300 Subject: [PATCH 11/42] Debugging unit test --- classes/models/FrmEntryValidate.php | 31 +++++++++++++++-------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 375bc57be7..7cf1ee38f4 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -622,21 +622,22 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ var_dump( $name_field_ids ); $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); var_dump( $form_id ); - if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { - // If there is name field in the form, we should always use it as author name. - return true; - } - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); - $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); - foreach ( $fields as $index => $field ) { - if ( __( 'Name', 'formidable' ) !== $field->name ) { - continue; - } - if ( isset( $fields[ $index + 1 ] ) && __( 'Last', 'formidable' ) === $fields[ $index + 1 ]->name ) { - $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; - return true; - } - } + return in_array( $field_id, $name_field_ids, true ); + // if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { + // // If there is name field in the form, we should always use it as author name. + // return true; + // } + // $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); + // $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); + // foreach ( $fields as $index => $field ) { + // if ( __( 'Name', 'formidable' ) !== $field->name ) { + // continue; + // } + // if ( isset( $fields[ $index + 1 ] ) && __( 'Last', 'formidable' ) === $fields[ $index + 1 ]->name ) { + // $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; + // return true; + // } + // } }//end switch return false; From 360b89b6d020a456bb8398b4e8efc4c1e25092a3 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 10:05:29 +0300 Subject: [PATCH 12/42] Debugging unit test --- classes/models/FrmEntryValidate.php | 34 ++++++++--------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 7cf1ee38f4..5055060081 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -582,7 +582,7 @@ private static function recursive_add_akismet_guest_info( &$datas, $values, $cus $field_id = ! is_null( $custom_index ) ? $custom_index : $index; foreach ( $datas['missing_keys'] as $key_index => $key ) { - $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values ); + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] ); if ( $found ) { $datas[ $key ] = $value; $datas['frm_duplicated'][] = $field_id; @@ -601,11 +601,9 @@ private static function recursive_add_akismet_guest_info( &$datas, $values, $cus * @param string $value Value to check. * @param int $field_id Field ID. * @param array $name_field_ids Name field IDs. - * @param array $values Array of posted values. - * * @return bool */ - private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) { + private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) { if ( ! $value || is_numeric( $value ) ) { return false; } @@ -618,27 +616,13 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return 0 === strpos( $value, 'http' ); case 'comment_author': - var_dump( $field_id ); - var_dump( $name_field_ids ); - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); - var_dump( $form_id ); - return in_array( $field_id, $name_field_ids, true ); - // if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { - // // If there is name field in the form, we should always use it as author name. - // return true; - // } - // $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); - // $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); - // foreach ( $fields as $index => $field ) { - // if ( __( 'Name', 'formidable' ) !== $field->name ) { - // continue; - // } - // if ( isset( $fields[ $index + 1 ] ) && __( 'Last', 'formidable' ) === $fields[ $index + 1 ]->name ) { - // $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; - // return true; - // } - // } - }//end switch + if ( $name_field_ids ) { + // If there is name field in the form, we should always use it as author name. + return in_array( $field_id, $name_field_ids, true ); + } + // debug + return strlen( $value ) < 200; + } return false; } From 9eab523e40c81640716d22e8dedfeb2489668cc0 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 10:07:11 +0300 Subject: [PATCH 13/42] Debugging unit test --- classes/models/FrmEntryValidate.php | 1 - 1 file changed, 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 5055060081..3b361be9a4 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -621,7 +621,6 @@ private static function is_akismet_guest_info_value( $key, $value, $field_id, $n return in_array( $field_id, $name_field_ids, true ); } // debug - return strlen( $value ) < 200; } return false; From d93a5245f742f7348a94dbf06d0d3c4ccf723cc0 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:05:00 +0300 Subject: [PATCH 14/42] Debug tests --- classes/models/FrmEntryValidate.php | 25 ++++++++++++++----- .../phpunit/entries/test_FrmEntryValidate.php | 4 +-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 3b361be9a4..16efb85089 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -513,6 +513,7 @@ private static function add_user_info_to_akismet( &$datas, $values ) { * @return array */ private static function get_spam_check_user_info( $values ) { + $a = $b; if ( ! is_user_logged_in() ) { return self::get_spam_check_user_info_for_guest( $values ); } @@ -582,7 +583,7 @@ private static function recursive_add_akismet_guest_info( &$datas, $values, $cus $field_id = ! is_null( $custom_index ) ? $custom_index : $index; foreach ( $datas['missing_keys'] as $key_index => $key ) { - $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] ); + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values ); if ( $found ) { $datas[ $key ] = $value; $datas['frm_duplicated'][] = $field_id; @@ -601,9 +602,11 @@ private static function recursive_add_akismet_guest_info( &$datas, $values, $cus * @param string $value Value to check. * @param int $field_id Field ID. * @param array $name_field_ids Name field IDs. + * @param array $values Array of posted values. + * * @return bool */ - private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) { + private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) { if ( ! $value || is_numeric( $value ) ) { return false; } @@ -616,12 +619,22 @@ private static function is_akismet_guest_info_value( $key, $value, $field_id, $n return 0 === strpos( $value, 'http' ); case 'comment_author': - if ( $name_field_ids ) { + if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { // If there is name field in the form, we should always use it as author name. - return in_array( $field_id, $name_field_ids, true ); + return true; } - // debug - } + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); + $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); + foreach ( $fields as $index => $field ) { + if ( __( 'Name', 'formidable' ) !== $field->name ) { + continue; + } + if ( isset( $fields[ $index + 1 ] ) && __( 'Last', 'formidable' ) === $fields[ $index + 1 ]->name ) { + $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; + return true; + } + } + }//end switch return false; } diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index aafe933009..2a01738824 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -37,7 +37,7 @@ public function test_get_spam_check_user_info() { $made_up_name_field_id = 4; $made_up_email_field_id = 12; $made_up_url_field_id = 16; - $test_name = 'Some Guy'; + $test_name = array( 'first' => 'Some', 'last' => 'Guy' ); $test_email = 'amadeupemail@email.com'; $test_url = 'http://madeupwebsite.com'; $values = array( @@ -54,7 +54,7 @@ public function test_get_spam_check_user_info() { $check = $this->get_spam_check_user_info( $values ); $this->assertTrue( empty( $check['user_ID'] ) ); $this->assertTrue( empty( $check['user_id'] ) ); - $this->assertEquals( $test_name, $check['comment_author'] ); + $this->assertEquals( 'Some Guy', $check['comment_author'] ); $this->assertEquals( $test_email, $check['comment_author_email'] ); $this->assertEquals( $test_url, $check['comment_author_url'] ); From 6d2f2eadf6a49c6faa84cead65259d54e936cc21 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:05:35 +0300 Subject: [PATCH 15/42] Debug tests --- classes/models/FrmEntryValidate.php | 1 - 1 file changed, 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 16efb85089..448d287b9d 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -513,7 +513,6 @@ private static function add_user_info_to_akismet( &$datas, $values ) { * @return array */ private static function get_spam_check_user_info( $values ) { - $a = $b; if ( ! is_user_logged_in() ) { return self::get_spam_check_user_info_for_guest( $values ); } From f44134e45c5d062d6d59069af51b21c792155e48 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:08:51 +0300 Subject: [PATCH 16/42] Debugging tests --- classes/models/FrmEntryValidate.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 448d287b9d..3c2eacce6c 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -618,6 +618,8 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return 0 === strpos( $value, 'http' ); case 'comment_author': + var_dump( $field_id ); + var_dump( $name_field_ids ); if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { // If there is name field in the form, we should always use it as author name. return true; From d5b59e7bcf14146d37d7be8bfed527d310759b23 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:16:27 +0300 Subject: [PATCH 17/42] Debugging tests --- classes/models/FrmEntryValidate.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 3c2eacce6c..5cd5009e1c 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -796,18 +796,22 @@ private static function prepare_values_for_spam_check( &$values ) { * @return array Form IDs. */ private static function get_all_form_ids_and_flatten_meta( &$values ) { + var_dump( $values ); $values['name_field_ids'] = array(); // Blacklist check for File field in the old version doesn't contain `form_id`. $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array(); foreach ( $values['item_meta'] as $field_id => $value ) { + var_dump( $value ); if ( ! is_numeric( $field_id ) ) { + var_dump('Existing'); // Maybe `other`. continue; } // Convert name array to string. if ( isset( $value['first'] ) && isset( $value['last'] ) ) { + var_dump('Name field'); $values['item_meta'][ $field_id ] = trim( implode( ' ', $value ) ); $values['name_field_ids'][] = $field_id; continue; From 0fefc9f04e2540cc199714151e0d87f4b3bb4417 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:29:36 +0300 Subject: [PATCH 18/42] Debugging tests --- classes/models/FrmEntryValidate.php | 2 +- .../phpunit/entries/test_FrmEntryValidate.php | 30 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 5cd5009e1c..1a9e47d0ee 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -796,7 +796,7 @@ private static function prepare_values_for_spam_check( &$values ) { * @return array Form IDs. */ private static function get_all_form_ids_and_flatten_meta( &$values ) { - var_dump( $values ); + var_dump('VALUES: '); var_dump( $values ); $values['name_field_ids'] = array(); // Blacklist check for File field in the old version doesn't contain `form_id`. diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index 2a01738824..5d0ce009f9 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -55,8 +55,8 @@ public function test_get_spam_check_user_info() { $this->assertTrue( empty( $check['user_ID'] ) ); $this->assertTrue( empty( $check['user_id'] ) ); $this->assertEquals( 'Some Guy', $check['comment_author'] ); - $this->assertEquals( $test_email, $check['comment_author_email'] ); - $this->assertEquals( $test_url, $check['comment_author_url'] ); + // $this->assertEquals( $test_email, $check['comment_author_email'] ); + // $this->assertEquals( $test_url, $check['comment_author_url'] ); // Test with repeater/embedded field. $values['item_meta'][ $made_up_name_field_id ] = array( @@ -72,19 +72,19 @@ public function test_get_spam_check_user_info() { 'https://someguy.com', ); - $check = $this->get_spam_check_user_info( $values ); - $this->assertEquals( 'John Doe', $check['comment_author'] ); - $this->assertEquals( 'johndoe@gmail.com', $check['comment_author_email'] ); - $this->assertEquals( 'https://johndoe.com', $check['comment_author_url'] ); - - wp_set_current_user( 1 ); - $user = wp_get_current_user(); - $check = $this->get_spam_check_user_info( $values ); - $this->assertEquals( $user->ID, $check['user_ID'] ); - $this->assertEquals( $user->ID, $check['user_id'] ); - $this->assertEquals( $user->display_name, $check['comment_author'] ); - $this->assertEquals( $user->user_email, $check['comment_author_email'] ); - $this->assertEquals( $user->user_url, $check['comment_author_url'] ); + // $check = $this->get_spam_check_user_info( $values ); + // $this->assertEquals( 'John Doe', $check['comment_author'] ); + // $this->assertEquals( 'johndoe@gmail.com', $check['comment_author_email'] ); + // $this->assertEquals( 'https://johndoe.com', $check['comment_author_url'] ); + + // wp_set_current_user( 1 ); + // $user = wp_get_current_user(); + // $check = $this->get_spam_check_user_info( $values ); + // $this->assertEquals( $user->ID, $check['user_ID'] ); + // $this->assertEquals( $user->ID, $check['user_id'] ); + // $this->assertEquals( $user->display_name, $check['comment_author'] ); + // $this->assertEquals( $user->user_email, $check['comment_author_email'] ); + // $this->assertEquals( $user->user_url, $check['comment_author_url'] ); } private function get_spam_check_user_info( $values ) { From 8d85638e82828e5dbc2f312d699c43664e2c6829 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:35:00 +0300 Subject: [PATCH 19/42] Debugging tests --- .../phpunit/entries/test_FrmEntryValidate.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index 5d0ce009f9..6568ec197c 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -59,18 +59,18 @@ public function test_get_spam_check_user_info() { // $this->assertEquals( $test_url, $check['comment_author_url'] ); // Test with repeater/embedded field. - $values['item_meta'][ $made_up_name_field_id ] = array( - 'John Doe', - 'Some Guy', - ); - $values['item_meta'][ $made_up_email_field_id ] = array( - 'johndoe@gmail.com', - 'someguy@gmail.com', - ); - $values['item_meta'][ $made_up_url_field_id ] = array( - 'https://johndoe.com', - 'https://someguy.com', - ); + // $values['item_meta'][ $made_up_name_field_id ] = array( + // 'John Doe', + // 'Some Guy', + // ); + // $values['item_meta'][ $made_up_email_field_id ] = array( + // 'johndoe@gmail.com', + // 'someguy@gmail.com', + // ); + // $values['item_meta'][ $made_up_url_field_id ] = array( + // 'https://johndoe.com', + // 'https://someguy.com', + // ); // $check = $this->get_spam_check_user_info( $values ); // $this->assertEquals( 'John Doe', $check['comment_author'] ); From 16464ce582044cd4a467777e234e56aa76d90ccc Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:40:15 +0300 Subject: [PATCH 20/42] Debugging tests --- .../phpunit/entries/test_FrmEntryValidate.php | 132 +++++++++--------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index 6568ec197c..978d555044 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -94,72 +94,72 @@ private function get_spam_check_user_info( $values ) { ); } - public function test_get_all_form_ids_and_flatten_meta() { - $test_values = array( - 'frm_action' => 'create', - 'form_id' => 1, - 'frm_hide_fields_1' => '', - 'form_key' => 'contact-form', - 'item_meta' => array( - 0 => null, - 1 => array( - 'first' => 'John', - 'last' => 'Doe', - ), - 2 => 'Doe', - 3 => 'johndoe@gmail.com', - 4 => 'Test', - 5 => 'This is a test', - 141 => 'Developer', - 'other' => array( 141 => null ), - 155 => null, - 156 => null, - 163 => array( - 'form' => 17, - 'row_ids' => array( - 0 => 0, - 1 => 1, - ), - 0 => array( - 0 => null, - 162 => 'Option 2', - ), - 1 => array( - 0 => null, - 162 => 'Option 1', - ), - ), - 165 => array( - 'form' => 11, - 'row_ids' => array( 0 => 0 ), - 0 => array( - 0 => null, - 118 => array( - 'first' => 'John', - 'last' => 'Doe', - ), - ), - ), - ), - 'frm_submit_entry_1' => '6e70504545', - '_wp_http_referer' => '/wp-admin/admin-ajax.php?action=frm_forms_preview&form=contact-form', - 'item_key' => '8wl00', - 'frm_verify' => null, - 'frm_state' => 'gfMW/S4I1MCpqXn7OnjXQHLIibJNuRkLkCYpp7MWM7Y=', - ); - - $form_ids = $this->run_private_method( - array( 'FrmEntryValidate', 'get_all_form_ids_and_flatten_meta' ), - array( &$test_values ) - ); - - $this->assertEquals( $form_ids, array( 1, 17, 11 ) ); - $this->assertFalse( isset( $test_values['item_meta'][163] ) ); - $this->assertFalse( isset( $test_values['item_meta'][165] ) ); - $this->assertEquals( $test_values['item_meta'][162], array( 'Option 2', 'Option 1' ) ); - $this->assertEquals( $test_values['item_meta'][118], array( 'John Doe' ) ); - $this->assertEquals( $test_values['item_meta'][1], 'John Doe' ); - } + // public function test_get_all_form_ids_and_flatten_meta() { + // $test_values = array( + // 'frm_action' => 'create', + // 'form_id' => 1, + // 'frm_hide_fields_1' => '', + // 'form_key' => 'contact-form', + // 'item_meta' => array( + // 0 => null, + // 1 => array( + // 'first' => 'John', + // 'last' => 'Doe', + // ), + // 2 => 'Doe', + // 3 => 'johndoe@gmail.com', + // 4 => 'Test', + // 5 => 'This is a test', + // 141 => 'Developer', + // 'other' => array( 141 => null ), + // 155 => null, + // 156 => null, + // 163 => array( + // 'form' => 17, + // 'row_ids' => array( + // 0 => 0, + // 1 => 1, + // ), + // 0 => array( + // 0 => null, + // 162 => 'Option 2', + // ), + // 1 => array( + // 0 => null, + // 162 => 'Option 1', + // ), + // ), + // 165 => array( + // 'form' => 11, + // 'row_ids' => array( 0 => 0 ), + // 0 => array( + // 0 => null, + // 118 => array( + // 'first' => 'John', + // 'last' => 'Doe', + // ), + // ), + // ), + // ), + // 'frm_submit_entry_1' => '6e70504545', + // '_wp_http_referer' => '/wp-admin/admin-ajax.php?action=frm_forms_preview&form=contact-form', + // 'item_key' => '8wl00', + // 'frm_verify' => null, + // 'frm_state' => 'gfMW/S4I1MCpqXn7OnjXQHLIibJNuRkLkCYpp7MWM7Y=', + // ); + + // $form_ids = $this->run_private_method( + // array( 'FrmEntryValidate', 'get_all_form_ids_and_flatten_meta' ), + // array( &$test_values ) + // ); + + // $this->assertEquals( $form_ids, array( 1, 17, 11 ) ); + // $this->assertFalse( isset( $test_values['item_meta'][163] ) ); + // $this->assertFalse( isset( $test_values['item_meta'][165] ) ); + // $this->assertEquals( $test_values['item_meta'][162], array( 'Option 2', 'Option 1' ) ); + // $this->assertEquals( $test_values['item_meta'][118], array( 'John Doe' ) ); + // $this->assertEquals( $test_values['item_meta'][1], 'John Doe' ); + // } public function test_skip_adding_values_to_akismet() { $form = $this->factory->form->create_and_get(); From e9d64aa7fba2faa29a625dddf969b64193e0a6b6 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:43:17 +0300 Subject: [PATCH 21/42] Debugging tests --- .../phpunit/entries/test_FrmEntryValidate.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index 978d555044..af0b0f0406 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -10,24 +10,24 @@ class test_FrmEntryValidate extends FrmUnitTest { * @covers FrmEntryValidate::validate */ public function test_validate() { - $add_a_custom_error = function ( $errors ) { - $errors['custom_error'] = 'Error message'; - return $errors; - }; + // $add_a_custom_error = function ( $errors ) { + // $errors['custom_error'] = 'Error message'; + // return $errors; + // }; - add_filter( 'frm_validate_entry', $add_a_custom_error ); + // add_filter( 'frm_validate_entry', $add_a_custom_error ); - $values = array( - 'form_id' => 1, - 'item_meta' => array(), - ); - $errors = FrmEntryValidate::validate( $values ); - $this->assertIsArray( $errors ); + // $values = array( + // 'form_id' => 1, + // 'item_meta' => array(), + // ); + // $errors = FrmEntryValidate::validate( $values ); + // $this->assertIsArray( $errors ); - $this->assertArrayHasKey( 'custom_error', $errors ); - $this->assertEquals( 'Error message', $errors['custom_error'] ); + // $this->assertArrayHasKey( 'custom_error', $errors ); + // $this->assertEquals( 'Error message', $errors['custom_error'] ); - remove_filter( 'frm_validate_entry', $add_a_custom_error ); + // remove_filter( 'frm_validate_entry', $add_a_custom_error ); } /** From c6c0d8221e1d85e3eeed6bb8db847729aa85ddbd Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:55:43 +0300 Subject: [PATCH 22/42] Prepare values for spam check --- classes/models/FrmEntryValidate.php | 3 +- .../phpunit/entries/test_FrmEntryValidate.php | 217 +++++++++--------- 2 files changed, 111 insertions(+), 109 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 1a9e47d0ee..50919915de 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -514,6 +514,7 @@ private static function add_user_info_to_akismet( &$datas, $values ) { */ private static function get_spam_check_user_info( $values ) { if ( ! is_user_logged_in() ) { + var_dump('Guest'); return self::get_spam_check_user_info_for_guest( $values ); } @@ -802,7 +803,7 @@ private static function get_all_form_ids_and_flatten_meta( &$values ) { // Blacklist check for File field in the old version doesn't contain `form_id`. $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array(); foreach ( $values['item_meta'] as $field_id => $value ) { - var_dump( $value ); + var_dump('VALUE:');var_dump( $value ); if ( ! is_numeric( $field_id ) ) { var_dump('Existing'); // Maybe `other`. diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index af0b0f0406..6250061b62 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -10,24 +10,24 @@ class test_FrmEntryValidate extends FrmUnitTest { * @covers FrmEntryValidate::validate */ public function test_validate() { - // $add_a_custom_error = function ( $errors ) { - // $errors['custom_error'] = 'Error message'; - // return $errors; - // }; + $add_a_custom_error = function ( $errors ) { + $errors['custom_error'] = 'Error message'; + return $errors; + }; - // add_filter( 'frm_validate_entry', $add_a_custom_error ); + add_filter( 'frm_validate_entry', $add_a_custom_error ); - // $values = array( - // 'form_id' => 1, - // 'item_meta' => array(), - // ); - // $errors = FrmEntryValidate::validate( $values ); - // $this->assertIsArray( $errors ); + $values = array( + 'form_id' => 1, + 'item_meta' => array(), + ); + $errors = FrmEntryValidate::validate( $values ); + $this->assertIsArray( $errors ); - // $this->assertArrayHasKey( 'custom_error', $errors ); - // $this->assertEquals( 'Error message', $errors['custom_error'] ); + $this->assertArrayHasKey( 'custom_error', $errors ); + $this->assertEquals( 'Error message', $errors['custom_error'] ); - // remove_filter( 'frm_validate_entry', $add_a_custom_error ); + remove_filter( 'frm_validate_entry', $add_a_custom_error ); } /** @@ -51,40 +51,41 @@ public function test_get_spam_check_user_info() { ); wp_set_current_user( null ); + $this->run_private_method( array( 'FrmEntryValidate', 'prepare_values_for_spam_check' ), array( &$values ) ); $check = $this->get_spam_check_user_info( $values ); $this->assertTrue( empty( $check['user_ID'] ) ); $this->assertTrue( empty( $check['user_id'] ) ); $this->assertEquals( 'Some Guy', $check['comment_author'] ); - // $this->assertEquals( $test_email, $check['comment_author_email'] ); - // $this->assertEquals( $test_url, $check['comment_author_url'] ); + $this->assertEquals( $test_email, $check['comment_author_email'] ); + $this->assertEquals( $test_url, $check['comment_author_url'] ); // Test with repeater/embedded field. - // $values['item_meta'][ $made_up_name_field_id ] = array( - // 'John Doe', - // 'Some Guy', - // ); - // $values['item_meta'][ $made_up_email_field_id ] = array( - // 'johndoe@gmail.com', - // 'someguy@gmail.com', - // ); - // $values['item_meta'][ $made_up_url_field_id ] = array( - // 'https://johndoe.com', - // 'https://someguy.com', - // ); - - // $check = $this->get_spam_check_user_info( $values ); - // $this->assertEquals( 'John Doe', $check['comment_author'] ); - // $this->assertEquals( 'johndoe@gmail.com', $check['comment_author_email'] ); - // $this->assertEquals( 'https://johndoe.com', $check['comment_author_url'] ); - - // wp_set_current_user( 1 ); - // $user = wp_get_current_user(); - // $check = $this->get_spam_check_user_info( $values ); - // $this->assertEquals( $user->ID, $check['user_ID'] ); - // $this->assertEquals( $user->ID, $check['user_id'] ); - // $this->assertEquals( $user->display_name, $check['comment_author'] ); - // $this->assertEquals( $user->user_email, $check['comment_author_email'] ); - // $this->assertEquals( $user->user_url, $check['comment_author_url'] ); + $values['item_meta'][ $made_up_name_field_id ] = array( + 'John Doe', + 'Some Guy', + ); + $values['item_meta'][ $made_up_email_field_id ] = array( + 'johndoe@gmail.com', + 'someguy@gmail.com', + ); + $values['item_meta'][ $made_up_url_field_id ] = array( + 'https://johndoe.com', + 'https://someguy.com', + ); + + $check = $this->get_spam_check_user_info( $values ); + $this->assertEquals( 'John Doe', $check['comment_author'] ); + $this->assertEquals( 'johndoe@gmail.com', $check['comment_author_email'] ); + $this->assertEquals( 'https://johndoe.com', $check['comment_author_url'] ); + + wp_set_current_user( 1 ); + $user = wp_get_current_user(); + $check = $this->get_spam_check_user_info( $values ); + $this->assertEquals( $user->ID, $check['user_ID'] ); + $this->assertEquals( $user->ID, $check['user_id'] ); + $this->assertEquals( $user->display_name, $check['comment_author'] ); + $this->assertEquals( $user->user_email, $check['comment_author_email'] ); + $this->assertEquals( $user->user_url, $check['comment_author_url'] ); } private function get_spam_check_user_info( $values ) { @@ -94,72 +95,72 @@ private function get_spam_check_user_info( $values ) { ); } - // public function test_get_all_form_ids_and_flatten_meta() { - // $test_values = array( - // 'frm_action' => 'create', - // 'form_id' => 1, - // 'frm_hide_fields_1' => '', - // 'form_key' => 'contact-form', - // 'item_meta' => array( - // 0 => null, - // 1 => array( - // 'first' => 'John', - // 'last' => 'Doe', - // ), - // 2 => 'Doe', - // 3 => 'johndoe@gmail.com', - // 4 => 'Test', - // 5 => 'This is a test', - // 141 => 'Developer', - // 'other' => array( 141 => null ), - // 155 => null, - // 156 => null, - // 163 => array( - // 'form' => 17, - // 'row_ids' => array( - // 0 => 0, - // 1 => 1, - // ), - // 0 => array( - // 0 => null, - // 162 => 'Option 2', - // ), - // 1 => array( - // 0 => null, - // 162 => 'Option 1', - // ), - // ), - // 165 => array( - // 'form' => 11, - // 'row_ids' => array( 0 => 0 ), - // 0 => array( - // 0 => null, - // 118 => array( - // 'first' => 'John', - // 'last' => 'Doe', - // ), - // ), - // ), - // ), - // 'frm_submit_entry_1' => '6e70504545', - // '_wp_http_referer' => '/wp-admin/admin-ajax.php?action=frm_forms_preview&form=contact-form', - // 'item_key' => '8wl00', - // 'frm_verify' => null, - // 'frm_state' => 'gfMW/S4I1MCpqXn7OnjXQHLIibJNuRkLkCYpp7MWM7Y=', - // ); - - // $form_ids = $this->run_private_method( - // array( 'FrmEntryValidate', 'get_all_form_ids_and_flatten_meta' ), - // array( &$test_values ) - // ); - - // $this->assertEquals( $form_ids, array( 1, 17, 11 ) ); - // $this->assertFalse( isset( $test_values['item_meta'][163] ) ); - // $this->assertFalse( isset( $test_values['item_meta'][165] ) ); - // $this->assertEquals( $test_values['item_meta'][162], array( 'Option 2', 'Option 1' ) ); - // $this->assertEquals( $test_values['item_meta'][118], array( 'John Doe' ) ); - // $this->assertEquals( $test_values['item_meta'][1], 'John Doe' ); - // } + public function test_get_all_form_ids_and_flatten_meta() { + $test_values = array( + 'frm_action' => 'create', + 'form_id' => 1, + 'frm_hide_fields_1' => '', + 'form_key' => 'contact-form', + 'item_meta' => array( + 0 => null, + 1 => array( + 'first' => 'John', + 'last' => 'Doe', + ), + 2 => 'Doe', + 3 => 'johndoe@gmail.com', + 4 => 'Test', + 5 => 'This is a test', + 141 => 'Developer', + 'other' => array( 141 => null ), + 155 => null, + 156 => null, + 163 => array( + 'form' => 17, + 'row_ids' => array( + 0 => 0, + 1 => 1, + ), + 0 => array( + 0 => null, + 162 => 'Option 2', + ), + 1 => array( + 0 => null, + 162 => 'Option 1', + ), + ), + 165 => array( + 'form' => 11, + 'row_ids' => array( 0 => 0 ), + 0 => array( + 0 => null, + 118 => array( + 'first' => 'John', + 'last' => 'Doe', + ), + ), + ), + ), + 'frm_submit_entry_1' => '6e70504545', + '_wp_http_referer' => '/wp-admin/admin-ajax.php?action=frm_forms_preview&form=contact-form', + 'item_key' => '8wl00', + 'frm_verify' => null, + 'frm_state' => 'gfMW/S4I1MCpqXn7OnjXQHLIibJNuRkLkCYpp7MWM7Y=', + ); + + $form_ids = $this->run_private_method( + array( 'FrmEntryValidate', 'get_all_form_ids_and_flatten_meta' ), + array( &$test_values ) + ); + + $this->assertEquals( $form_ids, array( 1, 17, 11 ) ); + $this->assertFalse( isset( $test_values['item_meta'][163] ) ); + $this->assertFalse( isset( $test_values['item_meta'][165] ) ); + $this->assertEquals( $test_values['item_meta'][162], array( 'Option 2', 'Option 1' ) ); + $this->assertEquals( $test_values['item_meta'][118], array( 'John Doe' ) ); + $this->assertEquals( $test_values['item_meta'][1], 'John Doe' ); + } public function test_skip_adding_values_to_akismet() { $form = $this->factory->form->create_and_get(); From a510a8632927cfa405875a107b692cb5f2914fd6 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:02:43 +0300 Subject: [PATCH 23/42] Remove debugging lines --- classes/models/FrmEntryValidate.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 50919915de..2a4d14d336 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -514,7 +514,6 @@ private static function add_user_info_to_akismet( &$datas, $values ) { */ private static function get_spam_check_user_info( $values ) { if ( ! is_user_logged_in() ) { - var_dump('Guest'); return self::get_spam_check_user_info_for_guest( $values ); } @@ -619,8 +618,6 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return 0 === strpos( $value, 'http' ); case 'comment_author': - var_dump( $field_id ); - var_dump( $name_field_ids ); if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { // If there is name field in the form, we should always use it as author name. return true; @@ -797,13 +794,11 @@ private static function prepare_values_for_spam_check( &$values ) { * @return array Form IDs. */ private static function get_all_form_ids_and_flatten_meta( &$values ) { - var_dump('VALUES: '); var_dump( $values ); $values['name_field_ids'] = array(); // Blacklist check for File field in the old version doesn't contain `form_id`. $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array(); foreach ( $values['item_meta'] as $field_id => $value ) { - var_dump('VALUE:');var_dump( $value ); if ( ! is_numeric( $field_id ) ) { var_dump('Existing'); // Maybe `other`. @@ -812,7 +807,6 @@ private static function get_all_form_ids_and_flatten_meta( &$values ) { // Convert name array to string. if ( isset( $value['first'] ) && isset( $value['last'] ) ) { - var_dump('Name field'); $values['item_meta'][ $field_id ] = trim( implode( ' ', $value ) ); $values['name_field_ids'][] = $field_id; continue; From 8eaa2f9ecd8549a0aff4ae461f05273dd2b83860 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:03:23 +0300 Subject: [PATCH 24/42] Remove debugging lines --- classes/models/FrmEntryValidate.php | 1 - 1 file changed, 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 2a4d14d336..448d287b9d 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -800,7 +800,6 @@ private static function get_all_form_ids_and_flatten_meta( &$values ) { $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array(); foreach ( $values['item_meta'] as $field_id => $value ) { if ( ! is_numeric( $field_id ) ) { - var_dump('Existing'); // Maybe `other`. continue; } From 77da237b879e4dae2aaf0b5f7d1fedae8b867d8d Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:07:53 +0300 Subject: [PATCH 25/42] Add assertion to cover field name pattern to build the comment author --- .../phpunit/entries/test_FrmEntryValidate.php | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index 6250061b62..155ebf89c7 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -59,6 +59,38 @@ public function test_get_spam_check_user_info() { $this->assertEquals( $test_email, $check['comment_author_email'] ); $this->assertEquals( $test_url, $check['comment_author_url'] ); + $form_id = $this->factory->form->create(); + $first_name_id = $this->factory->field->create( + array( + 'type' => 'text', + 'form_id' => $form_id, + 'name' => 'Name', + ) + ); + $last_name_id = $this->factory->field->create( + array( + 'type' => 'text', + 'form_id' => $form_id, + 'name' => 'Last', + ) + ); + + $values = array( + 'item_meta' => array( + 0 => '', + $first_name_id => 'John', + $last_name_id => 'Doe', + $made_up_email_field_id => $test_email, + $made_up_url_field_id => $test_url, + ), + 'name_field_ids' => array(), + ); + $_POST['form_id'] = $form_id; + $this->run_private_method( array( 'FrmEntryValidate', 'prepare_values_for_spam_check' ), array( &$values ) ); + + $check = $this->get_spam_check_user_info( $values );; + $this->assertEquals( 'John Doe', $check['comment_author'] ); + // Test with repeater/embedded field. $values['item_meta'][ $made_up_name_field_id ] = array( 'John Doe', @@ -89,10 +121,11 @@ public function test_get_spam_check_user_info() { } private function get_spam_check_user_info( $values ) { - return $this->run_private_method( - array( 'FrmEntryValidate', 'get_spam_check_user_info' ), - array( $values ) - ); + return FrmEntryValidate::get_spam_check_user_info( $values ); + // return $this->run_private_method( + // array( 'FrmEntryValidate', 'get_spam_check_user_info' ), + // array( $values ) + // ); } public function test_get_all_form_ids_and_flatten_meta() { From a7a1e8792b5fe53f48d7e79231bded5912022294 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:10:51 +0300 Subject: [PATCH 26/42] Add comment to clarify a test section --- tests/phpunit/entries/test_FrmEntryValidate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index 155ebf89c7..b6895b6e5d 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -59,6 +59,7 @@ public function test_get_spam_check_user_info() { $this->assertEquals( $test_email, $check['comment_author_email'] ); $this->assertEquals( $test_url, $check['comment_author_url'] ); + // Test "Name" + "Last" field name pattern to build the comment_author $form_id = $this->factory->form->create(); $first_name_id = $this->factory->field->create( array( @@ -87,7 +88,6 @@ public function test_get_spam_check_user_info() { ); $_POST['form_id'] = $form_id; $this->run_private_method( array( 'FrmEntryValidate', 'prepare_values_for_spam_check' ), array( &$values ) ); - $check = $this->get_spam_check_user_info( $values );; $this->assertEquals( 'John Doe', $check['comment_author'] ); From 9acf225b2135426e16a596236c260e00b11d8fcd Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:11:17 +0300 Subject: [PATCH 27/42] Uncomment original code --- tests/phpunit/entries/test_FrmEntryValidate.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index b6895b6e5d..142c92b4aa 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -121,11 +121,10 @@ public function test_get_spam_check_user_info() { } private function get_spam_check_user_info( $values ) { - return FrmEntryValidate::get_spam_check_user_info( $values ); - // return $this->run_private_method( - // array( 'FrmEntryValidate', 'get_spam_check_user_info' ), - // array( $values ) - // ); + return $this->run_private_method( + array( 'FrmEntryValidate', 'get_spam_check_user_info' ), + array( $values ) + ); } public function test_get_all_form_ids_and_flatten_meta() { From 85c7ac9c54b1d35f580f6fcb9aef2e18edd0651a Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:15:47 +0300 Subject: [PATCH 28/42] Fix PHPCS errors --- tests/phpunit/entries/test_FrmEntryValidate.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index 142c92b4aa..d3daf2dd02 100644 --- a/tests/phpunit/entries/test_FrmEntryValidate.php +++ b/tests/phpunit/entries/test_FrmEntryValidate.php @@ -37,7 +37,10 @@ public function test_get_spam_check_user_info() { $made_up_name_field_id = 4; $made_up_email_field_id = 12; $made_up_url_field_id = 16; - $test_name = array( 'first' => 'Some', 'last' => 'Guy' ); + $test_name = array( + 'first' => 'Some', + 'last' => 'Guy', + ); $test_email = 'amadeupemail@email.com'; $test_url = 'http://madeupwebsite.com'; $values = array( @@ -76,8 +79,8 @@ public function test_get_spam_check_user_info() { ) ); - $values = array( - 'item_meta' => array( + $values = array( + 'item_meta' => array( 0 => '', $first_name_id => 'John', $last_name_id => 'Doe', @@ -88,7 +91,7 @@ public function test_get_spam_check_user_info() { ); $_POST['form_id'] = $form_id; $this->run_private_method( array( 'FrmEntryValidate', 'prepare_values_for_spam_check' ), array( &$values ) ); - $check = $this->get_spam_check_user_info( $values );; + $check = $this->get_spam_check_user_info( $values ); $this->assertEquals( 'John Doe', $check['comment_author'] ); // Test with repeater/embedded field. From 3b8b66333f9e39a0d132bb5d198d138f76f7cb88 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:42:40 +0300 Subject: [PATCH 29/42] Avoid translating field names when comparing with expected strings --- classes/models/FrmEntryValidate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 448d287b9d..7de49d8aac 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -625,10 +625,10 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); foreach ( $fields as $index => $field ) { - if ( __( 'Name', 'formidable' ) !== $field->name ) { + if ( 'Name' !== $field->name ) { continue; } - if ( isset( $fields[ $index + 1 ] ) && __( 'Last', 'formidable' ) === $fields[ $index + 1 ]->name ) { + if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; return true; } From e6afde79938b60ad4808b1ca71f90ab340ada6fd Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 19 Sep 2024 06:36:01 +0300 Subject: [PATCH 30/42] Add a guard for array element --- classes/models/FrmEntryValidate.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 7de49d8aac..bec9594d1b 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -629,6 +629,9 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ continue; } if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { + if ( empty( $values[ $fields[ $index + 1 ]->id ] ) ) { + continue; + } $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; return true; } From ffbae2b227df805aff1a0441f8f3aa7e151f4b27 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 19 Sep 2024 06:40:47 +0300 Subject: [PATCH 31/42] Add a guard for array element --- classes/models/FrmEntryValidate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index bec9594d1b..e3f610341c 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -629,7 +629,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ continue; } if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { - if ( empty( $values[ $fields[ $index + 1 ]->id ] ) ) { + if ( empty( $values[ $fields[ $index + 1 ] ] ) ) { continue; } $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; From 5759e74e9a41a82909cd6b3450f85982d385470e Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 19 Sep 2024 09:26:30 +0300 Subject: [PATCH 32/42] Add a guard for array element --- classes/models/FrmEntryValidate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index e3f610341c..bec9594d1b 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -629,7 +629,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ continue; } if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { - if ( empty( $values[ $fields[ $index + 1 ] ] ) ) { + if ( empty( $values[ $fields[ $index + 1 ]->id ] ) ) { continue; } $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; From bb1c286fdc65d0b6da6edb22c4dbe3d3030c5067 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 19 Sep 2024 09:30:32 +0300 Subject: [PATCH 33/42] Add a guard for array element --- classes/models/FrmEntryValidate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index bec9594d1b..e3f610341c 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -629,7 +629,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ continue; } if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { - if ( empty( $values[ $fields[ $index + 1 ]->id ] ) ) { + if ( empty( $values[ $fields[ $index + 1 ] ] ) ) { continue; } $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; From 00b0268d4989f6b937c01861a9398110591331c7 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 19 Sep 2024 09:34:47 +0300 Subject: [PATCH 34/42] Add a guard for array element --- classes/models/FrmEntryValidate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index e3f610341c..30dd886e17 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -629,7 +629,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ continue; } if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { - if ( empty( $values[ $fields[ $index + 1 ] ] ) ) { + if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) { continue; } $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; From ce73c3d5555c6e97dc0ff3bad2356fba238d4200 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 24 Sep 2024 09:04:59 +0300 Subject: [PATCH 35/42] Limit fields queried to text fields --- classes/models/FrmEntryValidate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 30dd886e17..319a88fb35 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -623,7 +623,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return true; } $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); - $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); + $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id, 'type' => 'text' ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); foreach ( $fields as $index => $field ) { if ( 'Name' !== $field->name ) { continue; From 973bef1b6de73fc233c9091225178e230e79088e Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 24 Sep 2024 09:11:40 +0300 Subject: [PATCH 36/42] Fix PHPCS error --- classes/models/FrmEntryValidate.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 7293b5ffa2..13b6136a21 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -624,7 +624,13 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return true; } $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); - $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id, 'type' => 'text' ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); + $fields = FrmDb::get_results( 'frm_fields', array( + 'form_id' => $form_id, + 'type' => 'text', + ), + 'id,name', + array( 'order_by' => 'field_order ASC' ) + ); foreach ( $fields as $index => $field ) { if ( 'Name' !== $field->name ) { continue; From f6ab3e11ee83777997dfb780bb36873d5b741784 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 24 Sep 2024 09:13:11 +0300 Subject: [PATCH 37/42] Fix PHPCS errors --- classes/models/FrmEntryValidate.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 13b6136a21..2014caceb1 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -624,13 +624,16 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return true; } $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); - $fields = FrmDb::get_results( 'frm_fields', array( + $fields = FrmDb::get_results( + 'frm_fields', + array( 'form_id' => $form_id, 'type' => 'text', ), 'id,name', array( 'order_by' => 'field_order ASC' ) ); + foreach ( $fields as $index => $field ) { if ( 'Name' !== $field->name ) { continue; From 306333679b52c50f3f7dad8b44e36a0cc7fad4b0 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 14 Oct 2024 10:16:31 +0300 Subject: [PATCH 38/42] Filter 'name' column to 'Name' or 'Last' in fields query --- classes/models/FrmEntryValidate.php | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 2014caceb1..4f7c265f02 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -629,6 +629,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ array( 'form_id' => $form_id, 'type' => 'text', + 'name' => array( 'Name', 'Last' ), ), 'id,name', array( 'order_by' => 'field_order ASC' ) From 9da51cb23bebe1d570971ffda0b5ab652fd79ae0 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:26:39 +0300 Subject: [PATCH 39/42] Store fields query in a static variable --- classes/models/FrmEntryValidate.php | 45 ++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 4f7c265f02..661178e102 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -5,6 +5,13 @@ class FrmEntryValidate { + /** + * @since x.x + * + * @var array|null + */ + private static $name_text_fields; + /** * @param array $values * @param bool|string[] $exclude @@ -624,16 +631,7 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return true; } $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); - $fields = FrmDb::get_results( - 'frm_fields', - array( - 'form_id' => $form_id, - 'type' => 'text', - 'name' => array( 'Name', 'Last' ), - ), - 'id,name', - array( 'order_by' => 'field_order ASC' ) - ); + $fields = self::get_name_text_fields( $form_id ); foreach ( $fields as $index => $field ) { if ( 'Name' !== $field->name ) { @@ -652,6 +650,33 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ return false; } + /** + * Returns fields that have 'Name' and 'Last' as their name. + * + * @since x.x + * + * @param int $form_id + * @return array + */ + private static function get_name_text_fields( $form_id ) { + if ( null !== self::$name_text_fields ) { + return self::$name_text_fields; + } + $fields = FrmDb::get_results( + 'frm_fields', + array( + 'form_id' => $form_id, + 'type' => 'text', + 'name' => array( 'Name', 'Last' ), + ), + 'id,name', + array( 'order_by' => 'field_order ASC' ) + ); + + self::$name_text_fields = $fields; + return $fields; + } + private static function add_server_values_to_akismet( &$datas ) { foreach ( $_SERVER as $key => $value ) { $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key ); From 9ec1b4e7f454625179281f66f31a9bff1c59b5fe Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 19 Nov 2024 12:45:31 +0300 Subject: [PATCH 40/42] Avoid extra variable --- classes/models/FrmEntryValidate.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 661178e102..9791ced556 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -662,7 +662,7 @@ private static function get_name_text_fields( $form_id ) { if ( null !== self::$name_text_fields ) { return self::$name_text_fields; } - $fields = FrmDb::get_results( + self::$name_text_fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id, @@ -673,8 +673,7 @@ private static function get_name_text_fields( $form_id ) { array( 'order_by' => 'field_order ASC' ) ); - self::$name_text_fields = $fields; - return $fields; + return self::$name_text_fields; } private static function add_server_values_to_akismet( &$datas ) { From c6f9905ddb55e403bf161a1b6787d8d8df83b228 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 19 Nov 2024 13:13:41 +0300 Subject: [PATCH 41/42] Support multiple form submission per page --- classes/models/FrmEntryValidate.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 9791ced556..4b8b7f33f9 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -659,10 +659,13 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ * @return array */ private static function get_name_text_fields( $form_id ) { - if ( null !== self::$name_text_fields ) { - return self::$name_text_fields; + if ( null !== self::$name_text_fields && isset( self::$name_text_fields[ $form_id ] ) ) { + return self::$name_text_fields[ $form_id ]; } - self::$name_text_fields = FrmDb::get_results( + if ( null === self::$name_text_fields ) { + self::$name_text_fields = array(); + } + self::$name_text_fields[ $form_id ] = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form_id, @@ -673,7 +676,7 @@ private static function get_name_text_fields( $form_id ) { array( 'order_by' => 'field_order ASC' ) ); - return self::$name_text_fields; + return self::$name_text_fields[ $form_id ]; } private static function add_server_values_to_akismet( &$datas ) { From 5dc0d7edfe5ac4a7b95bdd211bd7236dcd360346 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 19 Nov 2024 13:25:31 +0300 Subject: [PATCH 42/42] Make code more readable --- classes/models/FrmEntryValidate.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 4b8b7f33f9..998e5a07f3 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -659,10 +659,11 @@ private static function is_akismet_guest_info_value( $key, &$value, $field_id, $ * @return array */ private static function get_name_text_fields( $form_id ) { - if ( null !== self::$name_text_fields && isset( self::$name_text_fields[ $form_id ] ) ) { + $name_text_fields_is_initialized = is_array( self::$name_text_fields ); + if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) { return self::$name_text_fields[ $form_id ]; } - if ( null === self::$name_text_fields ) { + if ( ! $name_text_fields_is_initialized ) { self::$name_text_fields = array(); } self::$name_text_fields[ $form_id ] = FrmDb::get_results(