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