Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //at init:
- add_filter('wp_handle_upload_prefilter', 'cud_pre_upload');
- // Called once for each uploaded file.
- function cud_pre_upload($file){
- [...]
- add_filter('upload_dir', 'cud_custom_upload_dir');
- return $file;
- }
- function cud_custom_upload_dir($path){
- if(!empty($path['error'])) { return $path; } //error; do nothing.
- $customdir = cud_generate_path();
- $path['path'] = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
- $path['url'] = str_replace($path['subdir'], '', $path['url']);
- $path['subdir'] = $customdir;
- $path['path'] .= $customdir;
- $path['url'] .= $customdir;
- return $path;
- }
- function cud_generate_path(){
- global $post, $post_id, $current_user, $cud_file_ext, $cud_file_type, $cud_rpc_id;
- $post_id = (!empty($post_id) ? $post_id : (!empty($_REQUEST['post_id']) ? $_REQUEST['post_id'] : (!empty($cud_rpc_id) ? $cud_rpc_id : '')));
- $my_post;
- if(empty($post) || (!empty($post) && is_numeric($post_id) && $post_id != $post->ID)){
- $my_post = get_post($post_id);
- }
- $customdir = = get_option('custom_upload_dir_template');
- //defaults
- $user_id = $post_type = $post_name = $author = '';
- $time = (!empty($_SERVER['REQUEST_TIME'])) ? $_SERVER['REQUEST_TIME'] : (time() + (get_option('gmt_offset')*3600));
- $user_id = (is_user_logged_in() && !empty($current_user)) ? $current_user->ID : '';
- if(empty($user_id)){
- $current_user = wp_get_current_user();
- if($current_user instanceof WP_User){
- $user_id = $current_user->ID;
- }
- }
- if(!empty($my_post)){
- $post_id = $my_post->ID;
- $time = ($my_post->post_date == '0000-00-00 00:00:00') ? $time : strtotime($my_post->post_date);
- $post_type = $my_post->post_type;
- $author = $my_post->post_author;
- $post_name = (!empty($my_post->post_name)) ? $my_post->post_name : (!empty($my_post->post_title) ? sanitize_title($my_post->post_title) : $post_id);
- }else{
- $post_id = '';
- }
- $date = explode(" ", date('Y m d H i s', $time));
- $tags = array('%post_id%','%postname%','%post_type%','%year%','%monthnum%','%month%', '%day%','%hour%','%minute%','%second%', '%file_ext%', '%file_type%');
- $replace = array($post_id, $post_name, $post_type, $date[0], $date[1], $date[1], $date[2], $date[3], $date[4], $date[5], $cud_file_ext, $cud_file_type);
- $customdir = str_replace($tags, $replace, $customdir); //do all cheap replacements in one go.
- $customdir = str_replace('%permalink%', cud_get_permalink($post_id),$customdir);
- $customdir = str_replace('%author%', cud_get_user_name($author), $customdir);
- $customdir = str_replace('%author_role%', cud_get_user_role($author), $customdir);
- $customdir = str_replace('%current_user%', cud_get_user_name($user_id),$customdir);
- $customdir = str_replace('%parent_name%', cud_get_parent_slug($my_post), $customdir);
- $customdir = str_replace('%post_category%', cud_get_category_name($post_id), $customdir);/*DEPRECATED SINCE 3.0.3*/
- $customdir = str_replace('%post_categories%',cud_get_categories_name($post_id), $customdir);/*DEPRECATED SINCE 3.0.3*/
- //Now, let's deal with taxonomies (category, tags, custom)
- $matches = array();
- if(preg_match_all('/%(.*?)%/s', $customdir, $matches) == true){
- for($i = 0; $i < count($matches[0]); $i++){
- if(taxonomy_exists($matches[1][$i])){
- $customdir = str_replace($matches[0][$i], cud_get_taxonomies($post_id, $matches[1][$i]), $customdir);
- }else{
- $customdir = str_replace($matches[0][$i], '', $customdir);
- }
- }
- }
- $customdir = cud_leadingslashit($customdir); //for good measure.
- $customdir = untrailingslashit($customdir);
- while(strpos($customdir, '//') !== false){
- $customdir = str_replace('//', '/', $customdir); //avoid duplicate slashes.
- }
- return apply_filters('cud_generate_path', $customdir, $post_id);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement