Repository navigation
Preserve literal field values and expose saved field data #3683
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
80de718
9fed47a
5bd9a0f
613fc28
26fa599
94b6cc9
41aa582
5c66c32
5faa144
1ca0774
ca5ada3
c3a4045
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 |
|---|---|---|
|
|
@@ -363,7 +363,7 @@ public static function get_entry_meta_by_field( $entry_id, $field_id ) { | |
|
|
||
| if ( $cached && isset( $cached->metas ) && isset( $cached->metas[ $field_id ] ) ) { | ||
| $result = $cached->metas[ $field_id ]; | ||
| return wp_unslash( $result ); | ||
| return self::maybe_unslash_field_value( $result, $field_id ); | ||
| } | ||
|
|
||
| $get_table = $wpdb->prefix . 'frm_item_metas'; | ||
|
|
@@ -382,7 +382,43 @@ public static function get_entry_meta_by_field( $entry_id, $field_id ) { | |
| $field_type = FrmField::get_type( $field_id ); | ||
| FrmFieldsHelper::prepare_field_value( $result, $field_type ); | ||
|
|
||
| return wp_unslash( $result ); | ||
| return self::maybe_unslash_field_value( $result, $field_id, $field_type ); | ||
| } | ||
|
|
||
| /** | ||
| * Keep literal source backslashes when a field opts out of legacy unslashing. | ||
| * | ||
| * Applied by FrmEntry, get_entry_meta_by_field(), get_entry_metas_for_field() and getAll() | ||
| * so every read path returns the same value for a field. The field type is only | ||
|
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. Blocking. The docblock says this filter is applied so "every read path returns the same value for a field", but
Fix: make $metas = $entry->metas ?? null;
unset( $entry->metas );
$entry = wp_unslash( $entry );
if ( is_array( $metas ) ) {
foreach ( $metas as $meta_field_id => $meta_value ) {
$metas[ $meta_field_id ] = FrmEntryMeta::maybe_unslash_field_value( $meta_value, $meta_field_id );
}
$entry->metas = $metas;
}Note that |
||
| * looked up when something is hooked to the filter. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @internal | ||
| * | ||
| * @param mixed $value Stored or cached field value. | ||
| * @param int|string $field_id Field ID or key. | ||
| * @param string|null $type Field type, when the caller already knows it. | ||
| * | ||
| * @return mixed | ||
| */ | ||
| public static function maybe_unslash_field_value( $value, $field_id, $type = null ) { | ||
| if ( ! has_filter( 'frm_should_unslash_entry_meta_value' ) ) { | ||
| return wp_unslash( $value ); | ||
| } | ||
|
|
||
| if ( null === $type ) { | ||
| $type = FrmField::get_type( $field_id ); | ||
| } | ||
|
|
||
| /** | ||
| * @since x.x | ||
| * | ||
| * @param bool $unslash Whether the field value should be unslashed. | ||
| * @param string $type Field type. | ||
| * @param int|string $field_id Field ID or key. | ||
| */ | ||
| return apply_filters( 'frm_should_unslash_entry_meta_value', true, $type, $field_id ) ? wp_unslash( $value ) : $value; | ||
|
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.
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -417,7 +453,7 @@ public static function get_entry_metas_for_field( $field_id, $order = '', $limit | |
| unset( $k, $v ); | ||
| } | ||
|
|
||
| return wp_unslash( $values ); | ||
| return self::maybe_unslash_field_value( $values, $field_id ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -491,7 +527,7 @@ public static function getAll( $where = array(), $order_by = '', $limit = '', $s | |
|
|
||
| foreach ( $results as $k => $result ) { | ||
| FrmAppHelper::unserialize_or_decode( $result->meta_value ); | ||
| $results[ $k ]->meta_value = wp_unslash( $result->meta_value ); | ||
| $results[ $k ]->meta_value = self::maybe_unslash_field_value( $result->meta_value, $result->field_id, $result->field_type ); | ||
| unset( $k, $result ); | ||
| } | ||
|
|
||
|
|
||
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.
Blocking.
field_value_is_literal( $field )is called with two different types. On export (forms_xml.php)$fieldis astdClasswith a stringtype. On import (fill_field()line 618) it is the rawSimpleXMLElement, so$field->typeis an object. A listener written the natural way,'code' === $field->type, returns true on export and false on import.I reproduced it with a filter that records its argument. Result:
[["stdClass", true], ["SimpleXMLElement", false]]. An unmarked<default_value><![CDATA[{"a":1}]]></default_value>on acodefield was then JSON-decoded intoarray('a' => 1)instead of staying a string. The new test hides this because it passes(object) array( 'type' => 'code' )for both directions.Fix: normalise before calling the filter so both directions see the same shape, then add a test that feeds
field_value_from_xml()a realSimpleXMLElement:Separately: when
base64_decode( ..., true )fails (line 2051), the method returns''with no notice. A truncated or pretty-printed file silently wipes the default value. Strict mode also rejects whitespace inside the CDATA. Strip whitespace first (preg_replace( '/\s+/', '', (string) $value )) and fall back to the raw string rather than''on failure.