diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index ac517f2d91..998e5a07f3 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 @@ -583,7 +590,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; @@ -602,9 +609,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; } @@ -617,16 +626,60 @@ 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; } - return strlen( $value ) < 200; - } + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); + $fields = self::get_name_text_fields( $form_id ); + + foreach ( $fields as $index => $field ) { + if ( 'Name' !== $field->name ) { + continue; + } + if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { + if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) { + continue; + } + $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; + return true; + } + } + }//end switch 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 ) { + $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 ( ! $name_text_fields_is_initialized ) { + self::$name_text_fields = array(); + } + self::$name_text_fields[ $form_id ] = FrmDb::get_results( + 'frm_fields', + array( + 'form_id' => $form_id, + 'type' => 'text', + 'name' => array( 'Name', 'Last' ), + ), + 'id,name', + array( 'order_by' => 'field_order ASC' ) + ); + + return self::$name_text_fields[ $form_id ]; + } + 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 ); diff --git a/tests/phpunit/entries/test_FrmEntryValidate.php b/tests/phpunit/entries/test_FrmEntryValidate.php index aafe933009..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 = 'Some Guy'; + $test_name = array( + 'first' => 'Some', + 'last' => 'Guy', + ); $test_email = 'amadeupemail@email.com'; $test_url = 'http://madeupwebsite.com'; $values = array( @@ -51,13 +54,46 @@ 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( $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'] ); + // 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( + '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',