Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict comment_author to name fields #1945

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
a270b79
Restrict comment_author to name fields
AbdiTolesa Aug 23, 2024
036c38f
Check field name patterns for building comment_author for non-name fi…
AbdiTolesa Aug 27, 2024
7fd8c2b
Optimize code a bit
AbdiTolesa Aug 27, 2024
2c36fe0
Use strict comparison for field names
AbdiTolesa Aug 27, 2024
ba41749
Merge if..else block
AbdiTolesa Aug 27, 2024
fe5344d
Shuffle if..else conditions
AbdiTolesa Aug 27, 2024
606906c
Correct if condition
AbdiTolesa Aug 27, 2024
c675db9
Fix PHPCS error
AbdiTolesa Aug 27, 2024
ac56f10
Debugging unit test
AbdiTolesa Aug 28, 2024
6ca7338
Debugging unit test
AbdiTolesa Aug 28, 2024
493ef34
Debugging unit test
AbdiTolesa Aug 28, 2024
360b89b
Debugging unit test
AbdiTolesa Aug 28, 2024
9eab523
Debugging unit test
AbdiTolesa Aug 28, 2024
d93a524
Debug tests
AbdiTolesa Aug 28, 2024
6d2f2ea
Debug tests
AbdiTolesa Aug 28, 2024
f44134e
Debugging tests
AbdiTolesa Aug 28, 2024
d5b59e7
Debugging tests
AbdiTolesa Aug 28, 2024
0fefc9f
Debugging tests
AbdiTolesa Aug 28, 2024
8d85638
Debugging tests
AbdiTolesa Aug 28, 2024
16464ce
Debugging tests
AbdiTolesa Aug 28, 2024
e9d64aa
Debugging tests
AbdiTolesa Aug 28, 2024
c6c0d82
Prepare values for spam check
AbdiTolesa Aug 28, 2024
a510a86
Remove debugging lines
AbdiTolesa Aug 28, 2024
8eaa2f9
Remove debugging lines
AbdiTolesa Aug 28, 2024
77da237
Add assertion to cover field name pattern to build the comment author
AbdiTolesa Aug 29, 2024
a7a1e87
Add comment to clarify a test section
AbdiTolesa Aug 29, 2024
9acf225
Uncomment original code
AbdiTolesa Aug 29, 2024
85c7ac9
Fix PHPCS errors
AbdiTolesa Aug 29, 2024
3b8b663
Avoid translating field names when comparing with expected strings
AbdiTolesa Aug 29, 2024
e6afde7
Add a guard for array element
AbdiTolesa Sep 19, 2024
ffbae2b
Add a guard for array element
AbdiTolesa Sep 19, 2024
5759e74
Add a guard for array element
AbdiTolesa Sep 19, 2024
bb1c286
Add a guard for array element
AbdiTolesa Sep 19, 2024
00b0268
Add a guard for array element
AbdiTolesa Sep 19, 2024
0125fea
Merge branch 'master' into issue-1781-restrict_comment_author_to_name…
Crabcyborg Sep 23, 2024
ce73c3d
Limit fields queried to text fields
AbdiTolesa Sep 24, 2024
4c94f7d
Merge branch 'issue-1781-restrict_comment_author_to_name_field' of ht…
AbdiTolesa Sep 24, 2024
973bef1
Fix PHPCS error
AbdiTolesa Sep 24, 2024
f6ab3e1
Fix PHPCS errors
AbdiTolesa Sep 24, 2024
3063336
Filter 'name' column to 'Name' or 'Last' in fields query
AbdiTolesa Oct 14, 2024
af9e1fb
Merge branch 'master' into issue-1781-restrict_comment_author_to_name…
Crabcyborg Oct 16, 2024
9da51cb
Store fields query in a static variable
AbdiTolesa Oct 18, 2024
8391831
Merge branch 'issue-1781-restrict_comment_author_to_name_field' of ht…
AbdiTolesa Oct 18, 2024
9ec1b4e
Avoid extra variable
AbdiTolesa Nov 19, 2024
c6f9905
Support multiple form submission per page
AbdiTolesa Nov 19, 2024
5dc0d7e
Make code more readable
AbdiTolesa Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions classes/models/FrmEntryValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

class FrmEntryValidate {

/**
* @since x.x
*
* @var array|null
*/
private static $name_text_fields;
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param array $values
* @param bool|string[] $exclude
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -617,16 +626,56 @@ 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;
Crabcyborg marked this conversation as resolved.
Show resolved Hide resolved
}
$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 ];
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved
Crabcyborg marked this conversation as resolved.
Show resolved Hide resolved
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 ) {
if ( null !== self::$name_text_fields ) {
return self::$name_text_fields;
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved
}
self::$name_text_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' )
);

return self::$name_text_fields;
}
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved

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 );
Expand Down
40 changes: 38 additions & 2 deletions tests/phpunit/entries/test_FrmEntryValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]';
$test_url = 'http://madeupwebsite.com';
$values = array(
Expand All @@ -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',
Expand Down
Loading