WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/17/2016 10:51:01 PM (5 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/tests/phpunit/tests/upload.php

    r30658 r36565  
    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'] );
    32         $this->assertEquals( '', $info['error'] );
     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'] );
     34        $this->assertEquals( false, $info['error'] );
    3335    }
    3436
     
    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();
    39         $this->delete_folders( ABSPATH . 'foo' );
     40        $info = _wp_upload_dir();
     41        $subdir = gmstrftime('/%Y/%m');
    4042
    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( '', $info['error'] );
     43        $this->assertEquals( get_option( 'siteurl' ) . '/foo/bar' . $subdir, $info['url'] );
     44        $this->assertEquals( ABSPATH . 'foo/bar' . $subdir, $info['path'] );
     45        $this->assertEquals( $subdir, $info['subdir'] );
     46        $this->assertEquals( false, $info['error'] );
    4547    }
    4648
     
    5052    function test_upload_dir_absolute() {
    5153        $path = '/tmp/wp-unit-test';
     54
    5255        // wp_upload_dir() with an absolute upload path
    5356        update_option( 'upload_path', $path );
     57
    5458        // doesn't make sense to use an absolute file path without setting the url path
    5559        update_option( 'upload_url_path', '/baz' );
    56         $info = wp_upload_dir();
    57         $this->delete_folders( $path );
    5860
    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'] );
    62         $this->assertEquals( '', $info['error'] );
     61        // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
     62        // It doesn't create the /year/month directories.
     63        $info = _wp_upload_dir();
     64        $subdir = gmstrftime('/%Y/%m');
     65
     66        $this->assertEquals( '/baz' . $subdir, $info['url'] );
     67        $this->assertEquals( $path . $subdir, $info['path'] );
     68        $this->assertEquals( $subdir, $info['subdir'] );
     69        $this->assertEquals( false, $info['error'] );
    6370    }
    6471
    6572    function test_upload_dir_no_yearnum() {
    6673        update_option( 'uploads_use_yearmonth_folders', 0 );
    67         $info = wp_upload_dir();
     74
     75        // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
     76        $info = _wp_upload_dir();
     77
    6878        $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads', $info['url'] );
    6979        $this->assertEquals( ABSPATH . 'wp-content/uploads', $info['path'] );
    7080        $this->assertEquals( '', $info['subdir'] );
    71         $this->assertEquals( '', $info['error'] );
     81        $this->assertEquals( false, $info['error'] );
    7282    }
    7383
    7484    function test_upload_path_absolute() {
    7585        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'] );
    80         $this->assertEquals( '', $info['error'] );
     86
     87        // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
     88        // It doesn't create the /year/month directories.
     89        $info = _wp_upload_dir();
     90        $subdir = gmstrftime('/%Y/%m');
     91
     92        $this->assertEquals( 'http://example.org/asdf' . $subdir, $info['url'] );
     93        $this->assertEquals( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
     94        $this->assertEquals( $subdir, $info['subdir'] );
     95        $this->assertEquals( false, $info['error'] );
    8196    }
    8297
     
    8499        // upload path setting is empty - it should default to 'wp-content/uploads'
    85100        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'] );
    90         $this->assertEquals( '', $info['error'] );
     101
     102        // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
     103        // It doesn't create the /year/month directories.
     104        $info = _wp_upload_dir();
     105        $subdir = gmstrftime('/%Y/%m');
     106
     107        $this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] );
     108        $this->assertEquals( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
     109        $this->assertEquals( $subdir, $info['subdir'] );
     110        $this->assertEquals( false, $info['error'] );
    91111    }
    92112
Note: See TracChangeset for help on using the changeset viewer.