$request_user = $this->validation->get_user_info();
   if(!$request_user)
   return $this->validation->response(“Missing header or invalid authtoken”, null , 400);
  
   $uid = $request_user->ID;
   global $user_current_id_API;
   $user_current_id_API = $uid;
   $cropped_full = USER_AVATAR_UPLOAD_PATH.“{$uid}/”.time().“-bpfull.jpg”;
    $cropped_thumb = USER_AVATAR_UPLOAD_PATH.“{$uid}/”.time().“-bpthumb.jpg”;
    
    $uploadedfile = $_FILES[‘file’];
        $upload_overrides = array( ‘test_form’ => false );
        
        add_filter(‘wp_handle_upload_prefilter’, ‘custom_upload_filter’ );
        function custom_upload_filter( $file ){
            $file[‘name’] = “{$uid}/”.time().“-bpfull.jpg”;
            return $file;
        }
        add_filter( ‘upload_dir’, ‘wpse_141088_upload_dir’ );
        function wpse_141088_upload_dir( $dir ) {
            global $user_current_id_API;
            return array(
                ‘path’  => $dir[‘basedir’] . ‘/avatars/’.$user_current_id_API,
                ‘url’    => $dir[‘baseurl’] . ‘/avatars/’.$user_current_id_API,
                ‘subdir’ => ‘/avatars/’.$user_current_id_API,
            ) + $dir;
            
            unset($user_current_id_API);
        }
        //delete previous files
        user_avatar_delete_files($uid);
        //uplod current files
        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
        
        //remove filter
        remove_filter( ‘upload_dir’, ‘wpse_141088_upload_dir’ );
        remove_filter( ‘wp_handle_upload_prefilter’, ‘custom_upload_filter’ );
        
        
        if ( $movefile && !isset( $movefile[‘error’] ) ) {
            return $this->validation->response(“success”, $movefile, 200);
        }else{
            return $this->validation->response(“Upload error”, null, 400);
        }
        
    return $this->validation->response(“Upload error”, null, 400);     
    }

Leave a Reply

Your email address will not be published. Required fields are marked *