Ticket #34359: 34359.4.patch
File 34359.4.patch, 10.7 KB (added by , 8 years ago) |
---|
-
src/wp-includes/functions.php
1778 1778 } 1779 1779 1780 1780 /** 1781 * Get uploads directory information. 1782 * 1783 * Same as wp_upload_dir() but "light weight" as it doesn't attempt to create the uploads directory. 1784 * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases when not uploading files. 1785 * 1786 * @since 4.5.0 1787 * 1788 * @return array See wp_upload_dir() for description. 1789 */ 1790 function wp_get_upload_dir() { 1791 return wp_upload_dir( null, false ); 1792 } 1793 1794 /** 1781 1795 * Get an array containing the current upload directory's path and url. 1782 1796 * 1783 1797 * Checks the 'upload_path' option, which should be from the web root folder, … … 1802 1816 * 'subdir' - sub directory if uploads use year/month folders option is on. 1803 1817 * 'basedir' - path without subdir. 1804 1818 * 'baseurl' - URL path without subdir. 1805 * 'error' - set to false.1819 * 'error' - false or error message. 1806 1820 * 1807 1821 * @since 2.0.0 1822 * @uses _wp_upload_dir() 1808 1823 * 1809 1824 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. 1825 * @param bool $create_dir Optional. Whether to check and create the uploads directory. Default true (backwards compatible). 1826 * @param bool $refresh_cache Optional. Whether to refresh the cache. Default false. 1810 1827 * @return array See above for description. 1811 1828 */ 1812 function wp_upload_dir( $time = null ) { 1829 function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) { 1830 static $cache = array(); 1831 1832 $key = sprintf( '%d-%s', get_current_blog_id(), (string) $time ); 1833 1834 if ( $refresh_cache || empty( $cache[ $key ] ) ) { 1835 $cache[ $key ] = _wp_upload_dir( $time ); 1836 } 1837 1838 /** 1839 * Filter the uploads directory data. 1840 * 1841 * @since 2.0.0 1842 * 1843 * @param array $uploads Array of upload directory data with keys of 'path', 1844 * 'url', 'subdir, 'basedir', and 'error'. 1845 */ 1846 $uploads = apply_filters( 'upload_dir', $cache[ $key ] ); 1847 1848 if ( $create_dir ) { 1849 $path = $uploads['path']; 1850 $tested_paths = wp_cache_get( 'upload_dir_tested_paths' ); 1851 1852 if ( ! is_array( $tested_paths ) ) { 1853 $tested_paths = array(); 1854 } 1855 1856 if ( array_key_exists( $path, $tested_paths ) ) { 1857 $uploads['error'] = $tested_paths[ $path ]; 1858 } else { 1859 if ( ! wp_mkdir_p( $uploads['path'] ) ) { 1860 if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) { 1861 $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir']; 1862 } else { 1863 $error_path = basename( $uploads['basedir'] ) . $uploads['subdir']; 1864 } 1865 1866 $uploads['error'] = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path ); 1867 } 1868 1869 $tested_paths[ $path ] = $uploads['error']; 1870 wp_cache_set( 'upload_dir_tested_paths', $tested_paths ); 1871 } 1872 } 1873 1874 return $uploads; 1875 } 1876 1877 /** 1878 * A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path. 1879 * 1880 * @access private 1881 * 1882 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. 1883 * @return array See wp_upload_dir() 1884 */ 1885 function _wp_upload_dir( $time = null ) { 1813 1886 $siteurl = get_option( 'siteurl' ); 1814 1887 $upload_path = trim( get_option( 'upload_path' ) ); 1815 1888 … … 1898 1971 $dir .= $subdir; 1899 1972 $url .= $subdir; 1900 1973 1901 /** 1902 * Filter the uploads directory data. 1903 * 1904 * @since 2.0.0 1905 * 1906 * @param array $uploads Array of upload directory data with keys of 'path', 1907 * 'url', 'subdir, 'basedir', and 'error'. 1908 */ 1909 $uploads = apply_filters( 'upload_dir', 1910 array( 1911 'path' => $dir, 1912 'url' => $url, 1913 'subdir' => $subdir, 1914 'basedir' => $basedir, 1915 'baseurl' => $baseurl, 1916 'error' => false, 1917 ) ); 1974 $uploads = array( 1975 'path' => $dir, 1976 'url' => $url, 1977 'subdir' => $subdir, 1978 'basedir' => $basedir, 1979 'baseurl' => $baseurl, 1980 'error' => false, 1981 ); 1918 1982 1919 // Make sure we have an uploads directory.1920 if ( ! wp_mkdir_p( $uploads['path'] ) ) {1921 if ( 0 === strpos( $uploads['basedir'], ABSPATH ) )1922 $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];1923 else1924 $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];1925 1926 $message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path );1927 $uploads['error'] = $message;1928 }1929 1930 1983 return $uploads; 1931 1984 } 1932 1985 -
tests/phpunit/includes/functions.php
115 115 function _set_default_permalink_structure_for_tests() { 116 116 update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' ); 117 117 } 118 119 /** 120 * Remove the subdir (year/month) part from the uploads 'path' and 'url'. 121 * 122 * For use when testing uploads that are not stored in sub directory. 123 */ 124 function _upload_dir_no_subdir( $uploads ) { 125 $subdir = $uploads['subdir']; 126 127 $uploads['subdir'] = ''; 128 $uploads['path'] = str_replace( $subdir, '', $uploads['path'] ); 129 $uploads['url'] = str_replace( $subdir, '', $uploads['url'] ); 130 131 return $uploads; 132 } -
tests/phpunit/tests/media.php
785 785 function test_wp_calculate_image_srcset_no_date_uploads() { 786 786 global $_wp_additional_image_sizes; 787 787 788 // Save the current setting for uploads folders789 $uploads_use_yearmonth_folders = get_option( 'uploads_use_yearmonth_folders' );790 791 788 // Disable date organized uploads 792 update_option( 'uploads_use_yearmonth_folders', 0);789 add_filter( 'upload_dir', '_upload_dir_no_subdir' ); 793 790 794 791 // Make an image. 795 792 $filename = DIR_TESTDATA . '/images/test-image-large.png'; … … 827 824 828 825 // Remove the attachment 829 826 wp_delete_attachment( $id ); 830 831 // Leave the uploads option the way you found it. 832 update_option( 'uploads_use_yearmonth_folders', $uploads_use_yearmonth_folders ); 827 remove_filter( 'upload_dir', '_upload_dir_no_subdir' ); 833 828 } 834 829 835 830 /** -
tests/phpunit/tests/upload.php
26 26 function test_upload_dir_default() { 27 27 // wp_upload_dir() with default parameters 28 28 $info = wp_upload_dir(); 29 $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] ); 30 $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] ); 31 $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] ); 29 $subdir = gmstrftime('/%Y/%m'); 30 31 $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] ); 32 $this->assertEquals( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] ); 33 $this->assertEquals( $subdir, $info['subdir'] ); 32 34 $this->assertEquals( '', $info['error'] ); 33 35 } 34 36 … … 35 37 function test_upload_dir_relative() { 36 38 // wp_upload_dir() with a relative upload path that is not 'wp-content/uploads' 37 39 update_option( 'upload_path', 'foo/bar' ); 38 $info = wp_upload_dir( );40 $info = wp_upload_dir( null, true, true ); 39 41 $this->delete_folders( ABSPATH . 'foo' ); 42 $subdir = gmstrftime('/%Y/%m'); 40 43 41 $this->assertEquals( get_option( 'siteurl' ) . '/foo/bar /' . gmstrftime('%Y/%m'), $info['url'] );42 $this->assertEquals( ABSPATH . 'foo/bar /' . gmstrftime('%Y/%m'), $info['path'] );43 $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );44 $this->assertEquals( get_option( 'siteurl' ) . '/foo/bar' . $subdir, $info['url'] ); 45 $this->assertEquals( ABSPATH . 'foo/bar' . $subdir, $info['path'] ); 46 $this->assertEquals( $subdir, $info['subdir'] ); 44 47 $this->assertEquals( '', $info['error'] ); 45 48 } 46 49 … … 53 56 update_option( 'upload_path', $path ); 54 57 // doesn't make sense to use an absolute file path without setting the url path 55 58 update_option( 'upload_url_path', '/baz' ); 56 $info = wp_upload_dir( );59 $info = wp_upload_dir( null, true, true ); 57 60 $this->delete_folders( $path ); 61 $subdir = gmstrftime('/%Y/%m'); 58 62 59 $this->assertEquals( '/baz /' . gmstrftime('%Y/%m'), $info['url'] );60 $this->assertEquals( "$path/" . gmstrftime('%Y/%m'), $info['path'] );61 $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );63 $this->assertEquals( '/baz' . $subdir, $info['url'] ); 64 $this->assertEquals( $path . $subdir, $info['path'] ); 65 $this->assertEquals( $subdir, $info['subdir'] ); 62 66 $this->assertEquals( '', $info['error'] ); 63 67 } 64 68 65 69 function test_upload_dir_no_yearnum() { 66 70 update_option( 'uploads_use_yearmonth_folders', 0 ); 67 $info = wp_upload_dir(); 71 $info = wp_upload_dir( null, true, true ); 72 68 73 $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads', $info['url'] ); 69 74 $this->assertEquals( ABSPATH . 'wp-content/uploads', $info['path'] ); 70 75 $this->assertEquals( '', $info['subdir'] ); … … 73 78 74 79 function test_upload_path_absolute() { 75 80 update_option( 'upload_url_path', 'http://example.org/asdf' ); 76 $info = wp_upload_dir(); 77 $this->assertEquals( 'http://example.org/asdf/' . gmstrftime('%Y/%m'), $info['url'] ); 78 $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] ); 79 $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] ); 81 $info = wp_upload_dir( null, true, true ); 82 $subdir = gmstrftime('/%Y/%m'); 83 84 $this->assertEquals( 'http://example.org/asdf' . $subdir, $info['url'] ); 85 $this->assertEquals( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] ); 86 $this->assertEquals( $subdir, $info['subdir'] ); 80 87 $this->assertEquals( '', $info['error'] ); 81 88 } 82 89 … … 83 90 function test_upload_dir_empty() { 84 91 // upload path setting is empty - it should default to 'wp-content/uploads' 85 92 update_option('upload_path', ''); 86 $info = wp_upload_dir(); 87 $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] ); 88 $this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] ); 89 $this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] ); 93 $info = wp_upload_dir( null, true, true ); 94 $subdir = gmstrftime('/%Y/%m'); 95 96 $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] ); 97 $this->assertEquals( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] ); 98 $this->assertEquals( $subdir, $info['subdir'] ); 90 99 $this->assertEquals( '', $info['error'] ); 91 100 } 92 101