.
ID) != 'userfile') return; $user_info = get_userdata($_POST['upf_user']); add_post_meta($post_id, 'upf_user', $user_info->user_login); update_post_meta($post_id, 'upf_user', $user_info->user_login); // Make sure the file array isn't empty if(!empty($_FILES['upf_file']['name'])) { // Setup the array of supported file types. In this case, it's just PDF. $supported_types = array('application/pdf'); // Get the file type of the upload $arr_file_type = wp_check_filetype(basename($_FILES['upf_file']['name'])); $uploaded_type = $arr_file_type['type']; // Check if the type is supported. If not, throw an error. if(in_array($uploaded_type, $supported_types)) { $upf_file = get_post_meta($post_id, 'upf_file', true); if ($upf_file) { $upf_file_path = WP_CONTENT_DIR.'/userfiles/'.$upf_file['file']; if (file_exists($upf_file_path)) unlink($upf_file_path); } // Use the WordPress API to upload the file $upload = wp_handle_upload( $_FILES['upf_file'], array( 'test_form' => false ) ); if(isset($upload['error']) && $upload['error'] != 0) { wp_die(__('There was an error uploading your file. The error is: ' . $upload['error'], 'user-private-files')); } else { // Update custom field $upload['file'] = substr($upload['file'],stripos($upload['file'],'wp-content/userfiles/')+21); add_post_meta($post_id, 'upf_file', $upload); update_post_meta($post_id, 'upf_file', $upload); } // end if/else } else { wp_die(__("The file type that you've uploaded is not a PDF.", 'user-private-files')); } // end if/else } // end if if ($_POST['upf_notify'] == '1') { $upf_file = get_post_meta($post_id, 'upf_file', true); $email_subject = get_option('upf_email_subject'); $email_msg = get_option('upf_email_message'); $email_msg = str_replace('%blogname%', get_bloginfo('name'), $email_msg); $email_msg = str_replace('%siteurl%', get_bloginfo('url'), $email_msg); $email_msg = str_replace('%user_login%', $user_info->user_login, $email_msg); $email_msg = str_replace('%filename%', basename($upf_file['file']), $email_msg); $email_msg = str_replace('%download_url%', get_bloginfo('url').'/?upf=dl&id='.$post_id, $email_msg); $cats = wp_get_post_terms($post_id, 'file_categories', array("fields" => "names")); $email_msg = str_replace('%category%', implode(", ", $cats), $email_msg); $headers[] ='From: "'.htmlspecialchars_decode(get_bloginfo('name'), ENT_QUOTES).'" <'.get_option('admin_email').'>'; wp_mail($user_info->user_email, $email_subject, $email_msg, $headers); } } add_filter( 'upload_dir', 'upf_custom_upload_dir' ); function upf_custom_upload_dir( $default_dir ) { if ( ! isset( $_POST['post_ID'] ) || $_POST['post_ID'] < 0 ) return $default_dir; if ( ! isset( $_POST['upf_user'] ) ) return $default_dir; if ( $_POST['post_type'] != 'userfile' ) return $default_dir; $dir = WP_CONTENT_DIR . '/userfiles'; $url = WP_CONTENT_URL . '/userfiles'; $bdir = $dir; $burl = $url; $subdir = '/'.upf_get_user_dir($_POST['upf_user']); $dir .= $subdir; $url .= $subdir; $custom_dir = array( 'path' => $dir, 'url' => $url, 'subdir' => $subdir, 'basedir' => $bdir, 'baseurl' => $burl, 'error' => false, ); return $custom_dir; } add_action('init', 'upf_get_download'); function upf_get_download() { if (isset($_GET['upf']) && isset($_GET['id'])) { if (is_user_logged_in()) { global $current_user; get_currentuserinfo(); // if the file was not assigned to the current user, return if (get_post_meta($_GET['id'], 'upf_user', true) != $current_user->user_login) return; $upf_file = get_post_meta($_GET['id'], 'upf_file', true); $upf_file_path = WP_CONTENT_DIR.'/userfiles/'.$upf_file['file']; $upf_file_name = substr($upf_file['file'], stripos($upf_file['file'], '/')+1); set_time_limit(0); $action = $_GET['upf']=='vw'?'view':'download'; output_file($upf_file_path, $upf_file_name, $upf_file['type'], $action); } else { wp_redirect(wp_login_url($_SERVER['REQUEST_URI'])); exit; } } } /*DOWNLOAD FUNCTION */ function output_file($file, $name, $mime_type='', $action = 'download') { if(!is_readable($file)) { //die('File not found or inaccessible!
' . __('View and Print', 'user-private-files') . '
' . __('Download', 'user-private-files') . '