-
Notifications
You must be signed in to change notification settings - Fork 93
gw-custom-modifier-file-upload.php
: Added snippet for custom filename
modifier for file upload fields.
#1144
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?
Conversation
…ame` modifier for file upload fields.
WalkthroughIntroduces a new Gravity Forms snippet that adds a "filename" merge tag modifier for file upload fields, handling single and multiple files, and integrates with GP Media Library to skip the modifier for images. Changes
Sequence Diagram(s)sequenceDiagram
participant GF as Gravity Forms (merge tag render)
participant Snippet as Filename Modifier Snippet
participant GPML as GP Media Library
GF->>Snippet: gform_merge_tag_filter(value, merge_tag, modifier, field, raw_value, format)
alt modifier == 'filename' and field.type == fileupload
alt multiple files
Snippet->>GF: Join basenames with <br />
else single file
Snippet->>GF: basename(raw_value)
end
else
Snippet->>GF: Return original value
end
GF->>GPML: gpml_image_merge_tag_skip_modifiers(mods, ...)
GPML-->>GF: ['filename'] (skip for images)
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested reviewers
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
|
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.
Actionable comments posted: 2
🧹 Nitpick comments (2)
gravity-forms/gw-custom-modifier-file-upload.php (2)
11-23
: Simplify control flow and add safe guards on $fieldEarly-return makes the intent clearer and avoids touching $field when it’s not an object.
-add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) { - if ( $merge_tag != 'all_fields' && $field->type == 'fileupload' && ! empty( $raw_value ) && $modifier == 'filename' ) { +add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) { + if ( $merge_tag === 'all_fields' || empty( $raw_value ) || $modifier !== 'filename' || ! is_object( $field ) || $field->type !== 'fileupload' ) { + return $value; + } + if ( empty( $field->multipleFiles ) ) { - if ( ! $field->multipleFiles ) { // ... } else { // ... } - } return $value; }, 10, 6 );
9-9
: Nit: fix grammar in instructions- * 1. Add to snippet to site. See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/. + * 1. Add this snippet to the site. See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/.
$value = basename( $raw_value ); | ||
} else { | ||
$file_list = []; | ||
foreach ( json_decode( $raw_value ) as $filepath ) { | ||
$file_list[] = basename( $filepath ); | ||
} | ||
$value = implode( '<br />', $file_list ); | ||
} |
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.
🛠️ Refactor suggestion
Escape output, strip query strings, honor $format, and harden JSON handling
Avoid potential XSS by escaping HTML output, remove query strings from URLs, respect text vs HTML output via $format, and guard against invalid JSON for multi-file values.
- $value = basename( $raw_value );
+ // Strip query string/fragment and escape for HTML when appropriate.
+ $path = is_string( $raw_value ) ? parse_url( $raw_value, PHP_URL_PATH ) : '';
+ $filename = wp_basename( $path ? $path : (string) $raw_value );
+ $value = ( $format === 'text' ) ? $filename : esc_html( $filename );
@@
- $file_list = [];
- foreach ( json_decode( $raw_value ) as $filepath ) {
- $file_list[] = basename( $filepath );
- }
- $value = implode( '<br />', $file_list );
+ $file_list = [];
+ $decoded = json_decode( $raw_value, true );
+ if ( is_array( $decoded ) ) {
+ foreach ( $decoded as $filepath ) {
+ $path = parse_url( $filepath, PHP_URL_PATH );
+ $file_list[] = ( $format === 'text' ) ? wp_basename( $path ) : esc_html( wp_basename( $path ) );
+ }
+ } else {
+ // Fallback if the value isn't valid JSON.
+ $path = is_string( $raw_value ) ? parse_url( $raw_value, PHP_URL_PATH ) : '';
+ $file_list[] = ( $format === 'text' ) ? wp_basename( $path ? $path : (string) $raw_value ) : esc_html( wp_basename( $path ? $path : (string) $raw_value ) );
+ }
+ $separator = ( $format === 'text' ) ? "\n" : '<br />';
+ $value = implode( $separator, $file_list );
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
$value = basename( $raw_value ); | |
} else { | |
$file_list = []; | |
foreach ( json_decode( $raw_value ) as $filepath ) { | |
$file_list[] = basename( $filepath ); | |
} | |
$value = implode( '<br />', $file_list ); | |
} | |
// Strip query string/fragment and escape for HTML when appropriate. | |
$path = is_string( $raw_value ) ? parse_url( $raw_value, PHP_URL_PATH ) : ''; | |
$filename = wp_basename( $path ? $path : (string) $raw_value ); | |
$value = ( $format === 'text' ) ? $filename : esc_html( $filename ); | |
} else { | |
$file_list = []; | |
$decoded = json_decode( $raw_value, true ); | |
if ( is_array( $decoded ) ) { | |
foreach ( $decoded as $filepath ) { | |
$path = parse_url( $filepath, PHP_URL_PATH ); | |
$file_list[] = ( $format === 'text' ) ? wp_basename( $path ) : esc_html( wp_basename( $path ) ); | |
} | |
} else { | |
// Fallback if the value isn't valid JSON. | |
$path = is_string( $raw_value ) ? parse_url( $raw_value, PHP_URL_PATH ) : ''; | |
$file_list[] = ( $format === 'text' ) | |
? wp_basename( $path ? $path : (string) $raw_value ) | |
: esc_html( wp_basename( $path ? $path : (string) $raw_value ) ); | |
} | |
$separator = ( $format === 'text' ) ? "\n" : '<br />'; | |
$value = implode( $separator, $file_list ); | |
} |
🤖 Prompt for AI Agents
In gravity-forms/gw-custom-modifier-file-upload.php around lines 14 to 21, the
code currently outputs raw basenames from $raw_value and multi-file JSON without
escaping, removing query strings, honoring the $format parameter, or validating
JSON; update it to: detect whether $raw_value is JSON and safely json_decode
with error checking, ensure the decoded value is an array before iterating,
strip any query string or params from each filepath (e.g. remove anything after
the first '?' or '#') before calling basename, escape the final output for HTML
when $format !== 'text' (and for text output return plain, newline-separated
values when $format === 'text'), and handle fallback to a single safe basename
when JSON is invalid — this will prevent XSS, respect $format, and harden JSON
handling.
|
||
// For GP Media Library | ||
add_filter( 'gpml_image_merge_tag_skip_modifiers', function( $skip_modifiers, $modifiers, $input_id, $image_ids ) { | ||
return [ 'filename' ]; |
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.
🛠️ Refactor suggestion
Preserve other modifiers skipped by GPML
Don’t clobber existing $skip_modifiers; merge with them to remain compatible with other customizations.
-return [ 'filename' ];
+$skip_modifiers = is_array( $skip_modifiers ) ? $skip_modifiers : array();
+$skip_modifiers[] = 'filename';
+return array_values( array_unique( $skip_modifiers ) );
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
return [ 'filename' ]; | |
// Preserve any previously skipped modifiers, then add 'filename' | |
$skip_modifiers = is_array( $skip_modifiers ) ? $skip_modifiers : array(); | |
$skip_modifiers[] = 'filename'; | |
return array_values( array_unique( $skip_modifiers ) ); |
🤖 Prompt for AI Agents
In gravity-forms/gw-custom-modifier-file-upload.php around line 28, the function
currently returns [ 'filename' ] which clobbers any existing $skip_modifiers;
instead merge your modifier into the incoming $skip_modifiers and return the
combined set. Modify the code to add 'filename' to $skip_modifiers (e.g.
$skip_modifiers[] = 'filename' or $skip_modifiers = array_merge($skip_modifiers,
['filename'])), then ensure uniqueness/reset keys (e.g. return
array_values(array_unique($skip_modifiers))) so other GPML/custom modifiers are
preserved.
@saifsultanc I'd recommend making this snippet title specific to the |
…ame` modifier for file upload fields.
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.
Actionable comments posted: 2
🧹 Nitpick comments (1)
gravity-forms/gw-filename-modifier-file-upload.php (1)
3-10
: Polish snippet header: brand, clarity, and title specificity
- Use “Gravity Wiz” (singular) for branding.
- Tighten wording and make the title explicitly about the :filename modifier, per the PR feedback.
- * Gravity Wizards // Gravity Forms // Filename Modifier for File Upload + * Gravity Wiz // Gravity Forms // :filename Modifier for File Upload fields @@ - * This snippet is an example of how to add filename modifiers to a Gravity Forms file upload field. + * This snippet adds the :filename merge tag modifier for Gravity Forms File Upload fields. @@ - * 1. Add to snippet to site. See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/. + * 1. Add this snippet to your site. See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/.
add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) { | ||
if ( $merge_tag != 'all_fields' && $field->type == 'fileupload' && ! empty( $raw_value ) && $modifier == 'filename' ) { | ||
if ( ! $field->multipleFiles ) { | ||
$value = basename( $raw_value ); | ||
} else { | ||
$file_list = []; | ||
foreach ( json_decode( $raw_value ) as $filepath ) { | ||
$file_list[] = basename( $filepath ); | ||
} | ||
$value = implode( '<br />', $file_list ); | ||
} | ||
} | ||
return $value; | ||
}, 10, 6 ); |
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.
🛠️ Refactor suggestion
Harden modifier handling, decoding, and output escaping (avoid warnings and potential XSS)
- json_decode() may return null; foreach on null will warn. Normalize $raw_value robustly.
- Strip query strings from URLs before basename().
- Escape filenames for HTML output to avoid HTML injection if a filename includes special chars.
- Honor stacked modifiers (e.g., {:filename:esc_html}) by checking substring presence.
- Use strict comparisons and format-aware separators (
vs newline).
-add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
- if ( $merge_tag != 'all_fields' && $field->type == 'fileupload' && ! empty( $raw_value ) && $modifier == 'filename' ) {
- if ( ! $field->multipleFiles ) {
- $value = basename( $raw_value );
- } else {
- $file_list = [];
- foreach ( json_decode( $raw_value ) as $filepath ) {
- $file_list[] = basename( $filepath );
- }
- $value = implode( '<br />', $file_list );
- }
- }
- return $value;
-}, 10, 6 );
+add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifiers, $field, $raw_value, $format ) {
+ // Only handle file upload fields with the :filename modifier (supports stacked modifiers).
+ if ( $merge_tag === 'all_fields' || ! $field || $field->type !== 'fileupload' || empty( $raw_value ) || strpos( (string) $modifiers, 'filename' ) === false ) {
+ return $value;
+ }
+
+ $is_html = ( $format === 'html' );
+ $sep = $is_html ? '<br />' : "\n";
+
+ // Normalize to an array of file paths/URLs.
+ if ( is_array( $raw_value ) ) {
+ $files = $raw_value;
+ } else {
+ $decoded = json_decode( $raw_value, true );
+ $files = is_array( $decoded ) ? $decoded : array( $raw_value );
+ }
+
+ // Extract safe filenames (strip query strings; escape for HTML).
+ $filenames = array();
+ foreach ( $files as $path ) {
+ if ( ! is_string( $path ) || $path === '' ) {
+ continue;
+ }
+ $clean_path = parse_url( $path, PHP_URL_PATH );
+ $basename = basename( $clean_path ?: $path );
+ $filenames[] = $is_html ? esc_html( $basename ) : $basename;
+ }
+
+ return implode( $sep, $filenames );
+}, 10, 6 );
🤖 Prompt for AI Agents
In gravity-forms/gw-filename-modifier-file-upload.php around lines 11 to 24, the
modifier handling should be hardened: treat $modifier as substring (use strpos)
so stacked modifiers like {:filename:esc_html} are honored; use strict
comparisons (=== / !==) when checking $merge_tag and $modifier; normalize
$raw_value to an array safely (if field is multipleFiles json_decode may return
null — default to an empty array or single-item array for a plain string) before
iterating to avoid warnings; strip query strings from file URLs before calling
basename (use parse_url or strtok on '?'); escape filenames for HTML output with
esc_html() and choose separator based on $format (use '<br />' for HTML and "\n"
for text); also guard access to $field->multipleFiles with isset/ property
check.
add_filter( 'gpml_image_merge_tag_skip_modifiers', function( $skip_modifiers, $modifiers, $input_id, $image_ids ) { | ||
return [ 'filename' ]; | ||
}, 10, 4 ); |
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.
Don’t clobber existing GPML skip modifiers
Returning only ['filename'] discards previously skipped modifiers from other plugins/snippets. Append instead of replace.
-add_filter( 'gpml_image_merge_tag_skip_modifiers', function( $skip_modifiers, $modifiers, $input_id, $image_ids ) {
- return [ 'filename' ];
-}, 10, 4 );
+add_filter( 'gpml_image_merge_tag_skip_modifiers', function( $skip_modifiers, $modifiers, $input_id, $image_ids ) {
+ $skip_modifiers[] = 'filename';
+ return array_values( array_unique( $skip_modifiers ) );
+}, 10, 4 );
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
add_filter( 'gpml_image_merge_tag_skip_modifiers', function( $skip_modifiers, $modifiers, $input_id, $image_ids ) { | |
return [ 'filename' ]; | |
}, 10, 4 ); | |
add_filter( 'gpml_image_merge_tag_skip_modifiers', function( $skip_modifiers, $modifiers, $input_id, $image_ids ) { | |
$skip_modifiers[] = 'filename'; | |
return array_values( array_unique( $skip_modifiers ) ); | |
}, 10, 4 ); |
🤖 Prompt for AI Agents
In gravity-forms/gw-filename-modifier-file-upload.php around lines 27 to 29, the
filter handler currently returns a fresh array ['filename'] which overwrites any
existing skip modifiers from other plugins; change it to append 'filename' to
the incoming $skip_modifiers and return the merged unique array (e.g., add
'filename' to $skip_modifiers and return array_unique($skip_modifiers)) so
existing modifiers are preserved.
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/3025552109/87377
https://github.com/gravitywiz/gp-media-library/pull/35#issuecomment-3156459815
Summary
This snippet is an example of how to add custom modifiers to a Gravity Forms file upload field
Also dependent on GPML https://github.com/gravitywiz/gp-media-library/pull/35