Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ultimate Member - Account form Image upload function
/**
* Tutorial: https://www.champ.ninja/2020/05/add-a-custom-account-tab-with-profile-form-fields/
*/
add_filter("um_user_pre_updating_files_array", function($files ){
$user_id = get_current_user_id();
if( empty( $user_id ) ) return $files;
$new_files = array();
$old_files = array();
$user_basedir = UM()->uploader()->get_upload_user_base_dir( $user_id, true );
foreach ( $files as $key => $filename ) {
//move temporary file from temp directory to the correct user directory
$temp_file_path = UM()->uploader()->get_core_temp_dir() . DIRECTORY_SEPARATOR . $filename;
if ( file_exists( $temp_file_path ) ) {
$extra_hash = hash( 'crc32b', current_time('timestamp') );
if ( strpos( $filename , 'stream_photo_' ) !== false ) {
$new_filename = str_replace("stream_photo_","stream_photo_{$extra_hash}_", $filename );
} else {
$new_filename = str_replace("file_","file_{$extra_hash}_", $filename );
}
$submitted = get_user_meta( $user_id, 'submitted', true );
$submitted = ! empty( $submitted ) ? $submitted : array();
$submitted[ $key ] = $new_filename;
update_user_meta( $user_id, 'submitted', $submitted );
if ( $move_only ) {
$file = $user_basedir . DIRECTORY_SEPARATOR . $filename;
if ( rename( $temp_file_path, $file ) ) {
$new_files[ $key ] = $filename;
}
} else {
$file = $user_basedir . DIRECTORY_SEPARATOR . $new_filename;
if ( rename( $temp_file_path, $file ) ) {
$new_files[ $key ] = $new_filename;
$old_files[ $key ] = get_user_meta( $user_id, $key, true );
update_user_meta( $user_id, $key, $new_filename );
$file_info = get_transient( "um_{$filename}" );
if ( ! $file_info ) {
$file_info = get_user_meta( $user_id, "{$key}_metadata_temp", true );
delete_user_meta( $user_id, "{$key}_metadata_temp" );
}
if ( $file_info ) {
update_user_meta( $user_id, "{$key}_metadata", $file_info );
delete_transient( "um_{$filename}" );
}
}
}
}
}
add_action( 'wp_ajax_um_remove_file', 'um_042821_ajax_remove_file' ,1 );
add_action( 'wp_ajax_nopriv_um_remove_file', 'um_042821_ajax_remove_file', 1 );
function um_042821_ajax_remove_file(){
UM()->check_ajax_nonce();
if ( empty( $_POST['src'] ) ) {
wp_send_json_error( __( 'Wrong path', 'ultimate-member' ) );
}
if ( empty( $_POST['mode'] ) ) {
wp_send_json_error( __( 'Wrong mode', 'ultimate-member' ) );
}
$src = $_POST['src'];
if ( strstr( $src, '?' ) ) {
$splitted = explode( '?', $src );
$src = $splitted[0];
}
$mode = sanitize_key( $_POST['mode'] );
if ( $mode == 'profile'){
$user_id = absint( $_POST['user_id'] );
if ( ! UM()->roles()->um_current_user_can( 'edit', $user_id ) ) {
wp_send_json_error( __( 'You have no permission to edit this user', 'ultimate-member' ) );
}
$is_temp = um_is_temp_upload( $src );
if ( ! $is_temp ) {
if ( ! empty( $_POST['filename'] ) && file_exists( UM()->uploader()->get_upload_user_base_dir( $user_id ) . DIRECTORY_SEPARATOR . $_POST['filename'] ) ) {
wp_send_json_success();
}
}
}
$files = new um\core\Files();
$raw_file = explode("/",$src);
$uid = $raw_file[6];
$field_key = $raw_file[5];
$file = get_user_meta( $uid, $field_key, true );
$file_path = UM()->uploader()->get_upload_base_dir() . $uid . DIRECTORY_SEPARATOR . $file;
if( file_exists( $file_path ) ){
unlink( $file_path );
delete_user_meta( $uid, $field_key );
delete_user_meta( $uid, $field_key."_metadata" );
delete_transient( "um_{$file}" );
wp_send_json_success();
}
if ($files->delete_file( $src ) ) {
wp_send_json_success();
} else {
wp_send_json_error( __( 'You have no permission to delete this file. ', 'ultimate-member' ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment