-
Notifications
You must be signed in to change notification settings - Fork 92
gpfup-update-filename-markup.php: Added new snippet.
#1050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
malayladu
merged 1 commit into
master
from
malay/add/72809-gpfup-update-filename-markup
Apr 18, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Gravity Perks // File Upload Pro // Update Filename Markup | ||
| * http://gravitywiz.com/documentation/gravity-forms-file-upload-pro | ||
| * | ||
| * This snippet allows you to update the filename markup to include the file URL. This snippet also works with the | ||
| * GP Easy Passthrough plugin when multiple files are uploaded using GP File Upload Pro. | ||
| * | ||
| * Installation instructions: | ||
| * 1. https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets | ||
| * 2. See usage instructions at the bottom of the file | ||
| */ | ||
| class GPFUP_Update_Filename_Markup { | ||
|
|
||
| private $_args = array(); | ||
|
|
||
| public function __construct( $args = array() ) { | ||
| // Set our default arguments, parse against the provided arguments, and store for use throughout the class. | ||
| $this->_args = wp_parse_args( $args, array( | ||
| 'form_id' => false, | ||
| 'field_id' => false, | ||
| ) ); | ||
|
|
||
| // Do not proceed if Gravity Forms is not installed & activated. | ||
| if ( ! class_exists( 'GFCommon' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| add_action( 'init', array( $this, 'init' ) ); | ||
| } | ||
|
|
||
| public function init() { | ||
| $form_id = $this->_args['form_id']; | ||
| $field_id = $this->_args['field_id']; | ||
|
|
||
| // Time for hooks. | ||
| add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 ); | ||
| add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 ); | ||
|
|
||
| $args = array( 'gpeb_post_file_population' ); | ||
|
|
||
| // If a form ID is provided, add it to the args. | ||
| if ( $form_id ) { | ||
| $args[] = $form_id; | ||
|
|
||
| // If a field ID is provided, add it to the args. | ||
| if ( $field_id ) { | ||
| $args[] = $field_id; | ||
| } | ||
| } | ||
|
|
||
| add_action( implode( '_', $args ), array( | ||
| $this, | ||
| 'populate_file_data', | ||
| ), 10, 3 ); | ||
| } | ||
|
|
||
| /** | ||
| * Populate file data. | ||
| * | ||
| * @param $file_upload_data | ||
| * @param $form | ||
| * @param $field | ||
| * | ||
| * @return void | ||
| */ | ||
| public function populate_file_data( $file_upload_data, $form, $field ) { | ||
| add_action( 'wp_print_footer_scripts', function () use ( $file_upload_data, $form, $field ) { | ||
| $form_id = rgar( $form, 'id' ); | ||
| $field_id = rgar( $field, 'id' ); | ||
|
|
||
| echo '<script> | ||
| jQuery(document).ready(function($) { | ||
| var fileData = ' . wp_json_encode( $file_upload_data ) . '; | ||
| var formId = ' . absint( $form_id ) . '; | ||
| var fieldId = ' . absint( $field_id ) . "; | ||
|
|
||
| sessionStorage.setItem('gpep_filedata_' + formId + '_' + fieldId, JSON.stringify(fileData)); | ||
| }); | ||
| </script>"; | ||
| } ); | ||
| } | ||
|
|
||
| public function load_form_script( $form, $is_ajax_enabled ) { | ||
| if ( ! $this->is_applicable_form( $form ) ) { | ||
| return $form; | ||
| } | ||
|
|
||
| if ( ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) { | ||
| add_action( 'wp_footer', array( $this, 'output_script' ) ); | ||
| } | ||
|
|
||
| if ( ! has_action( 'gform_preview_footer', array( $this, 'output_script' ) ) ) { | ||
| add_action( 'gform_preview_footer', array( $this, 'output_script' ) ); | ||
| } | ||
|
|
||
| return $form; | ||
| } | ||
|
|
||
| public function output_script() { | ||
| ?> | ||
|
|
||
| <script type="text/javascript"> | ||
|
|
||
| (function ($) { | ||
|
|
||
| window.<?php echo __CLASS__; ?> = function (args) { | ||
| self.init = function () { | ||
| /** | ||
| * Filter the file name markup to include the file URL. | ||
| */ | ||
| window.gform.addFilter('gpfup_filename_markup', function (fileName, formId, fieldId, file) { | ||
| var fileUrl = file.url || null; | ||
| var fileData = JSON.parse(sessionStorage.getItem('gpep_filedata_' + formId + '_' + fieldId)) || []; | ||
|
|
||
| if (!fileUrl) { | ||
| if (typeof file.getNative === 'function') { | ||
| var nativeFile = file.getNative(); | ||
| if (nativeFile instanceof File) { | ||
| fileUrl = URL.createObjectURL(nativeFile); | ||
| } | ||
| } else if (Array.isArray(fileData)) { // Find the file URL from `fileData` based on the uploaded file name. | ||
| var matchedFile = fileData.find(item => item.uploaded_filename === file.name); | ||
| if (matchedFile) { | ||
| fileUrl = matchedFile.url; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const sanitizedFileUrl = encodeURI(fileUrl || ''); | ||
| const sanitizedFileName = fileName.replace(/[<>&"']/g, (c) => { | ||
| const escapes = {'<': '<', '>': '>', '&': '&', '"': '"', "'": '''}; | ||
| return escapes[c]; | ||
| }); | ||
|
|
||
| return fileUrl | ||
| ? `<a href="${sanitizedFileUrl}" target="_blank" rel="noopener noreferrer">${sanitizedFileName}</a>` | ||
| : sanitizedFileName; | ||
| }); | ||
| }; | ||
|
|
||
| self.init(); | ||
| } | ||
|
|
||
| })(jQuery); | ||
|
|
||
| </script> | ||
|
|
||
| <?php | ||
| } | ||
|
|
||
| public function add_init_script( $form ) { | ||
| if ( ! $this->is_applicable_form( $form ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $args = array( | ||
| 'formId' => $this->_args['form_id'], | ||
| 'fieldId' => $this->_args['field_id'], | ||
| ); | ||
|
|
||
| $script = 'new ' . __CLASS__ . '( ' . wp_json_encode( $args ) . ' );'; | ||
| $slug = implode( '_', array( strtolower( __CLASS__ ), $this->_args['form_id'], $this->_args['field_id'] ) ); | ||
|
|
||
| GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script ); | ||
| } | ||
|
|
||
| public function is_applicable_form( $form ) { | ||
| $form_id = isset( $form['id'] ) ? $form['id'] : $form; | ||
|
|
||
| return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id']; | ||
| } | ||
| } | ||
|
|
||
| // Usage instructions. | ||
|
|
||
| /* | ||
| * Example Usage (for demonstration purposes only): | ||
| * | ||
| * - Update 'form_id' and 'field_id' to match your specific requirements. | ||
| */ | ||
| new GPFUP_Update_Filename_Markup( | ||
| array( | ||
| 'form_id' => 33, | ||
| 'field_id' => 4, // Update to your file upload field ID. | ||
| ) | ||
| ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.