mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 07:40:07 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@36565 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -115,3 +115,26 @@ function _wp_die_handler_txt( $message, $title, $args ) {
|
||||
function _set_default_permalink_structure_for_tests() {
|
||||
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper used with the `upload_dir` filter to remove the /year/month sub directories from the uploads path and URL.
|
||||
*/
|
||||
function _upload_dir_no_subdir( $uploads ) {
|
||||
$subdir = $uploads['subdir'];
|
||||
|
||||
$uploads['subdir'] = '';
|
||||
$uploads['path'] = str_replace( $subdir, '', $uploads['path'] );
|
||||
$uploads['url'] = str_replace( $subdir, '', $uploads['url'] );
|
||||
|
||||
return $uploads;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper used with the `upload_dir` filter to set https upload URL.
|
||||
*/
|
||||
function _upload_dir_https( $uploads ) {
|
||||
$uploads['url'] = str_replace( 'http://', 'https://', $uploads['url'] );
|
||||
$uploads['baseurl'] = str_replace( 'http://', 'https://', $uploads['baseurl'] );
|
||||
|
||||
return $uploads;
|
||||
}
|
||||
|
||||
@@ -895,11 +895,8 @@ EOF;
|
||||
function test_wp_calculate_image_srcset_no_date_uploads() {
|
||||
global $_wp_additional_image_sizes;
|
||||
|
||||
// Save the current setting for uploads folders
|
||||
$uploads_use_yearmonth_folders = get_option( 'uploads_use_yearmonth_folders' );
|
||||
|
||||
// Disable date organized uploads
|
||||
update_option( 'uploads_use_yearmonth_folders', 0 );
|
||||
add_filter( 'upload_dir', '_upload_dir_no_subdir' );
|
||||
|
||||
// Make an image.
|
||||
$filename = DIR_TESTDATA . '/images/test-image-large.png';
|
||||
@@ -937,9 +934,7 @@ EOF;
|
||||
|
||||
// Remove the attachment
|
||||
wp_delete_attachment( $id );
|
||||
|
||||
// Leave the uploads option the way you found it.
|
||||
update_option( 'uploads_use_yearmonth_folders', $uploads_use_yearmonth_folders );
|
||||
remove_filter( 'upload_dir', '_upload_dir_no_subdir' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -441,9 +441,8 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
|
||||
* @ticket 15928
|
||||
*/
|
||||
public function test_wp_get_attachment_url_should_force_https_when_administering_over_https_and_siteurl_is_https() {
|
||||
// Must set the upload_url_path to fake out `wp_upload_dir()`.
|
||||
$siteurl = get_option( 'siteurl' );
|
||||
update_option( 'upload_url_path', set_url_scheme( $siteurl, 'https' ) . '/uploads' );
|
||||
// Set https upload URL
|
||||
add_filter( 'upload_dir', '_upload_dir_https' );
|
||||
|
||||
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
|
||||
$contents = file_get_contents( $filename );
|
||||
@@ -463,6 +462,7 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
|
||||
// Cleanup.
|
||||
$_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
|
||||
set_current_screen( 'front' );
|
||||
remove_filter( 'upload_dir', '_upload_dir_https' );
|
||||
|
||||
$this->assertSame( set_url_scheme( $url, 'https' ), $url );
|
||||
}
|
||||
|
||||
@@ -26,22 +26,24 @@ class Tests_Upload extends WP_UnitTestCase {
|
||||
function test_upload_dir_default() {
|
||||
// wp_upload_dir() with default parameters
|
||||
$info = wp_upload_dir();
|
||||
$this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
|
||||
$this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
|
||||
$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
|
||||
$this->assertEquals( '', $info['error'] );
|
||||
$subdir = gmstrftime('/%Y/%m');
|
||||
|
||||
$this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] );
|
||||
$this->assertEquals( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
|
||||
$this->assertEquals( $subdir, $info['subdir'] );
|
||||
$this->assertEquals( false, $info['error'] );
|
||||
}
|
||||
|
||||
function test_upload_dir_relative() {
|
||||
// wp_upload_dir() with a relative upload path that is not 'wp-content/uploads'
|
||||
update_option( 'upload_path', 'foo/bar' );
|
||||
$info = wp_upload_dir();
|
||||
$this->delete_folders( ABSPATH . 'foo' );
|
||||
$info = _wp_upload_dir();
|
||||
$subdir = gmstrftime('/%Y/%m');
|
||||
|
||||
$this->assertEquals( get_option( 'siteurl' ) . '/foo/bar/' . gmstrftime('%Y/%m'), $info['url'] );
|
||||
$this->assertEquals( ABSPATH . 'foo/bar/' . gmstrftime('%Y/%m'), $info['path'] );
|
||||
$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
|
||||
$this->assertEquals( '', $info['error'] );
|
||||
$this->assertEquals( get_option( 'siteurl' ) . '/foo/bar' . $subdir, $info['url'] );
|
||||
$this->assertEquals( ABSPATH . 'foo/bar' . $subdir, $info['path'] );
|
||||
$this->assertEquals( $subdir, $info['subdir'] );
|
||||
$this->assertEquals( false, $info['error'] );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,45 +51,63 @@ class Tests_Upload extends WP_UnitTestCase {
|
||||
*/
|
||||
function test_upload_dir_absolute() {
|
||||
$path = '/tmp/wp-unit-test';
|
||||
|
||||
// wp_upload_dir() with an absolute upload path
|
||||
update_option( 'upload_path', $path );
|
||||
|
||||
// doesn't make sense to use an absolute file path without setting the url path
|
||||
update_option( 'upload_url_path', '/baz' );
|
||||
$info = wp_upload_dir();
|
||||
$this->delete_folders( $path );
|
||||
|
||||
$this->assertEquals( '/baz/' . gmstrftime('%Y/%m'), $info['url'] );
|
||||
$this->assertEquals( "$path/" . gmstrftime('%Y/%m'), $info['path'] );
|
||||
$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
|
||||
$this->assertEquals( '', $info['error'] );
|
||||
// Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
|
||||
// It doesn't create the /year/month directories.
|
||||
$info = _wp_upload_dir();
|
||||
$subdir = gmstrftime('/%Y/%m');
|
||||
|
||||
$this->assertEquals( '/baz' . $subdir, $info['url'] );
|
||||
$this->assertEquals( $path . $subdir, $info['path'] );
|
||||
$this->assertEquals( $subdir, $info['subdir'] );
|
||||
$this->assertEquals( false, $info['error'] );
|
||||
}
|
||||
|
||||
function test_upload_dir_no_yearnum() {
|
||||
update_option( 'uploads_use_yearmonth_folders', 0 );
|
||||
$info = wp_upload_dir();
|
||||
|
||||
// Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
|
||||
$info = _wp_upload_dir();
|
||||
|
||||
$this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads', $info['url'] );
|
||||
$this->assertEquals( ABSPATH . 'wp-content/uploads', $info['path'] );
|
||||
$this->assertEquals( '', $info['subdir'] );
|
||||
$this->assertEquals( '', $info['error'] );
|
||||
$this->assertEquals( false, $info['error'] );
|
||||
}
|
||||
|
||||
function test_upload_path_absolute() {
|
||||
update_option( 'upload_url_path', 'http://example.org/asdf' );
|
||||
$info = wp_upload_dir();
|
||||
$this->assertEquals( 'http://example.org/asdf/' . gmstrftime('%Y/%m'), $info['url'] );
|
||||
$this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
|
||||
$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
|
||||
$this->assertEquals( '', $info['error'] );
|
||||
|
||||
// Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
|
||||
// It doesn't create the /year/month directories.
|
||||
$info = _wp_upload_dir();
|
||||
$subdir = gmstrftime('/%Y/%m');
|
||||
|
||||
$this->assertEquals( 'http://example.org/asdf' . $subdir, $info['url'] );
|
||||
$this->assertEquals( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
|
||||
$this->assertEquals( $subdir, $info['subdir'] );
|
||||
$this->assertEquals( false, $info['error'] );
|
||||
}
|
||||
|
||||
function test_upload_dir_empty() {
|
||||
// upload path setting is empty - it should default to 'wp-content/uploads'
|
||||
update_option('upload_path', '');
|
||||
$info = wp_upload_dir();
|
||||
$this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads/' . gmstrftime('%Y/%m'), $info['url'] );
|
||||
$this->assertEquals( ABSPATH . 'wp-content/uploads/' . gmstrftime('%Y/%m'), $info['path'] );
|
||||
$this->assertEquals( gmstrftime('/%Y/%m'), $info['subdir'] );
|
||||
$this->assertEquals( '', $info['error'] );
|
||||
|
||||
// Use `_wp_upload_dir()` directly to bypass caching and work with the changed options.
|
||||
// It doesn't create the /year/month directories.
|
||||
$info = _wp_upload_dir();
|
||||
$subdir = gmstrftime('/%Y/%m');
|
||||
|
||||
$this->assertEquals( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] );
|
||||
$this->assertEquals( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] );
|
||||
$this->assertEquals( $subdir, $info['subdir'] );
|
||||
$this->assertEquals( false, $info['error'] );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user