rebase on oct-10-2023

This commit is contained in:
Rachit Bhargava
2023-10-10 17:23:21 -04:00
parent d37566ffb6
commit d096058d7d
4789 changed files with 254611 additions and 307223 deletions

View File

@@ -127,13 +127,16 @@ function get_home_path() {
*
* @since 2.6.0
* @since 4.9.0 Added the `$exclusions` parameter.
* @since 6.3.0 Added the `$include_hidden` parameter.
*
* @param string $folder Optional. Full path to folder. Default empty.
* @param int $levels Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
* @param string[] $exclusions Optional. List of folders and files to skip.
* @param string $folder Optional. Full path to folder. Default empty.
* @param int $levels Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
* @param string[] $exclusions Optional. List of folders and files to skip.
* @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
* Default false.
* @return string[]|false Array of files on success, false on failure.
*/
function list_files( $folder = '', $levels = 100, $exclusions = array() ) {
function list_files( $folder = '', $levels = 100, $exclusions = array(), $include_hidden = false ) {
if ( empty( $folder ) ) {
return false;
}
@@ -156,12 +159,12 @@ function list_files( $folder = '', $levels = 100, $exclusions = array() ) {
}
// Skip hidden and excluded files.
if ( '.' === $file[0] || in_array( $file, $exclusions, true ) ) {
if ( ( ! $include_hidden && '.' === $file[0] ) || in_array( $file, $exclusions, true ) ) {
continue;
}
if ( is_dir( $folder . $file ) ) {
$files2 = list_files( $folder . $file, $levels - 1 );
$files2 = list_files( $folder . $file, $levels - 1, array(), $include_hidden );
if ( $files2 ) {
$files = array_merge( $files, $files2 );
} else {
@@ -310,7 +313,7 @@ function wp_print_file_editor_templates() {
<?php
printf(
/* translators: 1: Line number, 2: File path. */
__( 'Your PHP code changes were rolled back due to an error on line %1$s of file %2$s. Please fix and try saving again.' ),
__( 'Your PHP code changes were not applied due to an error on line %1$s of file %2$s. Please fix and try saving again.' ),
'{{ data.line }}',
'{{ data.file }}'
);
@@ -571,8 +574,10 @@ function wp_edit_theme_plugin_file( $args ) {
}
if ( function_exists( 'session_status' ) && PHP_SESSION_ACTIVE === session_status() ) {
// Close any active session to prevent HTTP requests from timing out
// when attempting to connect back to the site.
/*
* Close any active session to prevent HTTP requests from timing out
* when attempting to connect back to the site.
*/
session_write_close();
}
@@ -684,7 +689,25 @@ function wp_tempnam( $filename = '', $dir = '' ) {
// Suffix some random data to avoid filename conflicts.
$temp_filename .= '-' . wp_generate_password( 6, false );
$temp_filename .= '.tmp';
$temp_filename = $dir . wp_unique_filename( $dir, $temp_filename );
$temp_filename = wp_unique_filename( $dir, $temp_filename );
/*
* Filesystems typically have a limit of 255 characters for a filename.
*
* If the generated unique filename exceeds this, truncate the initial
* filename and try again.
*
* As it's possible that the truncated filename may exist, producing a
* suffix of "-1" or "-10" which could exceed the limit again, truncate
* it to 252 instead.
*/
$characters_over_limit = strlen( $temp_filename ) - 252;
if ( $characters_over_limit > 0 ) {
$filename = substr( $filename, 0, -$characters_over_limit );
return wp_tempnam( $filename, $dir );
}
$temp_filename = $dir . $temp_filename;
$fp = @fopen( $temp_filename, 'x' );
@@ -890,7 +913,7 @@ function _wp_handle_upload( &$file, $overrides, $time, $action ) {
// If you override this, you must provide $ext and $type!!
$test_type = isset( $overrides['test_type'] ) ? $overrides['test_type'] : true;
$mimes = isset( $overrides['mimes'] ) ? $overrides['mimes'] : false;
$mimes = isset( $overrides['mimes'] ) ? $overrides['mimes'] : null;
// A correct form post will pass this test.
if ( $test_form && ( ! isset( $_POST['action'] ) || $_POST['action'] !== $action ) ) {
@@ -997,7 +1020,7 @@ function _wp_handle_upload( &$file, $overrides, $time, $action ) {
}
if ( false === $move_new_file ) {
if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
if ( str_starts_with( $uploads['basedir'], ABSPATH ) ) {
$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
} else {
$error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
@@ -1196,7 +1219,7 @@ function download_url( $url, $timeout = 300, $signature_verification = false ) {
if ( $content_disposition ) {
$content_disposition = strtolower( $content_disposition );
if ( 0 === strpos( $content_disposition, 'attachment; filename=' ) ) {
if ( str_starts_with( $content_disposition, 'attachment; filename=' ) ) {
$tmpfname_disposition = sanitize_file_name( substr( $content_disposition, 21 ) );
} else {
$tmpfname_disposition = '';
@@ -1248,12 +1271,14 @@ function download_url( $url, $timeout = 300, $signature_verification = false ) {
$signature = wp_remote_retrieve_header( $response, 'X-Content-Signature' );
if ( ! $signature ) {
// Retrieve signatures from a file if the header wasn't included.
// WordPress.org stores signatures at $package_url.sig.
/*
* Retrieve signatures from a file if the header wasn't included.
* WordPress.org stores signatures at $package_url.sig.
*/
$signature_url = false;
if ( is_string( $url_path ) && ( '.zip' === substr( $url_path, -4 ) || '.tar.gz' === substr( $url_path, -7 ) ) ) {
if ( is_string( $url_path ) && ( str_ends_with( $url_path, '.zip' ) || str_ends_with( $url_path, '.tar.gz' ) ) ) {
$signature_url = str_replace( $url_path, $url_path . '.sig', $url );
}
@@ -1377,14 +1402,16 @@ function verify_file_signature( $filename, $signatures, $filename_for_errors = f
);
}
// Check for a edge-case affecting PHP Maths abilities.
// Check for an edge-case affecting PHP Maths abilities.
if (
! extension_loaded( 'sodium' ) &&
in_array( PHP_VERSION_ID, array( 70200, 70201, 70202 ), true ) &&
extension_loaded( 'opcache' )
) {
// Sodium_Compat isn't compatible with PHP 7.2.0~7.2.2 due to a bug in the PHP Opcache extension, bail early as it'll fail.
// https://bugs.php.net/bug.php?id=75938
/*
* Sodium_Compat isn't compatible with PHP 7.2.0~7.2.2 due to a bug in the PHP Opcache extension, bail early as it'll fail.
* https://bugs.php.net/bug.php?id=75938
*/
return new WP_Error(
'signature_verification_unsupported',
sprintf(
@@ -1417,8 +1444,10 @@ function verify_file_signature( $filename, $signatures, $filename_for_errors = f
// phpcs:enable
}
// This cannot be performed in a reasonable amount of time.
// https://github.com/paragonie/sodium_compat#help-sodium_compat-is-slow-how-can-i-make-it-fast
/*
* This cannot be performed in a reasonable amount of time.
* https://github.com/paragonie/sodium_compat#help-sodium_compat-is-slow-how-can-i-make-it-fast
*/
if ( ! $sodium_compat_is_fast ) {
return new WP_Error(
'signature_verification_unsupported',
@@ -1646,7 +1675,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
}
if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
continue;
}
@@ -1659,7 +1688,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
$dirname = dirname( $info['name'] );
if ( '/' === substr( $info['name'], -1 ) ) {
if ( str_ends_with( $info['name'], '/' ) ) {
// Directory.
$needed_dirs[] = $to . untrailingslashit( $info['name'] );
} elseif ( '.' !== $dirname ) {
@@ -1693,7 +1722,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
continue;
}
if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
if ( ! str_contains( $dir, $to ) ) { // If the directory is not within the working directory, skip it.
continue;
}
@@ -1726,11 +1755,11 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
}
if ( '/' === substr( $info['name'], -1 ) ) { // Directory.
if ( str_ends_with( $info['name'], '/' ) ) { // Directory.
continue;
}
if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
continue;
}
@@ -1800,7 +1829,7 @@ function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
// Determine any children directories needed (From within the archive).
foreach ( $archive_files as $file ) {
if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory.
continue;
}
@@ -1834,7 +1863,7 @@ function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
continue;
}
if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
if ( ! str_contains( $dir, $to ) ) { // If the directory is not within the working directory, skip it.
continue;
}
@@ -1866,7 +1895,7 @@ function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
continue;
}
if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
if ( str_starts_with( $file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files.
continue;
}
@@ -1904,12 +1933,20 @@ function copy_dir( $from, $to, $skip_list = array() ) {
$dirlist = $wp_filesystem->dirlist( $from );
if ( false === $dirlist ) {
return new WP_Error( 'dirlist_failed_copy_dir', __( 'Directory listing failed.' ), basename( $to ) );
return new WP_Error( 'dirlist_failed_copy_dir', __( 'Directory listing failed.' ), basename( $from ) );
}
$from = trailingslashit( $from );
$to = trailingslashit( $to );
if ( ! $wp_filesystem->exists( $to ) && ! $wp_filesystem->mkdir( $to ) ) {
return new WP_Error(
'mkdir_destination_failed_copy_dir',
__( 'Could not create the destination directory.' ),
basename( $to )
);
}
foreach ( (array) $dirlist as $filename => $fileinfo ) {
if ( in_array( $filename, $skip_list, true ) ) {
continue;
@@ -1937,7 +1974,7 @@ function copy_dir( $from, $to, $skip_list = array() ) {
$sub_skip_list = array();
foreach ( $skip_list as $skip_item ) {
if ( 0 === strpos( $skip_item, $filename . '/' ) ) {
if ( str_starts_with( $skip_item, $filename . '/' ) ) {
$sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
}
}
@@ -2316,8 +2353,10 @@ function request_filesystem_credentials( $form_post, $type = '', $error = false,
'private_key' => 'FTP_PRIKEY',
);
// If defined, set it to that. Else, if POST'd, set it to that. If not, set it to an empty string.
// Otherwise, keep it as it previously was (saved details in option).
/*
* If defined, set it to that. Else, if POST'd, set it to that. If not, set it to an empty string.
* Otherwise, keep it as it previously was (saved details in option).
*/
foreach ( $ftp_constants as $key => $constant ) {
if ( defined( $constant ) ) {
$credentials[ $key ] = constant( $constant );
@@ -2520,8 +2559,10 @@ function request_filesystem_credentials( $form_post, $type = '', $error = false,
}
}
// Make sure the `submit_button()` function is available during the REST API call
// from WP_Site_Health_Auto_Updates::test_check_wp_filesystem_method().
/*
* Make sure the `submit_button()` function is available during the REST API call
* from WP_Site_Health_Auto_Updates::test_check_wp_filesystem_method().
*/
if ( ! function_exists( 'submit_button' ) ) {
require_once ABSPATH . 'wp-admin/includes/template.php';
}
@@ -2685,7 +2726,7 @@ function wp_opcache_invalidate_directory( $dir ) {
* with sub-directories represented as nested arrays.
* @param string $path Absolute path to the directory.
*/
$invalidate_directory = function( $dirlist, $path ) use ( &$invalidate_directory ) {
$invalidate_directory = static function( $dirlist, $path ) use ( &$invalidate_directory ) {
$path = trailingslashit( $path );
foreach ( $dirlist as $name => $details ) {