Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save champsupertramp/5bc9e1211fc6d1451dc379610768b111 to your computer and use it in GitHub Desktop.
Save champsupertramp/5bc9e1211fc6d1451dc379610768b111 to your computer and use it in GitHub Desktop.
Ultimate Member - Imports files and images( except cover and profile photo )
<?php
/**
* Imports files and images( except cover and profile photo )
* @since UM 2.0
*
* Sample Usage:
* - Fields should be created first in the UM Form Builder.
* - Files should be added to /wp-content/uploads/ultimatemember/<user_id>/ before running the import function.
*
* add_action('init', function(){
* // Users to import files, empty means all users
* $arr_users = [
* 1
* ];
* um_import_files( "image01.jpg", "test_image_upload", 'image', $arr_users );
* um_import_files( "image02.png", "test_file_upload", 'file', $arr_users );
* });
*/
function um_import_files( $file_name, $meta_key, $type = 'file', $arr_users = [] ){
$upload_dir = UM()->uploader()->get_upload_base_dir();
$upload_url = UM()->uploader()->get_upload_base_url();
$user_dirs = [];
if( empty( $arr_users ) ){
$user_dirs = glob( $upload_dir."*" );
}else{
foreach( $arr_users as $uid ){
$user_dirs[ ] = $upload_dir . $uid . DIRECTORY_SEPARATOR;
}
}
foreach( $user_dirs as $dir ){
$user_id = intval( basename( $dir ) );
if( $user_id <= 0 ) continue;
$user_basedir = $upload_dir . $user_id . DIRECTORY_SEPARATOR;
$user_baseurl = $upload_dir . $user_id . DIRECTORY_SEPARATOR;
if( ! file_exists( $user_basedir . $file_name ) ) continue;
$movefile = array();
$movefile['url'] = set_url_scheme( $user_baseurl . $file_name );
$file_type = wp_check_filetype( $file_path );
if( $type == 'file' ){
UM()->uploader()->upload_type = "file";
}elseif( $type == 'image'){
UM()->uploader()->upload_type = "image";
UM()->uploader()->upload_image_type = "stream_photo";
}
$new_file_name = UM()->uploader()->unique_filename( $user_basedir, $file_name, $file_type['ext'] );
rename( $user_basedir . $file_name , $user_basedir . $new_file_name );
$file_path = $user_basedir . $new_file_name;
$movefile['file_info']['name'] = $movefile['url'];
$movefile['file_info']['original_name'] = $file_name;
$movefile['file_info']['basename'] = wp_basename( $file_path );
$movefile['file_info']['ext'] = $file_type['ext'];
$movefile['file_info']['type'] = $file_type['type'];
$movefile['file_info']['size'] = filesize( $file_path );
$movefile['file_info']['size_format'] = size_format( $movefile['file_info']['size'] );
update_user_meta( $user_id, $meta_key, wp_basename( $file_path ) );
update_user_meta( $user_id, "{$meta_key}_metadata", $movefile['file_info'] );
}
}
?>
@ko31
Copy link

ko31 commented Feb 5, 2023

In my case, imported files weren't reflected on UM profile automatically. So, I tried to clear every user's cache after importing and it worked well as follows.
https://gist.github.com/ko31/c51b3f3e8a39acdbc81cee6b6a10d615

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment