Upload: Introduce pre_wp_unique_filename_file_list filter to allow for short-circuiting the scandir() call in wp_unique_filename().

This allows plugins to override the file fetching behavior to provide performance improvements for large directories.

Props joehoyle.
Fixes #50587.

git-svn-id: https://develop.svn.wordpress.org/trunk@48369 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-07-07 10:18:25 +00:00
parent 7348112444
commit 3eb2bd9f5b

View File

@ -2533,8 +2533,25 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = null )
// The (resized) image files would have name and extension, and will be in the uploads dir.
if ( $name && $ext && @is_dir( $dir ) && false !== strpos( $dir, $upload_dir['basedir'] ) ) {
// List of all files and directories contained in $dir.
$files = @scandir( $dir );
/**
* Filters the file list used for calculating a unique filename for a newly added file.
*
* Returning an array from the filter will effectively short-circuit retrieval
* from the filesystem and return the passed value instead.
*
* @since 5.5.0
*
* @param array|null $files The list of files to use for filename comparisons.
* Default null (to retrieve the list from the filesystem).
* @param string $dir The directory for the new file.
* @param string $filename The proposed filename for the new file.
*/
$files = apply_filters( 'pre_wp_unique_filename_file_list', null, $dir, $filename );
if ( null === $files ) {
// List of all files and directories contained in $dir.
$files = @scandir( $dir );
}
if ( ! empty( $files ) ) {
// Remove "dot" dirs.