auto-patch 638-dev-dev01-2024-05-14T20_44_36

This commit is contained in:
root
2024-05-14 20:44:36 +00:00
parent a941559057
commit 5dbb0b284e
1812 changed files with 29671 additions and 14588 deletions

View File

@@ -8,8 +8,12 @@ use Automattic\WooCommerce\Internal\Admin\Logging\FileV2\File;
use Automattic\WooCommerce\Internal\Admin\Logging\LogHandlerFileV2;
use Automattic\WooCommerce\Internal\Admin\Logging\FileV2\FileController;
use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods;
use Automattic\WooCommerce\Internal\Utilities\FilesystemUtil;
use Automattic\WooCommerce\Proxies\LegacyProxy;
use Exception;
use WC_Admin_Settings;
use WC_Log_Handler_DB, WC_Log_Handler_File, WC_Log_Levels;
use WP_Filesystem_Direct;
/**
* Settings class.
@@ -44,6 +48,51 @@ class Settings {
self::add_action( 'wc_logs_load_tab', array( $this, 'save_settings' ) );
}
/**
* Get the directory for storing log files.
*
* The `wp_upload_dir` function takes into account the possibility of multisite, and handles changing
* the directory if the context is switched to a different site in the network mid-request.
*
* @return string The full directory path, with trailing slash.
*/
public static function get_log_directory(): string {
if ( true === Constants::get_constant( 'WC_LOG_DIR_CUSTOM' ) ) {
$dir = Constants::get_constant( 'WC_LOG_DIR' );
} else {
$upload_dir = wc_get_container()->get( LegacyProxy::class )->call_function( 'wp_upload_dir' );
/**
* Filter to change the directory for storing WooCommerce's log files.
*
* @param string $dir The full directory path, with trailing slash.
*
* @since 8.8.0
*/
$dir = apply_filters( 'woocommerce_log_directory', $upload_dir['basedir'] . '/wc-logs/' );
}
$dir = trailingslashit( $dir );
$realpath = realpath( $dir );
if ( false === $realpath ) {
$result = wp_mkdir_p( $dir );
if ( true === $result ) {
// Create infrastructure to prevent listing contents of the logs directory.
try {
$filesystem = FilesystemUtil::get_wp_filesystem();
$filesystem->put_contents( $dir . '.htaccess', 'deny from all' );
$filesystem->put_contents( $dir . 'index.html', '' );
} catch ( Exception $exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// Creation failed.
}
}
}
return $dir;
}
/**
* The definitions used by WC_Admin_Settings to render and save settings controls.
*
@@ -242,7 +291,21 @@ class Settings {
*/
private function get_filesystem_settings_definitions(): array {
$location_info = array();
$directory = trailingslashit( Constants::get_constant( 'WC_LOG_DIR' ) );
$directory = self::get_log_directory();
$status_info = array();
try {
$filesystem = FilesystemUtil::get_wp_filesystem();
if ( $filesystem instanceof WP_Filesystem_Direct ) {
$status_info[] = __( '✅ Ready', 'woocommerce' );
} else {
$status_info[] = __( '⚠️ The file system is not configured for direct writes. This could cause problems for the logger.', 'woocommerce' );
$status_info[] = __( 'You may want to switch to the database for log storage.', 'woocommerce' );
}
} catch ( Exception $exception ) {
$status_info[] = __( '⚠️ The file system connection could not be initialized.', 'woocommerce' );
$status_info[] = __( 'You may want to switch to the database for log storage.', 'woocommerce' );
}
$location_info[] = sprintf(
// translators: %s is a location in the filesystem.
@@ -257,13 +320,6 @@ class Settings {
$location_info[] = __( '⚠️ This directory does not appear to be writable.', 'woocommerce' );
}
$location_info[] = sprintf(
// translators: %1$s is a code variable. %2$s is the name of a file.
__( 'Change the location by defining the %1$s constant in your %2$s file with a new path.', 'woocommerce' ),
'<code>WC_LOG_DIR</code>',
'<code>wp-config.php</code>'
);
$location_info[] = sprintf(
// translators: %s is an amount of computer disk space, e.g. 5 KB.
__( 'Directory size: %s', 'woocommerce' ),
@@ -276,6 +332,11 @@ class Settings {
'id' => self::PREFIX . 'settings',
'type' => 'title',
),
'file_status' => array(
'title' => __( 'Status', 'woocommerce' ),
'type' => 'info',
'text' => implode( "\n\n", $status_info ),
),
'log_directory' => array(
'title' => __( 'Location', 'woocommerce' ),
'type' => 'info',
@@ -299,7 +360,7 @@ class Settings {
$table = "{$wpdb->prefix}woocommerce_log";
$location_info = sprintf(
// translators: %s is a location in the filesystem.
// translators: %s is the name of a table in the database.
__( 'Log entries are stored in this database table: %s', 'woocommerce' ),
"<code>$table</code>"
);