Forked from champsupertramp/Ultimate Member - Imports files and images( except cover and profile photo )
Last active
February 5, 2023 06:58
-
-
Save ko31/c51b3f3e8a39acdbc81cee6b6a10d615 to your computer and use it in GitHub Desktop.
Ultimate Member - Imports files and images( except cover and profile photo )
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 | |
/** | |
* 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'] ); | |
// Clear user cache. | |
um_clear_user_cache( [ $user_id ] ); | |
} | |
} | |
/** | |
* Clear user cache. | |
* | |
* @param array $user_ids | |
*/ | |
function um_clear_user_cache( $user_ids = [] ) { | |
global $wpdb; | |
if ( ! is_array( $user_ids ) ) { | |
$user_ids = (array) $user_ids; | |
} | |
foreach ( $user_ids as $user_id ) { | |
$wpdb->query( | |
$wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = 'um_cache_userdata_%d'", $user_id ) | |
); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment