Skip to content

Commit 1fbdc4a

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

File tree

1 file changed

+167
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)