Skip to content

Commit b1e723e

Browse files
committed
gw-advanced-merge-tags.php: Added support for encoding file contents for File Upload fields rather than file URLs when using the :base64 modifier.
1 parent 4957c1b commit b1e723e

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

gravity-forms/gw-advanced-merge-tags.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,34 @@ public function handle_field_modifiers( $value, $input_id, $modifier, $field, $r
552552

553553
return $this->generate_gravatar($value, $modifiers);
554554
case 'base64':
555+
if ( $field->get_input_type() === 'fileupload' ) {
556+
// Handle file upload fields by encoding the actual file contents
557+
if ( ! empty( $raw_value ) ) {
558+
// Handle multiple files (array) or single file (string)
559+
$files = is_array( $raw_value ) ? $raw_value : json_decode( $raw_value, true );
560+
561+
if ( ! is_array( $files ) ) {
562+
$files = array( $raw_value );
563+
}
564+
565+
$encoded_files = array();
566+
567+
foreach ( $files as $file_url ) {
568+
$file_path = $this->get_file_path_from_url( $file_url );
569+
570+
if ( $file_path && file_exists( $file_path ) ) {
571+
$file_contents = file_get_contents( $file_path );
572+
if ( $file_contents !== false ) {
573+
$encoded_files[] = base64_encode( $file_contents );
574+
}
575+
}
576+
}
577+
578+
// Return first file if single file, or JSON array if multiple
579+
return count( $encoded_files ) === 1 ? $encoded_files[0] : json_encode( $encoded_files );
580+
}
581+
}
555582
return base64_encode( $value );
556-
break;
557583
}
558584
}
559585

@@ -567,6 +593,30 @@ public function mask_value( $value ) {
567593
return implode( '', array_merge( $first, array_pad( array(), count( $chars ), '*' ), $last ) );
568594
}
569595

596+
/**
597+
* Convert a file URL to a local file path.
598+
*
599+
* @param string $url File URL
600+
*
601+
* @return string|false File path on success, false on failure
602+
*/
603+
public function get_file_path_from_url( $url ) {
604+
// Remove query string if present
605+
$url = strtok( $url, '?' );
606+
607+
// Get upload directory info
608+
$upload_dir = wp_upload_dir();
609+
610+
// Check if URL is from the uploads directory
611+
if ( strpos( $url, $upload_dir['baseurl'] ) === 0 ) {
612+
// Replace the base URL with the base path
613+
$file_path = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $url );
614+
return $file_path;
615+
}
616+
617+
return false;
618+
}
619+
570620
public function parse_modifiers( $modifiers_str ) {
571621

572622
preg_match_all( '/([a-z0-9_]+)(?:(?:\[(.+?)\])|,?)/i', $modifiers_str, $modifiers, PREG_SET_ORDER );

0 commit comments

Comments
 (0)