Edit File by line
/home/zeestwma/redstone.../wp-admin/includes
File: file.php
* Assumes that WP_Filesystem() has already been called and setup.
[2000] Fix | Delete
*
[2001] Fix | Delete
* @since 2.5.0
[2002] Fix | Delete
*
[2003] Fix | Delete
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
[2004] Fix | Delete
*
[2005] Fix | Delete
* @param string $from Source directory.
[2006] Fix | Delete
* @param string $to Destination directory.
[2007] Fix | Delete
* @param string[] $skip_list An array of files/folders to skip copying.
[2008] Fix | Delete
* @return true|WP_Error True on success, WP_Error on failure.
[2009] Fix | Delete
*/
[2010] Fix | Delete
function copy_dir( $from, $to, $skip_list = array() ) {
[2011] Fix | Delete
global $wp_filesystem;
[2012] Fix | Delete
[2013] Fix | Delete
$dirlist = $wp_filesystem->dirlist( $from );
[2014] Fix | Delete
[2015] Fix | Delete
if ( false === $dirlist ) {
[2016] Fix | Delete
return new WP_Error( 'dirlist_failed_copy_dir', __( 'Directory listing failed.' ), basename( $from ) );
[2017] Fix | Delete
}
[2018] Fix | Delete
[2019] Fix | Delete
$from = trailingslashit( $from );
[2020] Fix | Delete
$to = trailingslashit( $to );
[2021] Fix | Delete
[2022] Fix | Delete
if ( ! $wp_filesystem->exists( $to ) && ! $wp_filesystem->mkdir( $to ) ) {
[2023] Fix | Delete
return new WP_Error(
[2024] Fix | Delete
'mkdir_destination_failed_copy_dir',
[2025] Fix | Delete
__( 'Could not create the destination directory.' ),
[2026] Fix | Delete
basename( $to )
[2027] Fix | Delete
);
[2028] Fix | Delete
}
[2029] Fix | Delete
[2030] Fix | Delete
foreach ( (array) $dirlist as $filename => $fileinfo ) {
[2031] Fix | Delete
if ( in_array( $filename, $skip_list, true ) ) {
[2032] Fix | Delete
continue;
[2033] Fix | Delete
}
[2034] Fix | Delete
[2035] Fix | Delete
if ( 'f' === $fileinfo['type'] ) {
[2036] Fix | Delete
if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
[2037] Fix | Delete
// If copy failed, chmod file to 0644 and try again.
[2038] Fix | Delete
$wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );
[2039] Fix | Delete
[2040] Fix | Delete
if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
[2041] Fix | Delete
return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename );
[2042] Fix | Delete
}
[2043] Fix | Delete
}
[2044] Fix | Delete
[2045] Fix | Delete
wp_opcache_invalidate( $to . $filename );
[2046] Fix | Delete
} elseif ( 'd' === $fileinfo['type'] ) {
[2047] Fix | Delete
if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
[2048] Fix | Delete
if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {
[2049] Fix | Delete
return new WP_Error( 'mkdir_failed_copy_dir', __( 'Could not create directory.' ), $to . $filename );
[2050] Fix | Delete
}
[2051] Fix | Delete
}
[2052] Fix | Delete
[2053] Fix | Delete
// Generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list.
[2054] Fix | Delete
$sub_skip_list = array();
[2055] Fix | Delete
[2056] Fix | Delete
foreach ( $skip_list as $skip_item ) {
[2057] Fix | Delete
if ( str_starts_with( $skip_item, $filename . '/' ) ) {
[2058] Fix | Delete
$sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
[2059] Fix | Delete
}
[2060] Fix | Delete
}
[2061] Fix | Delete
[2062] Fix | Delete
$result = copy_dir( $from . $filename, $to . $filename, $sub_skip_list );
[2063] Fix | Delete
[2064] Fix | Delete
if ( is_wp_error( $result ) ) {
[2065] Fix | Delete
return $result;
[2066] Fix | Delete
}
[2067] Fix | Delete
}
[2068] Fix | Delete
}
[2069] Fix | Delete
[2070] Fix | Delete
return true;
[2071] Fix | Delete
}
[2072] Fix | Delete
[2073] Fix | Delete
/**
[2074] Fix | Delete
* Moves a directory from one location to another.
[2075] Fix | Delete
*
[2076] Fix | Delete
* Recursively invalidates OPcache on success.
[2077] Fix | Delete
*
[2078] Fix | Delete
* If the renaming failed, falls back to copy_dir().
[2079] Fix | Delete
*
[2080] Fix | Delete
* Assumes that WP_Filesystem() has already been called and setup.
[2081] Fix | Delete
*
[2082] Fix | Delete
* This function is not designed to merge directories, copy_dir() should be used instead.
[2083] Fix | Delete
*
[2084] Fix | Delete
* @since 6.2.0
[2085] Fix | Delete
*
[2086] Fix | Delete
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
[2087] Fix | Delete
*
[2088] Fix | Delete
* @param string $from Source directory.
[2089] Fix | Delete
* @param string $to Destination directory.
[2090] Fix | Delete
* @param bool $overwrite Optional. Whether to overwrite the destination directory if it exists.
[2091] Fix | Delete
* Default false.
[2092] Fix | Delete
* @return true|WP_Error True on success, WP_Error on failure.
[2093] Fix | Delete
*/
[2094] Fix | Delete
function move_dir( $from, $to, $overwrite = false ) {
[2095] Fix | Delete
global $wp_filesystem;
[2096] Fix | Delete
[2097] Fix | Delete
if ( trailingslashit( strtolower( $from ) ) === trailingslashit( strtolower( $to ) ) ) {
[2098] Fix | Delete
return new WP_Error( 'source_destination_same_move_dir', __( 'The source and destination are the same.' ) );
[2099] Fix | Delete
}
[2100] Fix | Delete
[2101] Fix | Delete
if ( $wp_filesystem->exists( $to ) ) {
[2102] Fix | Delete
if ( ! $overwrite ) {
[2103] Fix | Delete
return new WP_Error( 'destination_already_exists_move_dir', __( 'The destination folder already exists.' ), $to );
[2104] Fix | Delete
} elseif ( ! $wp_filesystem->delete( $to, true ) ) {
[2105] Fix | Delete
// Can't overwrite if the destination couldn't be deleted.
[2106] Fix | Delete
return new WP_Error( 'destination_not_deleted_move_dir', __( 'The destination directory already exists and could not be removed.' ) );
[2107] Fix | Delete
}
[2108] Fix | Delete
}
[2109] Fix | Delete
[2110] Fix | Delete
if ( $wp_filesystem->move( $from, $to ) ) {
[2111] Fix | Delete
/*
[2112] Fix | Delete
* When using an environment with shared folders,
[2113] Fix | Delete
* there is a delay in updating the filesystem's cache.
[2114] Fix | Delete
*
[2115] Fix | Delete
* This is a known issue in environments with a VirtualBox provider.
[2116] Fix | Delete
*
[2117] Fix | Delete
* A 200ms delay gives time for the filesystem to update its cache,
[2118] Fix | Delete
* prevents "Operation not permitted", and "No such file or directory" warnings.
[2119] Fix | Delete
*
[2120] Fix | Delete
* This delay is used in other projects, including Composer.
[2121] Fix | Delete
* @link https://github.com/composer/composer/blob/2.5.1/src/Composer/Util/Platform.php#L228-L233
[2122] Fix | Delete
*/
[2123] Fix | Delete
usleep( 200000 );
[2124] Fix | Delete
wp_opcache_invalidate_directory( $to );
[2125] Fix | Delete
[2126] Fix | Delete
return true;
[2127] Fix | Delete
}
[2128] Fix | Delete
[2129] Fix | Delete
// Fall back to a recursive copy.
[2130] Fix | Delete
if ( ! $wp_filesystem->is_dir( $to ) ) {
[2131] Fix | Delete
if ( ! $wp_filesystem->mkdir( $to, FS_CHMOD_DIR ) ) {
[2132] Fix | Delete
return new WP_Error( 'mkdir_failed_move_dir', __( 'Could not create directory.' ), $to );
[2133] Fix | Delete
}
[2134] Fix | Delete
}
[2135] Fix | Delete
[2136] Fix | Delete
$result = copy_dir( $from, $to, array( basename( $to ) ) );
[2137] Fix | Delete
[2138] Fix | Delete
// Clear the source directory.
[2139] Fix | Delete
if ( true === $result ) {
[2140] Fix | Delete
$wp_filesystem->delete( $from, true );
[2141] Fix | Delete
}
[2142] Fix | Delete
[2143] Fix | Delete
return $result;
[2144] Fix | Delete
}
[2145] Fix | Delete
[2146] Fix | Delete
/**
[2147] Fix | Delete
* Initializes and connects the WordPress Filesystem Abstraction classes.
[2148] Fix | Delete
*
[2149] Fix | Delete
* This function will include the chosen transport and attempt connecting.
[2150] Fix | Delete
*
[2151] Fix | Delete
* Plugins may add extra transports, And force WordPress to use them by returning
[2152] Fix | Delete
* the filename via the {@see 'filesystem_method_file'} filter.
[2153] Fix | Delete
*
[2154] Fix | Delete
* @since 2.5.0
[2155] Fix | Delete
*
[2156] Fix | Delete
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
[2157] Fix | Delete
*
[2158] Fix | Delete
* @param array|false $args Optional. Connection args, These are passed
[2159] Fix | Delete
* directly to the `WP_Filesystem_*()` classes.
[2160] Fix | Delete
* Default false.
[2161] Fix | Delete
* @param string|false $context Optional. Context for get_filesystem_method().
[2162] Fix | Delete
* Default false.
[2163] Fix | Delete
* @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
[2164] Fix | Delete
* Default false.
[2165] Fix | Delete
* @return bool|null True on success, false on failure,
[2166] Fix | Delete
* null if the filesystem method class file does not exist.
[2167] Fix | Delete
*/
[2168] Fix | Delete
function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
[2169] Fix | Delete
global $wp_filesystem;
[2170] Fix | Delete
[2171] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
[2172] Fix | Delete
[2173] Fix | Delete
$method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership );
[2174] Fix | Delete
[2175] Fix | Delete
if ( ! $method ) {
[2176] Fix | Delete
return false;
[2177] Fix | Delete
}
[2178] Fix | Delete
[2179] Fix | Delete
if ( ! class_exists( "WP_Filesystem_$method" ) ) {
[2180] Fix | Delete
[2181] Fix | Delete
/**
[2182] Fix | Delete
* Filters the path for a specific filesystem method class file.
[2183] Fix | Delete
*
[2184] Fix | Delete
* @since 2.6.0
[2185] Fix | Delete
*
[2186] Fix | Delete
* @see get_filesystem_method()
[2187] Fix | Delete
*
[2188] Fix | Delete
* @param string $path Path to the specific filesystem method class file.
[2189] Fix | Delete
* @param string $method The filesystem method to use.
[2190] Fix | Delete
*/
[2191] Fix | Delete
$abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );
[2192] Fix | Delete
[2193] Fix | Delete
if ( ! file_exists( $abstraction_file ) ) {
[2194] Fix | Delete
return;
[2195] Fix | Delete
}
[2196] Fix | Delete
[2197] Fix | Delete
require_once $abstraction_file;
[2198] Fix | Delete
}
[2199] Fix | Delete
$method = "WP_Filesystem_$method";
[2200] Fix | Delete
[2201] Fix | Delete
$wp_filesystem = new $method( $args );
[2202] Fix | Delete
[2203] Fix | Delete
/*
[2204] Fix | Delete
* Define the timeouts for the connections. Only available after the constructor is called
[2205] Fix | Delete
* to allow for per-transport overriding of the default.
[2206] Fix | Delete
*/
[2207] Fix | Delete
if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) {
[2208] Fix | Delete
define( 'FS_CONNECT_TIMEOUT', 30 ); // 30 seconds.
[2209] Fix | Delete
}
[2210] Fix | Delete
if ( ! defined( 'FS_TIMEOUT' ) ) {
[2211] Fix | Delete
define( 'FS_TIMEOUT', 30 ); // 30 seconds.
[2212] Fix | Delete
}
[2213] Fix | Delete
[2214] Fix | Delete
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
[2215] Fix | Delete
return false;
[2216] Fix | Delete
}
[2217] Fix | Delete
[2218] Fix | Delete
if ( ! $wp_filesystem->connect() ) {
[2219] Fix | Delete
return false; // There was an error connecting to the server.
[2220] Fix | Delete
}
[2221] Fix | Delete
[2222] Fix | Delete
// Set the permission constants if not already set.
[2223] Fix | Delete
if ( ! defined( 'FS_CHMOD_DIR' ) ) {
[2224] Fix | Delete
define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
[2225] Fix | Delete
}
[2226] Fix | Delete
if ( ! defined( 'FS_CHMOD_FILE' ) ) {
[2227] Fix | Delete
define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
[2228] Fix | Delete
}
[2229] Fix | Delete
[2230] Fix | Delete
return true;
[2231] Fix | Delete
}
[2232] Fix | Delete
[2233] Fix | Delete
/**
[2234] Fix | Delete
* Determines which method to use for reading, writing, modifying, or deleting
[2235] Fix | Delete
* files on the filesystem.
[2236] Fix | Delete
*
[2237] Fix | Delete
* The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets
[2238] Fix | Delete
* (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2',
[2239] Fix | Delete
* 'ftpext' or 'ftpsockets'.
[2240] Fix | Delete
*
[2241] Fix | Delete
* The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`,
[2242] Fix | Delete
* or filtering via {@see 'filesystem_method'}.
[2243] Fix | Delete
*
[2244] Fix | Delete
* @link https://developer.wordpress.org/advanced-administration/wordpress/wp-config/#wordpress-upgrade-constants
[2245] Fix | Delete
*
[2246] Fix | Delete
* Plugins may define a custom transport handler, See WP_Filesystem().
[2247] Fix | Delete
*
[2248] Fix | Delete
* @since 2.5.0
[2249] Fix | Delete
*
[2250] Fix | Delete
* @global callable $_wp_filesystem_direct_method
[2251] Fix | Delete
*
[2252] Fix | Delete
* @param array $args Optional. Connection details. Default empty array.
[2253] Fix | Delete
* @param string $context Optional. Full path to the directory that is tested
[2254] Fix | Delete
* for being writable. Default empty.
[2255] Fix | Delete
* @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
[2256] Fix | Delete
* Default false.
[2257] Fix | Delete
* @return string The transport to use, see description for valid return values.
[2258] Fix | Delete
*/
[2259] Fix | Delete
function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) {
[2260] Fix | Delete
// Please ensure that this is either 'direct', 'ssh2', 'ftpext', or 'ftpsockets'.
[2261] Fix | Delete
$method = defined( 'FS_METHOD' ) ? FS_METHOD : false;
[2262] Fix | Delete
[2263] Fix | Delete
if ( ! $context ) {
[2264] Fix | Delete
$context = WP_CONTENT_DIR;
[2265] Fix | Delete
}
[2266] Fix | Delete
[2267] Fix | Delete
// If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it.
[2268] Fix | Delete
if ( WP_LANG_DIR === $context && ! is_dir( $context ) ) {
[2269] Fix | Delete
$context = dirname( $context );
[2270] Fix | Delete
}
[2271] Fix | Delete
[2272] Fix | Delete
$context = trailingslashit( $context );
[2273] Fix | Delete
[2274] Fix | Delete
if ( ! $method ) {
[2275] Fix | Delete
[2276] Fix | Delete
$temp_file_name = $context . 'temp-write-test-' . str_replace( '.', '-', uniqid( '', true ) );
[2277] Fix | Delete
$temp_handle = @fopen( $temp_file_name, 'w' );
[2278] Fix | Delete
if ( $temp_handle ) {
[2279] Fix | Delete
[2280] Fix | Delete
// Attempt to determine the file owner of the WordPress files, and that of newly created files.
[2281] Fix | Delete
$wp_file_owner = false;
[2282] Fix | Delete
$temp_file_owner = false;
[2283] Fix | Delete
if ( function_exists( 'fileowner' ) ) {
[2284] Fix | Delete
$wp_file_owner = @fileowner( __FILE__ );
[2285] Fix | Delete
$temp_file_owner = @fileowner( $temp_file_name );
[2286] Fix | Delete
}
[2287] Fix | Delete
[2288] Fix | Delete
if ( false !== $wp_file_owner && $wp_file_owner === $temp_file_owner ) {
[2289] Fix | Delete
/*
[2290] Fix | Delete
* WordPress is creating files as the same owner as the WordPress files,
[2291] Fix | Delete
* this means it's safe to modify & create new files via PHP.
[2292] Fix | Delete
*/
[2293] Fix | Delete
$method = 'direct';
[2294] Fix | Delete
$GLOBALS['_wp_filesystem_direct_method'] = 'file_owner';
[2295] Fix | Delete
} elseif ( $allow_relaxed_file_ownership ) {
[2296] Fix | Delete
/*
[2297] Fix | Delete
* The $context directory is writable, and $allow_relaxed_file_ownership is set,
[2298] Fix | Delete
* this means we can modify files safely in this directory.
[2299] Fix | Delete
* This mode doesn't create new files, only alter existing ones.
[2300] Fix | Delete
*/
[2301] Fix | Delete
$method = 'direct';
[2302] Fix | Delete
$GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership';
[2303] Fix | Delete
}
[2304] Fix | Delete
[2305] Fix | Delete
fclose( $temp_handle );
[2306] Fix | Delete
@unlink( $temp_file_name );
[2307] Fix | Delete
}
[2308] Fix | Delete
}
[2309] Fix | Delete
[2310] Fix | Delete
if ( ! $method && isset( $args['connection_type'] ) && 'ssh' === $args['connection_type'] && extension_loaded( 'ssh2' ) ) {
[2311] Fix | Delete
$method = 'ssh2';
[2312] Fix | Delete
}
[2313] Fix | Delete
if ( ! $method && extension_loaded( 'ftp' ) ) {
[2314] Fix | Delete
$method = 'ftpext';
[2315] Fix | Delete
}
[2316] Fix | Delete
if ( ! $method && ( extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) ) {
[2317] Fix | Delete
$method = 'ftpsockets'; // Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread.
[2318] Fix | Delete
}
[2319] Fix | Delete
[2320] Fix | Delete
/**
[2321] Fix | Delete
* Filters the filesystem method to use.
[2322] Fix | Delete
*
[2323] Fix | Delete
* @since 2.6.0
[2324] Fix | Delete
*
[2325] Fix | Delete
* @param string $method Filesystem method to return.
[2326] Fix | Delete
* @param array $args An array of connection details for the method.
[2327] Fix | Delete
* @param string $context Full path to the directory that is tested for being writable.
[2328] Fix | Delete
* @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable.
[2329] Fix | Delete
*/
[2330] Fix | Delete
return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership );
[2331] Fix | Delete
}
[2332] Fix | Delete
[2333] Fix | Delete
/**
[2334] Fix | Delete
* Displays a form to the user to request for their FTP/SSH details in order
[2335] Fix | Delete
* to connect to the filesystem.
[2336] Fix | Delete
*
[2337] Fix | Delete
* All chosen/entered details are saved, excluding the password.
[2338] Fix | Delete
*
[2339] Fix | Delete
* Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467)
[2340] Fix | Delete
* to specify an alternate FTP/SSH port.
[2341] Fix | Delete
*
[2342] Fix | Delete
* Plugins may override this form by returning true|false via the {@see 'request_filesystem_credentials'} filter.
[2343] Fix | Delete
*
[2344] Fix | Delete
* @since 2.5.0
[2345] Fix | Delete
* @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
[2346] Fix | Delete
*
[2347] Fix | Delete
* @global string $pagenow The filename of the current screen.
[2348] Fix | Delete
*
[2349] Fix | Delete
* @param string $form_post The URL to post the form to.
[2350] Fix | Delete
* @param string $type Optional. Chosen type of filesystem. Default empty.
[2351] Fix | Delete
* @param bool|WP_Error $error Optional. Whether the current request has failed
[2352] Fix | Delete
* to connect, or an error object. Default false.
[2353] Fix | Delete
* @param string $context Optional. Full path to the directory that is tested
[2354] Fix | Delete
* for being writable. Default empty.
[2355] Fix | Delete
* @param array $extra_fields Optional. Extra `POST` fields to be checked
[2356] Fix | Delete
* for inclusion in the post. Default null.
[2357] Fix | Delete
* @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
[2358] Fix | Delete
* Default false.
[2359] Fix | Delete
* @return bool|array True if no filesystem credentials are required,
[2360] Fix | Delete
* false if they are required but have not been provided,
[2361] Fix | Delete
* array of credentials if they are required and have been provided.
[2362] Fix | Delete
*/
[2363] Fix | Delete
function request_filesystem_credentials( $form_post, $type = '', $error = false, $context = '', $extra_fields = null, $allow_relaxed_file_ownership = false ) {
[2364] Fix | Delete
global $pagenow;
[2365] Fix | Delete
[2366] Fix | Delete
/**
[2367] Fix | Delete
* Filters the filesystem credentials.
[2368] Fix | Delete
*
[2369] Fix | Delete
* Returning anything other than an empty string will effectively short-circuit
[2370] Fix | Delete
* output of the filesystem credentials form, returning that value instead.
[2371] Fix | Delete
*
[2372] Fix | Delete
* A filter should return true if no filesystem credentials are required, false if they are required but have not been
[2373] Fix | Delete
* provided, or an array of credentials if they are required and have been provided.
[2374] Fix | Delete
*
[2375] Fix | Delete
* @since 2.5.0
[2376] Fix | Delete
* @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
[2377] Fix | Delete
*
[2378] Fix | Delete
* @param mixed $credentials Credentials to return instead. Default empty string.
[2379] Fix | Delete
* @param string $form_post The URL to post the form to.
[2380] Fix | Delete
* @param string $type Chosen type of filesystem.
[2381] Fix | Delete
* @param bool|WP_Error $error Whether the current request has failed to connect,
[2382] Fix | Delete
* or an error object.
[2383] Fix | Delete
* @param string $context Full path to the directory that is tested for
[2384] Fix | Delete
* being writable.
[2385] Fix | Delete
* @param array $extra_fields Extra POST fields.
[2386] Fix | Delete
* @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable.
[2387] Fix | Delete
*/
[2388] Fix | Delete
$req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership );
[2389] Fix | Delete
[2390] Fix | Delete
if ( '' !== $req_cred ) {
[2391] Fix | Delete
return $req_cred;
[2392] Fix | Delete
}
[2393] Fix | Delete
[2394] Fix | Delete
if ( empty( $type ) ) {
[2395] Fix | Delete
$type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership );
[2396] Fix | Delete
}
[2397] Fix | Delete
[2398] Fix | Delete
if ( 'direct' === $type ) {
[2399] Fix | Delete
return true;
[2400] Fix | Delete
}
[2401] Fix | Delete
[2402] Fix | Delete
if ( is_null( $extra_fields ) ) {
[2403] Fix | Delete
$extra_fields = array( 'version', 'locale' );
[2404] Fix | Delete
}
[2405] Fix | Delete
[2406] Fix | Delete
$credentials = get_option(
[2407] Fix | Delete
'ftp_credentials',
[2408] Fix | Delete
array(
[2409] Fix | Delete
'hostname' => '',
[2410] Fix | Delete
'username' => '',
[2411] Fix | Delete
)
[2412] Fix | Delete
);
[2413] Fix | Delete
[2414] Fix | Delete
$submitted_form = wp_unslash( $_POST );
[2415] Fix | Delete
[2416] Fix | Delete
// Verify nonce, or unset submitted form field values on failure.
[2417] Fix | Delete
if ( ! isset( $_POST['_fs_nonce'] ) || ! wp_verify_nonce( $_POST['_fs_nonce'], 'filesystem-credentials' ) ) {
[2418] Fix | Delete
unset(
[2419] Fix | Delete
$submitted_form['hostname'],
[2420] Fix | Delete
$submitted_form['username'],
[2421] Fix | Delete
$submitted_form['password'],
[2422] Fix | Delete
$submitted_form['public_key'],
[2423] Fix | Delete
$submitted_form['private_key'],
[2424] Fix | Delete
$submitted_form['connection_type']
[2425] Fix | Delete
);
[2426] Fix | Delete
}
[2427] Fix | Delete
[2428] Fix | Delete
$ftp_constants = array(
[2429] Fix | Delete
'hostname' => 'FTP_HOST',
[2430] Fix | Delete
'username' => 'FTP_USER',
[2431] Fix | Delete
'password' => 'FTP_PASS',
[2432] Fix | Delete
'public_key' => 'FTP_PUBKEY',
[2433] Fix | Delete
'private_key' => 'FTP_PRIKEY',
[2434] Fix | Delete
);
[2435] Fix | Delete
[2436] Fix | Delete
/*
[2437] Fix | Delete
* If defined, set it to that. Else, if POST'd, set it to that. If not, set it to an empty string.
[2438] Fix | Delete
* Otherwise, keep it as it previously was (saved details in option).
[2439] Fix | Delete
*/
[2440] Fix | Delete
foreach ( $ftp_constants as $key => $constant ) {
[2441] Fix | Delete
if ( defined( $constant ) ) {
[2442] Fix | Delete
$credentials[ $key ] = constant( $constant );
[2443] Fix | Delete
} elseif ( ! empty( $submitted_form[ $key ] ) ) {
[2444] Fix | Delete
$credentials[ $key ] = $submitted_form[ $key ];
[2445] Fix | Delete
} elseif ( ! isset( $credentials[ $key ] ) ) {
[2446] Fix | Delete
$credentials[ $key ] = '';
[2447] Fix | Delete
}
[2448] Fix | Delete
}
[2449] Fix | Delete
[2450] Fix | Delete
// Sanitize the hostname, some people might pass in odd data.
[2451] Fix | Delete
$credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); // Strip any schemes off.
[2452] Fix | Delete
[2453] Fix | Delete
if ( strpos( $credentials['hostname'], ':' ) ) {
[2454] Fix | Delete
list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 );
[2455] Fix | Delete
if ( ! is_numeric( $credentials['port'] ) ) {
[2456] Fix | Delete
unset( $credentials['port'] );
[2457] Fix | Delete
}
[2458] Fix | Delete
} else {
[2459] Fix | Delete
unset( $credentials['port'] );
[2460] Fix | Delete
}
[2461] Fix | Delete
[2462] Fix | Delete
if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) {
[2463] Fix | Delete
$credentials['connection_type'] = 'ssh';
[2464] Fix | Delete
} elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { // Only the FTP Extension understands SSL.
[2465] Fix | Delete
$credentials['connection_type'] = 'ftps';
[2466] Fix | Delete
} elseif ( ! empty( $submitted_form['connection_type'] ) ) {
[2467] Fix | Delete
$credentials['connection_type'] = $submitted_form['connection_type'];
[2468] Fix | Delete
} elseif ( ! isset( $credentials['connection_type'] ) ) { // All else fails (and it's not defaulted to something else saved), default to FTP.
[2469] Fix | Delete
$credentials['connection_type'] = 'ftp';
[2470] Fix | Delete
}
[2471] Fix | Delete
[2472] Fix | Delete
if ( ! $error
[2473] Fix | Delete
&& ( ! empty( $credentials['hostname'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['password'] )
[2474] Fix | Delete
|| 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] )
[2475] Fix | Delete
)
[2476] Fix | Delete
) {
[2477] Fix | Delete
$stored_credentials = $credentials;
[2478] Fix | Delete
[2479] Fix | Delete
if ( ! empty( $stored_credentials['port'] ) ) { // Save port as part of hostname to simplify above code.
[2480] Fix | Delete
$stored_credentials['hostname'] .= ':' . $stored_credentials['port'];
[2481] Fix | Delete
}
[2482] Fix | Delete
[2483] Fix | Delete
unset(
[2484] Fix | Delete
$stored_credentials['password'],
[2485] Fix | Delete
$stored_credentials['port'],
[2486] Fix | Delete
$stored_credentials['private_key'],
[2487] Fix | Delete
$stored_credentials['public_key']
[2488] Fix | Delete
);
[2489] Fix | Delete
[2490] Fix | Delete
if ( ! wp_installing() ) {
[2491] Fix | Delete
update_option( 'ftp_credentials', $stored_credentials, false );
[2492] Fix | Delete
}
[2493] Fix | Delete
[2494] Fix | Delete
return $credentials;
[2495] Fix | Delete
}
[2496] Fix | Delete
[2497] Fix | Delete
$hostname = isset( $credentials['hostname'] ) ? $credentials['hostname'] : '';
[2498] Fix | Delete
$username = isset( $credentials['username'] ) ? $credentials['username'] : '';
[2499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function