Edit File by line
/home/barbar84/www/wp-admin/includes
File: file.php
return $result;
[1500] Fix | Delete
} elseif ( is_wp_error( $result ) ) {
[1501] Fix | Delete
if ( 'incompatible_archive' !== $result->get_error_code() ) {
[1502] Fix | Delete
return $result;
[1503] Fix | Delete
}
[1504] Fix | Delete
}
[1505] Fix | Delete
}
[1506] Fix | Delete
// Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
[1507] Fix | Delete
return _unzip_file_pclzip( $file, $to, $needed_dirs );
[1508] Fix | Delete
}
[1509] Fix | Delete
[1510] Fix | Delete
/**
[1511] Fix | Delete
* Attempts to unzip an archive using the ZipArchive class.
[1512] Fix | Delete
*
[1513] Fix | Delete
* This function should not be called directly, use `unzip_file()` instead.
[1514] Fix | Delete
*
[1515] Fix | Delete
* Assumes that WP_Filesystem() has already been called and set up.
[1516] Fix | Delete
*
[1517] Fix | Delete
* @since 3.0.0
[1518] Fix | Delete
* @access private
[1519] Fix | Delete
*
[1520] Fix | Delete
* @see unzip_file()
[1521] Fix | Delete
*
[1522] Fix | Delete
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
[1523] Fix | Delete
*
[1524] Fix | Delete
* @param string $file Full path and filename of ZIP archive.
[1525] Fix | Delete
* @param string $to Full path on the filesystem to extract archive to.
[1526] Fix | Delete
* @param string[] $needed_dirs A partial list of required folders needed to be created.
[1527] Fix | Delete
* @return true|WP_Error True on success, WP_Error on failure.
[1528] Fix | Delete
*/
[1529] Fix | Delete
function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
[1530] Fix | Delete
global $wp_filesystem;
[1531] Fix | Delete
[1532] Fix | Delete
$z = new ZipArchive();
[1533] Fix | Delete
[1534] Fix | Delete
$zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS );
[1535] Fix | Delete
if ( true !== $zopen ) {
[1536] Fix | Delete
return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) );
[1537] Fix | Delete
}
[1538] Fix | Delete
[1539] Fix | Delete
$uncompressed_size = 0;
[1540] Fix | Delete
[1541] Fix | Delete
for ( $i = 0; $i < $z->numFiles; $i++ ) {
[1542] Fix | Delete
$info = $z->statIndex( $i );
[1543] Fix | Delete
if ( ! $info ) {
[1544] Fix | Delete
return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
[1545] Fix | Delete
}
[1546] Fix | Delete
[1547] Fix | Delete
if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
[1548] Fix | Delete
continue;
[1549] Fix | Delete
}
[1550] Fix | Delete
[1551] Fix | Delete
// Don't extract invalid files:
[1552] Fix | Delete
if ( 0 !== validate_file( $info['name'] ) ) {
[1553] Fix | Delete
continue;
[1554] Fix | Delete
}
[1555] Fix | Delete
[1556] Fix | Delete
$uncompressed_size += $info['size'];
[1557] Fix | Delete
[1558] Fix | Delete
$dirname = dirname( $info['name'] );
[1559] Fix | Delete
[1560] Fix | Delete
if ( '/' === substr( $info['name'], -1 ) ) {
[1561] Fix | Delete
// Directory.
[1562] Fix | Delete
$needed_dirs[] = $to . untrailingslashit( $info['name'] );
[1563] Fix | Delete
} elseif ( '.' !== $dirname ) {
[1564] Fix | Delete
// Path to a file.
[1565] Fix | Delete
$needed_dirs[] = $to . untrailingslashit( $dirname );
[1566] Fix | Delete
}
[1567] Fix | Delete
}
[1568] Fix | Delete
[1569] Fix | Delete
/*
[1570] Fix | Delete
* disk_free_space() could return false. Assume that any falsey value is an error.
[1571] Fix | Delete
* A disk that has zero free bytes has bigger problems.
[1572] Fix | Delete
* Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
[1573] Fix | Delete
*/
[1574] Fix | Delete
if ( wp_doing_cron() ) {
[1575] Fix | Delete
$available_space = @disk_free_space( WP_CONTENT_DIR );
[1576] Fix | Delete
if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
[1577] Fix | Delete
return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) );
[1578] Fix | Delete
}
[1579] Fix | Delete
}
[1580] Fix | Delete
[1581] Fix | Delete
$needed_dirs = array_unique( $needed_dirs );
[1582] Fix | Delete
foreach ( $needed_dirs as $dir ) {
[1583] Fix | Delete
// Check the parent folders of the folders all exist within the creation array.
[1584] Fix | Delete
if ( untrailingslashit( $to ) == $dir ) { // Skip over the working directory, we know this exists (or will exist).
[1585] Fix | Delete
continue;
[1586] Fix | Delete
}
[1587] Fix | Delete
if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
[1588] Fix | Delete
continue;
[1589] Fix | Delete
}
[1590] Fix | Delete
[1591] Fix | Delete
$parent_folder = dirname( $dir );
[1592] Fix | Delete
while ( ! empty( $parent_folder ) && untrailingslashit( $to ) != $parent_folder && ! in_array( $parent_folder, $needed_dirs, true ) ) {
[1593] Fix | Delete
$needed_dirs[] = $parent_folder;
[1594] Fix | Delete
$parent_folder = dirname( $parent_folder );
[1595] Fix | Delete
}
[1596] Fix | Delete
}
[1597] Fix | Delete
asort( $needed_dirs );
[1598] Fix | Delete
[1599] Fix | Delete
// Create those directories if need be:
[1600] Fix | Delete
foreach ( $needed_dirs as $_dir ) {
[1601] Fix | Delete
// Only check to see if the Dir exists upon creation failure. Less I/O this way.
[1602] Fix | Delete
if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) {
[1603] Fix | Delete
return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) );
[1604] Fix | Delete
}
[1605] Fix | Delete
}
[1606] Fix | Delete
unset( $needed_dirs );
[1607] Fix | Delete
[1608] Fix | Delete
for ( $i = 0; $i < $z->numFiles; $i++ ) {
[1609] Fix | Delete
$info = $z->statIndex( $i );
[1610] Fix | Delete
if ( ! $info ) {
[1611] Fix | Delete
return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
[1612] Fix | Delete
}
[1613] Fix | Delete
[1614] Fix | Delete
if ( '/' === substr( $info['name'], -1 ) ) { // Directory.
[1615] Fix | Delete
continue;
[1616] Fix | Delete
}
[1617] Fix | Delete
[1618] Fix | Delete
if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
[1619] Fix | Delete
continue;
[1620] Fix | Delete
}
[1621] Fix | Delete
[1622] Fix | Delete
// Don't extract invalid files:
[1623] Fix | Delete
if ( 0 !== validate_file( $info['name'] ) ) {
[1624] Fix | Delete
continue;
[1625] Fix | Delete
}
[1626] Fix | Delete
[1627] Fix | Delete
$contents = $z->getFromIndex( $i );
[1628] Fix | Delete
if ( false === $contents ) {
[1629] Fix | Delete
return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] );
[1630] Fix | Delete
}
[1631] Fix | Delete
[1632] Fix | Delete
if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE ) ) {
[1633] Fix | Delete
return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] );
[1634] Fix | Delete
}
[1635] Fix | Delete
}
[1636] Fix | Delete
[1637] Fix | Delete
$z->close();
[1638] Fix | Delete
[1639] Fix | Delete
return true;
[1640] Fix | Delete
}
[1641] Fix | Delete
[1642] Fix | Delete
/**
[1643] Fix | Delete
* Attempts to unzip an archive using the PclZip library.
[1644] Fix | Delete
*
[1645] Fix | Delete
* This function should not be called directly, use `unzip_file()` instead.
[1646] Fix | Delete
*
[1647] Fix | Delete
* Assumes that WP_Filesystem() has already been called and set up.
[1648] Fix | Delete
*
[1649] Fix | Delete
* @since 3.0.0
[1650] Fix | Delete
* @access private
[1651] Fix | Delete
*
[1652] Fix | Delete
* @see unzip_file()
[1653] Fix | Delete
*
[1654] Fix | Delete
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
[1655] Fix | Delete
*
[1656] Fix | Delete
* @param string $file Full path and filename of ZIP archive.
[1657] Fix | Delete
* @param string $to Full path on the filesystem to extract archive to.
[1658] Fix | Delete
* @param string[] $needed_dirs A partial list of required folders needed to be created.
[1659] Fix | Delete
* @return true|WP_Error True on success, WP_Error on failure.
[1660] Fix | Delete
*/
[1661] Fix | Delete
function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
[1662] Fix | Delete
global $wp_filesystem;
[1663] Fix | Delete
[1664] Fix | Delete
mbstring_binary_safe_encoding();
[1665] Fix | Delete
[1666] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
[1667] Fix | Delete
[1668] Fix | Delete
$archive = new PclZip( $file );
[1669] Fix | Delete
[1670] Fix | Delete
$archive_files = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING );
[1671] Fix | Delete
[1672] Fix | Delete
reset_mbstring_encoding();
[1673] Fix | Delete
[1674] Fix | Delete
// Is the archive valid?
[1675] Fix | Delete
if ( ! is_array( $archive_files ) ) {
[1676] Fix | Delete
return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), $archive->errorInfo( true ) );
[1677] Fix | Delete
}
[1678] Fix | Delete
[1679] Fix | Delete
if ( 0 === count( $archive_files ) ) {
[1680] Fix | Delete
return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) );
[1681] Fix | Delete
}
[1682] Fix | Delete
[1683] Fix | Delete
$uncompressed_size = 0;
[1684] Fix | Delete
[1685] Fix | Delete
// Determine any children directories needed (From within the archive).
[1686] Fix | Delete
foreach ( $archive_files as $file ) {
[1687] Fix | Delete
if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
[1688] Fix | Delete
continue;
[1689] Fix | Delete
}
[1690] Fix | Delete
[1691] Fix | Delete
$uncompressed_size += $file['size'];
[1692] Fix | Delete
[1693] Fix | Delete
$needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) );
[1694] Fix | Delete
}
[1695] Fix | Delete
[1696] Fix | Delete
/*
[1697] Fix | Delete
* disk_free_space() could return false. Assume that any falsey value is an error.
[1698] Fix | Delete
* A disk that has zero free bytes has bigger problems.
[1699] Fix | Delete
* Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
[1700] Fix | Delete
*/
[1701] Fix | Delete
if ( wp_doing_cron() ) {
[1702] Fix | Delete
$available_space = @disk_free_space( WP_CONTENT_DIR );
[1703] Fix | Delete
if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
[1704] Fix | Delete
return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) );
[1705] Fix | Delete
}
[1706] Fix | Delete
}
[1707] Fix | Delete
[1708] Fix | Delete
$needed_dirs = array_unique( $needed_dirs );
[1709] Fix | Delete
foreach ( $needed_dirs as $dir ) {
[1710] Fix | Delete
// Check the parent folders of the folders all exist within the creation array.
[1711] Fix | Delete
if ( untrailingslashit( $to ) == $dir ) { // Skip over the working directory, we know this exists (or will exist).
[1712] Fix | Delete
continue;
[1713] Fix | Delete
}
[1714] Fix | Delete
if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
[1715] Fix | Delete
continue;
[1716] Fix | Delete
}
[1717] Fix | Delete
[1718] Fix | Delete
$parent_folder = dirname( $dir );
[1719] Fix | Delete
while ( ! empty( $parent_folder ) && untrailingslashit( $to ) != $parent_folder && ! in_array( $parent_folder, $needed_dirs, true ) ) {
[1720] Fix | Delete
$needed_dirs[] = $parent_folder;
[1721] Fix | Delete
$parent_folder = dirname( $parent_folder );
[1722] Fix | Delete
}
[1723] Fix | Delete
}
[1724] Fix | Delete
asort( $needed_dirs );
[1725] Fix | Delete
[1726] Fix | Delete
// Create those directories if need be:
[1727] Fix | Delete
foreach ( $needed_dirs as $_dir ) {
[1728] Fix | Delete
// Only check to see if the dir exists upon creation failure. Less I/O this way.
[1729] Fix | Delete
if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) {
[1730] Fix | Delete
return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) );
[1731] Fix | Delete
}
[1732] Fix | Delete
}
[1733] Fix | Delete
unset( $needed_dirs );
[1734] Fix | Delete
[1735] Fix | Delete
// Extract the files from the zip.
[1736] Fix | Delete
foreach ( $archive_files as $file ) {
[1737] Fix | Delete
if ( $file['folder'] ) {
[1738] Fix | Delete
continue;
[1739] Fix | Delete
}
[1740] Fix | Delete
[1741] Fix | Delete
if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
[1742] Fix | Delete
continue;
[1743] Fix | Delete
}
[1744] Fix | Delete
[1745] Fix | Delete
// Don't extract invalid files:
[1746] Fix | Delete
if ( 0 !== validate_file( $file['filename'] ) ) {
[1747] Fix | Delete
continue;
[1748] Fix | Delete
}
[1749] Fix | Delete
[1750] Fix | Delete
if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE ) ) {
[1751] Fix | Delete
return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] );
[1752] Fix | Delete
}
[1753] Fix | Delete
}
[1754] Fix | Delete
return true;
[1755] Fix | Delete
}
[1756] Fix | Delete
[1757] Fix | Delete
/**
[1758] Fix | Delete
* Copies a directory from one location to another via the WordPress Filesystem
[1759] Fix | Delete
* Abstraction.
[1760] Fix | Delete
*
[1761] Fix | Delete
* Assumes that WP_Filesystem() has already been called and setup.
[1762] Fix | Delete
*
[1763] Fix | Delete
* @since 2.5.0
[1764] Fix | Delete
*
[1765] Fix | Delete
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
[1766] Fix | Delete
*
[1767] Fix | Delete
* @param string $from Source directory.
[1768] Fix | Delete
* @param string $to Destination directory.
[1769] Fix | Delete
* @param string[] $skip_list An array of files/folders to skip copying.
[1770] Fix | Delete
* @return true|WP_Error True on success, WP_Error on failure.
[1771] Fix | Delete
*/
[1772] Fix | Delete
function copy_dir( $from, $to, $skip_list = array() ) {
[1773] Fix | Delete
global $wp_filesystem;
[1774] Fix | Delete
[1775] Fix | Delete
$dirlist = $wp_filesystem->dirlist( $from );
[1776] Fix | Delete
[1777] Fix | Delete
if ( false === $dirlist ) {
[1778] Fix | Delete
return new WP_Error( 'dirlist_failed_copy_dir', __( 'Directory listing failed.' ), basename( $to ) );
[1779] Fix | Delete
}
[1780] Fix | Delete
[1781] Fix | Delete
$from = trailingslashit( $from );
[1782] Fix | Delete
$to = trailingslashit( $to );
[1783] Fix | Delete
[1784] Fix | Delete
foreach ( (array) $dirlist as $filename => $fileinfo ) {
[1785] Fix | Delete
if ( in_array( $filename, $skip_list, true ) ) {
[1786] Fix | Delete
continue;
[1787] Fix | Delete
}
[1788] Fix | Delete
[1789] Fix | Delete
if ( 'f' === $fileinfo['type'] ) {
[1790] Fix | Delete
if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
[1791] Fix | Delete
// If copy failed, chmod file to 0644 and try again.
[1792] Fix | Delete
$wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );
[1793] Fix | Delete
if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
[1794] Fix | Delete
return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename );
[1795] Fix | Delete
}
[1796] Fix | Delete
}
[1797] Fix | Delete
[1798] Fix | Delete
wp_opcache_invalidate( $to . $filename );
[1799] Fix | Delete
} elseif ( 'd' === $fileinfo['type'] ) {
[1800] Fix | Delete
if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
[1801] Fix | Delete
if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {
[1802] Fix | Delete
return new WP_Error( 'mkdir_failed_copy_dir', __( 'Could not create directory.' ), $to . $filename );
[1803] Fix | Delete
}
[1804] Fix | Delete
}
[1805] Fix | Delete
[1806] Fix | Delete
// Generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list.
[1807] Fix | Delete
$sub_skip_list = array();
[1808] Fix | Delete
foreach ( $skip_list as $skip_item ) {
[1809] Fix | Delete
if ( 0 === strpos( $skip_item, $filename . '/' ) ) {
[1810] Fix | Delete
$sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
[1811] Fix | Delete
}
[1812] Fix | Delete
}
[1813] Fix | Delete
[1814] Fix | Delete
$result = copy_dir( $from . $filename, $to . $filename, $sub_skip_list );
[1815] Fix | Delete
if ( is_wp_error( $result ) ) {
[1816] Fix | Delete
return $result;
[1817] Fix | Delete
}
[1818] Fix | Delete
}
[1819] Fix | Delete
}
[1820] Fix | Delete
[1821] Fix | Delete
return true;
[1822] Fix | Delete
}
[1823] Fix | Delete
[1824] Fix | Delete
/**
[1825] Fix | Delete
* Initializes and connects the WordPress Filesystem Abstraction classes.
[1826] Fix | Delete
*
[1827] Fix | Delete
* This function will include the chosen transport and attempt connecting.
[1828] Fix | Delete
*
[1829] Fix | Delete
* Plugins may add extra transports, And force WordPress to use them by returning
[1830] Fix | Delete
* the filename via the {@see 'filesystem_method_file'} filter.
[1831] Fix | Delete
*
[1832] Fix | Delete
* @since 2.5.0
[1833] Fix | Delete
*
[1834] Fix | Delete
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
[1835] Fix | Delete
*
[1836] Fix | Delete
* @param array|false $args Optional. Connection args, These are passed
[1837] Fix | Delete
* directly to the `WP_Filesystem_*()` classes.
[1838] Fix | Delete
* Default false.
[1839] Fix | Delete
* @param string|false $context Optional. Context for get_filesystem_method().
[1840] Fix | Delete
* Default false.
[1841] Fix | Delete
* @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
[1842] Fix | Delete
* Default false.
[1843] Fix | Delete
* @return bool|null True on success, false on failure,
[1844] Fix | Delete
* null if the filesystem method class file does not exist.
[1845] Fix | Delete
*/
[1846] Fix | Delete
function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
[1847] Fix | Delete
global $wp_filesystem;
[1848] Fix | Delete
[1849] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
[1850] Fix | Delete
[1851] Fix | Delete
$method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership );
[1852] Fix | Delete
[1853] Fix | Delete
if ( ! $method ) {
[1854] Fix | Delete
return false;
[1855] Fix | Delete
}
[1856] Fix | Delete
[1857] Fix | Delete
if ( ! class_exists( "WP_Filesystem_$method" ) ) {
[1858] Fix | Delete
[1859] Fix | Delete
/**
[1860] Fix | Delete
* Filters the path for a specific filesystem method class file.
[1861] Fix | Delete
*
[1862] Fix | Delete
* @since 2.6.0
[1863] Fix | Delete
*
[1864] Fix | Delete
* @see get_filesystem_method()
[1865] Fix | Delete
*
[1866] Fix | Delete
* @param string $path Path to the specific filesystem method class file.
[1867] Fix | Delete
* @param string $method The filesystem method to use.
[1868] Fix | Delete
*/
[1869] Fix | Delete
$abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );
[1870] Fix | Delete
[1871] Fix | Delete
if ( ! file_exists( $abstraction_file ) ) {
[1872] Fix | Delete
return;
[1873] Fix | Delete
}
[1874] Fix | Delete
[1875] Fix | Delete
require_once $abstraction_file;
[1876] Fix | Delete
}
[1877] Fix | Delete
$method = "WP_Filesystem_$method";
[1878] Fix | Delete
[1879] Fix | Delete
$wp_filesystem = new $method( $args );
[1880] Fix | Delete
[1881] Fix | Delete
/*
[1882] Fix | Delete
* Define the timeouts for the connections. Only available after the constructor is called
[1883] Fix | Delete
* to allow for per-transport overriding of the default.
[1884] Fix | Delete
*/
[1885] Fix | Delete
if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) {
[1886] Fix | Delete
define( 'FS_CONNECT_TIMEOUT', 30 );
[1887] Fix | Delete
}
[1888] Fix | Delete
if ( ! defined( 'FS_TIMEOUT' ) ) {
[1889] Fix | Delete
define( 'FS_TIMEOUT', 30 );
[1890] Fix | Delete
}
[1891] Fix | Delete
[1892] Fix | Delete
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
[1893] Fix | Delete
return false;
[1894] Fix | Delete
}
[1895] Fix | Delete
[1896] Fix | Delete
if ( ! $wp_filesystem->connect() ) {
[1897] Fix | Delete
return false; // There was an error connecting to the server.
[1898] Fix | Delete
}
[1899] Fix | Delete
[1900] Fix | Delete
// Set the permission constants if not already set.
[1901] Fix | Delete
if ( ! defined( 'FS_CHMOD_DIR' ) ) {
[1902] Fix | Delete
define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
[1903] Fix | Delete
}
[1904] Fix | Delete
if ( ! defined( 'FS_CHMOD_FILE' ) ) {
[1905] Fix | Delete
define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
[1906] Fix | Delete
}
[1907] Fix | Delete
[1908] Fix | Delete
return true;
[1909] Fix | Delete
}
[1910] Fix | Delete
[1911] Fix | Delete
/**
[1912] Fix | Delete
* Determines which method to use for reading, writing, modifying, or deleting
[1913] Fix | Delete
* files on the filesystem.
[1914] Fix | Delete
*
[1915] Fix | Delete
* The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets
[1916] Fix | Delete
* (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2',
[1917] Fix | Delete
* 'ftpext' or 'ftpsockets'.
[1918] Fix | Delete
*
[1919] Fix | Delete
* The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`,
[1920] Fix | Delete
* or filtering via {@see 'filesystem_method'}.
[1921] Fix | Delete
*
[1922] Fix | Delete
* @link https://wordpress.org/support/article/editing-wp-config-php/#wordpress-upgrade-constants
[1923] Fix | Delete
*
[1924] Fix | Delete
* Plugins may define a custom transport handler, See WP_Filesystem().
[1925] Fix | Delete
*
[1926] Fix | Delete
* @since 2.5.0
[1927] Fix | Delete
*
[1928] Fix | Delete
* @global callable $_wp_filesystem_direct_method
[1929] Fix | Delete
*
[1930] Fix | Delete
* @param array $args Optional. Connection details. Default empty array.
[1931] Fix | Delete
* @param string $context Optional. Full path to the directory that is tested
[1932] Fix | Delete
* for being writable. Default empty.
[1933] Fix | Delete
* @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
[1934] Fix | Delete
* Default false.
[1935] Fix | Delete
* @return string The transport to use, see description for valid return values.
[1936] Fix | Delete
*/
[1937] Fix | Delete
function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) {
[1938] Fix | Delete
// Please ensure that this is either 'direct', 'ssh2', 'ftpext', or 'ftpsockets'.
[1939] Fix | Delete
$method = defined( 'FS_METHOD' ) ? FS_METHOD : false;
[1940] Fix | Delete
[1941] Fix | Delete
if ( ! $context ) {
[1942] Fix | Delete
$context = WP_CONTENT_DIR;
[1943] Fix | Delete
}
[1944] Fix | Delete
[1945] Fix | Delete
// If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it.
[1946] Fix | Delete
if ( WP_LANG_DIR == $context && ! is_dir( $context ) ) {
[1947] Fix | Delete
$context = dirname( $context );
[1948] Fix | Delete
}
[1949] Fix | Delete
[1950] Fix | Delete
$context = trailingslashit( $context );
[1951] Fix | Delete
[1952] Fix | Delete
if ( ! $method ) {
[1953] Fix | Delete
[1954] Fix | Delete
$temp_file_name = $context . 'temp-write-test-' . str_replace( '.', '-', uniqid( '', true ) );
[1955] Fix | Delete
$temp_handle = @fopen( $temp_file_name, 'w' );
[1956] Fix | Delete
if ( $temp_handle ) {
[1957] Fix | Delete
[1958] Fix | Delete
// Attempt to determine the file owner of the WordPress files, and that of newly created files.
[1959] Fix | Delete
$wp_file_owner = false;
[1960] Fix | Delete
$temp_file_owner = false;
[1961] Fix | Delete
if ( function_exists( 'fileowner' ) ) {
[1962] Fix | Delete
$wp_file_owner = @fileowner( __FILE__ );
[1963] Fix | Delete
$temp_file_owner = @fileowner( $temp_file_name );
[1964] Fix | Delete
}
[1965] Fix | Delete
[1966] Fix | Delete
if ( false !== $wp_file_owner && $wp_file_owner === $temp_file_owner ) {
[1967] Fix | Delete
/*
[1968] Fix | Delete
* WordPress is creating files as the same owner as the WordPress files,
[1969] Fix | Delete
* this means it's safe to modify & create new files via PHP.
[1970] Fix | Delete
*/
[1971] Fix | Delete
$method = 'direct';
[1972] Fix | Delete
$GLOBALS['_wp_filesystem_direct_method'] = 'file_owner';
[1973] Fix | Delete
} elseif ( $allow_relaxed_file_ownership ) {
[1974] Fix | Delete
/*
[1975] Fix | Delete
* The $context directory is writable, and $allow_relaxed_file_ownership is set,
[1976] Fix | Delete
* this means we can modify files safely in this directory.
[1977] Fix | Delete
* This mode doesn't create new files, only alter existing ones.
[1978] Fix | Delete
*/
[1979] Fix | Delete
$method = 'direct';
[1980] Fix | Delete
$GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership';
[1981] Fix | Delete
}
[1982] Fix | Delete
[1983] Fix | Delete
fclose( $temp_handle );
[1984] Fix | Delete
@unlink( $temp_file_name );
[1985] Fix | Delete
}
[1986] Fix | Delete
}
[1987] Fix | Delete
[1988] Fix | Delete
if ( ! $method && isset( $args['connection_type'] ) && 'ssh' === $args['connection_type'] && extension_loaded( 'ssh2' ) ) {
[1989] Fix | Delete
$method = 'ssh2';
[1990] Fix | Delete
}
[1991] Fix | Delete
if ( ! $method && extension_loaded( 'ftp' ) ) {
[1992] Fix | Delete
$method = 'ftpext';
[1993] Fix | Delete
}
[1994] Fix | Delete
if ( ! $method && ( extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) ) {
[1995] Fix | Delete
$method = 'ftpsockets'; // Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread.
[1996] Fix | Delete
}
[1997] Fix | Delete
[1998] Fix | Delete
/**
[1999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function