Skip to main content
Gravity FormsWordPress

Gravity Forms Tips – Rinominare caricamento file allegati

By 17 Settembre 2020No Comments

Questa funzione vi permetterà di cambiare il nome dei file caricati dal modulo

Questa funzione può essere estremamente utile nel caso si ha la necessita di riorganizzare la directory con nomenclatura specifica magari in base al nome del cliente

Ecco la funzione da inserire nel file function.php del proprio tema di WordPress


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( ! is_callable( array( 'GFFormsModel', 'get_physical_file_path' ) ) ) {
return;
}

add_filter( 'gform_entry_post_save', array( $this, 'rename_uploaded_files' ), 9, 2 );
add_filter( 'gform_entry_post_save', array( $this, 'stash_uploaded_files' ), 99, 2 );

add_action( 'gform_after_update_entry', array( $this, 'rename_uploaded_files_after_update' ), 9, 2 );
add_action( 'gform_after_update_entry', array( $this, 'stash_uploaded_files_after_update' ), 99, 2 );

}

function rename_uploaded_files( $entry, $form ) {

if( ! $this->is_applicable_form( $form ) ) {
return $entry;
}

foreach( $form['fields'] as &$field ) {

if( ! $this->is_applicable_field( $field ) ) {
continue;
}

$uploaded_files = rgar( $entry, $field->id );

if( empty( $uploaded_files ) ) {
continue;
}

$uploaded_files = $this->parse_files( $uploaded_files, $field );
$stashed_files = $this->parse_files( gform_get_meta( $entry['id'], 'gprf_stashed_files' ), $field );
$renamed_files = array();

foreach( $uploaded_files as $_file ) {

// Don't rename the same files twice.
if( in_array( $_file, $stashed_files ) ) {
$renamed_files[] = $_file;
continue;
}

$dir = wp_upload_dir();
$dir = $this->get_upload_dir( $form['id'] );
$file = str_replace( $dir['url'], $dir['path'], $_file );

if( ! file_exists( $file ) ) {
continue;
}

$renamed_file = $this->rename_file( $file, $entry );

if ( ! is_dir( dirname( $renamed_file ) ) ) {
wp_mkdir_p( dirname( $renamed_file ) );
}

$result = rename( $file, $renamed_file );

$renamed_files[] = $this->get_url_by_path( $renamed_file, $form['id'] );

}

// In cases where 3rd party add-ons offload the image to a remote location, no images can be renamed.
if( empty( $renamed_files ) ) {
continue;
}

if( $field->get_input_type() == 'post_image' ) {
$value = str_replace( $uploaded_files[0], $renamed_files[0], rgar( $entry, $field->id ) );
} else if( $field->multipleFiles ) {
$value = json_encode( $renamed_files );
} else {
$value = $renamed_files[0];
}

GFAPI::update_entry_field( $entry['id'], $field->id, $value );

$entry[ $field->id ] = $value;

}

return $entry;
}

function get_upload_dir( $form_id ) {
$dir = GFFormsModel::get_file_upload_path( $form_id, 'PLACEHOLDER' );
$dir['path'] = dirname( $dir['path'] );
$dir['url'] = dirname( $dir['url'] );
return $dir;
}

function rename_uploaded_files_after_update( $form, $entry_id ) {
$entry = GFAPI::get_entry( $entry_id );
$this->rename_uploaded_files( $entry, $form );
}

/**
* Stash the "final" version of the files after other add-ons have had a chance to interact with them.
*
* @param $entry
* @param $form
*/
function stash_uploaded_files( $entry, $form ) {

foreach ( $form['fields'] as &$field ) {

if ( ! $this->is_applicable_field( $field ) ) {
continue;
}

$uploaded_files = rgar( $entry, $field->id );
gform_update_meta( $entry['id'], 'gprf_stashed_files', $uploaded_files );

}

return $entry;
}

function stash_uploaded_files_after_update( $form, $entry_id ) {
$entry = GFAPI::get_entry( $entry_id );
$this->stash_uploaded_files( $entry, $form );
}

function rename_file( $file, $entry ) {

$new_file = $this->get_template_value( $this->_args['template'], $file, $entry );
$new_file = $this->increment_file( $new_file );

return $new_file;
}

function increment_file( $file ) {

$file_path = GFFormsModel::get_physical_file_path( $file );
$pathinfo = pathinfo( $file_path );
$counter = 1;

// increment the filename if it already exists (i.e. balloons.jpg, balloons1.jpg, balloons2.jpg)
while ( file_exists( $file_path ) ) {
$file_path = str_replace( ".{$pathinfo['extension']}", "{$counter}.{$pathinfo['extension']}", GFFormsModel::get_physical_file_path( $file ) );
$counter++;
}

$file = str_replace( basename( $file ), basename( $file_path ), $file );

return $file;
}

function is_path( $filename ) {
return strpos( $filename, '/' ) !== false;
}

function get_template_value( $template, $file, $entry ) {

$info = pathinfo( $file );

if( strpos( $template, '/' ) === 0 ) {
$dir = wp_upload_dir();
$template = $dir['basedir'] . $template;
} else {
$template = $info['dirname'] . '/' . $template;
}

// replace our custom "{filename}" psuedo-merge-tag
$value = str_replace( '{filename}', $info['filename'], $template );

// replace merge tags
$form = GFAPI::get_form( $entry['form_id'] );
$value = GFCommon::replace_variables( $value, $form, $entry, false, true, false, 'text' );

// make sure filename is "clean"
$filename = $this->clean( basename( $value ) );
$value = str_replace( basename( $value ), $filename, $value );

// append our file ext
$value .= '.' . $info['extension'];

return $value;
}

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;
}

function clean( $str ) {
return $this->remove_slashes( sanitize_title_with_dashes( strtr(
utf8_decode( $str ),
utf8_decode( 'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ'),
'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy'
), 'save' ) );
}

function get_url_by_path( $file, $form_id ) {

$dir = $this->get_upload_dir( $form_id );
$url = str_replace( $dir['path'], $dir['url'], $file );

return $url;
}

function parse_files( $files, $field ) {

if( empty( $files ) ) {
return array();
}

if( $field->get_input_type() == 'post_image' ) {
$file_bits = explode( '|:|', $files );
$files = array( $file_bits[0] );
} else if( $field->multipleFiles ) {
$files = json_decode( $files );
} else {
$files = array( $files );
}

return $files;
}

}

# Configuration

new GW_Rename_Uploaded_Files( array(
'form_id' => 3, // Numero ID della form
'field_id' => 24, // Campo da utilizzare per rinominazione
'template' => '{Unique:21}_{filename}' // Nome finale del file
) );

Leave a Reply

Rispettiamo la tua privacy!

Su questo sito utilizziamo strumenti nostri o di terze parti che memorizzano piccoli file (cookie) sul tuo dispositivo. I cookie sono normalmente usati per permettere al sito di funzionare correttamente (cookie tecnici), per generare statistiche di uso/navigazione (cookie statistici). Abilitando questi cookie, ci aiuti ad offrirti una esperienza migliore con noi.

Privacy & Cookie Policy