WordPress.org

Make WordPress Core

Ticket #34359: 34359.3.patch

File 34359.3.patch, 10.5 KB (added by azaozz, 6 years ago)
  • src/wp-includes/functions.php

     
    17781778}
    17791779
    17801780/**
     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 */
     1790function wp_get_upload_dir() {
     1791        return wp_upload_dir( null, false );
     1792}
     1793
     1794/**
    17811795 * Get an array containing the current upload directory's path and url.
    17821796 *
    17831797 * Checks the 'upload_path' option, which should be from the web root folder,
     
    18021816 * 'subdir' - sub directory if uploads use year/month folders option is on.
    18031817 * 'basedir' - path without subdir.
    18041818 * 'baseurl' - URL path without subdir.
    1805  * 'error' - set to false.
     1819 * 'error' - false or error message.
    18061820 *
    18071821 * @since 2.0.0
     1822 * @uses _wp_upload_dir()
    18081823 *
    18091824 * @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.
     1826 * @param bool   $refresh_cache Optional. Whether to refresh the cache. Default false.
    18101827 * @return array See above for description.
    18111828 */
    1812 function wp_upload_dir( $time = null ) {
     1829function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) {
     1830        static $cache = array(), $tested_paths = 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
     1851                if ( array_key_exists( $path, $tested_paths ) ) {
     1852                        $uploads['error'] = $tested_paths[ $path ];
     1853                } else {
     1854                        if ( ! wp_mkdir_p( $uploads['path'] ) ) {
     1855                                if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
     1856                                        $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
     1857                                } else {
     1858                                        $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
     1859                                }
     1860
     1861                                $uploads['error'] = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path );
     1862                        }
     1863
     1864                        $tested_paths[ $path ] = $uploads['error'];
     1865                }
     1866        }
     1867
     1868        return $uploads;
     1869}
     1870
     1871/**
     1872 * A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
     1873 *
     1874 * @access private
     1875 *
     1876 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
     1877 * @return array See wp_upload_dir()
     1878 */
     1879function _wp_upload_dir( $time = null ) {
    18131880        $siteurl = get_option( 'siteurl' );
    18141881        $upload_path = trim( get_option( 'upload_path' ) );
    18151882
     
    18981965        $dir .= $subdir;
    18991966        $url .= $subdir;
    19001967
    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                 ) );
     1968        $uploads = array(
     1969                'path'    => $dir,
     1970                'url'     => $url,
     1971                'subdir'  => $subdir,
     1972                'basedir' => $basedir,
     1973                'baseurl' => $baseurl,
     1974                'error'   => false,
     1975        );
    19181976
    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                 else
    1924                         $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 
    19301977        return $uploads;
    19311978}
    19321979
  • tests/phpunit/includes/functions.php

     
    115115function _set_default_permalink_structure_for_tests() {
    116116        update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
    117117}
     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 */
     124function _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

     
    785785        function test_wp_calculate_image_srcset_no_date_uploads() {
    786786                global $_wp_additional_image_sizes;
    787787
    788                 // Save the current setting for uploads folders
    789                 $uploads_use_yearmonth_folders = get_option( 'uploads_use_yearmonth_folders' );
    790 
    791788                // Disable date organized uploads
    792                 update_option( 'uploads_use_yearmonth_folders', 0 );
     789                add_filter( 'upload_dir', '_upload_dir_no_subdir' );
    793790
    794791                // Make an image.
    795792                $filename = DIR_TESTDATA . '/images/test-image-large.png';
     
    827824
    828825                // Remove the attachment
    829826                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' );
    833828        }
    834829
    835830        /**
  • tests/phpunit/tests/upload.php

     
    2626        function test_upload_dir_default() {
    2727                // wp_upload_dir() with default parameters
    2828                $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'] );
    3234                $this->assertEquals( '', $info['error'] );
    3335        }
    3436
     
    3537        function test_upload_dir_relative() {
    3638                // wp_upload_dir() with a relative upload path that is not 'wp-content/uploads'
    3739                update_option( 'upload_path', 'foo/bar' );
    38                 $info = wp_upload_dir();
     40                $info = wp_upload_dir( null, true, true );
    3941                $this->delete_folders( ABSPATH . 'foo' );
     42                $subdir = gmstrftime('/%Y/%m');
    4043
    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'] );
    4447                $this->assertEquals( '', $info['error'] );
    4548        }
    4649
     
    5356                update_option( 'upload_path', $path );
    5457                // doesn't make sense to use an absolute file path without setting the url path
    5558                update_option( 'upload_url_path', '/baz' );
    56                 $info = wp_upload_dir();
     59                $info = wp_upload_dir( null, true, true );
    5760                $this->delete_folders( $path );
     61                $subdir = gmstrftime('/%Y/%m');
    5862
    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'] );
    6266                $this->assertEquals( '', $info['error'] );
    6367        }
    6468
    6569        function test_upload_dir_no_yearnum() {
    6670                update_option( 'uploads_use_yearmonth_folders', 0 );
    67                 $info = wp_upload_dir();
     71                $info = wp_upload_dir( null, true, true );
     72
    6873                $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads', $info['url'] );
    6974                $this->assertEquals( ABSPATH . 'wp-content/uploads', $info['path'] );
    7075                $this->assertEquals( '', $info['subdir'] );
     
    7378
    7479        function test_upload_path_absolute() {
    7580                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'] );
    8087                $this->assertEquals( '', $info['error'] );
    8188        }
    8289
     
    8390        function test_upload_dir_empty() {
    8491                // upload path setting is empty - it should default to 'wp-content/uploads'
    8592                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'] );
    9099                $this->assertEquals( '', $info['error'] );
    91100        }
    92101