|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // File Upload Pro // Show Images in Entry Details |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-file-upload-pro/ |
| 5 | + * |
| 6 | + * Instead of an unordered list with links to the files, display the images. Non-images will still |
| 7 | + * be displayed as a text link. |
| 8 | + * |
| 9 | + * By default, the images will be hyperlinks linking to the original image. |
| 10 | + * |
| 11 | + * Instructions: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ |
| 12 | + */ |
| 13 | +add_filter( 'gform_entry_field_value', function ( $display_value, $field, $entry, $form ) { |
| 14 | + if ( ! rgar( $field, 'multipleFiles' ) || ! rgar( $field, 'gpfupEnable' ) ) { |
| 15 | + return $display_value; |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Set to false if the images should not be links. |
| 20 | + */ |
| 21 | + $link_images = true; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string Value from the entry. Is JSON if files are provided. |
| 25 | + */ |
| 26 | + $entry_value = rgar( $entry, $field->id ); |
| 27 | + |
| 28 | + if ( ! $entry_value ) { |
| 29 | + return $entry_value; |
| 30 | + } |
| 31 | + |
| 32 | + $file_urls = json_decode( $entry_value, true ); |
| 33 | + $html = ''; |
| 34 | + |
| 35 | + foreach ( $file_urls as $file_index => $file_url ) { |
| 36 | + /* Skip file if the file is not an image supported by mPDF */ |
| 37 | + $extension = pathinfo( $file_url, PATHINFO_EXTENSION ); |
| 38 | + $file_name = pathinfo( $file_url, PATHINFO_FILENAME ); |
| 39 | + |
| 40 | + if ( ! in_array( strtolower( $extension ), array( |
| 41 | + 'gif', |
| 42 | + 'png', |
| 43 | + 'jpg', |
| 44 | + 'wmf', |
| 45 | + 'svg', |
| 46 | + 'bmp', |
| 47 | + ), true ) ) { |
| 48 | + // Link directly to other file types (e.g. PDF) without further processing |
| 49 | + $html .= '<ul><li><a href="' . esc_url( $file_url ) . '">' . $file_name . '</a>' . "</li></ul>\n"; |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + if ( $link_images ) { |
| 54 | + $html .= '<a href="' . esc_url( $file_url ) . '"><img src="' . $file_url . '" style="max-width: 100%" /></a>' . "\n"; |
| 55 | + } else { |
| 56 | + $html .= '<img src="' . $file_url . '" style="max-width: 100%" />' . "\n"; |
| 57 | + } |
| 58 | + |
| 59 | + if ( count( $file_urls ) > 1 && $file_index + 1 < count( $file_urls ) ) { |
| 60 | + $html .= "<br />\n"; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Strip all HTML except for img, a, ul, li, and br tags. |
| 66 | + */ |
| 67 | + return wp_kses( $html, array( |
| 68 | + 'img' => array( |
| 69 | + 'src' => array(), |
| 70 | + 'style' => array(), |
| 71 | + ), |
| 72 | + 'a' => array( 'href' => array() ), |
| 73 | + 'br' => array(), |
| 74 | + 'ul' => array(), |
| 75 | + 'li' => array(), |
| 76 | + ) ); |
| 77 | +}, 10, 4 ); |
0 commit comments