GravityView  2.1.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  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  *
75  * @since 2.0
76  * @param \GV\Template_Context The context.
77  *
78  * @return array Array of file output, with `file_path` and `html` keys (see comments above)
79  */
80  static function get_files_array( $value, $gv_class, $context = null ) {
81 
82  if ( $context instanceof \GV\Template_Context ) {
83  $field = $context->field->field;
84  $field_settings = $context->field->as_configuration();
85  $entry = $context->entry->as_entry();
86  $field_value = $context->value;
87  global $post;
88  $base_id = $post ? $post->ID : $context->view->ID;
89 
90  $is_single = $context->request->is_entry();
91  $lightbox = $context->view->settings->get( 'lightbox', false );
92 
93  /** A compatibility array that's required by some of the deprecated filters. */
94  $field_compat = array(
95  'form' => $context->source->form,
96  'field_id' => $context->field->ID,
97  'field' => $field,
98  'field_settings' => $field_settings,
99  'value' => $field_value,
100  'display_value' => $context->display_value,
101  'format' => 'html',
102  'entry' => $entry,
103  'field_type' => $context->field->type,
104  'field_path' => $context->template->located_template,
105  );
106  } else {
107 
108  _doing_it_wrong( __METHOD__, '2.0', 'Please pass a \GV\Template_Context object as the 3rd parameter' );
109 
111  /** @deprecated path */
112  $gv_field_array = $gravityview_view->getCurrentField();
113 
114  /** @var GF_Field_FileUpload $field */
115  $field = \GV\Utils::get( $gv_field_array, 'field' );
116  $field_settings = \GV\Utils::get( $gv_field_array, 'field_settings' );
117  $entry = \GV\Utils::get( $gv_field_array, 'entry' );
118  $field_value = \GV\Utils::get( $gv_field_array, 'value' );
119  $base_id = null;
120 
121  $is_single = gravityview_get_context() === 'single';
122  $lightbox = ! empty( $gravityview_view->atts['lightbox'] );
123  $field_compat = $gravityview_view->getCurrentField();
124  }
125 
126  $output_arr = array();
127 
128  // Get an array of file paths for the field.
129  $file_paths = \GV\Utils::get( $field , 'multipleFiles' ) ? json_decode( $value ) : array( $value );
130 
131  // The $value JSON was probably truncated; let's check lead_detail_long.
132  if ( ! is_array( $file_paths ) ) {
133  $full_value = RGFormsModel::get_lead_field_value( $entry, $field );
134  $file_paths = json_decode( $full_value );
135  }
136 
137  if ( ! is_array( $file_paths ) ) {
138  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 ) ) );
139  return $output_arr;
140  }
141 
142  // Process each file path
143  foreach ( $file_paths as $file_path ) {
144 
145  $rendered = null;
146 
147  // If the site is HTTPS, use HTTPS
148  if ( function_exists('set_url_scheme') ) {
149  $file_path = set_url_scheme( $file_path );
150  }
151 
152  // This is from Gravity Forms's code
153  $file_path = esc_attr( str_replace( " ", "%20", $file_path ) );
154 
155  // Get file path information
156  $file_path_info = pathinfo( $file_path );
157 
158  // If pathinfo() gave us the extension of the file, run the switch statement using that.
159  $extension = empty( $file_path_info['extension'] ) ? NULL : strtolower( $file_path_info['extension'] );
160  $basename = $file_path_info['basename'];
161 
162  // Get the secure download URL
163  $is_secure = false;
164  $insecure_file_path = $file_path;
165  $secure_file_path = $field->get_download_url( $file_path );
166  $text = $basename;
167 
168  if ( $secure_file_path !== $file_path ) {
169  $basename = basename( $secure_file_path );
170  $file_path = $secure_file_path;
171  $is_secure = true;
172  }
173 
174  /**
175  * @filter `gravityview/fields/fileupload/file_path` Modify the file path before generating a link to it
176  * @since 1.22.3
177  * @since 2.0 Added $context parameter
178  * @param string $file_path Path to the file uploaded by Gravity Forms
179  * @param array $field_settings Array of GravityView field settings
180  * @param \GV\Template_Context $context The context.
181  */
182  $file_path = apply_filters( 'gravityview/fields/fileupload/file_path', $file_path, $field_settings, $context );
183 
184  // Audio
185  if ( in_array( $extension, wp_get_audio_extensions() ) ) {
186  if ( shortcode_exists( 'audio' ) ) {
187 
188  /**
189  * @filter `gravityview_audio_settings` Modify the settings passed to the `wp_video_shortcode()` function
190  * @since 1.2
191  * @param array $audio_settings Array with `src` and `class` keys
192  * @since 2.0
193  * @param \GV\Template_Context $context The context.
194  */
195  $audio_settings = apply_filters( 'gravityview_audio_settings', array(
196  'src' => $insecure_file_path, // Needs to be insecure path so WP can parse extension
197  'class' => 'wp-audio-shortcode gv-audio gv-field-id-'.$field_settings['id']
198  ), $context );
199 
200  /**
201  * Generate the audio shortcode
202  * @see http://codex.wordpress.org/Audio_Shortcode
203  * @see https://developer.wordpress.org/reference/functions/wp_audio_shortcode/
204  */
205  $rendered = wp_audio_shortcode( $audio_settings );
206 
207  if ( $is_secure ) {
208 
209  // The shortcode adds instance URL args: add_query_arg( '_', $instance, $atts[ $fallback ] )
210  // these break the path, since we already have "?" in the URL
211  $rendered = str_replace( '?_=', '&_=', $rendered );
212 
213  foreach ( array( 'esc_attr', 'esc_html', 'esc_url', 'trim' /** noop */ ) as $f ) {
214  $rendered = str_replace( $f( $insecure_file_path ), $f( $secure_file_path ), $rendered );
215  }
216  }
217  }
218 
219  // Video
220  } else if ( in_array( $extension, wp_get_video_extensions() ) ) {
221 
222  if ( shortcode_exists( 'video' ) ) {
223 
224  /**
225  * @filter `gravityview_video_settings` Modify the settings passed to the `wp_video_shortcode()` function
226  * @since 1.2
227  * @param array $video_settings Array with `src` and `class` keys
228  * @since 2.0
229  * @param \GV\Template_Context $context The context.
230  */
231  $video_settings = apply_filters( 'gravityview_video_settings', array(
232  'src' => $insecure_file_path, // Needs to be insecure path so WP can parse extension
233  'class' => 'wp-video-shortcode gv-video gv-field-id-'.$field_settings['id']
234  ), $context );
235 
236  /**
237  * Generate the video shortcode
238  * @see http://codex.wordpress.org/Video_Shortcode
239  * @see https://developer.wordpress.org/reference/functions/wp_video_shortcode/
240  */
241  $rendered = wp_video_shortcode( $video_settings );
242 
243  if ( $is_secure ) {
244 
245  // The shortcode adds instance URL args: add_query_arg( '_', $instance, $atts[ $fallback ] )
246  // these break the path, since we already have "?" in the URL
247  $rendered = str_replace( '?_=', '&_=', $rendered );
248 
249  foreach ( array( 'esc_attr', 'esc_html', 'esc_url', 'trim' /** noop */ ) as $f ) {
250  $rendered = str_replace( $f( $insecure_file_path ), $f( $secure_file_path ), $rendered );
251  }
252  }
253  }
254 
255  // PDF
256  } else if ( $extension === 'pdf' ) {
257 
258  // PDF needs to be displayed in an IFRAME
259  $file_path = add_query_arg( array( 'TB_iframe' => 'true' ), $file_path );
260 
261  // Images
262  } else if ( in_array( $extension, array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ) ) ) {
263  $image_atts = array(
264  'src' => $file_path,
265  'class' => 'gv-image gv-field-id-' . $field_settings['id'],
266  'alt' => $field_settings['label'],
267  'width' => ( $is_single ? null : 250 )
268  );
269 
270  if ( $is_secure ) {
271  $image_atts['validate_src'] = false;
272  }
273 
274  /**
275  * Modify the default image attributes for uploaded images
276  *
277  * @since 2.0
278  * @see GravityView_Image For the available attributes
279  *
280  * @param array $image_atts
281  */
282  $image_atts = apply_filters( 'gravityview/fields/fileupload/image_atts', $image_atts );
283 
285 
287 
288  /**
289  * @filter `gravityview/fields/fileupload/allow_insecure_lightbox` Allow insecure links to be shown for the lighbox.
290  * Thickbox doesn't work with secure URLs :(
291  * @param[in,out] bool True or not. DANGER! DANGER! Default: false! Override at your own RISK!!!
292  * @param array $field_settings The field settings.
293  * @param \GV\Template_Context $context The context.
294  */
295  $override_security = apply_filters( 'gravityview/fields/fileupload/allow_insecure_lightbox', false, $file_path, $field_settings, $context );
296 
297  if ( $lightbox && empty( $field_settings['show_as_link'] ) && ( ! $is_secure || $override_security ) ) {
298  $lightbox_link_atts = array(
299  'rel' => sprintf( "%s-%s", $gv_class, $entry_slug ),
300  'class' => 'thickbox',
301  );
302 
303  if ( $override_security ) {
304  $image_atts['src'] = $insecure_file_path;
306  $file_path = $insecure_file_path;
307  // :( a kitten died somewhere
308  }
309 
310  $rendered = gravityview_get_link( $file_path, $image->html(), $lightbox_link_atts );
311  } else {
312  $rendered = $image->html();
313  }
314 
315  // Show as link should render the image regardless.
316  if ( ! empty( $field_settings['show_as_link'] ) ) {
317  $text = $rendered;
318  }
319  }
320 
321  /**
322  * @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
323  * @since 1.5.1
324  * @param bool $disable_wrapped_link whether to wrap the content with a link to the content object.
325  * @param array $field_compat Current GravityView field array
326  * @see GravityView_API:field_value() for info about $gravityview_view->field_data
327  * @since 2.0
328  * @param \GV\Template_Context $context The context.
329  */
330  $disable_wrapped_link = apply_filters( 'gravityview/fields/fileupload/disable_link', false, $field_compat, $context );
331 
332  // Output textualized content where
333  if ( ! $disable_wrapped_link && ( ! empty( $field_settings['link_to_file'] ) || ! empty( $field_settings['show_as_link'] ) ) ) {
334  /**
335  * Modify the link text (defaults to the file name)
336  *
337  * @since 1.7
338  *
339  * @param string $content The existing anchor content. Could be `<img>` tag, audio/video embed or the file name
340  * @param array $field_compat Current GravityView field array
341  * @since 2.0
342  * @param \GV\Template_Context $context The context.
343  */
344  $content = apply_filters( 'gravityview/fields/fileupload/link_content', $text, $field_compat, $context );
345 
346  if ( empty( $field_settings['show_as_link'] ) ) {
347  /**
348  * @filter `gravityview/fields/fileupload/link_atts` Modify the link attributes for a file upload field
349  * @param array|string $link_atts Array or attributes string
350  * @param array $field_compat Current GravityView field array
351  * @since 2.0
352  * @param \GV\Template_Context $context The context.
353  */
354  $link_atts = apply_filters( 'gravityview/fields/fileupload/link_atts', array( 'target' => '_blank' ), $field_compat, $context );
355 
357  }
358  } else {
359  $content = empty( $rendered ) ? $text : $rendered;
360  }
361 
362  $output_arr[] = array(
363  'file_path' => $file_path,
364  'content' => $content
365  );
366 
367  } // End foreach loop
368 
369  /**
370  * @filter `gravityview/fields/fileupload/files_array` Modify the files array
371  * @since 1.7
372  * @param array $output_arr Associative array of files \n
373  * @type string $file_path The path to the file as stored in Gravity Forms \n
374  * @type string $content The generated output for the file \n
375  * @param array $field_compat Current GravityView field array
376  * @since 2.0
377  * @param \GV\Template_Context $context The context.
378  */
379  $output_arr = apply_filters( 'gravityview/fields/fileupload/files_array', $output_arr, $field_compat, $context );
380 
381  return $output_arr;
382  }
383 }
384 
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
$image_atts
Definition: post_image.php:92
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
$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:497
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
global $post
gravityview_get_context()
Returns the current GravityView context, or empty string if not GravityView.
Definition: class-api.php:1158
$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