Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 13 additions & 2 deletions classes/helpers/FrmAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,18 @@ public static function get_simple_request( $args ) {
} elseif ( $args['type'] === 'post' ) {
if ( isset( $_POST[ $args['param'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
$value = wp_unslash( $_POST[ $args['param'] ] );
$value = $_POST[ $args['param'] ];

foreach ( $args['path'] ?? array() as $key ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: path is a new option on a widely used function, but it isn't in $defaults or the docblock, and FrmEntryMetaPathHelper::get_posted_row() is its only caller. Either add 'path' => array() to $defaults and document it, or keep the walk inside FrmEntryMetaPathHelper so get_simple_request stays as it was. The existing contract (wp_unslash after the lookup) is preserved either way.

if ( ! is_array( $value ) || ! isset( $value[ $key ] ) ) {
$value = $args['default'];
break;
}

$value = $value[ $key ];
}

$value = wp_unslash( $value );

if ( $args['serialized'] === true && is_serialized_string( $value ) && is_serialized( $value ) ) {
self::unserialize_or_decode( $value );
Expand All @@ -865,7 +876,7 @@ public static function get_simple_request( $args ) {
} elseif ( isset( $_REQUEST[ $args['param'] ] ) ) {
// phpcs:ignore phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$value = wp_unslash( $_REQUEST[ $args['param'] ] );
}
}//end if

self::sanitize_value( $args['sanitize'], $value );

Expand Down
33 changes: 16 additions & 17 deletions classes/helpers/FrmEntriesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ private static function get_field_value_for_new_entry( $field, $reset, $args ) {
* @return bool True if a value is posted.
*/
public static function value_is_posted( $field, $args ) {
if ( ! empty( $args['item_meta_path'] ) ) {
return isset( FrmEntryMetaPathHelper::get_posted_row( $args['item_meta_path'] )[ $field->id ] );
}

$value_is_posted = false;

if ( ! $_POST ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
Expand Down Expand Up @@ -392,7 +396,7 @@ public static function set_posted_value( $field, $value, $args ) {
$value = $args['temp_value'];
}

if ( empty( $args['parent_field_id'] ) ) {
if ( empty( $args['parent_field_id'] ) && empty( $args['item_meta_path'] ) ) {
$_POST['item_meta'][ $field->id ] = $value;
} else {
self::set_parent_field_posted_value( $field, $value, $args );
Expand All @@ -411,17 +415,7 @@ public static function set_posted_value( $field, $value, $args ) {
* @return void
*/
private static function set_parent_field_posted_value( $field, $value, $args ) {
if ( isset( $_POST['item_meta'][ $args['parent_field_id'] ] ) && is_array( $_POST['item_meta'][ $args['parent_field_id'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Files.LineLength.LineTooLong
if ( ! isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] ) || ! is_array( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Files.LineLength.LineTooLong
$_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] = array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
} else {
// All of the section was probably removed.
$_POST['item_meta'][ $args['parent_field_id'] ] = array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ] = array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}

$_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field->id ] = $value; // phpcs:ignore WordPress.Security.NonceVerification.Missing
FrmEntryMetaPathHelper::set_posted_value( $_POST, $field->id, $value, $args ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}

/**
Expand Down Expand Up @@ -467,12 +461,17 @@ public static function get_posted_value( $field, &$value, $args ) {
* @return mixed
*/
public static function get_posted_meta( $field_id, $args ) {
if ( ! empty( $args['item_meta_path'] ) ) {
return FrmEntryMetaPathHelper::get_posted_row( $args['item_meta_path'] )[ $field_id ] ?? '';
}

if ( empty( $args['parent_field_id'] ) ) {
// Sanitizing is done next.
$value = isset( $_POST['item_meta'][ $field_id ] ) ? wp_unslash( $_POST['item_meta'][ $field_id ] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Files.LineLength.LineTooLong
} else {
$value = isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] ) ? wp_unslash( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ][ $field_id ] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Files.LineLength.LineTooLong
}

return $value;
}

Expand Down Expand Up @@ -537,18 +536,18 @@ public static function set_other_repeating_vals( $field, &$value, &$args ) {
return;
}

// Check if there are any other posted "other" values for this field.
if ( ! FrmField::is_option_true( $field, 'other' ) || ! isset( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Files.LineLength.LineTooLong
$path = ! empty( $args['item_meta_path'] ) ? $args['item_meta_path'] : array( $args['parent_field_id'], $args['key_pointer'] );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: the early return at line 535 (if ( ! $args['parent_field_id'] ) { return; }, not in this diff) is the legacy argument, but the lines below it are now path-aware. A caller that passes only item_meta_path (the way Pro's new code calls get_posted_meta) hits an undefined-index notice here, and with parent_field_id empty it returns before the path is ever used. Pro #6881 happens to pass both today. Make that guard match the path logic so the two can't drift: if ( empty( $args['parent_field_id'] ) && empty( $args['item_meta_path'] ) ) { return; }.

The "Other" branch also has no test; test_FrmEntryMetaPathHelper covers read/write/malformed/legacy but not set_other_repeating_vals with a path. A short case with $_POST['item_meta'][10][0]['other'][30] would cover it.

$row = FrmEntryMetaPathHelper::get_posted_row( $path );

if ( ! FrmField::is_option_true( $field, 'other' ) || ! isset( $row['other'][ $field->id ] ) ) {
return;
}

// Save original value
$args['temp_value'] = $value;
$args['other'] = true;
$other_vals = wp_unslash( $_POST['item_meta'][ $args['parent_field_id'] ][ $args['key_pointer'] ]['other'][ $field->id ] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Files.LineLength.LineTooLong
$other_vals = $row['other'][ $field->id ];
FrmAppHelper::sanitize_value( 'sanitize_text_field', $other_vals );

// Set the validation value now.
self::set_other_validation_val( $value, $other_vals, $field, $args );
}

Expand Down
66 changes: 66 additions & 0 deletions classes/helpers/FrmEntryMetaPathHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}

/**
* Access field values inside embedded forms and repeater rows.
*
* @since x.x
*/
class FrmEntryMetaPathHelper {

/**
* Read the row containing fields at a nested submission path.
*
* @since x.x
*
* @param array $path Keys locating the row inside item_meta.
*
* @return array
*/
public static function get_posted_row( $path ) {
$row = FrmAppHelper::get_simple_request(
array(
'type' => 'post',
'param' => 'item_meta',
'path' => $path,
'default' => array(),
'sanitize' => '',
)
);
return is_array( $row ) ? $row : array();
}

/**
* Write a validated value without copying unrelated submitted rows.
*
* @since x.x
*
* @param array|null $posted The submission, passed by reference.
* @param int|string $field_id The field being validated.
* @param mixed $value The validated field value.
* @param array $args The parent field, row pointer and nested path.
*
* @return void
*/
public static function set_posted_value( &$posted, $field_id, $value, $args ) {
$path = ! empty( $args['item_meta_path'] ) ? $args['item_meta_path'] : array( $args['parent_field_id'], $args['key_pointer'] );
$path = array_merge( array( 'item_meta' ), $path, array( $field_id ) );
$row = &$posted;

foreach ( $path as $key ) {
if ( ! is_array( $row ) ) {
$row = array();
}

if ( ! isset( $row[ $key ] ) ) {
$row[ $key ] = array();
}

$row = &$row[ $key ];
}

$row = $value;
}
}
25 changes: 25 additions & 0 deletions js/formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ function frmFrontFormJS() {
}

fieldName = field.name;
if ( /^item_meta\[\d+\]\[i?\d+\]\[\d+\]\[(form|row_ids)\]/.test( fieldName ) ) {
return 0;
}

const embeddedRepeater = fieldName?.match(
/^item_meta\[\d+\]\[i?\d+\]\[(\d+)\]\[(i?\d+)\]\[(\d+|other|form|row_ids)\]/
);
if ( embeddedRepeater ) {
let [ , sectionId, rowId, nestedId ] = embeddedRepeater;
if ( nestedId === 'form' || nestedId === 'row_ids' ) {
return 0;
}
if ( nestedId === 'other' ) {
nestedId = fieldName.slice( embeddedRepeater[ 0 ].length ).match( /^\[(\d+)\]/ )?.[ 1 ];
if ( ! nestedId ) {
return 0;
}
}
if ( ! fullID ) {
return nestedId;
}
const container = field.closest( '[data-sub-field-name]' );
const suffix = container ? `-${ container.getAttribute( 'data-sub-field-name' ) }` : '';
return `${ nestedId }-${ sectionId }-${ rowId }${ suffix }`;
}

if ( fieldName === undefined ) {
fieldName = '';
Expand Down
Loading
Loading