GravityView  1.19.4
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 $group = 'advanced';
15 
16  public function __construct() {
17  $this->label = esc_html__( 'File Upload', 'gravityview' );
18  parent::__construct();
19  }
20 
21  function field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
22 
23  unset( $field_options['search_filter'] );
24 
25  if( 'edit' === $context ) {
26  return $field_options;
27  }
28 
29  $add_options['link_to_file'] = array(
30  'type' => 'checkbox',
31  'label' => __( 'Display as a Link:', 'gravityview' ),
32  'desc' => __('Display the uploaded files as links, rather than embedded content.', 'gravityview'),
33  'value' => false,
34  'merge_tags' => false,
35  );
36 
37  return $add_options + $field_options;
38  }
39 
40  /**
41  * Return an array of files prepared for output.
42  *
43  * Processes files by file type and generates unique output for each. Returns array for each file, with the following keys:
44  * - `file_path` => The file path of the file, with a line break
45  * - `html` => The file output HTML formatted
46  *
47  * @since 1.2
48  * @todo Support `playlist` shortcode for playlist of video/audio
49  * @param string $value Field value passed by Gravity Forms. String of file URL, or serialized string of file URL array
50  * @param string $gv_class Field class to add to the output HTML
51  * @return array Array of file output, with `file_path` and `html` keys (see comments above)
52  */
53  static function get_files_array( $value, $gv_class ) {
54 
56 
57  $gv_field_array = $gravityview_view->getCurrentField();
58 
59  /** @var GF_Field_FileUpload $field */
60  $field = rgar( $gv_field_array, 'field' );
61  $field_settings = rgar( $gv_field_array, 'field_settings' );
62  $entry = rgar( $gv_field_array, 'entry' );
63  $field_value = rgar( $gv_field_array, 'value' );
64 
65  $output_arr = array();
66 
67  // Get an array of file paths for the field.
68  $file_paths = rgar( $field , 'multipleFiles' ) ? json_decode( $value ) : array( $value );
69 
70  // The $value JSON was probably truncated; let's check lead_detail_long.
71  if ( ! is_array( $file_paths ) ) {
72  $full_value = RGFormsModel::get_lead_field_value( $entry, $field );
73  $file_paths = json_decode( $full_value );
74  }
75 
76  if ( ! is_array( $file_paths ) ) {
77  do_action( 'gravityview_log_error', __METHOD__ . ': Field does not have a valid image array. JSON decode may have failed.', array( '$value' => $value, '$field_value' => $field_value ) );
78  return $output_arr;
79  }
80 
81  // Process each file path
82  foreach( $file_paths as $file_path ) {
83 
84  // If the site is HTTPS, use HTTPS
85  if(function_exists('set_url_scheme')) { $file_path = set_url_scheme($file_path); }
86 
87  // This is from Gravity Forms's code
88  $file_path = esc_attr(str_replace(" ", "%20", $file_path));
89 
90  // If the field is set to link to the single entry, link to it.
91  $link = !empty( $field_settings['show_as_link'] ) ? GravityView_API::entry_link( $entry, $field ) : $file_path;
92 
93  // Get file path information
94  $file_path_info = pathinfo($file_path);
95 
96  $html_format = NULL;
97 
98  $disable_lightbox = false;
99 
100  $disable_wrapped_link = false;
101 
102  // Is this an image?
103  $image = new GravityView_Image(array(
104  'src' => $file_path,
105  'class' => 'gv-image gv-field-id-'.$field_settings['id'],
106  'alt' => $field_settings['label'],
107  'width' => (gravityview_get_context() === 'single' ? NULL : 250)
108  ));
109 
110  $content = $image->html();
111 
112  // The new default content is the image, if it exists. If not, use the file name as the content.
113  $content = !empty( $content ) ? $content : $file_path_info['basename'];
114 
115  // If pathinfo() gave us the extension of the file, run the switch statement using that.
116  $extension = empty( $file_path_info['extension'] ) ? NULL : strtolower( $file_path_info['extension'] );
117 
118 
119  switch( true ) {
120 
121  // Audio file
122  case in_array( $extension, wp_get_audio_extensions() ):
123 
124  $disable_lightbox = true;
125 
126  if( shortcode_exists( 'audio' ) ) {
127 
128  $disable_wrapped_link = true;
129 
130  /**
131  * @filter `gravityview_audio_settings` Modify the settings passed to the `wp_video_shortcode()` function
132  * @since 1.2
133  * @param array $audio_settings Array with `src` and `class` keys
134  */
135  $audio_settings = apply_filters( 'gravityview_audio_settings', array(
136  'src' => $file_path,
137  'class' => 'wp-audio-shortcode gv-audio gv-field-id-'.$field_settings['id']
138  ));
139 
140  /**
141  * Generate the audio shortcode
142  * @see http://codex.wordpress.org/Audio_Shortcode
143  * @see https://developer.wordpress.org/reference/functions/wp_audio_shortcode/
144  */
145  $content = wp_audio_shortcode( $audio_settings );
146 
147  }
148 
149  break;
150 
151  // Video file
152  case in_array( $extension, wp_get_video_extensions() ):
153 
154  $disable_lightbox = true;
155 
156  if( shortcode_exists( 'video' ) ) {
157 
158  $disable_wrapped_link = true;
159 
160  /**
161  * @filter `gravityview_video_settings` Modify the settings passed to the `wp_video_shortcode()` function
162  * @since 1.2
163  * @param array $video_settings Array with `src` and `class` keys
164  */
165  $video_settings = apply_filters( 'gravityview_video_settings', array(
166  'src' => $file_path,
167  'class' => 'wp-video-shortcode gv-video gv-field-id-'.$field_settings['id']
168  ));
169 
170  /**
171  * Generate the video shortcode
172  * @see http://codex.wordpress.org/Video_Shortcode
173  * @see https://developer.wordpress.org/reference/functions/wp_video_shortcode/
174  */
175  $content = wp_video_shortcode( $video_settings );
176 
177  }
178 
179  break;
180 
181  // PDF
182  case $extension === 'pdf':
183 
184  // PDF needs to be displayed in an IFRAME
185  $link = add_query_arg( array( 'TB_iframe' => 'true' ), $link );
186 
187  break;
188 
189  // if not image, do not set the lightbox (@since 1.5.3)
190  case !in_array( $extension, array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ) ):
191 
192  $disable_lightbox = true;
193 
194  break;
195 
196  }
197 
198  // If using Link to File, override the content.
199  // (We do this here so that the $disable_lightbox can be set. Yes, there's a little more processing time, but oh well.)
200  if( !empty( $field_settings['link_to_file'] ) ) {
201 
202  // Force the content to be the file name
203  $content = $file_path_info["basename"];
204 
205  // Restore the wrapped link
206  $disable_wrapped_link = false;
207 
208  }
209 
210  // Whether to use lightbox or not
211  if( $disable_lightbox || empty( $gravityview_view->atts['lightbox'] ) || !empty( $field_settings['show_as_link'] ) ) {
212 
213  $link_atts = empty( $field_settings['show_as_link'] ) ? array( 'target' => '_blank' ) : array();
214 
215  } else {
216 
218 
219  $link_atts = array(
220  'rel' => sprintf( "%s-%s", $gv_class, $entry_slug ),
221  'class' => 'thickbox',
222  );
223 
224  }
225 
226  /**
227  * @filter `gravityview/fields/fileupload/link_atts` Modify the link attributes for a file upload field
228  * @param array|string $link_atts Array or attributes string
229  * @param array $field Current GravityView field array
230  */
231  $link_atts = apply_filters( 'gravityview/fields/fileupload/link_atts', $link_atts, $gravityview_view->getCurrentField() );
232 
233  /**
234  * @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
235  * @since 1.5.1
236  * @param bool $disable_wrapped_link whether to wrap the content with a link to the content object.
237  * @param array $gravityview_view->field_data
238  * @see GravityView_API:field_value() for info about $gravityview_view->field_data
239  */
240  $disable_wrapped_link = apply_filters( 'gravityview/fields/fileupload/disable_link', $disable_wrapped_link, $gravityview_view->getCurrentField() );
241 
242  // If the HTML output hasn't been overridden by the switch statement above, use the default format
243  if( !empty( $content ) && empty( $disable_wrapped_link ) ) {
244 
245  /**
246  * Modify the link text (defaults to the file name)
247  *
248  * @since 1.7
249  *
250  * @param string $content The existing anchor content. Could be `<img>` tag, audio/video embed or the file name
251  * @param array $field GravityView array of the current field being processed
252  */
253  $content = apply_filters( 'gravityview/fields/fileupload/link_content', $content, $gravityview_view->getCurrentField() );
254 
255  $content = gravityview_get_link( $link, $content, $link_atts );
256  }
257 
258  $output_arr[] = array(
259  'file_path' => $file_path,
260  'content' => $content
261  );
262 
263  } // End foreach loop
264 
265  /**
266  * @filter `gravityview/fields/fileupload/files_array` Modify the files array
267  * @since 1.7
268  * @param array $output_arr Associative array of files \n
269  * @type string $file_path The path to the file as stored in Gravity Forms \n
270  * @type string $content The generated output for the file \n
271  * @param array $field GravityView array of the current field being processed
272  */
273  $output_arr = apply_filters( 'gravityview/fields/fileupload/files_array', $output_arr, $gravityview_view->getCurrentField() );
274 
275  return $output_arr;
276  }
277 
278 }
279 
Modify field settings by extending this class.
$image
Definition: post_image.php:98
Generic class for generating image tag.
$field_settings['content']
Definition: custom.php:27
field_options($field_options, $template_id, $field_id, $context, $input_type)
static entry_link($entry, $post_id=NULL, $add_directory_args=true)
return href for single entry
Definition: class-api.php:642
gravityview_get_link($href= '', $anchor_text= '', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
$field_id
Definition: time.php:17
static getInstance($passed_post=NULL)
gravityview_get_context()
GravityView_View $gravityview_view
Definition: class-api.php:1045
$entry_slug
Definition: notes.php:30
$entry
Definition: notes.php:27
static get_entry_slug($id_or_string, $entry=array())
Get the entry slug for the entry.
Definition: class-api.php:567
$field_value
Definition: checkbox.php:24
$field
Definition: gquiz_grade.php:11
if(empty($field_settings['content'])) $content
Definition: custom.php:37