1808 | | $siteurl = get_option( 'siteurl' ); |
1809 | | $upload_path = trim( get_option( 'upload_path' ) ); |
1810 | | |
1811 | | if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) { |
1812 | | $dir = WP_CONTENT_DIR . '/uploads'; |
1813 | | } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) { |
1814 | | // $dir is absolute, $upload_path is (maybe) relative to ABSPATH |
1815 | | $dir = path_join( ABSPATH, $upload_path ); |
1816 | | } else { |
1817 | | $dir = $upload_path; |
| 1808 | if ( ! $time ) { |
| 1809 | $time = current_time( 'mysql' ); |
1820 | | if ( !$url = get_option( 'upload_url_path' ) ) { |
1821 | | if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) ) |
1822 | | $url = WP_CONTENT_URL . '/uploads'; |
1823 | | else |
1824 | | $url = trailingslashit( $siteurl ) . $upload_path; |
1825 | | } |
| 1812 | // Generate cache key with yeatr_month suffix |
| 1813 | $year = substr( $time, 0, 4 ); |
| 1814 | $month = substr( $time, 5, 2 ); |
| 1815 | $cache_key = "upload_dir_{$year}_{$month}"; |
1839 | | if ( ! get_site_option( 'ms_files_rewriting' ) ) { |
1840 | | /* |
1841 | | * If ms-files rewriting is disabled (networks created post-3.5), it is fairly |
1842 | | * straightforward: Append sites/%d if we're not on the main site (for post-MU |
1843 | | * networks). (The extra directory prevents a four-digit ID from conflicting with |
1844 | | * a year-based directory for the main site. But if a MU-era network has disabled |
1845 | | * ms-files rewriting manually, they don't need the extra directory, as they never |
1846 | | * had wp-content/uploads for the main site.) |
1847 | | */ |
| 1823 | if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) { |
| 1824 | $dir = WP_CONTENT_DIR . '/uploads'; |
| 1825 | } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) { |
| 1826 | // $dir is absolute, $upload_path is (maybe) relative to ABSPATH |
| 1827 | $dir = path_join( ABSPATH, $upload_path ); |
| 1828 | } else { |
| 1829 | $dir = $upload_path; |
| 1830 | } |
1857 | | } elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) { |
1858 | | /* |
1859 | | * Handle the old-form ms-files.php rewriting if the network still has that enabled. |
1860 | | * When ms-files rewriting is enabled, then we only listen to UPLOADS when: |
1861 | | * 1) We are not on the main site in a post-MU network, as wp-content/uploads is used |
1862 | | * there, and |
1863 | | * 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect |
1864 | | * the original blog ID. |
1865 | | * |
1866 | | * Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute. |
1867 | | * (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as |
1868 | | * as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files |
1869 | | * rewriting in multisite, the resulting URL is /files. (#WP22702 for background.) |
1870 | | */ |
| 1849 | // If multisite (and if not the main site in a post-MU network) |
| 1850 | if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) { |
1872 | | if ( defined( 'BLOGUPLOADDIR' ) ) |
1873 | | $dir = untrailingslashit( BLOGUPLOADDIR ); |
1874 | | else |
1875 | | $dir = ABSPATH . UPLOADS; |
1876 | | $url = trailingslashit( $siteurl ) . 'files'; |
| 1852 | if ( ! get_site_option( 'ms_files_rewriting' ) ) { |
| 1853 | /* |
| 1854 | * If ms-files rewriting is disabled (networks created post-3.5), it is fairly |
| 1855 | * straightforward: Append sites/%d if we're not on the main site (for post-MU |
| 1856 | * networks). (The extra directory prevents a four-digit ID from conflicting with |
| 1857 | * a year-based directory for the main site. But if a MU-era network has disabled |
| 1858 | * ms-files rewriting manually, they don't need the extra directory, as they never |
| 1859 | * had wp-content/uploads for the main site.) |
| 1860 | */ |
| 1861 | |
| 1862 | if ( defined( 'MULTISITE' ) ) { |
| 1863 | $ms_dir = '/sites/' . get_current_blog_id(); |
| 1864 | } else { |
| 1865 | $ms_dir = '/' . get_current_blog_id(); |
| 1866 | } |
| 1867 | |
| 1868 | $dir .= $ms_dir; |
| 1869 | $url .= $ms_dir; |
| 1870 | |
| 1871 | } elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) { |
| 1872 | /* |
| 1873 | * Handle the old-form ms-files.php rewriting if the network still has that enabled. |
| 1874 | * When ms-files rewriting is enabled, then we only listen to UPLOADS when: |
| 1875 | * 1) We are not on the main site in a post-MU network, as wp-content/uploads is used |
| 1876 | * there, and |
| 1877 | * 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect |
| 1878 | * the original blog ID. |
| 1879 | * |
| 1880 | * Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute. |
| 1881 | * (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as |
| 1882 | * as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files |
| 1883 | * rewriting in multisite, the resulting URL is /files. (#WP22702 for background.) |
| 1884 | */ |
| 1885 | |
| 1886 | if ( defined( 'BLOGUPLOADDIR' ) ) { |
| 1887 | $dir = untrailingslashit( BLOGUPLOADDIR ); |
| 1888 | } else { |
| 1889 | $dir = ABSPATH . UPLOADS; |
| 1890 | } |
| 1891 | |
| 1892 | $url = trailingslashit( $siteurl ) . 'files'; |
| 1893 | } |
1883 | | $subdir = ''; |
1884 | | if ( get_option( 'uploads_use_yearmonth_folders' ) ) { |
1885 | | // Generate the yearly and monthly dirs |
1886 | | if ( !$time ) |
1887 | | $time = current_time( 'mysql' ); |
1888 | | $y = substr( $time, 0, 4 ); |
1889 | | $m = substr( $time, 5, 2 ); |
1890 | | $subdir = "/$y/$m"; |
| 1899 | $subdir = ''; |
| 1900 | if ( get_option( 'uploads_use_yearmonth_folders' ) ) { |
| 1901 | $subdir = "/{$year}/{$month}"; |
| 1902 | } |
| 1903 | |
| 1904 | $dir .= $subdir; |
| 1905 | $url .= $subdir; |
| 1906 | |
| 1907 | $uploads = array( |
| 1908 | 'path' => $dir, |
| 1909 | 'url' => $url, |
| 1910 | 'subdir' => $subdir, |
| 1911 | 'basedir' => $basedir, |
| 1912 | 'baseurl' => $baseurl, |
| 1913 | 'error' => false, |
| 1914 | ); |
| 1915 | |
| 1916 | // Cache the $uploads array before filtering. |
| 1917 | wp_cache_set( $cache_key, $uploads ); |
1914 | | // Make sure we have an uploads directory. |
1915 | | if ( ! wp_mkdir_p( $uploads['path'] ) ) { |
1916 | | if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) |
1917 | | $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir']; |
1918 | | else |
1919 | | $error_path = basename( $uploads['basedir'] ) . $uploads['subdir']; |
| 1930 | $filtered_path = $filtered_uploads['path']; |
| 1931 | $tested_paths = wp_cache_get( 'upload_dir_paths' ); |
1925 | | return $uploads; |
| 1937 | if ( ! array_key_exists( $filtered_path, $tested_paths ) ) { |
| 1938 | // Make sure we have an uploads directory. |
| 1939 | if ( ! wp_mkdir_p( $filtered_path ) ) { |
| 1940 | if ( 0 === strpos( $filtered_uploads['basedir'], ABSPATH ) ) { |
| 1941 | $error_path = str_replace( ABSPATH, '', $filtered_uploads['basedir'] ) . $filtered_uploads['subdir']; |
| 1942 | } else { |
| 1943 | $error_path = basename( $filtered_uploads['basedir'] ) . $filtered_uploads['subdir']; |
| 1944 | } |
| 1945 | |
| 1946 | $message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path ); |
| 1947 | $filtered_uploads['error'] = $message; |
| 1948 | } |
| 1949 | |
| 1950 | $tested_paths[ $filtered_path ] = $filtered_uploads['error']; |
| 1951 | wp_cache_set( 'upload_dir_paths', $tested_paths ); |
| 1952 | } |
| 1953 | |
| 1954 | // Set the proper error for this path. |
| 1955 | $filtered_uploads['error'] = $tested_paths[ $filtered_path ]; |
| 1956 | |
| 1957 | return $filtered_uploads; |