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
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; | ||
| } | ||
|
|
||
|
|
@@ -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'] ); | ||
|
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: the early return at line 535 ( The "Other" branch also has no test; |
||
| $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 ); | ||
| } | ||
|
|
||
|
|
||
| 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; | ||
| } | ||
| } |
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:
pathis a new option on a widely used function, but it isn't in$defaultsor the docblock, andFrmEntryMetaPathHelper::get_posted_row()is its only caller. Either add'path' => array()to$defaultsand document it, or keep the walk insideFrmEntryMetaPathHelpersoget_simple_requeststays as it was. The existing contract (wp_unslashafter the lookup) is preserved either way.