Forked from spivurno/gw-gravity-forms-rename-uploaded-files-usage.php
Created
September 5, 2016 04:36
-
-
Save phillipwilhelm/7ebc99c5d410743bfe6f8c4b1cf0ae09 to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms // Rename Uploaded Files
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
| <?php | |
| # First and Last Name | |
| new GW_Rename_Uploaded_Files( array( | |
| 'form_id' => 628, | |
| 'field_id' => 3, | |
| 'template' => '{Name (First):1.3}-{Name (Last):1.6}-{filename}' // most merge tags are supported, original file extension is preserved | |
| ) ); | |
| # Form Title Merge Tag | |
| new GW_Rename_Uploaded_Files( array( | |
| 'form_id' => 12, | |
| 'field_id' => 18, | |
| 'template' => '{form_title}-{filename}' // most merge tags are supported, original file extension is preserved | |
| ) ); | |
| # Static File Name | |
| new GW_Rename_Uploaded_Files( array( | |
| 'form_id' => 628, | |
| 'field_id' => 5, | |
| 'template' => 'static-file-name' | |
| ) ); |
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
| <?php | |
| /** | |
| * Gravity Wiz // Gravity Forms // Rename Uploaded Files | |
| * | |
| * Rename uploaded files for Gravity Forms. You can create a static naming template or using merge tags to base names on user input. | |
| * | |
| * Features: | |
| * + supports single and multi-file upload fields | |
| * + flexible naming template with support for static and dynamic values via GF merge tags | |
| * | |
| * Uses: | |
| * + add a prefix or suffix to file uploads | |
| * + include identifying submitted data in the file name like the user's first and last name | |
| * | |
| * @version 1.2 | |
| * @author David Smith <[email protected]> | |
| * @license GPL-2.0+ | |
| * @link http://gravitywiz.com/... | |
| */ | |
| class GW_Rename_Uploaded_Files { | |
| 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, | |
| 'template' => '' | |
| ) ); | |
| // do version check in the init to make sure if GF is going to be loaded, it is already loaded | |
| add_action( 'init', array( $this, 'init' ) ); | |
| } | |
| public function init() { | |
| // make sure we're running the required minimum version of Gravity Forms | |
| if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) { | |
| return; | |
| } | |
| add_action( 'gform_pre_submission', array( $this, 'rename_uploaded_files' ) ); | |
| } | |
| function rename_uploaded_files( $form ) { | |
| if( ! $this->is_applicable_form( $form ) ) { | |
| return; | |
| } | |
| foreach( $form['fields'] as &$field ) { | |
| if( ! $this->is_applicable_field( $field ) ) { | |
| continue; | |
| } | |
| $is_multi_file = rgar( $field, 'multipleFiles' ) == true; | |
| $input_name = sprintf( 'input_%s', $field['id'] ); | |
| $uploaded_files = rgars( GFFormsModel::$uploaded_files, "{$form['id']}/{$input_name}" ); | |
| if( $is_multi_file && ! empty( $uploaded_files ) && is_array( $uploaded_files ) ) { | |
| foreach( $uploaded_files as &$file ) { | |
| $file['uploaded_filename'] = $this->rename_file( $file['uploaded_filename'] ); | |
| } | |
| GFFormsModel::$uploaded_files[ $form['id'] ][ $input_name ] = $uploaded_files; | |
| } else { | |
| if( empty( $uploaded_files ) ) { | |
| $uploaded_files = rgar( $_FILES, $input_name ); | |
| if( empty( $uploaded_files ) || empty( $uploaded_files['name'] ) ) { | |
| continue; | |
| } | |
| $uploaded_files['name'] = $this->rename_file( $uploaded_files['name'] ); | |
| $_FILES[ $input_name ] = $uploaded_files; | |
| } else { | |
| $uploaded_files = $this->rename_file( $uploaded_files ); | |
| GFFormsModel::$uploaded_files[ $form['id'] ][ $input_name ] = $uploaded_files; | |
| } | |
| } | |
| } | |
| } | |
| function rename_file( $filename ) { | |
| $file_info = pathinfo( $filename ); | |
| $new_filename = $this->remove_slashes( $this->get_template_value( $this->_args['template'], GFFormsModel::get_current_lead(), $file_info['filename'] ) ); | |
| return sprintf( '%s.%s', $new_filename, rgar( $file_info, 'extension' ) ); | |
| } | |
| function get_template_value( $template, $entry, $filename ) { | |
| $form = GFAPI::get_form( $entry['form_id'] ); | |
| $template = GFCommon::replace_variables( $template, $form, $entry, false, true, false, 'text' ); | |
| // replace our custom "{filename}" psuedo-merge-tag | |
| $template = str_replace( '{filename}', $filename, $template ); | |
| return $template; | |
| } | |
| function remove_slashes( $value ) { | |
| return stripslashes( str_replace( '/', '', $value ) ); | |
| } | |
| function is_applicable_form( $form ) { | |
| $form_id = isset( $form['id'] ) ? $form['id'] : $form; | |
| return $form_id == $this->_args['form_id']; | |
| } | |
| function is_applicable_field( $field ) { | |
| $is_file_upload_field = in_array( GFFormsModel::get_input_type( $field ), array( 'fileupload', 'post_image' ) ); | |
| $is_applicable_field_id = $this->_args['field_id'] ? $field['id'] == $this->_args['field_id'] : true; | |
| return $is_file_upload_field && $is_applicable_field_id; | |
| } | |
| } | |
| # Configuration | |
| new GW_Rename_Uploaded_Files( array( | |
| 'form_id' => 628, | |
| 'field_id' => 3, | |
| 'template' => '{Name (First):1.3}-{Name (Last):1.6}-{filename}' // most merge tags are supported, original file extension is preserved | |
| ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment