WooCommerce renaming a digital download file - woocommerce

I've been using woocommerce to sell a few digital goods. I have set the following setting "Append a unique string to filename for security" And I was wondering if there is a way to rename the file back to it's original name when the customer downloads it.
For example the filename is product-9j978f.zip and when the customer downloads it would be renamed to product.zip instead.
Thank you and any help would be gladly appreciated

Here is the code
/**
* Change the downloadable file name
*
* #param string $filename Downloadable file name.
* #param int $product_id WC product id.
* #return string
*/
function change_woocommerce_file_download_filename( $filename, $product_id ) {
// Get the file extension from the processed file name.
$filetype = wp_check_filetype( $filename );
// You can generate a new file using Product ID if you need to.
$new_file_name = 'New File Name';
// Join the new file name with file extension with (.).
return join( '.', array( $new_file_name, $filetype['ext'] ) );
}
add_filter( 'woocommerce_file_download_filename', 'change_woocommerce_file_download_filename', 10, 2 );
You get 2 parameters in this filter hook. 1: Filename 2: Product ID then you can do the name change according to your need and return the file name.
NOTE: new file name should have a file extension with it.

Related

Delete Gravity Forms file attachments after submission but keep all other fields?

OK, we have a job submission form on the site where users need to complete the form and attach resume and cover letter. For privacy reasons, we do not want to keep the resumes and cover letters on the server, so what we've been doing is send emails with form submission, attach resume and cover letter, and then completely delete the submission.
This is the code we've been using and everything works the way it should.
/**
*
* Target submissions from form ID 2.
*
* Change gform_after_submission_2 to reflect your target form ID,
* or use gform_after_submission to target all forms.
*
*/
add_action( 'gform_after_submission_2', 'remove_form_entry' );
function remove_form_entry( $entry ) {
GFAPI::delete_entry( $entry['id'] );
}
I was wondering if there's a way to keep the form entries but just delete the attachments?
I have just wrote the function, tested and it works fine to delete resume file.
You can add more code same way for cover letters.
add_filter( 'gform_after_submission_2', 'remove_form_entry', 10, 2 );
function remove_form_entry( $entry, $form ) {
global $wpdb;
$lead_id = $entry['id'];
$meta_key = 1; //it is the uploads field id.
$entry_table = $wpdb->prefix . 'gf_entry_meta'; //meta table of gravity forms
$get_resume_file_statement = $wpdb->prepare( "SELECT `meta_value` FROM `$entry_table` WHERE `meta_key` = %d AND `entry_id` = %d", $meta_key, $lead_id);
$meta_values_of_resume_file = $wpdb->get_col( $get_resume_file_statement ); // get uploaded csv file url
$resume_url = explode('uploads', $meta_values_of_resume_file [0]); //get file url after uploads folder. It returns file url like http://localhost/g2a/wp-content/uploads/gravity_forms/3-dbbe121585c30ed9e49ec2a6803270b0/2021/07/89636498_188586552577282_8867025936109797376_n.jpg
$resume_file_name = end($resume_url ); //get file url after uploads folder. value like /gravity_forms/3-dbbe121585c30ed9e49ec2a6803270b0/2021/07/89636498_188586552577282_8867025936109797376_n.jpg
$upload_dir = wp_upload_dir(); //uploads dir
$full_path = $upload_dir['basedir'] . $resume_file_name; //get full path of file
wp_delete_file( $full_path ); //delete the file
//update entry meta field value here or delete the entry meta.
}

Woocommerce change order download files

I am trying to manipulate the download URL link for files inside an order.
Basically, I have a use case when a download product is bought I need to add files to the order to be able to download it. So I cannot add an order ID to a product because there is no order at that moment.
Point is, every downloadable product needs to have a dynamic link because it will be generated on the fly .
So how can I add data to the product download URL link ->
WooCommerce offers 2 filters that can be used to achieve what you are looking for:
A) woocommerce_download_product_filepath used to specify what URL contains the file to be downloaded and it takes the following parameters:
#param string $file_path File path
#param string $email_address Email address
#param WC_Order|bool $order Order object or false
#param WC_Product $product Product object
#param WC_Customer_Download $download Download data
And it is registered like this:
add_filter( 'woocommerce_download_product_filepath', 'change_woocommerce_file_download_path', 10, 5 );
More info here https://wp-kama.com/plugin/woocommerce/hook/woocommerce_download_product_filepath
B) woocommerce_file_download_filename used to manipulate the downloaded file's name and it takes the following parameters:
#param string $filename Downloadable file name.
#param int $product_id WC product id
And it is registered like this:
add_filter( 'woocommerce_file_download_filename', 'change_woocommerce_file_download_filename', 10, 2 );
More info here https://wp-kama.com/plugin/woocommerce/hook/woocommerce_file_download_filename

Is there a way to transfer data from a repeater field in WP Formidable Forms to ActiveCampaign?

Currently I am trying to transfer data from a repeater field in Wordpress Formidable Forms to a list in a CRM system known as ActiveCampaign.
Unfortunately the ActiveCampaign Add-On does not recognise fields inside the repeater field.
When exporting the Child Form, I get exactly what I want. Is there any way to export this file as soon as an entry is created?
Any support would be greatly appreciated.
Maybe this link can help you: https://www.fdmdigital.co.uk/export-a-form-entry-to-csv-on-submit/
add_action('frm_after_create_entry', 'create_csv', 30, 2);
function create_csv($entry_id, $form_id) {
if ($form_id == 1234) { //replace 1234 with the id of the form
// Collect the form data - Add a new row for each field you want to export and give it a unique name
$firstName = isset($_POST['item_meta'][1537]) ? $_POST['item_meta'][1537] : '';
$lastName = isset($_POST['item_meta'][1538]) ? $_POST['item_meta'][1538] : '';
$email = isset($_POST['item_meta'][1540]) ? $_POST['item_meta'][1540] : '';
$organizationId = isset($_POST['item_meta'][1547]) ? $_POST['item_meta'][1547] : '';
$organizationName = isset($_POST['item_meta'][1548]) ? $_POST['item_meta'][1548] : '';
$createdAt = isset($_POST['item_meta'][1555]) ? $_POST['item_meta'][1555] : '';
// The header row of the CSV - Add a name for each field in the form
$header = "firstName,lastName,email,organizationId,organizationName,createdAt\n";
// The data of the CSV - Add each of the form fields listed above
$data = "$firstName,$lastName,$email,$organizationId,$organizationName,$createdAt\n";
/*
* The file name of the CSV.
*
* NB: To save a single file per entry you will need to make the file name unique
* This can be done using the entry ID or the date & time stamp by including the hour, minutes & seconds.
* E.g at 12:38:43 the file will be named "TestUser-21-02-05-12-38-43-request.csv".
* One second later the time (and file name) will be "12:38:44".
* Then a new file "TestUser-21-02-05-12-38-44-request.csv" will be created.
* How you name the file will determine if you have a new file for each entry, a new file per user or a single file with all entry data.
*/
$fileName = dirname(__DIR__, 3) . "/your-project-folder/" . $refUserId . "-" .$createdAt . "-request" . ".csv";
/*
* Create the CSV file.
* If file exists, append the data to it. Otherwise create a new file.
*/
if (file_exists($fileName)) {
// Add only data. The header is already added in the existing file.
file_put_contents($fileName, $data, FILE_APPEND);
} else {
// Add CSV header and data.
file_put_contents($fileName, $header.$data);
}
}
}

Change wordpress gallery shortcode slug to filename

Is it possible to change the attachment page slug to the referring filename? In short... i use the Gallery Shortcode to build a simple page based gallery.
I change the orignal filename (like DSC1223.jpg) during the upload process (to 3b1871561aab.jpg) but it does not appery as slug within the url. It uses only DSC1223.
Is there anyway to change ist?
Regards, Steve
The best way would be to write something like this in my functions.php
function twentyten_filter_wp_handle_upload () {
$upload = wp_handle_upload();
}
add_filter( 'wp_handle_upload', 'twentyten_filter_wp_handle_upload', 10, 2);
Add this to the hash upload filename plugin and you should be good to go;
/**
* Filter new attachments and set the post_name the same as the hashed
* filename.
*
* #param int $post_ID
*/
function change_attachment_name_to_hash($post_ID)
{
$file = get_attached_file($post_ID);
$info = pathinfo($file);
$name = trim( substr($info['basename'], 0, -(1 + strlen($info['extension'])) ) );
wp_update_post(array(
'ID' => $post_ID,
'post_name' => $name
));
}
add_action('add_attachment', 'change_attachment_name_to_hash');
Don't hesitate to ask if you're not sure what each line does!
UPDATE:
This function hooks onto the add_attachment event, just after a new attachment is saved to the database. This action is called from within wp_insert_attachment().
We first grab the filename of the attachment (get_attached_file()). Then we use a native PHP function pathinfo() to get the path components, and strip the directory path and file extension away.
Then we call wp_update_post(), updating the post_name of the attachment in the database.

Rename files during upload within Wordpress backend

is there a way to rename files during the upload progress within the Wordpress 3.0 backend? I would like to have a consistent naming of files, especially for images.
I think an 12 (+-) digit hash value of the original filename or something similar would be awesome. Any suggestions?
Regards
But it would really be easier to do that before uploading files.
Not quite sure about that - this seems fairly easy;
/**
* #link http://stackoverflow.com/a/3261107/247223
*/
function so_3261107_hash_filename( $filename ) {
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
return md5( $name ) . $ext;
}
add_filter( 'sanitize_file_name', 'so_3261107_hash_filename', 10 );
This filter creates a 32 character hash of the original filename, preserving the file extension. You could chop it down a little using substr() if you wanted to.
This filter runs once the file has been uploaded to a temporary directory on your server, but before it is resized (if applicable) and saved to your uploads folder.
Note that there is no risk of file overwrite - in the event that a newly hashed file is the same as one that already exists, WordPress will try appending an incrementing digit to the filename until there is no longer a collision.
WordPress Plugin
<?php
/**
* Plugin Name: Hash Upload Filename
* Plugin URI: http://stackoverflow.com/questions/3259696
* Description: Rename uploaded files as the hash of their original.
* Version: 0.1
*/
/**
* Filter {#see sanitize_file_name()} and return an MD5 hash.
*
* #param string $filename
* #return string
*/
function so_3261107_hash_filename( $filename ) {
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
return md5( $name ) . $ext;
}
add_filter( 'sanitize_file_name', 'so_3261107_hash_filename', 10 );
http://wpapi.com/change-image-name-to-wordpress-post-slug-during-upload/
BTW:
Add filter to sanitize_file_name is totally wrong, as sanitize_file_name() function is a helper function to format string, it may be used elsewhere like plugins or themes.
function wp_modify_uploaded_file_names($file) {
$info = pathinfo($file['name']);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$name = basename($file['name'], $ext);
$file['name'] = uniqid() . $ext; // uniqid method
// $file['name'] = md5($name) . $ext; // md5 method
// $file['name'] = base64_encode($name) . $ext; // base64 method
return $file;
}
add_filter('wp_handle_upload_prefilter', 'wp_modify_uploaded_file_names', 1, 1);
I've made a plugin for it. I did it because i was having too much trouble with my clients trying to upload images with special characters
http://wordpress.org/plugins/file-renaming-on-upload
I implemented the same thing, I wanted a more random filename, than the original, as the site I am using this for is for pics only and all files are in one directory.
i did the following
return md5($ip . uniqid(mt_rand(), true)) . $ext;
I was really looking for a plugin that could do it properly, and finally I ended up making this one myself. It's available on my blog: http://www.meow.fr/media-file-renamer ! If you use it, please give me a feedback :) I sincerely hope it helps!
You can't autorename file with the media library function. I would recommend to rename files before you upload them. Even after uploading a file you can't rename it throug WordPress but only through FTP.
The only way to do that would be a plugin that hooks itself into the media library upload process. But it would really be easier to do that before uploading files.

Resources