GravityView  2.10.1
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-fileupload.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-fileupload.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
9 
10  var $name = 'fileupload';
11 
12  var $_gf_field_class_name = 'GF_Field_FileUpload';
13 
14  var $is_searchable = true;
15 
16  var $search_operators = array( 'contains' );
17 
18  var $group = 'advanced';
19 
20  var $icon = 'dashicons-upload';
21 
22  public function __construct() {
23  $this->label = esc_html__( 'File Upload', 'gravityview' );
24  parent::__construct();
25  }
26 
27  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
28 
29  unset( $field_options['search_filter'] );
30 
31  if( 'edit' === $context ) {
32  return $field_options;
33  }
34 
35  $add_options['link_to_file'] = array(
36  'type' => 'checkbox',
37  'label' => __( 'Display as a Link:', 'gravityview' ),
38  'desc' => __('Display the uploaded files as links, rather than embedded content.', 'gravityview'),
39  'value' => false,
40  'merge_tags' => false,
41  );
42 
43  $add_options['image_width'] = array(
44  'type' => 'text',
45  'label' => __( 'Custom Width:', 'gravityview' ),
46  'desc' => __( 'Override the default image width (250).', 'gravityview' ),
47  'value' => '250',
48  'merge_tags' => false,
49  );
50 
51  return $add_options + $field_options;
52  }
53 
54  /**
55  * Trick the GF fileupload field to render with the proper HTML ID to enable the plupload JS to work properly
56  *
57  * @param array $form The Form Object currently being processed.
58  * @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
59  * @param null|array $entry Null or the Entry Object currently being edited.
60  * @param GF_Field_FileUpload $field Gravity Forms field
61  *
62  * @return string
63  */
65 
66  $field->_is_entry_detail = true;
67 
68  $return = $field->get_field_input( $form, $field_value, $entry );
69 
70  return $return;
71  }
72 
73  /**
74  * Return an array of files prepared for output.
75  *
76  * Processes files by file type and generates unique output for each. Returns array for each file, with the following keys:
77  * - `file_path` => The file path of the file, with a line break
78  * - `html` => The file output HTML formatted
79  *
80  * @since 1.2
81  * @todo Support `playlist` shortcode for playlist of video/audio
82  * @param string $value Field value passed by Gravity Forms. String of file URL, or serialized string of file URL array
83  * @param string $gv_class Field class to add to the output HTML
84  *
85  * @since 2.0
86  * @param \GV\Template_Context The context.
87  *
88  * @return array Array of file output, with `file_path` and `html` keys (see comments above)
89  */
90  static function get_files_array( $value, $gv_class, $context = null ) {
91 
92  if ( $context instanceof \GV\Template_Context ) {
93  $field = $context->field->field;
94  $field_settings = $context->field->as_configuration();
95  $entry = $context->entry->as_entry();
96  $field_value = $context->value;
97  global $post;
98  $base_id = $post ? $post->ID : $context->view->ID;
99 
100  $is_single = $context->request->is_entry();
101  $lightbox = $context->view->settings->get( 'lightbox', false );
102 
103  /** A compatibility array that's required by some of the deprecated filters. */
104  $field_compat = array(
105  'form' => $context->source->form,
106  'field_id' => $context->field->ID,
107  'field' => $field,
108  'field_settings' => $field_settings,
109  'value' => $field_value,
110  'display_value' => $context->display_value,
111  'format' => 'html',
112  'entry' => $entry,
113  'field_type' => $context->field->type,
114  'field_path' => $context->template->located_template,
115  );
116  } else {
117 
118  _doing_it_wrong( __METHOD__, '2.0', 'Please pass a \GV\Template_Context object as the 3rd parameter' );
119 
121  /** @deprecated path */
122  $gv_field_array = $gravityview_view->getCurrentField();
123 
124  /** @type GF_Field_FileUpload $field */
125  $field = \GV\Utils::get( $gv_field_array, 'field' );
126  $field_settings = \GV\Utils::get( $gv_field_array, 'field_settings' );
127  $entry = \GV\Utils::get( $gv_field_array, 'entry' );
128  $field_value = \GV\Utils::get( $gv_field_array, 'value' );
129  $base_id = null;
130 
131  $is_single = gravityview_get_context() === 'single';
132  $lightbox = ! empty( $gravityview_view->atts['lightbox'] );
133  $field_compat = $gravityview_view->getCurrentField();
134  }
135 
136  $output_arr = array();
137 
138  // Get an array of file paths for the field.
139  $file_paths = \GV\Utils::get( $field , 'multipleFiles' ) ? json_decode( $value ) : array( $value );
140 
141  // The $value JSON was probably truncated; let's check lead_detail_long.
142  if ( ! is_array( $file_paths ) ) {
143  $full_value = RGFormsModel::get_lead_field_value( $entry, $field );
144  $file_paths = json_decode( $full_value );
145  }
146 
147  if ( ! is_array( $file_paths ) ) {
148  gravityview()->log->error( 'Field does not have a valid image array. JSON decode may have failed.', array( 'data' => array( '$value' => $value, '$field_value' => $field_value ) ) );
149  return $output_arr;
150  }
151 
152  $field_settings_backup = $field_settings;
153  // Process each file path
154  foreach ( $file_paths as $index => $file_path ) {
155 
156  $rendered = null;
157 
158  // If the site is HTTPS, use HTTPS
159  if ( function_exists('set_url_scheme') ) {
160  $file_path = set_url_scheme( $file_path );
161  }
162 
163  // This is from Gravity Forms's code
164  $file_path = esc_attr( str_replace( " ", "%20", $file_path ) );
165 
166  // Get file path information
167  $file_path_info = pathinfo( $file_path );
168 
169  // If pathinfo() gave us the extension of the file, run the switch statement using that.
170  $extension = empty( $file_path_info['extension'] ) ? NULL : strtolower( $file_path_info['extension'] );
171  $basename = $file_path_info['basename'];
172 
173  // Get the secure download URL
174  $is_secure = false;
175  $disable_lightbox = false;
176  $insecure_file_path = $file_path;
177  $secure_file_path = $field->get_download_url( $file_path );
178  $text = $basename;
179 
180  if ( $secure_file_path !== $file_path ) {
181  $basename = basename( $secure_file_path );
182  $file_path = $secure_file_path;
183  $is_secure = true;
184  }
185 
186  /**
187  * @filter `gravityview/fields/fileupload/file_path` Modify the file path before generating a link to it
188  * @since 1.22.3
189  * @since 2.0 Added $context parameter
190  * @since 2.8.2
191  * @param string $file_path Path to the file uploaded by Gravity Forms
192  * @param array $field_settings Array of GravityView field settings
193  * @param \GV\Template_Context $context The context.
194  * @param int $index The current index of the $file_paths array being processed
195  */
196  $file_path = apply_filters( 'gravityview/fields/fileupload/file_path', $file_path, $field_settings, $context, $index );
197 
198  // Audio
199  if ( in_array( $extension, wp_get_audio_extensions() ) ) {
200  if ( shortcode_exists( 'audio' ) ) {
201 
202  /**
203  * @filter `gravityview_audio_settings` Modify the settings passed to the `wp_video_shortcode()` function
204  * @since 1.2
205  * @param array $audio_settings Array with `src` and `class` keys
206  * @since 2.0
207  * @param \GV\Template_Context $context The context.
208  */
209  $audio_settings = apply_filters( 'gravityview_audio_settings', array(
210  'src' => $insecure_file_path, // Needs to be insecure path so WP can parse extension
211  'class' => 'wp-audio-shortcode gv-audio gv-field-id-'.$field_settings['id']
212  ), $context );
213 
214  /**
215  * Generate the audio shortcode
216  * @see http://codex.wordpress.org/Audio_Shortcode
217  * @see https://developer.wordpress.org/reference/functions/wp_audio_shortcode/
218  */
219  $rendered = wp_audio_shortcode( $audio_settings );
220 
221  if ( $is_secure ) {
222 
223  // The shortcode adds instance URL args: add_query_arg( '_', $instance, $atts[ $fallback ] )
224  // these break the path, since we already have "?" in the URL
225  $rendered = str_replace( '?_=', '&_=', $rendered );
226 
227  foreach ( array( 'esc_attr', 'esc_html', 'esc_url', 'trim' /** noop */ ) as $f ) {
228  $rendered = str_replace( $f( $insecure_file_path ), $f( $secure_file_path ), $rendered );
229  }
230  }
231  }
232 
233  // Video
234  } else if ( in_array( $extension, wp_get_video_extensions() ) ) {
235 
236  if ( shortcode_exists( 'video' ) ) {
237 
238  /**
239  * @filter `gravityview_video_settings` Modify the settings passed to the `wp_video_shortcode()` function
240  * @since 1.2
241  * @param array $video_settings Array with `src` and `class` keys
242  * @since 2.0
243  * @param \GV\Template_Context $context The context.
244  */
245  $video_settings = apply_filters( 'gravityview_video_settings', array(
246  'src' => $insecure_file_path, // Needs to be insecure path so WP can parse extension
247  'class' => 'wp-video-shortcode gv-video gv-field-id-'.$field_settings['id']
248  ), $context );
249 
250  /**
251  * Generate the video shortcode
252  * @see http://codex.wordpress.org/Video_Shortcode
253  * @see https://developer.wordpress.org/reference/functions/wp_video_shortcode/
254  */
255  $rendered = wp_video_shortcode( $video_settings );
256 
257  if ( $is_secure ) {
258 
259  // The shortcode adds instance URL args: add_query_arg( '_', $instance, $atts[ $fallback ] )
260  // these break the path, since we already have "?" in the URL
261  $rendered = str_replace( '?_=', '&_=', $rendered );
262 
263  foreach ( array( 'esc_attr', 'esc_html', 'esc_url', 'trim' /** noop */ ) as $f ) {
264  $rendered = str_replace( $f( $insecure_file_path ), $f( $secure_file_path ), $rendered );
265  }
266  }
267  }
268 
269  // PDF or Text
270  } else if ( in_array( $extension, array( 'pdf', 'txt' ), true ) ) {
271 
272  // Don't add query arg when exporting as CSV
273  if ( $context instanceof \GV\Template_Context && ! ( $context->template instanceof \GV\Field_CSV_Template ) ) {
274  // File needs to be displayed in an IFRAME
275  $file_path = add_query_arg( array( 'gv-iframe' => 'true' ), $file_path );
276  }
277 
278  $field_settings['link_to_file'] = true;
279 
280  // Images
281  } else if ( in_array( $extension, array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ) ) ) {
282  $width = \GV\Utils::get( $field_settings, 'image_width', 250 );
283  $image_atts = array(
284  'src' => $file_path,
285  'class' => 'gv-image gv-field-id-' . $field_settings['id'],
286  'alt' => $field_settings['label'],
287  'width' => ( $is_single ? null : ( $width ? $width: 250 ) )
288  );
289 
290  if ( $is_secure ) {
291  $image_atts['validate_src'] = false;
292  }
293 
294  /**
295  * Modify the default image attributes for uploaded images
296  *
297  * @since 2.0
298  * @see GravityView_Image For the available attributes
299  *
300  * @param array $image_atts
301  */
302  $image_atts = apply_filters( 'gravityview/fields/fileupload/image_atts', $image_atts );
303 
305 
306  $gv_entry = \GV\GF_Entry::from_entry( $entry );
307 
308  $entry_slug = $gv_entry->get_slug();
309 
310  unset( $gv_entry );
311 
312  if ( $lightbox && empty( $field_settings['show_as_link'] ) ) {
313  $lightbox_link_atts = array(
314  'rel' => sprintf( "%s-%s", $gv_class, $entry_slug ),
315  'class' => '',
316  );
317 
318  $lightbox_link_atts = apply_filters( 'gravityview/fields/fileupload/link_atts', $lightbox_link_atts, $field_compat, $context );
319 
320  $rendered = gravityview_get_link( $file_path, $image->html(), $lightbox_link_atts );
321  } else {
322  $rendered = $image->html();
323  }
324 
325  // Show as link should render the image regardless.
326  if ( ! empty( $field_settings['show_as_link'] ) ) {
327  $text = $rendered;
328  }
329  }
330  // For all other non-media file types (ZIP, for example), always show as a link regardless of setting.
331  else {
332  $field_settings['link_to_file'] = true;
333  $disable_lightbox = true;
334  }
335 
336  /**
337  * @filter `gravityview/fields/fileupload/disable_link` Filter to alter the default behaviour of wrapping images (or image names) with a link to the content object
338  * @since 1.5.1
339  * @param bool $disable_wrapped_link whether to wrap the content with a link to the content object.
340  * @param array $field_compat Current GravityView field array
341  * @see GravityView_API:field_value() for info about $gravityview_view->field_data
342  * @since 2.0
343  * @param \GV\Template_Context $context The context.
344  */
345  $disable_wrapped_link = apply_filters( 'gravityview/fields/fileupload/disable_link', false, $field_compat, $context );
346 
347  // Output textualized content where
348  if ( ! $disable_wrapped_link && ( ! empty( $field_settings['link_to_file'] ) || ! empty( $field_settings['show_as_link'] ) ) ) {
349  /**
350  * Modify the link text (defaults to the file name)
351  *
352  * @since 1.7
353  *
354  * @param string $content The existing anchor content. Could be `<img>` tag, audio/video embed or the file name
355  * @param array $field_compat Current GravityView field array
356  * @since 2.0
357  * @param \GV\Template_Context $context The context.
358  */
359  $content = apply_filters( 'gravityview/fields/fileupload/link_content', $text, $field_compat, $context );
360 
361  if ( empty( $field_settings['show_as_link'] ) ) {
362  /**
363  * @filter `gravityview/fields/fileupload/link_atts` Modify the link attributes for a file upload field
364  * @since 2.0 Added $context
365  * @since 2.11 Added $additional_details
366  * @param array|string $link_atts Array or attributes string
367  * @param array $field_compat Current GravityView field array
368  * @param \GV\Template_Context $context The context.
369  * @param array $additional_details Array of additional details about the file. {
370  * @type string $file_path URL to file.
371  * @type string $insecure_file_path URL to insecure file.
372  * }
373  */
374  $link_atts = apply_filters( 'gravityview/fields/fileupload/link_atts', array( 'target' => '_blank' ), $field_compat, $context, compact( 'file_path', 'insecure_file_path', 'disable_lightbox' ) );
375 
377  }
378  } else {
379  $content = empty( $rendered ) ? $text : $rendered;
380  }
381 
382  $output_arr[] = array(
383  'file_path' => $file_path,
384  'content' => $content
385  );
386 
387  $field_settings = $field_settings_backup; // reset to default
388  } // End foreach loop
389 
390  /**
391  * @filter `gravityview/fields/fileupload/files_array` Modify the files array
392  * @since 1.7
393  * @since 2.0 Added $context
394  * @param array $output_arr Associative array of files. {
395  * @type string $file_path The path to the file as stored in Gravity Forms.
396  * @type string $content The generated output for the file.
397  * }
398  * @param array $field_compat Current GravityView field array.
399  * @param \GV\Template_Context $context The context.
400  */
401  $output_arr = apply_filters( 'gravityview/fields/fileupload/files_array', $output_arr, $field_compat, $context );
402 
403  return $output_arr;
404  }
405 }
406 
Modify field settings by extending this class.
$image
Definition: post_image.php:98
Generic class for generating image tag.
static getInstance( $passed_post=NULL)
get_field_input( $form, $field_value, $entry, $field)
Trick the GF fileupload field to render with the proper HTML ID to enable the plupload JS to work pro...
$field_settings['content']
Definition: custom.php:27
gravityview_get_link( $href='', $anchor_text='', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
gravityview()
Definition: _stubs.php:26
global $post
Definition: delete-entry.php:7
static from_entry( $entry)
Construct a instance from a Gravity Forms entry array.
$image_atts
Definition: post_image.php:92
if(gravityview() ->plugin->is_GF_25()) $form
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
static get_files_array( $value, $gv_class, $context=null)
Return an array of files prepared for output.
$field_id
Definition: time.php:17
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
if(empty( $created_by)) $form_id
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview_get_context()
Returns the current GravityView context, or empty string if not GravityView.
Definition: class-api.php:1318
$entry_slug
Definition: notes.php:30
$entry
Definition: notes.php:27
$field_value
Definition: checkbox.php:24
$field
Definition: gquiz_grade.php:11