Media: Don't cache the results of `wp_mkdir_p()` in a persistent cache.

To improve the performance of `wp_upload_dir()` the result of `wp_mkdir_p()` was stored in a persistent cache, introduced in [36565]. But this becomes an issue when WordPress is scaled horizontally. You may end up caching a value for a server where the directory doesn't exist which will prevent further uploads on other servers because of the persistent cache.
The fix is to use a non-persistent cache.

Props azaozz, ocean90.
See #34359.
Fixes #36621.
Built from https://develop.svn.wordpress.org/trunk@37285


git-svn-id: http://core.svn.wordpress.org/trunk@37251 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2016-04-21 19:58:27 +00:00
parent 959f1a07a6
commit 3df07b17dd
2 changed files with 7 additions and 11 deletions

View File

@ -1854,7 +1854,7 @@ function wp_get_upload_dir() {
* @return array See above for description. * @return array See above for description.
*/ */
function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) { function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) {
static $cache = array(); static $cache = array(), $tested_paths = array();
$key = sprintf( '%d-%s', get_current_blog_id(), (string) $time ); $key = sprintf( '%d-%s', get_current_blog_id(), (string) $time );
@ -1874,13 +1874,10 @@ function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false
if ( $create_dir ) { if ( $create_dir ) {
$path = $uploads['path']; $path = $uploads['path'];
$tested_paths = wp_cache_get( 'upload_dir_tested_paths' );
if ( ! is_array( $tested_paths ) ) { if ( array_key_exists( $path, $tested_paths ) ) {
$tested_paths = array(); $uploads['error'] = $tested_paths[ $path ];
} } else {
if ( ! in_array( $path, $tested_paths, true ) ) {
if ( ! wp_mkdir_p( $path ) ) { if ( ! wp_mkdir_p( $path ) ) {
if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) { if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir']; $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
@ -1889,10 +1886,9 @@ function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false
} }
$uploads['error'] = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), esc_html( $error_path ) ); $uploads['error'] = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), esc_html( $error_path ) );
} else {
$tested_paths[] = $path;
wp_cache_set( 'upload_dir_tested_paths', $tested_paths );
} }
$tested_paths[ $path ] = $uploads['error'];
} }
} }

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.6-alpha-37283'; $wp_version = '4.6-alpha-37285';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.