-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgpfup-update-filename-markup.php
188 lines (151 loc) · 5.38 KB
/
gpfup-update-filename-markup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* Gravity Perks // File Upload Pro // Update Filename Markup
* http://gravitywiz.com/documentation/gravity-forms-file-upload-pro
*
* This snippet allows you to update the filename markup to include the file URL. This snippet also works with the
* GP Easy Passthrough plugin when multiple files are uploaded using GP File Upload Pro.
*
* Installation instructions:
* 1. https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets
* 2. See usage instructions at the bottom of the file
*/
class GPFUP_Update_Filename_Markup {
private $_args = array();
public function __construct( $args = array() ) {
// Set our default arguments, parse against the provided arguments, and store for use throughout the class.
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_id' => false,
) );
// Do not proceed if Gravity Forms is not installed & activated.
if ( ! class_exists( 'GFCommon' ) ) {
return;
}
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
$form_id = $this->_args['form_id'];
$field_id = $this->_args['field_id'];
// Time for hooks.
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
$args = array( 'gpeb_post_file_population' );
// If a form ID is provided, add it to the args.
if ( $form_id ) {
$args[] = $form_id;
// If a field ID is provided, add it to the args.
if ( $field_id ) {
$args[] = $field_id;
}
}
add_action( implode( '_', $args ), array(
$this,
'populate_file_data',
), 10, 3 );
}
/**
* Populate file data.
*
* @param $file_upload_data
* @param $form
* @param $field
*
* @return void
*/
public function populate_file_data( $file_upload_data, $form, $field ) {
add_action( 'wp_print_footer_scripts', function () use ( $file_upload_data, $form, $field ) {
$form_id = rgar( $form, 'id' );
$field_id = rgar( $field, 'id' );
echo '<script>
jQuery(document).ready(function($) {
var fileData = ' . wp_json_encode( $file_upload_data ) . ';
var formId = ' . absint( $form_id ) . ';
var fieldId = ' . absint( $field_id ) . ";
sessionStorage.setItem('gpep_filedata_' + formId + '_' + fieldId, JSON.stringify(fileData));
});
</script>";
} );
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $form;
}
if ( ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
}
if ( ! has_action( 'gform_preview_footer', array( $this, 'output_script' ) ) ) {
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
(function ($) {
window.<?php echo __CLASS__; ?> = function (args) {
self.init = function () {
/**
* Filter the file name markup to include the file URL.
*/
window.gform.addFilter('gpfup_filename_markup', function (fileName, formId, fieldId, file) {
var fileUrl = file.url || null;
var fileData = JSON.parse(sessionStorage.getItem('gpep_filedata_' + formId + '_' + fieldId)) || [];
if ( !fileUrl) {
if (typeof file.getNative === 'function') {
var nativeFile = file.getNative();
if (nativeFile instanceof File) {
fileUrl = URL.createObjectURL(nativeFile);
}
} else if (Array.isArray(fileData)) { // Find the file URL from `fileData` based on the uploaded file name.
var matchedFile = fileData.find(item => item.uploaded_filename === file.name);
if (matchedFile) {
fileUrl = matchedFile.url;
}
}
}
const sanitizedFileUrl = encodeURI(fileUrl || '');
const sanitizedFileName = fileName.replace(/[<>&"']/g, (c) => {
const escapes = {'<': '<', '>': '>', '&': '&', '"': '"', "'": '''};
return escapes[c];
});
return fileUrl
? `<a href="${sanitizedFileUrl}" target="_blank" rel="noopener noreferrer">${sanitizedFileName}</a>`
: sanitizedFileName;
});
};
self.init();
}
})(jQuery);
</script>
<?php
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}
$args = array(
'formId' => $this->_args['form_id'],
'fieldId' => $this->_args['field_id'],
);
$script = 'new ' . __CLASS__ . '( ' . wp_json_encode( $args ) . ' );';
$slug = implode( '_', array( strtolower( __CLASS__ ), $this->_args['form_id'], $this->_args['field_id'] ) );
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id'];
}
}
// Usage instructions.
/*
* Example Usage (for demonstration purposes only):
*
* - Update 'form_id' and 'field_id' to match your specific requirements.
*/
new GPFUP_Update_Filename_Markup(
array(
'form_id' => 33,
'field_id' => 4, // Update to your file upload field ID.
)
);