Repository navigation
Support repeater paths inside embedded forms #3602
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
base: master
Are you sure you want to change the base?
Changes from all commits
dcc4e06
a4feb25
b71c8d6
70fa31b
8a0bdff
598c93e
210a2c9
e4644bc
338e48d
32209e1
9f6bb2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||
|
|
@@ -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 ); | ||||||||||||||||||||||||||||||||||
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
|
|
@@ -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; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -533,22 +532,22 @@ public static function maybe_set_other_validation( $field, &$value, &$args ) { | |||||||||||||||||||||||||||||||||
| * @return void | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public static function set_other_repeating_vals( $field, &$value, &$args ) { | ||||||||||||||||||||||||||||||||||
| if ( ! $args['parent_field_id'] ) { | ||||||||||||||||||||||||||||||||||
| if ( ! FrmField::is_option_true( $field, 'other' ) || ( empty( $args['parent_field_id'] ) && empty( $args['item_meta_path'] ) ) ) { | ||||||||||||||||||||||||||||||||||
| 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'] ); | ||||||||||||||||||||||||||||||||||
| $row = FrmEntryMetaPathHelper::get_posted_row( $path ); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if ( ! isset( $row['other'][ $field->id ] ) ) { | ||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+539
to
544
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking efficiency note.
Suggested change
Same behavior, no unslash for fields without Other. |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // 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 ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?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', | ||
| 'default' => array(), | ||
| 'sanitize' => '', | ||
| ) | ||
| ); | ||
|
|
||
| foreach ( $path as $key ) { | ||
| if ( ! is_array( $row ) || ! isset( $row[ $key ] ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $row = $row[ $key ]; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } |
There was a problem hiding this comment.
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 onlyitem_meta_path(the way Pro's new code callsget_posted_meta) hits an undefined-index notice here, and withparent_field_idempty 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_FrmEntryMetaPathHelpercovers read/write/malformed/legacy but notset_other_repeating_valswith a path. A short case with$_POST['item_meta'][10][0]['other'][30]would cover it.