Last active
May 1, 2020 05:22
-
-
Save wpmudev-sls/ab7530960022a2de1aeea9e845624591 to your computer and use it in GitHub Desktop.
[Forminator] - Store uploaded files in separated folders by date & email
This file contains 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] - Store uploaded files in separated folders by date & email | |
* Description: [Forminator] - Store uploaded files in separated folders by date & email [root/members/2019/11/email@gmail.com/] | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_forminator_custom_upload_files_path_func', 100 ); | |
function wpmudev_forminator_custom_upload_files_path_func() { | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
class WPMUDEV_Forminator_Custom_Upload_Path{ | |
private static $_instance; | |
private $folder_field_key; | |
private $hash_folder; | |
private $sub_folder = 'members'; //it's sub folder | |
public function __construct(){ | |
$this->folder_field_key = 'email-1'; //enter field id here. The name field maybe not unique, so if you want to use unique folder for each member, we suggest use email field instead of. | |
$this->hash_folder = false; // if enabled it, the folder will be hash with method crc32 (the result like this: 3d653119 ), source: https://www.php.net/manual/en/function.hash.php | |
add_filter( 'forminator_custom_form_pseudo_submitted_data', array( $this, 'add_anchor_change_upload_dir_byname' ) ); | |
add_filter( 'upload_dir', array( $this, 'upload_dir' ) ); | |
add_filter( 'forminator_custom_form_submit_errors', array( $this, 'remove_anchor_change_upload_dir_byname' ) ); | |
} | |
public static function get_instance(){ | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
// public function get_folder_path( $user_name ){ | |
// $uploads = wp_get_upload_dir(); | |
// $user_name = sanitize_email( $user_name ); | |
// if( $this->hash_folder ){ | |
// $user_name = hash('crc32', $user_name); | |
// } | |
// $folder_path = trailingslashit( $uploads['basedir'] ); | |
// if( $this->sub_folder ){ | |
// $folder_path .= $this->sub_folder .'/'; | |
// } | |
// return $folder_path; | |
// } | |
// /** | |
// * get real file path by name or email | |
// * @param string $file_name | |
// * @param string $user_name User name or Email | |
// * @return string file path | |
// */ | |
// public function get_file_path( $file_name, $user_name ){ | |
// $folder_path = $this->get_folder_path( $user_name ); | |
// // if( @file_exists( $file_path ) ) | |
// return $folder_path .'/'. $file_name; | |
// } | |
function add_anchor_change_upload_dir_byname( $pseudo_submitted_data ){ | |
global $wpmudev_forminator_upload_dir_byname; | |
$wpmudev_forminator_upload_dir_byname = 1; | |
return $pseudo_submitted_data; | |
} | |
public function get_upload_path( $uploads ){ | |
// $upload_path = trailingslashit( $uploads['basedir'] );// wp-content/uploads | |
$upload_path = ABSPATH; | |
if( $this->sub_folder ){ | |
$upload_path .= $this->sub_folder .'/'; | |
} | |
$upload_path .= date('Y') .'/'; | |
$upload_path .= date('m') .'/'; | |
return $upload_path; | |
} | |
function upload_dir( $uploads ){ | |
global $wpmudev_forminator_upload_dir_byname; | |
if( $wpmudev_forminator_upload_dir_byname && ! empty( $uploads['path'] ) && ! empty( $_POST[ $this->folder_field_key ] ) ){ | |
$folder = sanitize_email( $_POST[ $this->folder_field_key ] );//get email | |
if( $this->hash_folder ){ | |
$folder = hash('crc32', $folder); | |
} | |
$uploads['path'] = $this->get_upload_path( $uploads ); | |
$uploads['path'] .= $folder; | |
$uploads['url'] = str_replace( ABSPATH, site_url('/'), $uploads['path'] ); | |
} | |
return $uploads; | |
} | |
function remove_anchor_change_upload_dir_byname( $sumbit_errors ){ | |
global $wpmudev_forminator_upload_dir_byname; | |
if( $wpmudev_forminator_upload_dir_byname ){ | |
unset( $GLOBALS['wpmudev_forminator_upload_dir_byname'] ); | |
} | |
return $sumbit_errors; | |
} | |
} | |
$run = WPMUDEV_Forminator_Custom_Upload_Path::get_instance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment