Skip to content

Commit 2423099

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

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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 not proceed if Gravity Forms is not installed & activated.
25+
if ( ! class_exists( 'GFCommon' ) ) {
26+
return;
27+
}
28+
29+
// Do version check in the init to make sure if GF is going to be loaded, it is already loaded.
30+
add_action( 'init', array( $this, 'init' ) );
31+
}
32+
33+
public function init() {
34+
$form_id = $this->_args['form_id'];
35+
$field_id = $this->_args['field_id'];
36+
37+
// Time for hooks.
38+
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
39+
add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
40+
41+
if ( $form_id && $field_id ) {
42+
add_action( "gpeb_post_file_population_{$form_id}_{$field_id}", array(
43+
$this,
44+
'populate_file_Data',
45+
), 10, 3 );
46+
}
47+
}
48+
49+
/**
50+
* Populate file data.
51+
*
52+
* @param $file_upload_data
53+
* @param $form
54+
* @param $field
55+
*
56+
* @return void
57+
*/
58+
public function populate_file_Data( $file_upload_data, $form, $field ) {
59+
add_action( 'wp_print_footer_scripts', function () use ( $file_upload_data, $form, $field ) {
60+
$form_id = rgar( $form, 'id' );
61+
$field_id = rgar( $field, 'id' );
62+
63+
echo "<script>
64+
jQuery(document).ready(function($) {
65+
var fileData = " . json_encode( $file_upload_data ) . ";
66+
var formId = " . $form_id . ";
67+
var fieldId = " . $field_id . ";
68+
69+
sessionStorage.setItem('gpep_filedata_' + formId + '_' + fieldId, JSON.stringify(fileData));
70+
});
71+
</script>";
72+
} );
73+
}
74+
75+
public function load_form_script( $form, $is_ajax_enabled ) {
76+
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
77+
add_action( 'wp_footer', array( $this, 'output_script' ) );
78+
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
79+
}
80+
81+
return $form;
82+
}
83+
84+
public function output_script() {
85+
?>
86+
87+
<script type="text/javascript">
88+
89+
(function ($) {
90+
91+
window.<?php echo __CLASS__; ?> = function (args) {
92+
self.init = function () {
93+
/**
94+
* Filter the file name markup to include the file URL.
95+
*/
96+
window.gform.addFilter('gpfup_filename_markup', function (fileName, formId, fieldId, file, additionalArgs) {
97+
var fileUrl = file.url || null;
98+
var fileData = JSON.parse(sessionStorage.getItem('gpep_filedata_' + formId + '_' + fieldId)) || [];
99+
100+
if ( !fileUrl) {
101+
if (typeof file.getNative === 'function') {
102+
var nativeFile = file.getNative();
103+
if (nativeFile instanceof File) {
104+
fileUrl = URL.createObjectURL(nativeFile);
105+
}
106+
} else if (Array.isArray(fileData)) { // Find the file URL from `fileData` based on the uploaded file name.
107+
var matchedFile = fileData.find(item => item.uploaded_filename === file.name);
108+
if (matchedFile) {
109+
fileUrl = matchedFile.url;
110+
}
111+
}
112+
}
113+
114+
return fileUrl ? `<a href="${fileUrl}" target="__blank">${fileName}</a>` : fileName;
115+
});
116+
};
117+
118+
self.init();
119+
}
120+
121+
})(jQuery);
122+
123+
</script>
124+
125+
<?php
126+
}
127+
128+
public function add_init_script( $form ) {
129+
if ( ! $this->is_applicable_form( $form ) ) {
130+
return;
131+
}
132+
133+
$args = array(
134+
'formId' => $this->_args['form_id'],
135+
'fieldId' => $this->_args['field_id'],
136+
);
137+
138+
$script = 'new ' . __CLASS__ . '( ' . json_encode( $args ) . ' );';
139+
$slug = implode( '_', array( strtolower( __CLASS__ ), $this->_args['form_id'], $this->_args['field_id'] ) );
140+
141+
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
142+
}
143+
144+
public function is_applicable_form( $form ) {
145+
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
146+
147+
return empty( $this->_args['form_id'] ) || (int) $form_id == (int) $this->_args['form_id'];
148+
}
149+
150+
}
151+
152+
// Usage instructions.
153+
new GPFUP_Update_Filename_Markup(
154+
array(
155+
'form_id' => 33, // Update to your form ID.
156+
'field_id' => 4, // Update to your file upload field ID.
157+
)
158+
);

0 commit comments

Comments
 (0)