Skip to content

Commit c3bb17b

Browse files
committed
gpfup-update-filename-markup.php: Added new snippet.
1 parent 6b9e42a commit c3bb17b

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/**
3+
* Gravity Perks // File Upload Pro // Update Filename Markup
4+
* http://gravitywiz.com/documentation/gravity-forms-file-upload-pro
5+
*
6+
* This snippet allows you to update the filename markup to include the file URL. This snippet also works with the
7+
* GP Easy Passthrough plugin when multiple files are uploaded using GP File Upload Pro.
8+
*
9+
* Installation instructions:
10+
* 1. https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets
11+
* 2. See usage instructions at the bottom of the file
12+
*/
13+
class GPFUP_Update_Filename_Markup {
14+
15+
private $_args = array();
16+
17+
public function __construct( $args = array() ) {
18+
// Set our default arguments, parse against the provided arguments, and store for use throughout the class.
19+
$this->_args = wp_parse_args( $args, array(
20+
'form_id' => false,
21+
'field_id' => false,
22+
) );
23+
24+
// Do version check in the init to make sure if GF is going to be loaded, it is already loaded.
25+
add_action( 'init', array( $this, 'init' ) );
26+
}
27+
28+
public function init() {
29+
$form_id = $this->_args['form_id'];
30+
$field_id = $this->_args['field_id'];
31+
32+
// Time for hooks.
33+
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
34+
add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
35+
36+
if ( $form_id && $field_id ) {
37+
add_action( "gpeb_post_file_population_{$form_id}_{$field_id}", array(
38+
$this,
39+
'populate_file_Data',
40+
), 10, 3 );
41+
}
42+
}
43+
44+
/**
45+
* Populate file data.
46+
*
47+
* @param $file_upload_data
48+
* @param $form
49+
* @param $field
50+
*
51+
* @return void
52+
*/
53+
public function populate_file_Data( $file_upload_data, $form, $field ) {
54+
add_action( 'wp_print_footer_scripts', function () use ( $file_upload_data, $form, $field ) {
55+
$form_id = rgar( $form, 'id' );
56+
$field_id = rgar( $field, 'id' );
57+
58+
echo "<script>
59+
jQuery(document).ready(function($) {
60+
var fileData = " . json_encode( $file_upload_data ) . ";
61+
var formId = " . $form_id . ";
62+
var fieldId = " . $field_id . ";
63+
64+
sessionStorage.setItem('gpep_filedata_' + formId + '_' + fieldId, JSON.stringify(fileData));
65+
});
66+
</script>";
67+
} );
68+
}
69+
70+
public function load_form_script( $form, $is_ajax_enabled ) {
71+
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
72+
add_action( 'wp_footer', array( $this, 'output_script' ) );
73+
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
74+
}
75+
76+
return $form;
77+
}
78+
79+
public function output_script() {
80+
?>
81+
82+
<script type="text/javascript">
83+
84+
(function ($) {
85+
86+
window.<?php echo __CLASS__; ?> = function (args) {
87+
self.init = function () {
88+
/**
89+
* Filter the file name markup to include the file URL.
90+
*/
91+
window.gform.addFilter('gpfup_filename_markup', function (fileName, formId, fieldId, file, additionalArgs) {
92+
var fileUrl = file.url || null;
93+
var fileData = JSON.parse(sessionStorage.getItem('gpep_filedata_' + formId + '_' + fieldId)) || [];
94+
95+
if ( !fileUrl) {
96+
if (typeof file.getNative === 'function') {
97+
var nativeFile = file.getNative();
98+
if (nativeFile instanceof File) {
99+
fileUrl = URL.createObjectURL(nativeFile);
100+
}
101+
} else if (Array.isArray(fileData)) { // Find the file URL from `fileData` based on the uploaded file name.
102+
var matchedFile = fileData.find(item => item.uploaded_filename === file.name);
103+
if (matchedFile) {
104+
fileUrl = matchedFile.url;
105+
}
106+
}
107+
}
108+
109+
return fileUrl ? `<a href="${fileUrl}" target="__blank">${fileName}</a>` : fileName;
110+
});
111+
};
112+
113+
self.init();
114+
}
115+
116+
})(jQuery);
117+
118+
</script>
119+
120+
<?php
121+
}
122+
123+
public function add_init_script( $form ) {
124+
if ( ! $this->is_applicable_form( $form ) ) {
125+
return;
126+
}
127+
128+
$args = array(
129+
'formId' => $this->_args['form_id'],
130+
'fieldId' => $this->_args['field_id'],
131+
);
132+
133+
$script = 'new ' . __CLASS__ . '( ' . json_encode( $args ) . ' );';
134+
$slug = implode( '_', array( strtolower( __CLASS__ ), $this->_args['form_id'], $this->_args['field_id'] ) );
135+
136+
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
137+
}
138+
139+
public function is_applicable_form( $form ) {
140+
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
141+
142+
return empty( $this->_args['form_id'] ) || (int) $form_id == (int) $this->_args['form_id'];
143+
}
144+
145+
}
146+
147+
// Usage instructions.
148+
new GPFUP_Update_Filename_Markup(
149+
array(
150+
'form_id' => 33, // Update to your form ID.
151+
'field_id' => 4, // Update to your file upload field ID.
152+
)
153+
);

0 commit comments

Comments
 (0)