Last active
February 28, 2023 16:36
-
-
Save wpmudev-sls/639f2b6d711016e74ffa86d3a8e3c0d3 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Change Upload Path.
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 | |
| /** | |
| * Plugin Name: [Forminator Pro] - Change Upload Path | |
| * Plugin URI: https://premium.wpmudev.org/ | |
| * Description: This snippet should be changing the Forminator upload path to wp-content/forminator/form_id/YYYY/MM/DD/submission_id | |
| * Task: 0/11289012348292/1162642373521897 | |
| * Version: 1.0.0 | |
| * Author: Panos Lyrakis @ WPMUDEV | |
| * Author URI: https://premium.wpmudev.org/ | |
| * License: GPLv2 or later | |
| */ | |
| if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
| return; | |
| } | |
| if ( ! class_exists( 'Change_Forminator_Upload_Dir_Entry_ID' ) ) { | |
| class Change_Forminator_Upload_Dir_Entry_ID { | |
| /** | |
| * Entry id. | |
| * | |
| * @var $entry_id | |
| */ | |
| private $entry_id = 0; | |
| /** | |
| * Temporary hash. | |
| * | |
| * @var $temp_hash | |
| */ | |
| private $temp_hash = null; | |
| /** | |
| * Temporary upload dir path. | |
| * | |
| * @var $upload_dir_path | |
| */ | |
| private $upload_dir_path = ''; | |
| /** | |
| * Form id. | |
| * | |
| * @var $form_id | |
| */ | |
| private $form_id = 0; | |
| private static $_instance = null; | |
| public static function get_instance() { | |
| if( is_null( self::$_instance ) ){ | |
| self::$_instance = new Change_Forminator_Upload_Dir_Entry_ID(); | |
| } | |
| return self::$_instance; | |
| } | |
| /** | |
| * Constructor. | |
| */ | |
| public function __construct() { | |
| // For non Ajax submit | |
| add_action( 'forminator_custom_form_before_handle_submit', array( $this, 'trigger_set_upload_dir' ) ); | |
| add_action( 'forminator_custom_form_after_handle_submit', array( $this, 'restore_upload_dir' ) ); | |
| // For Ajax submit | |
| add_action( 'forminator_custom_form_before_save_entry', array( $this, 'trigger_set_upload_dir' ) ); | |
| add_action( 'forminator_custom_form_after_save_entry', array( $this, 'restore_upload_dir' ) ); | |
| add_action( 'forminator_custom_form_submit_before_set_fields', array( $this, 'set_entry_id' ), 20, 3 ); | |
| add_filter( 'wp_mail', array( $this, 'replace_temp_hash_on_email_content' ) ); | |
| } | |
| public function replace_temp_hash_on_email_content( $mail_args ) { | |
| if ( ! empty( $this->temp_hash ) && ! empty( $this->entry_id ) ) { | |
| $mail_args[ 'message' ] = str_replace( $this->temp_hash, $this->entry_id, $mail_args[ 'message' ] ); | |
| $mail_args[ 'attachments' ] = str_replace( $this->temp_hash, $this->entry_id, $mail_args[ 'attachments' ] ); | |
| } | |
| return $mail_args; | |
| } | |
| public function trigger_set_upload_dir( $form_id ) { | |
| $this->form_id = $form_id; | |
| add_filter( 'upload_dir', array( $this, 'set_upload_dir' ) ); | |
| } | |
| public function set_entry_id( $entry, $form_id, $field_data_array ) { | |
| global $wpdb; | |
| $this->entry_id = $entry->entry_id; | |
| $temp_hash = $this->get_temp_hash(); | |
| // Replace the hash in upload dir with the entry id | |
| $new_upload_dir_path = str_replace( $temp_hash, $entry->entry_id, $this->upload_dir_path ); | |
| rename( $this->upload_dir_path, $new_upload_dir_path ); | |
| $connected_addons = forminator_get_addons_instance_connected_with_form( $form_id ); | |
| foreach ( $connected_addons as $connected_addon ) { | |
| $addon_slug = $connected_addon->get_slug(); | |
| add_filter( 'forminator_addon_' . $addon_slug . '_form_entry_fields', array( $this, 'replace_temp_hash_on_addons' ), 20, 4 ); | |
| } | |
| } | |
| public function replace_temp_hash_on_addons( $form_entry_fields, $submitted_data, $form_id, $form_settings_instance ) { | |
| if ( ! empty( $this->temp_hash ) && ! empty( $this->entry_id ) ) { | |
| foreach ( $form_entry_fields as $key => $field ) { | |
| if ( isset( $field['value']['file']['file_url'] ) && isset( $field['value']['file']['file_path'] ) ) { | |
| $field['value']['file']['file_url'] = str_replace( $this->temp_hash, $this->entry_id, $field['value']['file']['file_url'] ); | |
| $field['value']['file']['file_path'] = str_replace( $this->temp_hash, $this->entry_id, $field['value']['file']['file_path'] ); | |
| $form_entry_fields[ $key ] = $field; | |
| } | |
| } | |
| } | |
| return $form_entry_fields; | |
| } | |
| /** | |
| * Hook into the upload_dir filter and change the path. | |
| * | |
| * @param array $param The upload dir parameters array. | |
| */ | |
| public function set_upload_dir( $param ) { | |
| $temp_hash = $this->get_temp_hash(); | |
| $year = date( 'Y' ); | |
| $month = date( 'm' ); | |
| $day = date( 'd' ); | |
| $upload_dir_url = WP_CONTENT_URL . "/forminator/{$this->form_id}/{$year}/{$month}/{$day}/{$temp_hash}"; | |
| $this->upload_dir_path = WP_CONTENT_DIR . "/forminator/{$this->form_id}/{$year}/{$month}/{$day}/{$temp_hash}"; | |
| $param['path'] = $this->upload_dir_path; | |
| $param['url'] = $upload_dir_url; | |
| return $param; | |
| } | |
| private function get_temp_hash() { | |
| if ( is_null( $this->temp_hash ) ) { | |
| $this->temp_hash = md5( uniqid( session_id(), true ) ); | |
| } | |
| return $this->temp_hash; | |
| } | |
| /** | |
| * Hook into Forminator and remove the upload_dir filter. | |
| * | |
| * @param int $form_id The form ID. | |
| */ | |
| public function restore_upload_dir( $form_id ) { | |
| // Replace the hash in the entry meta with the entry id | |
| global $wpdb; | |
| $temp_hash = $this->get_temp_hash(); | |
| $like_q = $wpdb->esc_like( $temp_hash ); | |
| $entry = new Forminator_Form_Entry_Model( $this->entry_id ); | |
| $entries_meta_table = "{$wpdb->prefix}frmt_form_entry_meta"; | |
| $s = $wpdb->prepare( | |
| "SELECT `meta_id`, `meta_key`, `meta_value` FROM `{$entries_meta_table}` | |
| WHERE | |
| `entry_id` = %d AND | |
| `meta_value` LIKE %s", | |
| $this->entry_id, "%{$like_q}%" | |
| ); | |
| $results = $wpdb->get_results( $s ); | |
| if ( ! empty( $results ) ) { | |
| foreach ( $results as $result ) { | |
| $meta_key = $result->meta_key; | |
| if ( substr( $result->meta_key, 0, strlen( 'upload' ) ) !== 'upload' ) { | |
| continue; | |
| } | |
| $meta_value = unserialize( $result->meta_value ); | |
| $meta_value[ 'file' ][ 'file_url' ] = str_replace( $temp_hash, $this->entry_id, $meta_value[ 'file' ][ 'file_url' ] ); | |
| $meta_value[ 'file' ][ 'file_path' ] = str_replace( $temp_hash, $this->entry_id, $meta_value[ 'file' ][ 'file_path' ] ); | |
| $entry->update_meta( $result->meta_id, $result->meta_key, serialize( $meta_value ), date_i18n( 'Y-m-d H:i:s' ) ); | |
| } | |
| } | |
| $this->new_dir_id = 0; | |
| remove_filter( 'upload_dir', array( $this, 'set_upload_dir' ) ); | |
| } | |
| } | |
| if ( ! function_exists( 'wpmudev_forminator_upload_dir_entry_id' ) ) { | |
| function wpmudev_forminator_upload_dir_entry_id() { | |
| return Change_Forminator_Upload_Dir_Entry_ID::get_instance(); | |
| }; | |
| add_action( 'plugins_loaded', 'wpmudev_forminator_upload_dir_entry_id', 10 ); | |
| } | |
| } |
Under wp-content, create a new sub-directory called mu-plugins. Drop the .php file underneath it and you are all set!
thanks so much Eric :)
Eri, one more thing as I can comment on the forum, not a paid member
Does it only work with Pro version of the Forminator plugin?
As nothing happens
I changed lines 146, 147 and nothing else:
$upload_dir_url = WP_CONTENT_URL . "/uploads/freetry/{$this->form_id}/{$temp_hash}";
$this->upload_dir_path = WP_CONTENT_DIR . "/uploads/freetry/{$this->form_id}/{$temp_hash}";
Just need to know if a Pro version is needed or there is something on my end
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For more historical conversation on this subject, check out https://wpmudev.com/forums/topic/forminator-create-separate-folder-for-uploads/