/** * Get an array containing the current upload directory's path and url. * * Checks the 'upload_path' option, which should be from the web root folder, * and if it isn't empty it will be used. If it is empty, then the path will be * 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will * override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path. * * The upload URL path is set either by the 'upload_url_path' option or by using * the 'WP_CONTENT_URL' constant and appending '/uploads' to the path. * * If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in * the administration settings panel), then the time will be used. The format * will be year first and then month. * * If the path couldn't be created, then an error will be returned with the key * 'error' containing the error message. The error suggests that the parent * directory is not writable by the server. * * On success, the returned array will have many indices: * 'path' - base directory and sub directory or full path to upload directory. * 'url' - base url and sub directory or absolute URL to upload directory. * 'subdir' - sub directory if uploads use year/month folders option is on. * 'basedir' - path without subdir. * 'baseurl' - URL path without subdir. * 'error' - false or error message. * * @since 2.0.0 * @uses _wp_upload_dir() * * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. * @param bool $create_dir Optional. Whether to check and create the uploads directory. Default true (backwards compatible). * @param bool $refresh_cache Optional. Whether to refresh the cache. Default false. * @return array See above for description. */ function wp_upload_dir($time = null, $create_dir = true, $refresh_cache = false) { static $cache = array(); $key = sprintf('%d-%s', get_current_blog_id(), (string) $time); if ($refresh_cache || empty($cache[$key])) { $cache[$key] = _wp_upload_dir($time); } /** * Filter the uploads directory data. * * @since 2.0.0 * * @param array $uploads Array of upload directory data with keys of 'path', * 'url', 'subdir, 'basedir', and 'error'. */ $uploads = apply_filters('upload_dir', $cache[$key]); if ($create_dir) { $path = $uploads['path']; $tested_paths = wp_cache_get('upload_dir_tested_paths'); if (!is_array($tested_paths)) { $tested_paths = array(); } if (!in_array($path, $tested_paths, true)) { if (!wp_mkdir_p($path)) { if (0 === strpos($uploads['basedir'], ABSPATH)) { $error_path = str_replace(ABSPATH, '', $uploads['basedir']) . $uploads['subdir']; } else { $error_path = basename($uploads['basedir']) . $uploads['subdir']; } $uploads['error'] = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), esc_html($error_path)); } else { $tested_paths[] = $path; wp_cache_set('upload_dir_tested_paths', $tested_paths); } } } return $uploads; }
function test_upload_dir_empty() { // upload path setting is empty - it should default to 'wp-content/uploads' update_option('upload_path', ''); // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options. // It doesn't create the /year/month directories. $info = _wp_upload_dir(); $subdir = gmstrftime('/%Y/%m'); $this->assertEquals(get_option('siteurl') . '/wp-content/uploads' . $subdir, $info['url']); $this->assertEquals(ABSPATH . 'wp-content/uploads' . $subdir, $info['path']); $this->assertEquals($subdir, $info['subdir']); $this->assertEquals(false, $info['error']); }