Ticket #34359: 34359.5.patch
File 34359.5.patch, 10.8 KB (added by , 7 years ago) |
---|
-
src/wp-includes/functions.php
1800 1800 } 1801 1801 1802 1802 /** 1803 * Get uploads directory information. 1804 * 1805 * Same as wp_upload_dir() but "light weight" as it doesn't attempt to create the uploads directory. 1806 * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases when not uploading files. 1807 * 1808 * @since 4.5.0 1809 * 1810 * @return array See wp_upload_dir() for description. 1811 */ 1812 function wp_get_upload_dir() { 1813 return wp_upload_dir( null, false ); 1814 } 1815 1816 /** 1803 1817 * Get an array containing the current upload directory's path and url. 1804 1818 * 1805 1819 * Checks the 'upload_path' option, which should be from the web root folder, … … 1824 1838 * 'subdir' - sub directory if uploads use year/month folders option is on. 1825 1839 * 'basedir' - path without subdir. 1826 1840 * 'baseurl' - URL path without subdir. 1827 * 'error' - set to false.1841 * 'error' - false or error message. 1828 1842 * 1829 1843 * @since 2.0.0 1844 * @uses _wp_upload_dir() 1830 1845 * 1831 1846 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. 1847 * @param bool $create_dir Optional. Whether to check and create the uploads directory. Default true (backwards compatible). 1848 * @param bool $refresh_cache Optional. Whether to refresh the cache. Default false. 1832 1849 * @return array See above for description. 1833 1850 */ 1834 function wp_upload_dir( $time = null ) { 1851 function wp_upload_dir( $time = null, $create_dir = 'wp-default-value', $refresh_cache = false ) { 1852 static $cache = array(); 1853 1854 $key = sprintf( '%d-%s', get_current_blog_id(), (string) $time ); 1855 1856 if ( $refresh_cache || empty( $cache[ $key ] ) ) { 1857 $cache[ $key ] = _wp_upload_dir( $time ); 1858 } 1859 1860 /** 1861 * Filter the uploads directory data. 1862 * 1863 * @since 2.0.0 1864 * 1865 * @param array $uploads Array of upload directory data with keys of 'path', 1866 * 'url', 'subdir, 'basedir', and 'error'. 1867 */ 1868 $uploads = apply_filters( 'upload_dir', $cache[ $key ] ); 1869 1870 if ( $create_dir === 'wp-default-value' ) { 1871 $create_dir = is_user_logged_in(); 1872 } 1873 1874 if ( $create_dir ) { 1875 $path = $uploads['path']; 1876 $tested_paths = wp_cache_get( 'upload_dir_tested_paths' ); 1877 1878 if ( ! is_array( $tested_paths ) ) { 1879 $tested_paths = array(); 1880 } 1881 1882 if ( array_key_exists( $path, $tested_paths ) ) { 1883 $uploads['error'] = $tested_paths[ $path ]; 1884 } else { 1885 if ( ! wp_mkdir_p( $uploads['path'] ) ) { 1886 if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) { 1887 $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir']; 1888 } else { 1889 $error_path = basename( $uploads['basedir'] ) . $uploads['subdir']; 1890 } 1891 1892 $uploads['error'] = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path ); 1893 } 1894 1895 $tested_paths[ $path ] = $uploads['error']; 1896 wp_cache_set( 'upload_dir_tested_paths', $tested_paths ); 1897 } 1898 } 1899 1900 return $uploads; 1901 } 1902 1903 /** 1904 * A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path. 1905 * 1906 * @access private 1907 * 1908 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. 1909 * @return array See wp_upload_dir() 1910 */ 1911 function _wp_upload_dir( $time = null ) { 1835 1912 $siteurl = get_option( 'siteurl' ); 1836 1913 $upload_path = trim( get_option( 'upload_path' ) ); 1837 1914 … … 1920 1997 $dir .= $subdir; 1921 1998 $url .= $subdir; 1922 1999 1923 /** 1924 * Filter the uploads directory data. 1925 * 1926 * @since 2.0.0 1927 * 1928 * @param array $uploads Array of upload directory data with keys of 'path', 1929 * 'url', 'subdir, 'basedir', and 'error'. 1930 */ 1931 $uploads = apply_filters( 'upload_dir', 1932 array( 1933 'path' => $dir, 1934 'url' => $url, 1935 'subdir' => $subdir, 1936 'basedir' => $basedir, 1937 'baseurl' => $baseurl, 1938 'error' => false, 1939 ) ); 2000 $uploads = array( 2001 'path' => $dir, 2002 'url' => $url, 2003 'subdir' => $subdir, 2004 'basedir' => $basedir, 2005 'baseurl' => $baseurl, 2006 'error' => false, 2007 ); 1940 2008 1941 // Make sure we have an uploads directory.1942 if ( ! wp_mkdir_p( $uploads['path'] ) ) {1943 if ( 0 === strpos( $uploads['basedir'], ABSPATH ) )1944 $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];1945 else1946 $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];1947 1948 $message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path );1949 $uploads['error'] = $message;1950 }1951 1952 2009 return $uploads; 1953 2010 } 1954 2011 -
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
895 895 function test_wp_calculate_image_srcset_no_date_uploads() { 896 896 global $_wp_additional_image_sizes; 897 897 898 // Save the current setting for uploads folders899 $uploads_use_yearmonth_folders = get_option( 'uploads_use_yearmonth_folders' );900 901 898 // Disable date organized uploads 902 update_option( 'uploads_use_yearmonth_folders', 0);899 add_filter( 'upload_dir', '_upload_dir_no_subdir' ); 903 900 904 901 // Make an image. 905 902 $filename = DIR_TESTDATA . '/images/test-image-large.png'; … … 937 934 938 935 // Remove the attachment 939 936 wp_delete_attachment( $id ); 940 941 // Leave the uploads option the way you found it. 942 update_option( 'uploads_use_yearmonth_folders', $uploads_use_yearmonth_folders ); 937 remove_filter( 'upload_dir', '_upload_dir_no_subdir' ); 943 938 } 944 939 945 940 /** -
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