Make WordPress Core

Ticket #34359: 34359.5.patch

File 34359.5.patch, 10.8 KB (added by azaozz, 7 years ago)
  • src/wp-includes/functions.php

     
    18001800}
    18011801
    18021802/**
     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 */
     1812function wp_get_upload_dir() {
     1813        return wp_upload_dir( null, false );
     1814}
     1815
     1816/**
    18031817 * Get an array containing the current upload directory's path and url.
    18041818 *
    18051819 * Checks the 'upload_path' option, which should be from the web root folder,
     
    18241838 * 'subdir' - sub directory if uploads use year/month folders option is on.
    18251839 * 'basedir' - path without subdir.
    18261840 * 'baseurl' - URL path without subdir.
    1827  * 'error' - set to false.
     1841 * 'error' - false or error message.
    18281842 *
    18291843 * @since 2.0.0
     1844 * @uses _wp_upload_dir()
    18301845 *
    18311846 * @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.
    18321849 * @return array See above for description.
    18331850 */
    1834 function wp_upload_dir( $time = null ) {
     1851function 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 */
     1911function _wp_upload_dir( $time = null ) {
    18351912        $siteurl = get_option( 'siteurl' );
    18361913        $upload_path = trim( get_option( 'upload_path' ) );
    18371914
     
    19201997        $dir .= $subdir;
    19211998        $url .= $subdir;
    19221999
    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        );
    19402008
    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                 else
    1946                         $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 
    19522009        return $uploads;
    19532010}
    19542011
  • 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

     
    895895        function test_wp_calculate_image_srcset_no_date_uploads() {
    896896                global $_wp_additional_image_sizes;
    897897
    898                 // Save the current setting for uploads folders
    899                 $uploads_use_yearmonth_folders = get_option( 'uploads_use_yearmonth_folders' );
    900 
    901898                // Disable date organized uploads
    902                 update_option( 'uploads_use_yearmonth_folders', 0 );
     899                add_filter( 'upload_dir', '_upload_dir_no_subdir' );
    903900
    904901                // Make an image.
    905902                $filename = DIR_TESTDATA . '/images/test-image-large.png';
     
    937934
    938935                // Remove the attachment
    939936                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' );
    943938        }
    944939
    945940        /**
  • 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