Make WordPress Core


Ignore:
Timestamp:
02/17/2016 10:51:01 PM (9 years ago)
Author:
azaozz
Message:

Improve the performance of wp_upload_dir():

  • Cache the output in non-persistent cache.
  • Cache the result from wp_mkdir_p() in persistent cache (when present).
  • Introduce wp_get_upload_dir() for use when not uploading files. It is equivalent to wp_upload_dir() but does not check for the existence or create the upload directory.
  • Change tests to use the non-cached _wp_upload_dir(). They change options on the fly (should never be used in production) to simulate different environments.
  • Introduce _upload_dir_no_subdir() and _upload_dir_https() to facilitate testing. These use the proper upload_dir filter to simulate different environments.

Props kovshenin, azaozz.
See #34359.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r36541 r36565  
    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 *
     
    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 = true, $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 ) {
     1871        $path = $uploads['path'];
     1872        $tested_paths = wp_cache_get( 'upload_dir_tested_paths' );
     1873
     1874        if ( ! is_array( $tested_paths ) ) {
     1875            $tested_paths = array();
     1876        }
     1877
     1878        if ( array_key_exists( $path, $tested_paths ) ) {
     1879            $uploads['error'] = $tested_paths[ $path ];
     1880        } else {
     1881            if ( ! wp_mkdir_p( $path ) ) {
     1882                if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
     1883                    $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
     1884                } else {
     1885                    $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
     1886                }
     1887
     1888                $uploads['error'] = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path );
     1889            }
     1890
     1891            $tested_paths[ $path ] = $uploads['error'];
     1892            wp_cache_set( 'upload_dir_tested_paths', $tested_paths );
     1893        }
     1894    }
     1895
     1896    return $uploads;
     1897}
     1898
     1899/**
     1900 * A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
     1901 *
     1902 * @access private
     1903 *
     1904 * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
     1905 * @return array See wp_upload_dir()
     1906 */
     1907function _wp_upload_dir( $time = null ) {
    18351908    $siteurl = get_option( 'siteurl' );
    18361909    $upload_path = trim( get_option( 'upload_path' ) );
     
    19211994    $url .= $subdir;
    19221995
    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         ) );
    1940 
    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 
    1952     return $uploads;
     1996    return array(
     1997        'path'    => $dir,
     1998        'url'     => $url,
     1999        'subdir'  => $subdir,
     2000        'basedir' => $basedir,
     2001        'baseurl' => $baseurl,
     2002        'error'   => false,
     2003    );
    19532004}
    19542005
Note: See TracChangeset for help on using the changeset viewer.