Share this diff
1<?php 1<?php
2/* 2/*
3 Plugin Name: Custom Upload Dir 3 Plugin Name: Custom Upload Dir
4 Plugin URI: http://wordpress.org/extend/plugins/custom-upload-dir/ 4 Plugin URI: http://wordpress.org/extend/plugins/custom-upload-dir/
5 Description: Keeps your uploaded files organized in smart folder structures. 5 Description: Keeps your uploaded files organized in smart folder structures.
6 Version: 3.4 6 Version: 3.4
7 Author: Ulf Benjaminsson 7 Author: Ulf Benjaminsson
8 Author URI: http://www.ulfben.com/ 8 Author URI: http://www.ulfben.com/
9 License: GPL2 9 License: GPL2
10 Text Domain: cud 10 Text Domain: cud
11 Domain Path: /lang 11 Domain Path: /lang
12*/ 12*/
13add_action('plugins_loaded', 'cud_init'); 13add_action('plugins_loaded', 'cud_init');
14register_activation_hook(__FILE__, 'cud_install_options'); 14register_activation_hook(__FILE__, 'cud_install_options');
15register_uninstall_hook(__FILE__, 'cud_delete_options'); 15register_uninstall_hook(__FILE__, 'cud_delete_options');
16global $cud_filetype, $cud_rpc_id; 16global $cud_filetype, $cud_rpc_id;
17$cud_file_ext = ''; //file extension 17$cud_file_ext = ''; //file extension
18function cud_init() { 18function cud_init() {
19 if(!defined('ULFBEN_DONATE_URL')){ 19 if(!defined('ULFBEN_DONATE_URL')){
20 define('ULFBEN_DONATE_URL', 'http://www.amazon.com/gp/registry/wishlist/2QB6SQ5XX2U0N/105-3209188-5640446?reveal
=unpurchased&filter=all&sort=priority&layout=standard&x=21&y=17');
20 define('ULFBEN_DONATE_URL', 'http://www.amazon.com/gp/registry/wishlist/2QB6SQ5XX2U0N/105-3209188-5640446?reveal
=unpurchased&filter=all&sort=priority&layout=standard&x=21&y=17');
21 } 21 }
22 load_plugin_textdomain('cud', false, dirname(plugin_basename(__FILE__ )).'/lang/'); 22 load_plugin_textdomain('cud', false, dirname(plugin_basename(__FILE__ )).'/lang/');
23 define('CUD_BASENAME', plugin_basename(__FILE__)); 23 define('CUD_BASENAME', plugin_basename(__FILE__));
24 add_filter('wp_handle_upload_prefilter', 'cud_pre_upload'); 24 add_filter('wp_handle_upload_prefilter', 'cud_pre_upload');
25 add_action('xmlrpc_call', 'cud_xmlrpc_call'); //to hook into upload_dir on remote client uploads. 25 add_action('xmlrpc_call', 'cud_xmlrpc_call'); //to hook into upload_dir on remote client uploads.
26 add_filter('wp_handle_upload', 'cud_post_upload'); 26 add_filter('wp_handle_upload', 'cud_post_upload');
27 add_action('admin_init', 'cud_register_settings'); 27 add_action('admin_init', 'cud_register_settings');
28 add_action('admin_menu', 'cud_register_menu_item'); 28 add_action('admin_menu', 'cud_register_menu_item');
29 add_filter('plugin_row_meta', 'cud_set_plugin_meta', 2, 10); 29 add_filter('plugin_row_meta', 'cud_set_plugin_meta', 2, 10);
30} 30}
31function cud_register_menu_item() { 31function cud_register_menu_item() {
32 add_options_page('Custom Upload Dir Settings', 'Custom Upload Dir', 'manage_options', __FILE__, 'cud_option_page'); 32 add_options_page('Custom Upload Dir Settings', 'Custom Upload Dir', 'manage_options', __FILE__, 'cud_option_page');
33} 33}
34function cud_register_settings(){ 34function cud_register_settings(){
35 register_setting('cud-settings-group', 'custom_upload_dir', 'cud_sanitize_settings'); 35 register_setting('cud-settings-group', 'custom_upload_dir', 'cud_sanitize_settings');
36} 36}
37function cud_add_admin_footer(){ //shows some plugin info in the footer of the config screen. 37function cud_add_admin_footer(){ //shows some plugin info in the footer of the config screen.
38 $plugin_data = get_plugin_data(__FILE__); 38 $plugin_data = get_plugin_data(__FILE__);
39 printf('%1$s by %2$s (who <a href="'.ULFBEN_DONATE_URL.'">appreciates books</a>) :)<br />', $plugin_data['Title'].' '.$plugin_data['Version'], $plugin_data['Author']); 39 printf('%1$s by %2$s (who <a href="'.ULFBEN_DONATE_URL.'">appreciates books</a>) :)<br />', $plugin_data['Title'].' '.$plugin_data['Version'], $plugin_data['Author']);
40} 40}
41function cud_set_plugin_meta($links, $file) { // Add a link to this plugin's settings page 41function cud_set_plugin_meta($links, $file) { // Add a link to this plugin's settings page
42 if($file == CUD_BASENAME) { 42 if($file == CUD_BASENAME) {
43 return array_merge($links, array(sprintf( '<a href="%s/wp-admin/options-general.php?page=%s">%s</a>', get_option('siteurl'), CUD_BASENAME, __('Settings', 'cud')))); 43 return array_merge($links, array(sprintf( '<a href="%s/wp-admin/options-general.php?page=%s">%s</a>', get_option('siteurl'), CUD_BASENAME, __('Settings', 'cud'))));
44 } 44 }
45 return $links; 45 return $links;
46} 46}
47function cud_delete_options(){ 47function cud_delete_options(){
48 delete_option('custom_upload_dir'); 48 delete_option('custom_upload_dir');
49} 49}
50function cud_install_options(){ 50function cud_install_options(){
51 $defaults = array( 51 $defaults = array(
52 'custom_upload_dir' => '', 52 'custom_upload_dir' => '',
53 'all_parents' => true, //get parents even if post belongs only to leaf cat 53 'all_parents' => true, //get parents even if post belongs only to leaf cat
54 'flatten_hierarchy' => false, //don't split taxonomy hierarchies into subfolders (folders will be like: "categoryA-categoryB") 54 'flatten_hierarchy' => false, //don't split taxonomy hierarchies into subfolders (folders will be like: "categoryA-categoryB")
55 'only_leaf_nodes' => false, //even if post belongs to entire tree, use only leaf node 55 'only_leaf_nodes' => false, //even if post belongs to entire tree, use only leaf node
56 'only_base_nodes' => false, 56 'only_base_nodes' => false,
57 'test_ids' => '-1', 57 'test_ids' => '-1',
58 'template' => '' 58 'template' => ''
59 ); 59 );
60 $old = get_option('custom_upload_dir'); 60 $old = get_option('custom_upload_dir');
61 if(is_array($old)){ 61 if(is_array($old)){
62 if(!empty($old['template'])){ //update deprecated placeholders to match permalink tags 62 if(!empty($old['template'])){ //update deprecated placeholders to match permalink tags
63 $newtags = array('%post_id%','%postname%','%author%','%monthnum%'); 63 $newtags = array('%post_id%','%postname%','%author%','%monthnum%');
64 $oldtags = array('%ID%', '%post_name%','%post_author%','%month%'); 64 $oldtags = array('%ID%', '%post_name%','%post_author%','%month%');
65 $old['template'] = str_replace($oldtags, $newtags, $old['template']); 65 $old['template'] = str_replace($oldtags, $newtags, $old['template']);
66 } 66 }
67 $defaults = array_merge($defaults, $old); 67 $defaults = array_merge($defaults, $old);
68 } 68 }
69 if(is_array($defaults['test_ids'])){ 69 if(is_array($defaults['test_ids'])){
70 $defaults['test_ids'] = implode(',', $defaults['test_ids']); 70 $defaults['test_ids'] = implode(',', $defaults['test_ids']);
71 } 71 }
72 update_option('custom_upload_dir', $defaults); 72 update_option('custom_upload_dir', $defaults);
73} 73}
74 74
75function cud_xmlrpc_call($call){ 75function cud_xmlrpc_call($call){
76 if($call !== 'metaWeblog.newMediaObject'){return;} 76 if($call !== 'metaWeblog.newMediaObject'){return;}
77 global $wp_xmlrpc_server, $cud_rpc_id; //class-wp-xmlrpc-server.php 77 global $wp_xmlrpc_server, $cud_rpc_id; //class-wp-xmlrpc-server.php
78 $data = $wp_xmlrpc_server->message->params[3]; 78 $data = $wp_xmlrpc_server->message->params[3];
79 //$name = sanitize_file_name($data['name']); 79 //$name = sanitize_file_name($data['name']);
80 //$type = $data['type']; 80 //$type = $data['type'];
81 if(!empty($data['post_id'])){ //sometimes, an RPC call will include a post_id. If you're lucky. :/ 81 if(!empty($data['post_id'])){ //sometimes, an RPC call will include a post_id. If you're lucky. :/
82 $cud_rpc_id = (int) $data['post_id']; 82 $cud_rpc_id = (int) $data['post_id'];
83 }else{ 83 }else{
84 $cud_rpc_id = ''; 84 $cud_rpc_id = '';
85 } 85 }
86 cud_pre_upload($data);//Q&D to avoid duplication of code. $data is not an element of $_FILES, but has "name" which is all we currently use. 86 cud_pre_upload($data);//Q&D to avoid duplication of code. $data is not an element of $_FILES, but has "name" which is all we currently use.
87} 87}
88 88
89 89
90//* @param array $file Reference to a single element of $_FILES. Call the function once for each uploaded file. 90//* @param array $file Reference to a single element of $_FILES. Call the function once for each uploaded file.
91function cud_pre_upload($file){ 91function cud_pre_upload($file){
92 global $cud_file_ext; 92 global $cud_file_ext,$cud_file_type;
93 $cud_file_ext = ''; 93 $cud_file_ext = '';
 94 $cud_file_type = '';
94 if(!empty($file['name'])){ 95 if(!empty($file['name'])){
95 $wp_filetype = wp_check_filetype($file['name']); 96 $wp_filetype = wp_check_filetype($file['name']);
96 $cud_file_ext = (!empty($wp_filetype['ext'])) ? $wp_filetype['ext'] : ''; 97 $cud_file_ext = (!empty($wp_filetype['ext'])) ? $wp_filetype['ext'] : '';
 98 $cud_file_type = (!empty($wp_filetype['type'])) ? $wp_filetype['type'] : '';
97 } 99 }
98 add_filter('upload_dir', 'cud_custom_upload_dir'); 100 add_filter('upload_dir', 'cud_custom_upload_dir');
99 return $file; 101 return $file;
100} 102}
101 103
102function cud_post_upload($fileinfo){ 104function cud_post_upload($fileinfo){
103 remove_filter('upload_dir', 'cud_custom_upload_dir'); 105 remove_filter('upload_dir', 'cud_custom_upload_dir');
104 return $fileinfo; 106 return $fileinfo;
105} 107}
106 108
107/* 109/*
108$path 110$path
109 [path] - base directory and sub directory or full path to upload directory. 111 [path] - base directory and sub directory or full path to upload directory.
110 [url] - base url and sub directory or absolute URL to upload directory. 112 [url] - base url and sub directory or absolute URL to upload directory.
111 [subdir] - sub directory if uploads use year/month folders option is on. 113 [subdir] - sub directory if uploads use year/month folders option is on.
112 [basedir] - path without subdir. 114 [basedir] - path without subdir.
113 [baseurl] - URL path without subdir. 115 [baseurl] - URL path without subdir.
114 [error] - set to false. 116 [error] - set to false.
115 117
116 [path] => C:\path\to\wordpress\wp-content\uploads\2010\05 118 [path] => C:\path\to\wordpress\wp-content\uploads\2010\05
117 [url] => http://example.com/wp-content/uploads/2010/05 119 [url] => http://example.com/wp-content/uploads/2010/05
118 [subdir] => /2010/05 120 [subdir] => /2010/05
119 [basedir] => C:\path\to\wordpress\wp-content\uploads 121 [basedir] => C:\path\to\wordpress\wp-content\uploads
120 [baseurl] => http://example.com/wp-content/uploads 122 [baseurl] => http://example.com/wp-content/uploads
121 [error] => 123 [error] =>
122 //since WP 2.7, this filter is called for each attachment displayed in the gallery-view. 124 //since WP 2.7, this filter is called for each attachment displayed in the gallery-view.
123*/ 125*/
124/* 126/*
125NOTE: since WP 3.3 this filter is only run between wp_handle_upload_prefilter and wp_handle_upload - thus I removed the 127NOTE: since WP 3.3 this filter is only run between wp_handle_upload_prefilter and wp_handle_upload - thus I removed the
126checks for redundant calls when viewing the media library. 128checks for redundant calls when viewing the media library.
127*/ 129*/
128function cud_custom_upload_dir($path){ 130function cud_custom_upload_dir($path){
129 if(!empty($path['error'])) { return $path; } //error; do nothing. 131 if(!empty($path['error'])) { return $path; } //error; do nothing.
130 $customdir = cud_generate_dir(); 132 $customdir = cud_generate_dir();
131 $path['path'] = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month) 133 $path['path'] = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
132 $path['url'] = str_replace($path['subdir'], '', $path['url']); 134 $path['url'] = str_replace($path['subdir'], '', $path['url']);
133 $path['subdir'] = $customdir; 135 $path['subdir'] = $customdir;
134 $path['path'] .= $customdir; 136 $path['path'] .= $customdir;
135 $path['url'] .= $customdir; 137 $path['url'] .= $customdir;
136 return $path; 138 return $path;
137} 139}
138 140
139function cud_generate_dir(){ 141function cud_generate_dir(){
140 global $post, $post_id, $current_user, $cud_file_ext, $cud_rpc_id; 142 global $post, $post_id, $current_user, $cud_file_ext, $cud_file_type, $cud_rpc_id;
141 $post_id = (!empty($post_id) ? $post_id : (!empty($_REQUEST['post_id']) ? $_REQUEST['post_id'] : (!empty($cud_rpc_id) ? $cud_rpc_id : ''))); 143 $post_id = (!empty($post_id) ? $post_id : (!empty($_REQUEST['post_id']) ? $_REQUEST['post_id'] : (!empty($cud_rpc_id) ? $cud_rpc_id : '')));
142 144
143 if(empty($post) || (!empty($post) && is_numeric($post_id) && $post_id != $post->ID)){ 145 if(empty($post) || (!empty($post) && is_numeric($post_id) && $post_id != $post->ID)){
144 $post = get_post($post_id); 146 $post = get_post($post_id);
145 } 147 }
146 $customdir = get_option('custom_upload_dir'); 148 $customdir = get_option('custom_upload_dir');
147 $customdir = $customdir['template']; 149 $customdir = $customdir['template'];
148 150
149 //defaults 151 //defaults
150 $user_id = $post_type = $post_name = $author = ''; 152 $user_id = $post_type = $post_name = $author = '';
151 $time = (!empty($_SERVER['REQUEST_TIME'])) ? $_SERVER['REQUEST_TIME'] : (time() + (get_option('gmt_offset')*3600)); 153 $time = (!empty($_SERVER['REQUEST_TIME'])) ? $_SERVER['REQUEST_TIME'] : (time() + (get_option('gmt_offset')*3600));
152 $user_id = (is_user_logged_in() && !empty($current_user)) ? $current_user->ID : ''; 154 $user_id = (is_user_logged_in() && !empty($current_user)) ? $current_user->ID : '';
153 if(empty($user_id)){ 155 if(empty($user_id)){
154 $current_user = wp_get_current_user(); 156 $current_user = wp_get_current_user();
155 if($current_user instanceof WP_User){ 157 if($current_user instanceof WP_User){
156 $user_id = $current_user->ID; 158 $user_id = $current_user->ID;
157 } 159 }
158 } 160 }
159 if(!empty($post)){ 161 if(!empty($post)){
160 $post_id = $post->ID; 162 $post_id = $post->ID;
161 $time = ($post->post_date == '0000-00-00 00:00:00') ? $time : strtotime($post->post_date); 163 $time = ($post->post_date == '0000-00-00 00:00:00') ? $time : strtotime($post->post_date);
162 $post_type = $post->post_type; 164 $post_type = $post->post_type;
163 $author = $post->post_author; 165 $author = $post->post_author;
164 $post_name = (!empty($post->post_name)) ? $post->post_name : (!empty($post->post_title) ? sanitize_title($post->post_title) : $post_id); 166 $post_name = (!empty($post->post_name)) ? $post->post_name : (!empty($post->post_title) ? sanitize_title($post->post_title) : $post_id);
165 }else{ 167 }else{
166 $post_id = ''; 168 $post_id = '';
167 } 169 }
168 170
169 $date = explode(" ", date('Y m d H i s', $time)); 171 $date = explode(" ", date('Y m d H i s', $time));
170 $tags = array('%post_id%','%postname%','%post_type%','%year%','%monthnum%','%month%', '%day%','%hour%','%minute%','%second%', '%file_ext%'); 172 $tags = array('%post_id%','%postname%','%post_type%','%year%','%monthnum%','%month%', '%day%','%hour%','%minute%','%second%', '%file_ext%', '%file_type%');
171 $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); 173 $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);
172 174
173 $customdir = str_replace($tags, $replace, $customdir); //do all cheap replacements in one go. 175 $customdir = str_replace($tags, $replace, $customdir); //do all cheap replacements in one go.
174 $customdir = str_replace('%permalink%', cud_get_permalink($post_id),$customdir); 176 $customdir = str_replace('%permalink%', cud_get_permalink($post_id),$customdir);
175 $customdir = str_replace('%author%', cud_get_user_name($author), $customdir); 177 $customdir = str_replace('%author%', cud_get_user_name($author), $customdir);
176 $customdir = str_replace('%current_user%', cud_get_user_name($user_id),$customdir); 178 $customdir = str_replace('%current_user%', cud_get_user_name($user_id),$customdir);
177 $customdir = str_replace('%parent_name%', cud_get_parent_slug($post), $customdir); 179 $customdir = str_replace('%parent_name%', cud_get_parent_slug($post), $customdir);
178 $customdir = str_replace('%post_category%', cud_get_category_name($post_id), $customdir);/*DEPRECATED SINCE 3.0.3*/ 180 $customdir = str_replace('%post_category%', cud_get_category_name($post_id), $customdir);/*DEPRECATED SINCE 3.0.3*/
179 $customdir = str_replace('%post_categories%',cud_get_categories_name($post_id), $customdir);/*DEPRECATED SINCE 3.0.3*/ 181 $customdir = str_replace('%post_categories%',cud_get_categories_name($post_id), $customdir);/*DEPRECATED SINCE 3.0.3*/
180 182
181 //Now, let's deal with taxonomies (category, tags, custom) 183 //Now, let's deal with taxonomies (category, tags, custom)
182 //hopefully this also catches the permalink tag "%tag%". I've only tested with the 'post_tags' 184 //hopefully this also catches the permalink tag "%tag%". I've only tested with the 'post_tags'
183 $matches = array(); 185 $matches = array();
184 if(preg_match_all('/%(.*?)%/s', $customdir, $matches) == true){ 186 if(preg_match_all('/%(.*?)%/s', $customdir, $matches) == true){
185 for($i = 0; $i < count($matches[0]); $i++){ 187 for($i = 0; $i < count($matches[0]); $i++){
186 //$prepend = $use_taxonomy_name ? $matches[1][$i].'/' : ''; 188 //$prepend = $use_taxonomy_name ? $matches[1][$i].'/' : '';
187 $customdir = str_replace($matches[0][$i], cud_get_taxonomies($post_id, $matches[1][$i]), $customdir); 189 $customdir = str_replace($matches[0][$i], cud_get_taxonomies($post_id, $matches[1][$i]), $customdir);
188 } 190 }
189 } 191 }
190 $customdir = cud_leadingslashit($customdir); //for good measure. 192 $customdir = cud_leadingslashit($customdir); //for good measure.
191 $customdir = untrailingslashit($customdir); 193 $customdir = untrailingslashit($customdir);
192 while(strpos($customdir, '//') !== false){ 194 while(strpos($customdir, '//') !== false){
193 $customdir = str_replace('//', '/', $customdir); //avoid duplicate slashes. 195 $customdir = str_replace('//', '/', $customdir); //avoid duplicate slashes.
194 } 196 }
195 return $customdir; 197 return $customdir;
196} 198}
197//ripped wp_upload_dir to generate a basepath preview on the admin page. 199//ripped wp_upload_dir to generate a basepath preview on the admin page.
198function cud_wp_upload_dir($time = null){ 200function cud_wp_upload_dir($time = null){
199 $upload_path = trim( get_option( 'upload_path' ) ); 201 $upload_path = trim( get_option( 'upload_path' ) );
200 $basedir = ''; 202 $basedir = '';
201 if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) { 203 if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
202 $basedir = WP_CONTENT_DIR . '/uploads'; 204 $basedir = WP_CONTENT_DIR . '/uploads';
203 } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) { 205 } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
204 // $basedir is absolute, $upload_path is (maybe) relative to ABSPATH 206 // $basedir is absolute, $upload_path is (maybe) relative to ABSPATH
205 $basedir = path_join( ABSPATH, $upload_path ); 207 $basedir = path_join( ABSPATH, $upload_path );
206 } else { 208 } else {
207 $basedir = $upload_path; 209 $basedir = $upload_path;
208 } 210 }
209 // Obey the value of UPLOADS. This happens as long as ms-files rewriting is disabled. 211 // Obey the value of UPLOADS. This happens as long as ms-files rewriting is disabled.
210 // We also sometimes obey UPLOADS when rewriting is enabled -- see the next block. 212 // We also sometimes obey UPLOADS when rewriting is enabled -- see the next block.
211 if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) { 213 if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
212 $basedir = ABSPATH . UPLOADS; 214 $basedir = ABSPATH . UPLOADS;
213 } 215 }
214 // If multisite (and if not the main site in a post-MU network) 216 // If multisite (and if not the main site in a post-MU network)
215 if ( is_multisite() && ! ( is_main_site() && defined( 'MULTISITE' ) ) ) { 217 if ( is_multisite() && ! ( is_main_site() && defined( 'MULTISITE' ) ) ) {
216 if ( ! get_site_option( 'ms_files_rewriting' ) ) { 218 if ( ! get_site_option( 'ms_files_rewriting' ) ) {
217 // If ms-files rewriting is disabled (networks created post-3.5), it is fairly straightforward: 219 // If ms-files rewriting is disabled (networks created post-3.5), it is fairly straightforward:
218 // Append sites/%d if we're not on the main site (for post-MU networks). (The extra directory 220 // Append sites/%d if we're not on the main site (for post-MU networks). (The extra directory
219 // prevents a four-digit ID from conflicting with a year-based directory for the main site. 221 // prevents a four-digit ID from conflicting with a year-based directory for the main site.
220 // But if a MU-era network has disabled ms-files rewriting manually, they don't need the extra 222 // But if a MU-era network has disabled ms-files rewriting manually, they don't need the extra
221 // directory, as they never had wp-content/uploads for the main site.) 223 // directory, as they never had wp-content/uploads for the main site.)
222 if ( defined( 'MULTISITE' ) ) 224 if ( defined( 'MULTISITE' ) )
223 $ms_dir = '/sites/' . get_current_blog_id(); 225 $ms_dir = '/sites/' . get_current_blog_id();
224 else 226 else
225 $ms_dir = '/' . get_current_blog_id(); 227 $ms_dir = '/' . get_current_blog_id();
226 228
227 $basedir .= $ms_dir; 229 $basedir .= $ms_dir;
228 } elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) { 230 } elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) {
229 // Handle the old-form ms-files.php rewriting if the network still has that enabled. 231 // Handle the old-form ms-files.php rewriting if the network still has that enabled.
230 // When ms-files rewriting is enabled, then we only listen to UPLOADS when: 232 // When ms-files rewriting is enabled, then we only listen to UPLOADS when:
231 // 1) we are not on the main site in a post-MU network, 233 // 1) we are not on the main site in a post-MU network,
232 // as wp-content/uploads is used there, and 234 // as wp-content/uploads is used there, and
233 // 2) we are not switched, as ms_upload_constants() hardcodes 235 // 2) we are not switched, as ms_upload_constants() hardcodes
234 // these constants to reflect the original blog ID. 236 // these constants to reflect the original blog ID.
235 // 237 //
236 // Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute. 238 // Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute.
237 // (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as 239 // (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as
238 // as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files 240 // as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files
239 // rewriting in multisite, the resulting URL is /files. (#WP22702 for background.) 241 // rewriting in multisite, the resulting URL is /files. (#WP22702 for background.)
240 242
241 if ( defined( 'BLOGUPLOADDIR' ) ) 243 if ( defined( 'BLOGUPLOADDIR' ) )
242 $basedir = untrailingslashit( BLOGUPLOADDIR ); 244 $basedir = untrailingslashit( BLOGUPLOADDIR );
243 else 245 else
244 $basedir = ABSPATH . UPLOADS; 246 $basedir = ABSPATH . UPLOADS;
245 } 247 }
246 } 248 }
247 return $basedir; 249 return $basedir;
248} 250}
249 251
250 252
251function cud_get_permalink($post_id){ 253function cud_get_permalink($post_id){
252 $url = get_permalink($post_id); 254 $url = get_permalink($post_id);
253 return ($url) ? str_replace(get_option('siteurl'), '', $url) : ''; 255 return ($url) ? str_replace(get_option('siteurl'), '', $url) : '';
254} 256}
255function cud_get_parent_slug($post){ 257function cud_get_parent_slug($post){
256 if(empty($post)){return '';} 258 if(empty($post)){return '';}
257 $parent_slug = ''; 259 $parent_slug = '';
258 if($post->post_parent) { 260 if($post->post_parent) {
259 $parent_slug = basename(get_permalink($post->post_parent)); 261 $parent_slug = basename(get_permalink($post->post_parent));
260 } elseif (get_post_type_object($post->post_type)->rewrite["slug"]) { 262 } elseif (get_post_type_object($post->post_type)->rewrite["slug"]) {
261 $parent_slug = (get_post_type_object($post->post_type)->rewrite["slug"]); 263 $parent_slug = (get_post_type_object($post->post_type)->rewrite["slug"]);
262 } 264 }
263 return $parent_slug; 265 return $parent_slug;
264} 266}
265function cud_get_taxonomies($post_id, $taxonomy, $count = -1){ //deals with categories, tags or whatever else is in the terms table. 267function cud_get_taxonomies($post_id, $taxonomy, $count = -1){ //deals with categories, tags or whatever else is in the terms table.
266 if($post_id === ''){return '';} 268 if($post_id === ''){return '';}
267 $terms = wp_get_object_terms($post_id, $taxonomy, array('orderby' => 'slug', 'order' => 'ASC', 'fields' => 'all')); 269 $terms = wp_get_object_terms($post_id, $taxonomy, array('orderby' => 'slug', 'order' => 'ASC', 'fields' => 'all'));
268 if(!is_array($terms)){return '';} 270 if(!is_array($terms)){return '';}
269 $options = get_option('custom_upload_dir'); 271 $options = get_option('custom_upload_dir');
270 if($options['all_parents'] && !$options['only_leaf_nodes']){ 272 if($options['all_parents'] && !$options['only_leaf_nodes']){
271 $terms = cud_get_parents($terms, $taxonomy); 273 $terms = cud_get_parents($terms, $taxonomy);
272 } 274 }
273 if($count > 0){ 275 if($count > 0){
274 $terms = array_slice($terms, 0, $count); 276 $terms = array_slice($terms, 0, $count);
275 } 277 }
276 $levels = array(0 => $terms); 278 $levels = array(0 => $terms);
277 if(is_taxonomy_hierarchical($taxonomy)){ 279 if(is_taxonomy_hierarchical($taxonomy)){
278 $levels = cud_sort_by_levels($terms, $taxonomy); 280 $levels = cud_sort_by_levels($terms, $taxonomy);
279 if($options['only_leaf_nodes']){ 281 if($options['only_leaf_nodes']){
280 $levels = cud_find_leafs($levels); 282 $levels = cud_find_leafs($levels);
281 }else if($options['only_base_nodes']){ 283 }else if($options['only_base_nodes']){
282 $levels = array_slice($levels, 0, 1); //get rid of all levels beyond the first. 284 $levels = array_slice($levels, 0, 1); //get rid of all levels beyond the first.
283 } 285 }
284 } 286 }
285 return cud_build_term_path($levels, $taxonomy, $options['flatten_hierarchy']); 287 return cud_build_term_path($levels, $taxonomy, $options['flatten_hierarchy']);
286} 288}
287function cud_build_term_path($levels, $taxonomy, $flatten_hierarchy){ 289function cud_build_term_path($levels, $taxonomy, $flatten_hierarchy){
288 $path = ''; 290 $path = '';
289 foreach($levels as $level => $terms){ 291 foreach($levels as $level => $terms){
290 $path .= implode('-', array_unique(wp_list_pluck($terms, 'slug'))) . '/'; 292 $path .= implode('-', array_unique(wp_list_pluck($terms, 'slug'))) . '/';
291 } 293 }
292 $path = untrailingslashit($path); 294 $path = untrailingslashit($path);
293 if($flatten_hierarchy){ 295 if($flatten_hierarchy){
294 $path = str_replace('/', '-', $path); 296 $path = str_replace('/', '-', $path);
295 } 297 }
296 return $path; 298 return $path;
297} 299}
298function cud_find_leafs($levels /* array(0 => array(terms), [...], n => array(terms)) */){ 300function cud_find_leafs($levels /* array(0 => array(terms), [...], n => array(terms)) */){
299 $parents = array(); 301 $parents = array();
300 $leafs = array(); 302 $leafs = array();
301 $levels = array_reverse($levels); 303 $levels = array_reverse($levels);
302 foreach($levels as $level => $terms){ 304 foreach($levels as $level => $terms){
303 foreach($terms as $term){ 305 foreach($terms as $term){
304 if(!in_array($term->term_id, $parents)){ 306 if(!in_array($term->term_id, $parents)){
305 if(empty($leafs[$level])){ 307 if(empty($leafs[$level])){
306 $leafs[$level] = array(); 308 $leafs[$level] = array();
307 } 309 }
308 $leafs[$level][] = $term; 310 $leafs[$level][] = $term;
309 } 311 }
310 $parents[] = $term->parent; 312 $parents[] = $term->parent;
311 } 313 }
312 } 314 }
313 return array_reverse($leafs); 315 return array_reverse($leafs);
314} 316}
315function cud_get_parents($terms, $taxonomy){ 317function cud_get_parents($terms, $taxonomy){
316 $hierarcy = array(); 318 $hierarcy = array();
317 foreach($terms as $term){ 319 foreach($terms as $term){
318 $parent_ids = get_ancestors($term->term_id, $taxonomy); 320 $parent_ids = get_ancestors($term->term_id, $taxonomy);
319 $parents = array(); 321 $parents = array();
320 if(is_array($parent_ids)){ 322 if(is_array($parent_ids)){
321 foreach($parent_ids as $id){ 323 foreach($parent_ids as $id){
322 $parents[] = get_term($id, $taxonomy); 324 $parents[] = get_term($id, $taxonomy);
323 } 325 }
324 } 326 }
325 $parents[] = $term; 327 $parents[] = $term;
326 $hierarcy = array_merge($hierarcy, $parents); 328 $hierarcy = array_merge($hierarcy, $parents);
327 } 329 }
328 return $hierarcy; 330 return $hierarcy;
329} 331}
330function cud_sort_by_levels($terms, $taxonomy){ 332function cud_sort_by_levels($terms, $taxonomy){
331 $levels = array(); 333 $levels = array();
332 foreach($terms as $term){ 334 foreach($terms as $term){
333 $level = count(get_ancestors($term->term_id, $taxonomy)); 335 $level = count(get_ancestors($term->term_id, $taxonomy));
334 if(empty($levels[$level])){ 336 if(empty($levels[$level])){
335 $levels[$level] = array(); 337 $levels[$level] = array();
336 } 338 }
337 $levels[$level][] = $term; 339 $levels[$level][] = $term;
338 } 340 }
339 ksort($levels); 341 ksort($levels);
340 return $levels; 342 return $levels;
341} 343}
342function cud_get_term_parents($term_id, $taxonomy) { /*UNUSED*/ 344function cud_get_term_parents($term_id, $taxonomy) { /*UNUSED*/
343 $parent_ids = &get_ancestors($term_id, $taxonomy); 345 $parent_ids = &get_ancestors($term_id, $taxonomy);
344 if(!is_array($parent_ids)){ return '';} 346 if(!is_array($parent_ids)){ return '';}
345 $terms = wp_get_object_terms($parent_ids, $taxonomy); //let's hope get_objects returns them in the same order as IDs are given. 347 $terms = wp_get_object_terms($parent_ids, $taxonomy); //let's hope get_objects returns them in the same order as IDs are given.
346 if(!is_array($terms)){return '';} 348 if(!is_array($terms)){return '';}
347 $terms = wp_list_pluck($terms, 'slug'); /*slug is the same as category_nicename*/ 349 $terms = wp_list_pluck($terms, 'slug'); /*slug is the same as category_nicename*/
348 return implode('/', $terms); 350 return implode('/', $terms);
349} 351}
350function cud_get_user_name($user_id){ 352function cud_get_user_name($user_id){
351 if(!is_numeric($user_id)) {return '';} 353 if(!is_numeric($user_id)) {return '';}
352 $user = get_userdata($user_id); 354 $user = get_userdata($user_id);
353 if(!$user){return '';} 355 if(!$user){return '';}
354 return sanitize_title($user->user_nicename);//$user->display_name 356 return sanitize_title($user->user_nicename);//$user->display_name
355} 357}
356function cud_get_category_name($post_id){ /*DEPRECATED SINCE 3.0.3*/ 358function cud_get_category_name($post_id){ /*DEPRECATED SINCE 3.0.3*/
357 if($post_id === ''){return '';} 359 if($post_id === ''){return '';}
358 $categories = get_the_category($post_id); 360 $categories = get_the_category($post_id);
359 if(!$categories){return '';} 361 if(!$categories){return '';}
360 //usort($categories, '_usort_terms_by_ID'); // order by ID to match WordPress usage in permalinks 362 //usort($categories, '_usort_terms_by_ID'); // order by ID to match WordPress usage in permalinks
361 return $categories[0]->category_nicename; 363 return $categories[0]->category_nicename;
362} 364}
363function cud_get_categories_name($post_id){ /*DEPRECATED SINCE 3.0.3*/ 365function cud_get_categories_name($post_id){ /*DEPRECATED SINCE 3.0.3*/
364 if($post_id === ''){return '';} 366 if($post_id === ''){return '';}
365 $categories = get_the_category($post_id); 367 $categories = get_the_category($post_id);
366 if(!$categories){return '';} 368 if(!$categories){return '';}
367 $cats = array(); 369 $cats = array();
368 foreach($categories as $cat){ 370 foreach($categories as $cat){
369 $cats[] = $cat->category_nicename; 371 $cats[] = $cat->category_nicename;
370 } 372 }
371 return implode('-', $cats); 373 return implode('-', $cats);
372} 374}
373function cud_leadingslashit($s){ 375function cud_leadingslashit($s){
374 return ($s && $s[0] !== '/') ? '/'.$s : $s; 376 return ($s && $s[0] !== '/') ? '/'.$s : $s;
375} 377}
376function cud_sanitize_settings($options){ 378function cud_sanitize_settings($options){
377 if(empty($options)){return;} 379 if(empty($options)){return;}
378 if(!isset($_REQUEST['submit'])){ 380 if(!isset($_REQUEST['submit'])){
379 return; 381 return;
380 } 382 }
381 update_option('uploads_use_yearmonth_folders', isset($options['wp_use_yearmonth'])); 383 update_option('uploads_use_yearmonth_folders', isset($options['wp_use_yearmonth']));
382 $clean_options = array(); 384 $clean_options = array();
383 $clean_options['test_ids'] = $options['test_ids']; 385 $clean_options['test_ids'] = $options['test_ids'];
384 $options['template'] = cud_leadingslashit($options['template']); 386 $options['template'] = cud_leadingslashit($options['template']);
385 if(get_option('uploads_use_yearmonth_folders') && stripos($options['template'], '/%year%/%monthnum%') !== 0){ 387 if(get_option('uploads_use_yearmonth_folders') && stripos($options['template'], '/%year%/%monthnum%') !== 0){
386 $options['template'] = '/%year%/%monthnum%'.$options['template']; 388 $options['template'] = '/%year%/%monthnum%'.$options['template'];
387 } 389 }
388 $clean_options['template'] = preg_replace('/[^a-z0-9-%\/-\_]/','-',$options['template']); //allow only alphanumeric, '%', '_' and '/' 390 $clean_options['template'] = preg_replace('/[^a-z0-9-%\/-\_]/','-',$options['template']); //allow only alphanumeric, '%', '_' and '/'
389 while(strpos($clean_options['template'], '//') !== false){ 391 while(strpos($clean_options['template'], '//') !== false){
390 $clean_options['template'] = str_replace('//', '/', $clean_options['template']); //avoid duplicate slashes. 392 $clean_options['template'] = str_replace('//', '/', $clean_options['template']); //avoid duplicate slashes.
391 } 393 }
392 $clean_options['only_leaf_nodes'] = !empty($options['only_leaf_nodes']); 394 $clean_options['only_leaf_nodes'] = !empty($options['only_leaf_nodes']);
393 $clean_options['only_base_nodes'] = (!empty($options['only_base_nodes'])) && !$clean_options['only_leaf_nodes']; 395 $clean_options['only_base_nodes'] = (!empty($options['only_base_nodes'])) && !$clean_options['only_leaf_nodes'];
394 $clean_options['flatten_hierarchy'] = !empty($options['flatten_hierarchy']); 396 $clean_options['flatten_hierarchy'] = !empty($options['flatten_hierarchy']);
395 $clean_options['all_parents'] = (!empty($options['all_parents'])) && !$clean_options['only_leaf_nodes']; 397 $clean_options['all_parents'] = (!empty($options['all_parents'])) && !$clean_options['only_leaf_nodes'];
396 //print_r($options); 398 //print_r($options);
397 return $clean_options; 399 return $clean_options;
398} 400}
399 401
400function cud_option_page() { 402function cud_option_page() {
401 if(!function_exists('current_user_can') || !current_user_can('manage_options') ){ 403 if(!function_exists('current_user_can') || !current_user_can('manage_options') ){
402 die(__('Cheatin&#8217; uh?')); 404 die(__('Cheatin&#8217; uh?'));
403 } 405 }
404 add_action('in_admin_footer', 'cud_add_admin_footer'); 406 add_action('in_admin_footer', 'cud_add_admin_footer');
405 $options = get_option('custom_upload_dir'); 407 $options = get_option('custom_upload_dir');
406 if(!isset($options['all_parents'])){ 408 if(!isset($options['all_parents'])){
407 cud_install_options(); //for some reason the installations sometimes fail this step 409 cud_install_options(); //for some reason the installations sometimes fail this step
408 } 410 }
409 $options = get_option('custom_upload_dir'); 411 $options = get_option('custom_upload_dir');
410 412
411 echo '<script type=\'text/javascript\'> 413 echo '<script type=\'text/javascript\'>
412 jQuery(document).ready(function(){ 414 jQuery(document).ready(function(){
413 var path = jQuery("#template"); 415 var path = jQuery("#template");
414 jQuery("div.updated").delay(2000).slideUp("slow"); 416 jQuery("div.updated").delay(2000).slideUp("slow");
415 jQuery(".template").click(function(){ 417 jQuery(".template").click(function(){
416 var sep = (path.val().charAt(path.val().length-1) !== "/") ? "/" : ""; 418 var sep = (path.val().charAt(path.val().length-1) !== "/") ? "/" : "";
417 path.val(path.val()+sep+jQuery(this).html()); 419 path.val(path.val()+sep+jQuery(this).html());
418 }); 420 });
419 }); </script>'; 421 }); </script>';
420 $placeholder = array( 422 $placeholder = array(
 423 'file_type' => __('The file type', 'cud'),
421 'file_ext' => __('The file extension', 'cud'), 424 'file_ext' => __('The file extension', 'cud'),
422 'post_id' => __('The post ID', 'cud'), 425 'post_id' => __('The post ID', 'cud'),
423 'author' => __('The post author', 'cud'), 426 'author' => __('The post author', 'cud'),
424 //'post_category' => __('The post\'s first category', 'cud'), 427 //'post_category' => __('The post\'s first category', 'cud'),
425 //'post_categories' => __('All the post\'s categories', 'cud'), 428 //'post_categories' => __('All the post\'s categories', 'cud'),
426 //'post_status' => __('The post status (publish|draft|private|static|object|attachment|inherit|future)', 'cud'), 429 //'post_status' => __('The post status (publish|draft|private|static|object|attachment|inherit|future)', 'cud'),
427 'postname' => __('The post\'s URL slug', 'cud'), 430 'postname' => __('The post\'s URL slug', 'cud'),
428 'parent_name' => __('The parent URL slug', 'cud'), 431 'parent_name' => __('The parent URL slug', 'cud'),
429 'post_type' => __('(post|page|attachment)', 'cud'), 432 'post_type' => __('(post|page|attachment)', 'cud'),
430 'year' => __('The post\'s year (YYYY)', 'cud'), 433 'year' => __('The post\'s year (YYYY)', 'cud'),
431 'monthnum' => __('The post\'s month (MM)', 'cud'), 434 'monthnum' => __('The post\'s month (MM)', 'cud'),
432 'day' => __('The post\'s day (DD)', 'cud'), 435 'day' => __('The post\'s day (DD)', 'cud'),
433 'permalink' => __('Match your blog\'s permalink structure', 'cud'), 436 'permalink' => __('Match your blog\'s permalink structure', 'cud'),
434 'current_user' => __('The currently logged in user', 'cud'), 437 'current_user' => __('The currently logged in user', 'cud'),
435 'category' => __('The post\'s categories (see: Taxonomies)', 'cud'), 438 'category' => __('The post\'s categories (see: Taxonomies)', 'cud'),
436 'post_tag' => __('The post\'s tags (see: Taxonomies)', 'cud'), 439 'post_tag' => __('The post\'s tags (see: Taxonomies)', 'cud'),
437 ); 440 );
438?> 441?>
439<div class="wrap"> 442<div class="wrap">
440<?php include_once(plugin_dir_path(__FILE__).'about.php'); ?> 443<?php include_once(plugin_dir_path(__FILE__).'about.php'); ?>
441<div id="about" style="clear: both;margin-top: 10px;"> 444<div id="about" style="clear: both;margin-top: 10px;">
442 <h3><?php esc_html_e('Taxonomies:');?></h3> 445 <h3><?php esc_html_e('Taxonomies:');?></h3>
443 <p>A taxonomy is a way to group things together; <code>category</code> and <code>post_tag</code> are built in taxanomies, 446 <p>A taxonomy is a way to group things together; <code>category</code> and <code>post_tag</code> are built in taxanomies,
444 but you can use <a href="http://codex.wordpress.org/Taxonomies">your own</a> too.<br /><br /> 447 but you can use <a href="http://codex.wordpress.org/Taxonomies">your own</a> too.<br /><br />
445 Some taxonomies are hierarchical (think: categories) and some are not (tags). 448 Some taxonomies are hierarchical (think: categories) and some are not (tags).
446 This is how Custom Upload Dir handles the different scenarios:</pp> 449 This is how Custom Upload Dir handles the different scenarios:</pp>
447 450
448 <p>Hierarchies are turned into folders<br /> 451 <p>Hierarchies are turned into folders<br />
449 <code>/operating-systems<span style="color:red;">/</span>linux<span style="color:red;">/</span>debian</code></p> 452 <code>/operating-systems<span style="color:red;">/</span>linux<span style="color:red;">/</span>debian</code></p>
450 453
451 <p>'Flat filesystem' ignores hierarchies:<br /> 454 <p>'Flat filesystem' ignores hierarchies:<br />
452 <code>/operating-systems<span style="color:red;">-</span>linux<span style="color:red;">-</span>debian</code></p> 455 <code>/operating-systems<span style="color:red;">-</span>linux<span style="color:red;">-</span>debian</code></p>
453 456
454 <p title="<?php esc_attr_e('Only use the leaf node of the hierarchy.');?>">'Ignore parents': <code>/debian</code></p> 457 <p title="<?php esc_attr_e('Only use the leaf node of the hierarchy.');?>">'Ignore parents': <code>/debian</code></p>
455 458
456 <p>'Ignore children': <code>/operating-systems</code></p> 459 <p>'Ignore children': <code>/operating-systems</code></p>
457 460
458 <p>Same-level terms are sorted (alphabet) in folder names:<br /> 461 <p>Same-level terms are sorted (alphabet) in folder names:<br />
459 <code>/operating-systems/linux/<span style="color:red;">debian-redhat-ubuntu</span></code></p> 462 <code>/operating-systems/linux/<span style="color:red;">debian-redhat-ubuntu</span></code></p>
460</div> 463</div>
461<form method="post" action="options.php"> 464<form method="post" action="options.php">
462<table> 465<table>
463 <?php settings_fields('cud-settings-group'); ?> 466 <?php settings_fields('cud-settings-group'); ?>
464 <tr> 467 <tr>
465 <td><h2><?php esc_html_e('Customize upload directory:');?></h2></td> 468 <td><h2><?php esc_html_e('Customize upload directory:');?></h2></td>
466 </tr> 469 </tr>
467 <tr> 470 <tr>
468 <td class="field_info" title="<?php esc_attr_e('Add placeholders to create a template for the filestructure you\'d like. Separate folders with \'/\'.');?>" ><label for="template"><?php esc_html_e('Build a path template:');?></label></td> 471 <td class="field_info" title="<?php esc_attr_e('Add placeholders to create a template for the filestructure you\'d like. Separate folders with \'/\'.');?>" ><label for="template"><?php esc_html_e('Build a path template:');?></label></td>
469 <td><input id="template" name='custom_upload_dir[template]' type='text' class='code' value='<?php esc_attr_e($options['template']); ?>' size='60' title="<?php esc_attr_e('Add placeholders to create a template for the filestructure you\'d like. Separate folders with \'/\'.');?>"/></td> 472 <td><input id="template" name='custom_upload_dir[template]' type='text' class='code' value='<?php esc_attr_e($options['template']); ?>' size='60' title="<?php esc_attr_e('Add placeholders to create a template for the filestructure you\'d like. Separate folders with \'/\'.');?>"/></td>
470 </tr> 473 </tr>
471 <tr> 474 <tr>
472 <td class="field_info" valign="top" title="<?php esc_attr_e('These are replaced with specific values when you upload something.');?>">Use any of these placeholders:</td> 475 <td class="field_info" valign="top" title="<?php esc_attr_e('These are replaced with specific values when you upload something.');?>">Use any of these placeholders:</td>
473 <td valign="top"> 476 <td valign="top">
474 <ul> 477 <ul>
475 <?php 478 <?php
476 foreach($placeholder as $key => $description){ 479 foreach($placeholder as $key => $description){
477 printf('<li><code class="template">%%%1$s%%</code> => %2$s</li>', esc_html($key), esc_html($description)); 480 printf('<li><code class="template">%%%1$s%%</code> => %2$s</li>', esc_html($key), esc_html($description));
478 } 481 }
479 ?> 482 ?>
480 </td> 483 </td>
481 </tr> 484 </tr>
482 <tr><td>&nbsp;</td></tr> 485 <tr><td>&nbsp;</td></tr>
483 <tr> 486 <tr>
484 <td valign="top" class="field_info">&nbsp;</td> 487 <td valign="top" class="field_info">&nbsp;</td>
485 <td valign="top"><input name='custom_upload_dir[wp_use_yearmonth]' type='checkbox' id='wp_use_yearmonth' <?php checked(get_option('uploads_use_yearmonth_folders'), 1); ?> /><label for='wp_use_yearmonth' title="<?php esc_attr_e('These are built-in settings found at WordPress \'Media\' screen. Feel free to edit them here too.');?>"><?php esc_html_e('Organize my uploads into month- and year-based folders','cud'); ?></label></td> 488 <td valign="top"><input name='custom_upload_dir[wp_use_yearmonth]' type='checkbox' id='wp_use_yearmonth' <?php checked(get_option('uploads_use_yearmonth_folders'), 1); ?> /><label for='wp_use_yearmonth' title="<?php esc_attr_e('These are built-in settings found at WordPress \'Media\' screen. Feel free to edit them here too.');?>"><?php esc_html_e('Organize my uploads into month- and year-based folders','cud'); ?></label></td>
486 </tr> 489 </tr>
487 <tr><td>&nbsp;</td></tr> 490 <tr><td>&nbsp;</td></tr>
488 <tr> 491 <tr>
489 <td valign="top" class="field_info">Settings for taxonomy hierarchies:</td> 492 <td valign="top" class="field_info">Settings for taxonomy hierarchies:</td>
490 <td valign="top"><input name='custom_upload_dir[all_parents]' type='checkbox' id='all_parents' value="1" <?php checked($options['all_parents'], 1); ?> /> 493 <td valign="top"><input name='custom_upload_dir[all_parents]' type='checkbox' id='all_parents' value="1" <?php checked($options['all_parents'], 1); ?> />
491 <label for='all_parents' title="<?php esc_attr_e('Get entire hierarchy even if post belongs only to leaf.');?>"> 494 <label for='all_parents' title="<?php esc_attr_e('Get entire hierarchy even if post belongs only to leaf.');?>">
492 <?php esc_html_e('Always get all parents','cud'); ?> 495 <?php esc_html_e('Always get all parents','cud'); ?>
493 </label> 496 </label>
494 </td> 497 </td>
495 </tr> 498 </tr>
496 <tr> 499 <tr>
497 <td valign="top" class="field_info">&nbsp;</td> 500 <td valign="top" class="field_info">&nbsp;</td>
498 <td valign="top"><input name='custom_upload_dir[flatten_hierarchy]' type='checkbox' id='flatten_hierarchy' value="1" <?php checked($options['flatten_hierarchy'], 1); ?> /> 501 <td valign="top"><input name='custom_upload_dir[flatten_hierarchy]' type='checkbox' id='flatten_hierarchy' value="1" <?php checked($options['flatten_hierarchy'], 1); ?> />
499 <label for='flatten_hierarchy' title="<?php esc_attr_e('Don\'t split taxonomy hierarchies into subfolders (folders will be like: "categoryA-categoryB").');?>"> 502 <label for='flatten_hierarchy' title="<?php esc_attr_e('Don\'t split taxonomy hierarchies into subfolders (folders will be like: "categoryA-categoryB").');?>">
500 <?php esc_html_e('Flat filesystem (no subfolders for children)','cud'); ?> 503 <?php esc_html_e('Flat filesystem (no subfolders for children)','cud'); ?>
501 </label> 504 </label>
502 </td> 505 </td>
503 </tr> 506 </tr>
504 <tr> 507 <tr>
505 <td valign="top" class="field_info">&nbsp;</td> 508 <td valign="top" class="field_info">&nbsp;</td>
506 <td valign="top"><input name='custom_upload_dir[only_leaf_nodes]' type='checkbox' id='only_leaf_nodes' value="1" <?php checked($options['only_leaf_nodes'], 1); ?> /> 509 <td valign="top"><input name='custom_upload_dir[only_leaf_nodes]' type='checkbox' id='only_leaf_nodes' value="1" <?php checked($options['only_leaf_nodes'], 1); ?> />
507 <label for='only_leaf_nodes' title="<?php esc_attr_e('Even if post belongs to entire tree, use only leaf node.');?>"> 510 <label for='only_leaf_nodes' title="<?php esc_attr_e('Even if post belongs to entire tree, use only leaf node.');?>">
508 <?php esc_html_e('Ignore parents (use only leaf node of hierarcy)','cud'); ?> 511 <?php esc_html_e('Ignore parents (use only leaf node of hierarcy)','cud'); ?>
509 </label> 512 </label>
510 </td> 513 </td>
511 </tr> 514 </tr>
512 <tr> 515 <tr>
513 <td valign="top" class="field_info">&nbsp;</td> 516 <td valign="top" class="field_info">&nbsp;</td>
514 <td valign="top"><input name='custom_upload_dir[only_base_nodes]' type='checkbox' id='only_base_nodes' value="1" <?php checked($options['only_base_nodes'], 1); ?> /> 517 <td valign="top"><input name='custom_upload_dir[only_base_nodes]' type='checkbox' id='only_base_nodes' value="1" <?php checked($options['only_base_nodes'], 1); ?> />
515 <label for='only_base_nodes' title="<?php esc_attr_e('Even if post belongs to multiple levels of a hierarchy, use only the top most ancestor.');?>"> 518 <label for='only_base_nodes' title="<?php esc_attr_e('Even if post belongs to multiple levels of a hierarchy, use only the top most ancestor.');?>">
516 <?php esc_html_e('Ignore children (use only the root of hierarchy)','cud'); ?> 519 <?php esc_html_e('Ignore children (use only the root of hierarchy)','cud'); ?>
517 </label> 520 </label>
518 </td> 521 </td>
519 </tr> 522 </tr>
520 <tr> 523 <tr>
521 <td valign="top" colspan="2" style="text-align:center;font-weight:bold;"><br><font color="orange"><?php esc_html_e('Try to always name and save your post before uploading attachments.');?></font></td> 524 <td valign="top" colspan="2" style="text-align:center;font-weight:bold;"><br><font color="orange"><?php esc_html_e('Try to always name and save your post before uploading attachments.');?></font></td>
522 </tr> 525 </tr>
523 <tr> 526 <tr>
524 <td><h3><?php esc_html_e('Test it:');?></h3></td> 527 <td><h3><?php esc_html_e('Test it:');?></h3></td>
525 </tr> 528 </tr>
526 <tr> 529 <tr>
527 <?php 530 <?php
528 $test_ids = explode(',', $options['test_ids']); 531 $test_ids = explode(',', $options['test_ids']);
529 foreach($test_ids as $post_id){ 532 foreach($test_ids as $post_id){
530 $_REQUEST['post_id'] = $post_id; 533 $_REQUEST['post_id'] = $post_id;
531 $post_title = get_the_title($post_id); 534 $post_title = get_the_title($post_id);
532 $url = get_permalink($post_id); 535 $url = get_permalink($post_id);
533 if($post_title && $url){ 536 if($post_title && $url){
534 echo "<tr><td class=\"field_info\"><strong>Post {$post_id}, \"<a href='{$url}'>{$post_title}</a>\"</strong> would upload to:</td><td><code>".cud_wp_upload_dir().cud_generate_dir().'</code></td></tr>'; 537 echo "<tr><td class=\"field_info\"><strong>Post {$post_id}, \"<a href='{$url}'>{$post_title}</a>\"</strong> would upload to:</td><td><code>".cud_wp_upload_dir().cud_generate_dir().'</code></td></tr>';
535 } 538 }
536 } 539 }
537 ?> 540 ?>
538 <td class="field_info"><label for="test_ids" title="<?php esc_attr_e('A comma separated list of post IDs that you\'d like to test.');?>"><?php esc_html_e('Enter some post IDs to test the path generated:');?></label></td> 541 <td class="field_info"><label for="test_ids" title="<?php esc_attr_e('A comma separated list of post IDs that you\'d like to test.');?>"><?php esc_html_e('Enter some post IDs to test the path generated:');?></label></td>
539 <td><input id="test_ids" name='custom_upload_dir[test_ids]' type='text' class='code' value='<?php esc_attr_e($options['test_ids']); ?>' size='30' title="<?php esc_attr_e('A comma separated list of post IDs that you\'d like to test.');?>"/></td> 542 <td><input id="test_ids" name='custom_upload_dir[test_ids]' type='text' class='code' value='<?php esc_attr_e($options['test_ids']); ?>' size='30' title="<?php esc_attr_e('A comma separated list of post IDs that you\'d like to test.');?>"/></td>
540 </tr> 543 </tr>
541 <tr><td><p class="submit"><input type="submit" name="submit" value="<?php esc_html_e('Update Settings &raquo;','cud') ?>" /></p></td></tr> 544 <tr><td><p class="submit"><input type="submit" name="submit" value="<?php esc_html_e('Update Settings &raquo;','cud') ?>" /></p></td></tr>
542</table> 545</table>
543 546
544</div> 547</div>
545</form> 548</form>
546<?php 549<?php
547} 550}
548?>551?>
ORIGINAL TEXT
CHANGED TEXT